blob: 0afdd61c4285c0a22227031009f8d1cfb225bd60 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Andrzej Kurekc929a822019-01-14 03:51:11 -050062#if defined(MBEDTLS_USE_PSA_CRYPTO)
63#include "mbedtls/psa_util.h"
64#endif
65
Hanno Becker2a43f6f2018-08-10 11:12:52 +010066static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010067static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010068
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081/*
82 * Start a timer.
83 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020087 if( ssl->f_set_timer == NULL )
88 return;
89
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
91 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020099 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200100 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200101
102 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 {
104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200106 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
108 return( 0 );
109}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200110
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100111static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
112 mbedtls_ssl_transform *transform );
113static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
114 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100115
116#define SSL_DONT_FORCE_FLUSH 0
117#define SSL_FORCE_FLUSH 1
118
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200119#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100120
Hanno Beckerd5847772018-08-28 10:09:23 +0100121/* Forward declarations for functions related to message buffering. */
122static void ssl_buffering_free( mbedtls_ssl_context *ssl );
123static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
124 uint8_t slot );
125static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
126static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
127static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
128static int ssl_buffer_message( mbedtls_ssl_context *ssl );
129static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100130static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100131
Hanno Beckera67dee22018-08-22 10:05:20 +0100132static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100133static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100134{
Hanno Becker11682cc2018-08-22 14:41:02 +0100135 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100136
137 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100138 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100139
140 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
141}
142
Hanno Becker67bc7c32018-08-06 11:33:50 +0100143static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
144{
Hanno Becker11682cc2018-08-22 14:41:02 +0100145 size_t const bytes_written = ssl->out_left;
146 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100147
148 /* Double-check that the write-index hasn't gone
149 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100150 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100151 {
152 /* Should never happen... */
153 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
154 }
155
156 return( (int) ( mtu - bytes_written ) );
157}
158
159static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
160{
161 int ret;
162 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400163 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100164
165#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
166 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
167
168 if( max_len > mfl )
169 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100170
171 /* By the standard (RFC 6066 Sect. 4), the MFL extension
172 * only limits the maximum record payload size, so in theory
173 * we would be allowed to pack multiple records of payload size
174 * MFL into a single datagram. However, this would mean that there's
175 * no way to explicitly communicate MTU restrictions to the peer.
176 *
177 * The following reduction of max_len makes sure that we never
178 * write datagrams larger than MFL + Record Expansion Overhead.
179 */
180 if( max_len <= ssl->out_left )
181 return( 0 );
182
183 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100184#endif
185
186 ret = ssl_get_remaining_space_in_datagram( ssl );
187 if( ret < 0 )
188 return( ret );
189 remaining = (size_t) ret;
190
191 ret = mbedtls_ssl_get_record_expansion( ssl );
192 if( ret < 0 )
193 return( ret );
194 expansion = (size_t) ret;
195
196 if( remaining <= expansion )
197 return( 0 );
198
199 remaining -= expansion;
200 if( remaining >= max_len )
201 remaining = max_len;
202
203 return( (int) remaining );
204}
205
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200206/*
207 * Double the retransmit timeout value, within the allowed range,
208 * returning -1 if the maximum value has already been reached.
209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200211{
212 uint32_t new_timeout;
213
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200214 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200215 return( -1 );
216
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200217 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
218 * in the following way: after the initial transmission and a first
219 * retransmission, back off to a temporary estimated MTU of 508 bytes.
220 * This value is guaranteed to be deliverable (if not guaranteed to be
221 * delivered) of any compliant IPv4 (and IPv6) network, and should work
222 * on most non-IP stacks too. */
223 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400224 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200225 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
227 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200228
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200229 new_timeout = 2 * ssl->handshake->retransmit_timeout;
230
231 /* Avoid arithmetic overflow and range overflow */
232 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200233 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200234 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200235 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200236 }
237
238 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200240 ssl->handshake->retransmit_timeout ) );
241
242 return( 0 );
243}
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200246{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200247 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200249 ssl->handshake->retransmit_timeout ) );
250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200254/*
255 * Convert max_fragment_length codes to length.
256 * RFC 6066 says:
257 * enum{
258 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
259 * } MaxFragmentLength;
260 * and we add 0 -> extension unused
261 */
Angus Grattond8213d02016-05-25 20:56:48 +1000262static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200263{
Angus Grattond8213d02016-05-25 20:56:48 +1000264 switch( mfl )
265 {
266 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
267 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
268 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
269 return 512;
270 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
271 return 1024;
272 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
273 return 2048;
274 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
275 return 4096;
276 default:
277 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
278 }
279}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200281
Hanno Becker52055ae2019-02-06 14:30:46 +0000282int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
283 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_ssl_session_free( dst );
286 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200289 if( src->peer_cert != NULL )
290 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200291 int ret;
292
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200293 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200294 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200295 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200300 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303 dst->peer_cert = NULL;
304 return( ret );
305 }
306 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000307
308#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
309 if( src->peer_cert_digest != NULL )
310 {
311 dst->peer_cert_digest_len = src->peer_cert_digest_len;
312 dst->peer_cert_digest =
313 mbedtls_calloc( 1, dst->peer_cert_digest_len );
314 if( dst->peer_cert_digest == NULL )
315 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
316
317 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
318 src->peer_cert_digest_len );
319 dst->peer_cert_digest_type = src->peer_cert_digest_type;
320 }
321#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200324
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200325#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200326 if( src->ticket != NULL )
327 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200328 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200329 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200330 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200331
332 memcpy( dst->ticket, src->ticket, src->ticket_len );
333 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200334#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200335
336 return( 0 );
337}
338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
340int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200341 const unsigned char *key_enc, const unsigned char *key_dec,
342 size_t keylen,
343 const unsigned char *iv_enc, const unsigned char *iv_dec,
344 size_t ivlen,
345 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200346 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
348int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
349int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
350int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
351int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
352#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000353
Paul Bakker5121ce52009-01-03 21:22:43 +0000354/*
355 * Key material generation
356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200358static int ssl3_prf( const unsigned char *secret, size_t slen,
359 const char *label,
360 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000361 unsigned char *dstbuf, size_t dlen )
362{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100363 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000364 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200365 mbedtls_md5_context md5;
366 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000367 unsigned char padding[16];
368 unsigned char sha1sum[20];
369 ((void)label);
370
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200371 mbedtls_md5_init( &md5 );
372 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200373
Paul Bakker5f70b252012-09-13 14:23:06 +0000374 /*
375 * SSLv3:
376 * block =
377 * MD5( secret + SHA1( 'A' + secret + random ) ) +
378 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
379 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
380 * ...
381 */
382 for( i = 0; i < dlen / 16; i++ )
383 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200384 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000385
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100386 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100387 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100388 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100389 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100390 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100391 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100392 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100393 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100394 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100395 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000396
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100397 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100398 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100399 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100400 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100401 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100402 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100403 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100404 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000405 }
406
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100407exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200408 mbedtls_md5_free( &md5 );
409 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000410
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500411 mbedtls_platform_zeroize( padding, sizeof( padding ) );
412 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000413
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100414 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200419static int tls1_prf( const unsigned char *secret, size_t slen,
420 const char *label,
421 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000422 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000423{
Paul Bakker23986e52011-04-24 08:57:21 +0000424 size_t nb, hs;
425 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200426 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 unsigned char tmp[128];
428 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 const mbedtls_md_info_t *md_info;
430 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100431 int ret;
432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000434
435 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
438 hs = ( slen + 1 ) / 2;
439 S1 = secret;
440 S2 = secret + slen - hs;
441
442 nb = strlen( label );
443 memcpy( tmp + 20, label, nb );
444 memcpy( tmp + 20 + nb, random, rlen );
445 nb += rlen;
446
447 /*
448 * First compute P_md5(secret,label+random)[0..dlen]
449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
451 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100454 return( ret );
455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
457 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
458 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
460 for( i = 0; i < dlen; i += 16 )
461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_md_hmac_reset ( &md_ctx );
463 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
464 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_md_hmac_reset ( &md_ctx );
467 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
468 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
470 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
471
472 for( j = 0; j < k; j++ )
473 dstbuf[i + j] = h_i[j];
474 }
475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100477
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 /*
479 * XOR out with P_sha1(secret,label+random)[0..dlen]
480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
482 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100485 return( ret );
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
488 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
489 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000490
491 for( i = 0; i < dlen; i += 20 )
492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_md_hmac_reset ( &md_ctx );
494 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
495 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_md_hmac_reset ( &md_ctx );
498 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
499 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
501 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
502
503 for( j = 0; j < k; j++ )
504 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
505 }
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100508
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500509 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
510 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
512 return( 0 );
513}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500517#if defined(MBEDTLS_USE_PSA_CRYPTO)
518static int tls_prf_generic( mbedtls_md_type_t md_type,
519 const unsigned char *secret, size_t slen,
520 const char *label,
521 const unsigned char *random, size_t rlen,
522 unsigned char *dstbuf, size_t dlen )
523{
524 psa_status_t status;
525 psa_algorithm_t alg;
526 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500527 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500528 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
529
Andrzej Kurek2f760752019-01-28 08:08:15 -0500530 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500531 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500532
Andrzej Kurekc929a822019-01-14 03:51:11 -0500533 if( md_type == MBEDTLS_MD_SHA384 )
534 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
535 else
536 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
537
Andrzej Kurek2f760752019-01-28 08:08:15 -0500538 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500539 psa_key_policy_set_usage( &policy,
540 PSA_KEY_USAGE_DERIVE,
541 alg );
542 status = psa_set_key_policy( master_slot, &policy );
543 if( status != PSA_SUCCESS )
544 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
545
546 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
547 if( status != PSA_SUCCESS )
548 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
549
550 status = psa_key_derivation( &generator,
551 master_slot, alg,
552 random, rlen,
553 (unsigned char const *) label,
554 (size_t) strlen( label ),
555 dlen );
556 if( status != PSA_SUCCESS )
557 {
558 psa_generator_abort( &generator );
559 psa_destroy_key( master_slot );
560 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
561 }
562
563 status = psa_generator_read( &generator, dstbuf, dlen );
564 if( status != PSA_SUCCESS )
565 {
566 psa_generator_abort( &generator );
567 psa_destroy_key( master_slot );
568 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
569 }
570
571 status = psa_generator_abort( &generator );
572 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500573 {
574 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500575 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500576 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500577
578 status = psa_destroy_key( master_slot );
579 if( status != PSA_SUCCESS )
580 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
581
Andrzej Kurek33171262019-01-15 03:25:18 -0500582 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500583}
584
585#else /* MBEDTLS_USE_PSA_CRYPTO */
586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100588 const unsigned char *secret, size_t slen,
589 const char *label,
590 const unsigned char *random, size_t rlen,
591 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000592{
593 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100594 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000595 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
597 const mbedtls_md_info_t *md_info;
598 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100599 int ret;
600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
604 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100607
608 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000610
611 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100612 memcpy( tmp + md_len, label, nb );
613 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000614 nb += rlen;
615
616 /*
617 * Compute P_<hash>(secret, label + random)[0..dlen]
618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100620 return( ret );
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
623 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
624 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100625
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100626 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_md_hmac_reset ( &md_ctx );
629 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
630 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_md_hmac_reset ( &md_ctx );
633 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
634 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000635
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100636 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000637
638 for( j = 0; j < k; j++ )
639 dstbuf[i + j] = h_i[j];
640 }
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100643
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500644 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
645 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000646
647 return( 0 );
648}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500649#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100651static int tls_prf_sha256( const unsigned char *secret, size_t slen,
652 const char *label,
653 const unsigned char *random, size_t rlen,
654 unsigned char *dstbuf, size_t dlen )
655{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100657 label, random, rlen, dstbuf, dlen ) );
658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200662static int tls_prf_sha384( const unsigned char *secret, size_t slen,
663 const char *label,
664 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000665 unsigned char *dstbuf, size_t dlen )
666{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100668 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000669}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#endif /* MBEDTLS_SHA512_C */
671#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
676 defined(MBEDTLS_SSL_PROTO_TLS1_1)
677static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200678#endif
Paul Bakker380da532012-04-18 16:10:25 +0000679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#if defined(MBEDTLS_SSL_PROTO_SSL3)
681static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
682static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200683#endif
684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
686static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
687static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200688#endif
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
691#if defined(MBEDTLS_SHA256_C)
692static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
693static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
694static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200695#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697#if defined(MBEDTLS_SHA512_C)
698static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
699static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
700static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100701#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000703
Hanno Becker7d0a5692018-10-23 15:26:22 +0100704#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
705 defined(MBEDTLS_USE_PSA_CRYPTO)
706static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
707{
708 if( ssl->conf->f_psk != NULL )
709 {
710 /* If we've used a callback to select the PSK,
711 * the static configuration is irrelevant. */
712 if( ssl->handshake->psk_opaque != 0 )
713 return( 1 );
714
715 return( 0 );
716 }
717
718 if( ssl->conf->psk_opaque != 0 )
719 return( 1 );
720
721 return( 0 );
722}
723#endif /* MBEDTLS_USE_PSA_CRYPTO &&
724 MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000727{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200728 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000729#if defined(MBEDTLS_USE_PSA_CRYPTO)
730 int psa_fallthrough;
731#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000732 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 unsigned char keyblk[256];
734 unsigned char *key1;
735 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100736 unsigned char *mac_enc;
737 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000738 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200739 size_t iv_copy_len;
Hanno Beckerf704bef2018-11-16 15:21:18 +0000740 size_t taglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 const mbedtls_cipher_info_t *cipher_info;
742 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100743
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000744 /* cf. RFC 5246, Section 8.1:
745 * "The master secret is always exactly 48 bytes in length." */
746 size_t const master_secret_len = 48;
747
Hanno Becker35b23c72018-10-23 12:10:41 +0100748#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
749 unsigned char session_hash[48];
750#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 mbedtls_ssl_session *session = ssl->session_negotiate;
753 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
754 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100759 if( cipher_info == NULL )
760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100762 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100764 }
765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100767 if( md_info == NULL )
768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100770 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100772 }
773
Paul Bakker5121ce52009-01-03 21:22:43 +0000774 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000775 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#if defined(MBEDTLS_SSL_PROTO_SSL3)
778 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000779 {
Paul Bakker48916f92012-09-16 19:57:18 +0000780 handshake->tls_prf = ssl3_prf;
781 handshake->calc_verify = ssl_calc_verify_ssl;
782 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000783 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200784 else
785#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
787 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000788 {
Paul Bakker48916f92012-09-16 19:57:18 +0000789 handshake->tls_prf = tls1_prf;
790 handshake->calc_verify = ssl_calc_verify_tls;
791 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000792 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200793 else
794#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
796#if defined(MBEDTLS_SHA512_C)
797 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
798 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000799 {
Paul Bakker48916f92012-09-16 19:57:18 +0000800 handshake->tls_prf = tls_prf_sha384;
801 handshake->calc_verify = ssl_calc_verify_tls_sha384;
802 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000803 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000804 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200805#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806#if defined(MBEDTLS_SHA256_C)
807 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000808 {
Paul Bakker48916f92012-09-16 19:57:18 +0000809 handshake->tls_prf = tls_prf_sha256;
810 handshake->calc_verify = ssl_calc_verify_tls_sha256;
811 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000812 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200813 else
814#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
818 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200819 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000820
821 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000822 * SSLv3:
823 * master =
824 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
825 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
826 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200827 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200828 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000829 * master = PRF( premaster, "master secret", randbytes )[0..47]
830 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100831 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000832 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100833 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
834 }
835 else
836 {
837 /* The label for the KDF used for key expansion.
838 * This is either "master secret" or "extended master secret"
839 * depending on whether the Extended Master Secret extension
840 * is used. */
841 char const *lbl = "master secret";
842
843 /* The salt for the KDF used for key expansion.
844 * - If the Extended Master Secret extension is not used,
845 * this is ClientHello.Random + ServerHello.Random
846 * (see Sect. 8.1 in RFC 5246).
847 * - If the Extended Master Secret extension is used,
848 * this is the transcript of the handshake so far.
849 * (see Sect. 4 in RFC 7627). */
850 unsigned char const *salt = handshake->randbytes;
851 size_t salt_len = 64;
852
853#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
854 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
855 ssl->transform_negotiate->ciphersuite_info;
856 mbedtls_md_type_t const md_type = ciphersuite_info->mac;
857#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Paul Bakker5121ce52009-01-03 21:22:43 +0000858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
860 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200863
Hanno Becker35b23c72018-10-23 12:10:41 +0100864 lbl = "extended master secret";
865 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200866 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
868 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870#if defined(MBEDTLS_SHA512_C)
Hanno Becker35b23c72018-10-23 12:10:41 +0100871 if( md_type == MBEDTLS_MD_SHA384 )
872 salt_len = 48;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200873 else
Hanno Becker35b23c72018-10-23 12:10:41 +0100874#endif /* MBEDTLS_SHA512_C */
875 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200876 }
877 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100879 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200880
Hanno Becker35b23c72018-10-23 12:10:41 +0100881 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200882 }
Hanno Becker35b23c72018-10-23 12:10:41 +0100883#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
884
Hanno Becker7d0a5692018-10-23 15:26:22 +0100885#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
886 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
887 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
888 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
889 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100890 {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100891 /* Perform PSK-to-MS expansion in a single step. */
892 psa_status_t status;
893 psa_algorithm_t alg;
894 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500895 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100896
897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
898
899 psk = ssl->conf->psk_opaque;
900 if( ssl->handshake->psk_opaque != 0 )
901 psk = ssl->handshake->psk_opaque;
902
903 if( md_type == MBEDTLS_MD_SHA384 )
904 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
905 else
906 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
907
908 status = psa_key_derivation( &generator, psk, alg,
909 salt, salt_len,
910 (unsigned char const *) lbl,
911 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000912 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100913 if( status != PSA_SUCCESS )
914 {
915 psa_generator_abort( &generator );
916 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
917 }
918
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000919 status = psa_generator_read( &generator, session->master,
920 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100921 if( status != PSA_SUCCESS )
922 {
923 psa_generator_abort( &generator );
924 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
925 }
926
927 status = psa_generator_abort( &generator );
928 if( status != PSA_SUCCESS )
929 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100930 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100931 else
932#endif
933 {
934 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
935 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000936 session->master,
937 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100938 if( ret != 0 )
939 {
940 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
941 return( ret );
942 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200943
Hanno Becker7d0a5692018-10-23 15:26:22 +0100944 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
945 handshake->premaster,
946 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +0100947
Hanno Becker7d0a5692018-10-23 15:26:22 +0100948 mbedtls_platform_zeroize( handshake->premaster,
949 sizeof(handshake->premaster) );
950 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000951 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000952
953 /*
954 * Swap the client and server random values.
955 */
Paul Bakker48916f92012-09-16 19:57:18 +0000956 memcpy( tmp, handshake->randbytes, 64 );
957 memcpy( handshake->randbytes, tmp + 32, 32 );
958 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500959 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000960
961 /*
962 * SSLv3:
963 * key block =
964 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
965 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
966 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
967 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
968 * ...
969 *
970 * TLSv1:
971 * key block = PRF( master, "key expansion", randbytes )
972 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100973 ret = handshake->tls_prf( session->master, 48, "key expansion",
974 handshake->randbytes, 64, keyblk, 256 );
975 if( ret != 0 )
976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100978 return( ret );
979 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
982 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
983 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
984 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
985 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500987 mbedtls_platform_zeroize( handshake->randbytes,
988 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000989
990 /*
991 * Determine the appropriate key, IV and MAC length.
992 */
Paul Bakker68884e32013-01-07 18:20:04 +0100993
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200994 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200997 cipher_info->mode == MBEDTLS_MODE_CCM ||
998 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001000 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001001
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001002 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001003 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001004
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001005 /* All modes haves 96-bit IVs;
1006 * GCM and CCM has 4 implicit and 8 explicit bytes
1007 * ChachaPoly has all 12 bytes implicit
1008 */
Paul Bakker68884e32013-01-07 18:20:04 +01001009 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001010 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1011 transform->fixed_ivlen = 12;
1012 else
1013 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001014
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001015 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
1016 taglen = transform->ciphersuite_info->flags &
1017 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
1018
1019
1020 /* Minimum length of encrypted record */
1021 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
1022 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001023 }
1024 else
1025 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001026 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1028 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001031 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001032 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001033
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001034 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001035 mac_key_len = mbedtls_md_get_size( md_info );
1036 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001039 /*
1040 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1041 * (rfc 6066 page 13 or rfc 2104 section 4),
1042 * so we only need to adjust the length here.
1043 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001047
1048#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1049 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001050 * HMAC implementation which also truncates the key
1051 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001052 mac_key_len = transform->maclen;
1053#endif
1054 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001056
1057 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001058 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001059
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001060 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001062 transform->minlen = transform->maclen;
1063 else
Paul Bakker68884e32013-01-07 18:20:04 +01001064 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001065 /*
1066 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001067 * 1. if EtM is in use: one block plus MAC
1068 * otherwise: * first multiple of blocklen greater than maclen
1069 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1072 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001073 {
1074 transform->minlen = transform->maclen
1075 + cipher_info->block_size;
1076 }
1077 else
1078#endif
1079 {
1080 transform->minlen = transform->maclen
1081 + cipher_info->block_size
1082 - transform->maclen % cipher_info->block_size;
1083 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1086 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1087 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001088 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001089 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001090#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1092 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1093 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001094 {
1095 transform->minlen += transform->ivlen;
1096 }
1097 else
1098#endif
1099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1101 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001102 }
Paul Bakker68884e32013-01-07 18:20:04 +01001103 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 }
1105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001107 transform->keylen, transform->minlen, transform->ivlen,
1108 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001109
1110 /*
1111 * Finally setup the cipher contexts, IVs and MAC secrets.
1112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001114 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001115 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001116 key1 = keyblk + mac_key_len * 2;
1117 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
Paul Bakker68884e32013-01-07 18:20:04 +01001119 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001120 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001121
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001122 /*
1123 * This is not used in TLS v1.1.
1124 */
Paul Bakker48916f92012-09-16 19:57:18 +00001125 iv_copy_len = ( transform->fixed_ivlen ) ?
1126 transform->fixed_ivlen : transform->ivlen;
1127 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
1128 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001129 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 }
1131 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132#endif /* MBEDTLS_SSL_CLI_C */
1133#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001134 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001136 key1 = keyblk + mac_key_len * 2 + transform->keylen;
1137 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
Hanno Becker81c7b182017-11-09 18:39:33 +00001139 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001140 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001141
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001142 /*
1143 * This is not used in TLS v1.1.
1144 */
Paul Bakker48916f92012-09-16 19:57:18 +00001145 iv_copy_len = ( transform->fixed_ivlen ) ?
1146 transform->fixed_ivlen : transform->ivlen;
1147 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
1148 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001149 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001150 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001151 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1155 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001156 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#if defined(MBEDTLS_SSL_PROTO_SSL3)
1159 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001160 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001161 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1164 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001165 }
1166
Hanno Becker81c7b182017-11-09 18:39:33 +00001167 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1168 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001169 }
1170 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1172#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1173 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1174 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001175 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001176 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1177 For AEAD-based ciphersuites, there is nothing to do here. */
1178 if( mac_key_len != 0 )
1179 {
1180 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1181 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1182 }
Paul Bakker68884e32013-01-07 18:20:04 +01001183 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001184 else
1185#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1188 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001189 }
Paul Bakker68884e32013-01-07 18:20:04 +01001190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1192 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001193 {
1194 int ret = 0;
1195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001199 transform->iv_enc, transform->iv_dec,
1200 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001201 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001202 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1205 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001206 }
1207 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001209
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001210#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1211 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001212 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001213 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1214 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001215 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001216 iv_copy_len );
1217 }
1218#endif
1219
Hanno Beckerf704bef2018-11-16 15:21:18 +00001220#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001221
1222 /* Only use PSA-based ciphers for TLS-1.2.
1223 * That's relevant at least for TLS-1.0, where
1224 * we assume that mbedtls_cipher_crypt() updates
1225 * the structure field for the IV, which the PSA-based
1226 * implementation currently doesn't. */
1227#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1228 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001229 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001230 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
1231 cipher_info, taglen );
1232 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1233 {
1234 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1235 return( ret );
1236 }
1237
1238 if( ret == 0 )
1239 {
1240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based encryption cipher context" ) );
1241 psa_fallthrough = 0;
1242 }
1243 else
1244 {
1245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1246 psa_fallthrough = 1;
1247 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001248 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001249 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001250 psa_fallthrough = 1;
1251#else
1252 psa_fallthrough = 1;
1253#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001254
Hanno Beckercb1cc802018-11-17 22:27:38 +00001255 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001256#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001257 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001258 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001260 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001261 return( ret );
1262 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001263
Hanno Beckerf704bef2018-11-16 15:21:18 +00001264#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001265 /* Only use PSA-based ciphers for TLS-1.2.
1266 * That's relevant at least for TLS-1.0, where
1267 * we assume that mbedtls_cipher_crypt() updates
1268 * the structure field for the IV, which the PSA-based
1269 * implementation currently doesn't. */
1270#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1271 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001272 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001273 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
1274 cipher_info, taglen );
1275 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1276 {
1277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1278 return( ret );
1279 }
1280
1281 if( ret == 0 )
1282 {
1283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based decryption cipher context" ) );
1284 psa_fallthrough = 0;
1285 }
1286 else
1287 {
1288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1289 psa_fallthrough = 1;
1290 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001291 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001292 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001293 psa_fallthrough = 1;
1294#else
1295 psa_fallthrough = 1;
1296#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001297
Hanno Beckercb1cc802018-11-17 22:27:38 +00001298 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001299#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001300 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001301 cipher_info ) ) != 0 )
1302 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001303 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001304 return( ret );
1305 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001308 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001312 return( ret );
1313 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001316 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001320 return( ret );
1321 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#if defined(MBEDTLS_CIPHER_MODE_CBC)
1324 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1327 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001330 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001331 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1334 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001337 return( ret );
1338 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001342 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001345 // Initialize compression
1346 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001348 {
Paul Bakker16770332013-10-11 09:59:44 +02001349 if( ssl->compress_buf == NULL )
1350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001352 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001353 if( ssl->compress_buf == NULL )
1354 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001356 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001357 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001358 }
1359 }
1360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001362
Paul Bakker48916f92012-09-16 19:57:18 +00001363 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1364 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001365
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001366 if( deflateInit( &transform->ctx_deflate,
1367 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001368 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1371 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001372 }
1373 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001377
1378 return( 0 );
1379}
1380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381#if defined(MBEDTLS_SSL_PROTO_SSL3)
1382void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001383{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001384 mbedtls_md5_context md5;
1385 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 unsigned char pad_1[48];
1387 unsigned char pad_2[48];
1388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001391 mbedtls_md5_init( &md5 );
1392 mbedtls_sha1_init( &sha1 );
1393
1394 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1395 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001396
Paul Bakker380da532012-04-18 16:10:25 +00001397 memset( pad_1, 0x36, 48 );
1398 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001400 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1401 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1402 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001403
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001404 mbedtls_md5_starts_ret( &md5 );
1405 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1406 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1407 mbedtls_md5_update_ret( &md5, hash, 16 );
1408 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001409
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001410 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1411 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1412 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001413
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001414 mbedtls_sha1_starts_ret( &sha1 );
1415 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1416 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1417 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1418 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001422
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001423 mbedtls_md5_free( &md5 );
1424 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001425
Paul Bakker380da532012-04-18 16:10:25 +00001426 return;
1427}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1431void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001432{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001433 mbedtls_md5_context md5;
1434 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001437
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001438 mbedtls_md5_init( &md5 );
1439 mbedtls_sha1_init( &sha1 );
1440
1441 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1442 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001443
Andrzej Kurekeb342242019-01-29 09:14:33 -05001444 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001445 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001449
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001450 mbedtls_md5_free( &md5 );
1451 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001452
Paul Bakker380da532012-04-18 16:10:25 +00001453 return;
1454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1458#if defined(MBEDTLS_SHA256_C)
1459void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001460{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001461#if defined(MBEDTLS_USE_PSA_CRYPTO)
1462 size_t hash_size;
1463 psa_status_t status;
1464 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1465
1466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1467 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1468 if( status != PSA_SUCCESS )
1469 {
1470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1471 return;
1472 }
1473
1474 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1475 if( status != PSA_SUCCESS )
1476 {
1477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1478 return;
1479 }
1480 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1482#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001483 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001484
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001485 mbedtls_sha256_init( &sha256 );
1486
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001488
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001489 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001490 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001494
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001495 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001496#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001497 return;
1498}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#if defined(MBEDTLS_SHA512_C)
1502void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001503{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001504#if defined(MBEDTLS_USE_PSA_CRYPTO)
1505 size_t hash_size;
1506 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001507 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001508
1509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001510 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001511 if( status != PSA_SUCCESS )
1512 {
1513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1514 return;
1515 }
1516
Andrzej Kurek972fba52019-01-30 03:29:12 -05001517 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001518 if( status != PSA_SUCCESS )
1519 {
1520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1521 return;
1522 }
1523 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1525#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001526 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001527
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001528 mbedtls_sha512_init( &sha512 );
1529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001531
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001532 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001533 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001537
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001538 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001539#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 return;
1541}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#endif /* MBEDTLS_SHA512_C */
1543#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1546int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001547{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001548 unsigned char *p = ssl->handshake->premaster;
1549 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001550 const unsigned char *psk = ssl->conf->psk;
1551 size_t psk_len = ssl->conf->psk_len;
1552
1553 /* If the psk callback was called, use its result */
1554 if( ssl->handshake->psk != NULL )
1555 {
1556 psk = ssl->handshake->psk;
1557 psk_len = ssl->handshake->psk_len;
1558 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001559
1560 /*
1561 * PMS = struct {
1562 * opaque other_secret<0..2^16-1>;
1563 * opaque psk<0..2^16-1>;
1564 * };
1565 * with "other_secret" depending on the particular key exchange
1566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1568 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001569 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001570 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001572
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001573 *(p++) = (unsigned char)( psk_len >> 8 );
1574 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001575
1576 if( end < p || (size_t)( end - p ) < psk_len )
1577 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1578
1579 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001580 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001581 }
1582 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1584#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1585 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001586 {
1587 /*
1588 * other_secret already set by the ClientKeyExchange message,
1589 * and is 48 bytes long
1590 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001591 if( end - p < 2 )
1592 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1593
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001594 *p++ = 0;
1595 *p++ = 48;
1596 p += 48;
1597 }
1598 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1600#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1601 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001602 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001603 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001604 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001605
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001606 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001608 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001609 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001612 return( ret );
1613 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001614 *(p++) = (unsigned char)( len >> 8 );
1615 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001616 p += len;
1617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001619 }
1620 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1622#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1623 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001624 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001625 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001626 size_t zlen;
1627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001629 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001630 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001633 return( ret );
1634 }
1635
1636 *(p++) = (unsigned char)( zlen >> 8 );
1637 *(p++) = (unsigned char)( zlen );
1638 p += zlen;
1639
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001640 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1641 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001642 }
1643 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001648 }
1649
1650 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001651 if( end - p < 2 )
1652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001653
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001654 *(p++) = (unsigned char)( psk_len >> 8 );
1655 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001656
1657 if( end < p || (size_t)( end - p ) < psk_len )
1658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1659
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001660 memcpy( p, psk, psk_len );
1661 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001662
1663 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1664
1665 return( 0 );
1666}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001670/*
1671 * SSLv3.0 MAC functions
1672 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001673#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001674static void ssl_mac( mbedtls_md_context_t *md_ctx,
1675 const unsigned char *secret,
1676 const unsigned char *buf, size_t len,
1677 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001678 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001679{
1680 unsigned char header[11];
1681 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001682 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1684 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001685
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001686 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001688 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001689 else
Paul Bakker68884e32013-01-07 18:20:04 +01001690 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001691
1692 memcpy( header, ctr, 8 );
1693 header[ 8] = (unsigned char) type;
1694 header[ 9] = (unsigned char)( len >> 8 );
1695 header[10] = (unsigned char)( len );
1696
Paul Bakker68884e32013-01-07 18:20:04 +01001697 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 mbedtls_md_starts( md_ctx );
1699 mbedtls_md_update( md_ctx, secret, md_size );
1700 mbedtls_md_update( md_ctx, padding, padlen );
1701 mbedtls_md_update( md_ctx, header, 11 );
1702 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001703 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001704
Paul Bakker68884e32013-01-07 18:20:04 +01001705 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 mbedtls_md_starts( md_ctx );
1707 mbedtls_md_update( md_ctx, secret, md_size );
1708 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001709 mbedtls_md_update( md_ctx, out, md_size );
1710 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001711}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1715 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001716 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001717#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001718#endif
1719
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001720/* The function below is only used in the Lucky 13 counter-measure in
1721 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1722#if defined(SSL_SOME_MODES_USE_MAC) && \
1723 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1724 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1725 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1726/* This function makes sure every byte in the memory region is accessed
1727 * (in ascending addresses order) */
1728static void ssl_read_memory( unsigned char *p, size_t len )
1729{
1730 unsigned char acc = 0;
1731 volatile unsigned char force;
1732
1733 for( ; len != 0; p++, len-- )
1734 acc ^= *p;
1735
1736 force = acc;
1737 (void) force;
1738}
1739#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1740
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001741/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001742 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001745{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001747 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001750
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001751 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1754 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001755 }
1756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001760 ssl->out_msg, ssl->out_msglen );
1761
Paul Bakker5121ce52009-01-03 21:22:43 +00001762 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001763 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001764 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001765#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 if( mode == MBEDTLS_MODE_STREAM ||
1767 ( mode == MBEDTLS_MODE_CBC
1768#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1769 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001770#endif
1771 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#if defined(MBEDTLS_SSL_PROTO_SSL3)
1774 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001775 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001776 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001777
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001778 ssl_mac( &ssl->transform_out->md_ctx_enc,
1779 ssl->transform_out->mac_enc,
1780 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001781 ssl->out_ctr, ssl->out_msgtype,
1782 mac );
1783
1784 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001785 }
1786 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001787#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1789 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1790 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001791 {
Hanno Becker992b6872017-11-09 18:57:39 +00001792 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1795 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1796 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1797 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001798 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001799 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001801
1802 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001803 }
1804 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001805#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1808 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001809 }
1810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001812 ssl->out_msg + ssl->out_msglen,
1813 ssl->transform_out->maclen );
1814
1815 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001816 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001817 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001818#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001819
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001820 /*
1821 * Encrypt
1822 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1824 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001825 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001826 int ret;
1827 size_t olen = 0;
1828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001830 "including %d bytes of padding",
1831 ssl->out_msglen, 0 ) );
1832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001834 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001835 ssl->transform_out->ivlen,
1836 ssl->out_msg, ssl->out_msglen,
1837 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001840 return( ret );
1841 }
1842
1843 if( ssl->out_msglen != olen )
1844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1846 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001847 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001848 }
Paul Bakker68884e32013-01-07 18:20:04 +01001849 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001851#if defined(MBEDTLS_GCM_C) || \
1852 defined(MBEDTLS_CCM_C) || \
1853 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001855 mode == MBEDTLS_MODE_CCM ||
1856 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001857 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001858 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001859 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001860 unsigned char *enc_msg;
1861 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001862 unsigned char iv[12];
1863 mbedtls_ssl_transform *transform = ssl->transform_out;
1864 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001866 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001867
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001868 /*
1869 * Prepare additional authenticated data
1870 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001871 memcpy( add_data, ssl->out_ctr, 8 );
1872 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001874 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001875 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1876 add_data[12] = ssl->out_msglen & 0xFF;
1877
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001878 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001879
Paul Bakker68884e32013-01-07 18:20:04 +01001880 /*
1881 * Generate IV
1882 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001883 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1884 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001885 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001886 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1887 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1888 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1889
1890 }
1891 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1892 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001893 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001894 unsigned char i;
1895
1896 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1897
1898 for( i = 0; i < 8; i++ )
1899 iv[i+4] ^= ssl->out_ctr[i];
1900 }
1901 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001902 {
1903 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1905 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001906 }
1907
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001908 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1909 iv, transform->ivlen );
1910 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1911 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001912
Paul Bakker68884e32013-01-07 18:20:04 +01001913 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001914 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001915 */
1916 enc_msg = ssl->out_msg;
1917 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001918 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001921 "including 0 bytes of padding",
1922 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001923
Paul Bakker68884e32013-01-07 18:20:04 +01001924 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001925 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001926 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001927 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1928 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001929 add_data, 13,
1930 enc_msg, enc_msglen,
1931 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001932 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001935 return( ret );
1936 }
1937
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001938 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1941 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001942 }
1943
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001944 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001945 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001948 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1951#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001952 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001954 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001955 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001956 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001957 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001958
Paul Bakker48916f92012-09-16 19:57:18 +00001959 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1960 ssl->transform_out->ivlen;
1961 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001962 padlen = 0;
1963
1964 for( i = 0; i <= padlen; i++ )
1965 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1966
1967 ssl->out_msglen += padlen + 1;
1968
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001969 enc_msglen = ssl->out_msglen;
1970 enc_msg = ssl->out_msg;
1971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001973 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001974 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1975 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001976 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001978 {
1979 /*
1980 * Generate IV
1981 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001982 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001983 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001984 if( ret != 0 )
1985 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001986
Paul Bakker92be97b2013-01-02 17:30:03 +01001987 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001988 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001989
1990 /*
1991 * Fix pointer positions and message length with added IV
1992 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001993 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001994 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001995 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001996 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002000 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002001 ssl->out_msglen, ssl->transform_out->ivlen,
2002 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02002005 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002006 ssl->transform_out->ivlen,
2007 enc_msg, enc_msglen,
2008 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002011 return( ret );
2012 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002013
Paul Bakkercca5b812013-08-31 17:40:26 +02002014 if( enc_msglen != olen )
2015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2017 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002018 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2021 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002022 {
2023 /*
2024 * Save IV in SSL3 and TLS1
2025 */
2026 memcpy( ssl->transform_out->iv_enc,
2027 ssl->transform_out->cipher_ctx_enc.iv,
2028 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002030#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002033 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002034 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002035 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2036
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002037 /*
2038 * MAC(MAC_write_key, seq_num +
2039 * TLSCipherText.type +
2040 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002041 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002042 * IV + // except for TLS 1.0
2043 * ENC(content + padding + padding_length));
2044 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002045 unsigned char pseudo_hdr[13];
2046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002048
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002049 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
2050 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002051 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
2052 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
2053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
2057 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002058 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00002059 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002061
Hanno Becker3d8c9072018-01-05 16:24:22 +00002062 memcpy( ssl->out_iv + ssl->out_msglen, mac,
2063 ssl->transform_out->maclen );
2064
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002065 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002066 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002067 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002070 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002072 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2075 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002076 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002077
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002078 /* Make extra sure authentication was performed, exactly once */
2079 if( auth_done != 1 )
2080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2082 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002083 }
2084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002086
2087 return( 0 );
2088}
2089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002091{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002093 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002094#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002095 size_t padlen = 0, correct = 1;
2096#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002099
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002100 if( ssl->session_in == NULL || ssl->transform_in == NULL )
2101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2103 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002104 }
2105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002107
Paul Bakker48916f92012-09-16 19:57:18 +00002108 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00002111 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 }
2114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2116 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002117 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002118 int ret;
2119 size_t olen = 0;
2120
Paul Bakker68884e32013-01-07 18:20:04 +01002121 padlen = 0;
2122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002124 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002125 ssl->transform_in->ivlen,
2126 ssl->in_msg, ssl->in_msglen,
2127 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002130 return( ret );
2131 }
2132
2133 if( ssl->in_msglen != olen )
2134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002137 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 }
Paul Bakker68884e32013-01-07 18:20:04 +01002139 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002141#if defined(MBEDTLS_GCM_C) || \
2142 defined(MBEDTLS_CCM_C) || \
2143 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002145 mode == MBEDTLS_MODE_CCM ||
2146 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002147 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002148 int ret;
2149 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002150 unsigned char *dec_msg;
2151 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002152 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002153 unsigned char iv[12];
2154 mbedtls_ssl_transform *transform = ssl->transform_in;
2155 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002157 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002158
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002159 /*
2160 * Compute and update sizes
2161 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002162 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002165 "+ taglen (%d)", ssl->in_msglen,
2166 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002168 }
2169 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2170
Paul Bakker68884e32013-01-07 18:20:04 +01002171 dec_msg = ssl->in_msg;
2172 dec_msg_result = ssl->in_msg;
2173 ssl->in_msglen = dec_msglen;
2174
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002175 /*
2176 * Prepare additional authenticated data
2177 */
Paul Bakker68884e32013-01-07 18:20:04 +01002178 memcpy( add_data, ssl->in_ctr, 8 );
2179 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002181 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002182 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2183 add_data[12] = ssl->in_msglen & 0xFF;
2184
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002185 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002186
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002187 /*
2188 * Prepare IV
2189 */
2190 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2191 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002192 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002193 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2194 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002195
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002196 }
2197 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2198 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002199 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002200 unsigned char i;
2201
2202 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2203
2204 for( i = 0; i < 8; i++ )
2205 iv[i+4] ^= ssl->in_ctr[i];
2206 }
2207 else
2208 {
2209 /* Reminder if we ever add an AEAD mode with a different size */
2210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2211 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2212 }
2213
2214 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002216
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002217 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002218 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002221 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002222 add_data, 13,
2223 dec_msg, dec_msglen,
2224 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002225 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2230 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002231
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002232 return( ret );
2233 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002234 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002235
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002236 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2239 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002240 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002241 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2244#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002245 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002247 {
Paul Bakker45829992013-01-03 14:52:21 +01002248 /*
2249 * Decrypt and check the padding
2250 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002251 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002252 unsigned char *dec_msg;
2253 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002254 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002255 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002256 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002257
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 /*
Paul Bakker45829992013-01-03 14:52:21 +01002259 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2262 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002263 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002264#endif
Paul Bakker45829992013-01-03 14:52:21 +01002265
2266 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2267 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002270 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2271 ssl->transform_in->ivlen,
2272 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002274 }
2275
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002276 dec_msglen = ssl->in_msglen;
2277 dec_msg = ssl->in_msg;
2278 dec_msg_result = ssl->in_msg;
2279
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002280 /*
2281 * Authenticate before decrypt if enabled
2282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2284 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002285 {
Hanno Becker992b6872017-11-09 18:57:39 +00002286 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002287 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002290
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002291 dec_msglen -= ssl->transform_in->maclen;
2292 ssl->in_msglen -= ssl->transform_in->maclen;
2293
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002294 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2295 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2296 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2297 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2302 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002303 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002304 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002308 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002309 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002310 ssl->transform_in->maclen );
2311
Hanno Becker992b6872017-11-09 18:57:39 +00002312 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2313 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002318 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002319 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002320 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002322
2323 /*
2324 * Check length sanity
2325 */
2326 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002329 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002331 }
2332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002334 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002335 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002338 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002339 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002340 dec_msglen -= ssl->transform_in->ivlen;
2341 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002342
Paul Bakker48916f92012-09-16 19:57:18 +00002343 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002344 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002345 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002349 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002350 ssl->transform_in->ivlen,
2351 dec_msg, dec_msglen,
2352 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002355 return( ret );
2356 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002357
Paul Bakkercca5b812013-08-31 17:40:26 +02002358 if( dec_msglen != olen )
2359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2361 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002362 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2365 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002366 {
2367 /*
2368 * Save IV in SSL3 and TLS1
2369 */
2370 memcpy( ssl->transform_in->iv_dec,
2371 ssl->transform_in->cipher_ctx_dec.iv,
2372 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002373 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002374#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002375
2376 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002377
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002378 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002379 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381#if defined(MBEDTLS_SSL_DEBUG_ALL)
2382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002383 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002384#endif
Paul Bakker45829992013-01-03 14:52:21 +01002385 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002386 correct = 0;
2387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389#if defined(MBEDTLS_SSL_PROTO_SSL3)
2390 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 {
Paul Bakker48916f92012-09-16 19:57:18 +00002392 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394#if defined(MBEDTLS_SSL_DEBUG_ALL)
2395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002397 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002398#endif
Paul Bakker45829992013-01-03 14:52:21 +01002399 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 }
2401 }
2402 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2404#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2405 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2406 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 {
2408 /*
Paul Bakker45829992013-01-03 14:52:21 +01002409 * TLSv1+: always check the padding up to the first failure
2410 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002412 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002413 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002414 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002415
Paul Bakker956c9e02013-12-19 14:42:28 +01002416 /*
2417 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002418 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002419 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002420 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002421 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002422 *
2423 * In both cases we reset padding_idx to a safe value (0) to
2424 * prevent out-of-buffer reads.
2425 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002426 correct &= ( padlen <= ssl->in_msglen );
2427 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002428 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002429
2430 padding_idx *= correct;
2431
Angus Grattonb512bc12018-06-19 15:57:50 +10002432 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002433 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002434 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002435 pad_count += real_count *
2436 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2437 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002438
2439 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002442 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002444#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002445 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002447 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2449 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2452 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002453 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002454
2455 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002457 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002459 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2462 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002463 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002464
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002465#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002468#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002469
2470 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002471 * Authenticate if not done yet.
2472 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002474#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002475 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002476 {
Hanno Becker992b6872017-11-09 18:57:39 +00002477 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002478
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002479 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002481 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2482 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484#if defined(MBEDTLS_SSL_PROTO_SSL3)
2485 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002486 {
2487 ssl_mac( &ssl->transform_in->md_ctx_dec,
2488 ssl->transform_in->mac_dec,
2489 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002490 ssl->in_ctr, ssl->in_msgtype,
2491 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002492 }
2493 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2495#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2496 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2497 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002498 {
2499 /*
2500 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002501 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002502 *
2503 * Known timing attacks:
2504 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2505 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002506 * To compensate for different timings for the MAC calculation
2507 * depending on how much padding was removed (which is determined
2508 * by padlen), process extra_run more blocks through the hash
2509 * function.
2510 *
2511 * The formula in the paper is
2512 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2513 * where L1 is the size of the header plus the decrypted message
2514 * plus CBC padding and L2 is the size of the header plus the
2515 * decrypted message. This is for an underlying hash function
2516 * with 64-byte blocks.
2517 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2518 * correctly. We round down instead of up, so -56 is the correct
2519 * value for our calculations instead of -55.
2520 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002521 * Repeat the formula rather than defining a block_size variable.
2522 * This avoids requiring division by a variable at runtime
2523 * (which would be marginally less efficient and would require
2524 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002525 */
2526 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002527
2528 /*
2529 * The next two sizes are the minimum and maximum values of
2530 * in_msglen over all padlen values.
2531 *
2532 * They're independent of padlen, since we previously did
2533 * in_msglen -= padlen.
2534 *
2535 * Note that max_len + maclen is never more than the buffer
2536 * length, as we previously did in_msglen -= maclen too.
2537 */
2538 const size_t max_len = ssl->in_msglen + padlen;
2539 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2540
Gilles Peskine20b44082018-05-29 14:06:49 +02002541 switch( ssl->transform_in->ciphersuite_info->mac )
2542 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002543#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2544 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002545 case MBEDTLS_MD_MD5:
2546 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002547 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002548 /* 8 bytes of message size, 64-byte compression blocks */
2549 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2550 ( 13 + ssl->in_msglen + 8 ) / 64;
2551 break;
2552#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002553#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002554 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002555 /* 16 bytes of message size, 128-byte compression blocks */
2556 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2557 ( 13 + ssl->in_msglen + 16 ) / 128;
2558 break;
2559#endif
2560 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002562 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2563 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002564
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002565 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2568 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2569 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2570 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002571 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002572 /* Make sure we access everything even when padlen > 0. This
2573 * makes the synchronisation requirements for just-in-time
2574 * Prime+Probe attacks much tighter and hopefully impractical. */
2575 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002576 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002577
2578 /* Call mbedtls_md_process at least once due to cache attacks
2579 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002580 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002584
2585 /* Make sure we access all the memory that could contain the MAC,
2586 * before we check it in the next code block. This makes the
2587 * synchronisation requirements for just-in-time Prime+Probe
2588 * attacks much tighter and hopefully impractical. */
2589 ssl_read_memory( ssl->in_msg + min_len,
2590 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002591 }
2592 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2594 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2597 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002598 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002599
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002600#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002601 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2602 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2603 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002604#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
Hanno Becker992b6872017-11-09 18:57:39 +00002606 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2607 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609#if defined(MBEDTLS_SSL_DEBUG_ALL)
2610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002611#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002612 correct = 0;
2613 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002614 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002615 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002616
2617 /*
2618 * Finally check the correct flag
2619 */
2620 if( correct == 0 )
2621 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002622#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002623
2624 /* Make extra sure authentication was performed, exactly once */
2625 if( auth_done != 1 )
2626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2628 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002629 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002630
2631 if( ssl->in_msglen == 0 )
2632 {
Angus Gratton34817922018-06-19 15:58:22 +10002633#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2634 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2635 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2636 {
2637 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2639 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2640 }
2641#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2642
Paul Bakker5121ce52009-01-03 21:22:43 +00002643 ssl->nb_zero++;
2644
2645 /*
2646 * Three or more empty messages may be a DoS attack
2647 * (excessive CPU consumption).
2648 */
2649 if( ssl->nb_zero > 3 )
2650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002652 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 }
2655 }
2656 else
2657 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002660 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002661 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002662 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002663 }
2664 else
2665#endif
2666 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002667 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002668 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2669 if( ++ssl->in_ctr[i - 1] != 0 )
2670 break;
2671
2672 /* The loop goes to its end iff the counter is wrapping */
2673 if( i == ssl_ep_len( ssl ) )
2674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2676 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002677 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002678 }
2679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002681
2682 return( 0 );
2683}
2684
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002685#undef MAC_NONE
2686#undef MAC_PLAINTEXT
2687#undef MAC_CIPHERTEXT
2688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002690/*
2691 * Compression/decompression functions
2692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002694{
2695 int ret;
2696 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002697 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002698 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002699 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002702
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002703 if( len_pre == 0 )
2704 return( 0 );
2705
Paul Bakker2770fbd2012-07-03 13:30:23 +00002706 memcpy( msg_pre, ssl->out_msg, len_pre );
2707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002709 ssl->out_msglen ) );
2710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002712 ssl->out_msg, ssl->out_msglen );
2713
Paul Bakker48916f92012-09-16 19:57:18 +00002714 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2715 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2716 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002717 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002718
Paul Bakker48916f92012-09-16 19:57:18 +00002719 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002720 if( ret != Z_OK )
2721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2723 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002724 }
2725
Angus Grattond8213d02016-05-25 20:56:48 +10002726 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002727 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002730 ssl->out_msglen ) );
2731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002733 ssl->out_msg, ssl->out_msglen );
2734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002736
2737 return( 0 );
2738}
2739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002741{
2742 int ret;
2743 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002744 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002745 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002746 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002749
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002750 if( len_pre == 0 )
2751 return( 0 );
2752
Paul Bakker2770fbd2012-07-03 13:30:23 +00002753 memcpy( msg_pre, ssl->in_msg, len_pre );
2754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002756 ssl->in_msglen ) );
2757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002759 ssl->in_msg, ssl->in_msglen );
2760
Paul Bakker48916f92012-09-16 19:57:18 +00002761 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2762 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2763 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002764 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002765 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002766
Paul Bakker48916f92012-09-16 19:57:18 +00002767 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002768 if( ret != Z_OK )
2769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2771 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002772 }
2773
Angus Grattond8213d02016-05-25 20:56:48 +10002774 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002775 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002778 ssl->in_msglen ) );
2779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002781 ssl->in_msg, ssl->in_msglen );
2782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002784
2785 return( 0 );
2786}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2790static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792#if defined(MBEDTLS_SSL_PROTO_DTLS)
2793static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002794{
2795 /* If renegotiation is not enforced, retransmit until we would reach max
2796 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002797 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002798 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002799 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002800 unsigned char doublings = 1;
2801
2802 while( ratio != 0 )
2803 {
2804 ++doublings;
2805 ratio >>= 1;
2806 }
2807
2808 if( ++ssl->renego_records_seen > doublings )
2809 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002811 return( 0 );
2812 }
2813 }
2814
2815 return( ssl_write_hello_request( ssl ) );
2816}
2817#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002819
Paul Bakker5121ce52009-01-03 21:22:43 +00002820/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002821 * Fill the input message buffer by appending data to it.
2822 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002823 *
2824 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2825 * available (from this read and/or a previous one). Otherwise, an error code
2826 * is returned (possibly EOF or WANT_READ).
2827 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002828 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2829 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2830 * since we always read a whole datagram at once.
2831 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002832 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002833 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002836{
Paul Bakker23986e52011-04-24 08:57:21 +00002837 int ret;
2838 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002842 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002845 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002847 }
2848
Angus Grattond8213d02016-05-25 20:56:48 +10002849 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2852 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002853 }
2854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002856 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002857 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002858 uint32_t timeout;
2859
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002860 /* Just to be sure */
2861 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2862 {
2863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2864 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2865 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2866 }
2867
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002868 /*
2869 * The point is, we need to always read a full datagram at once, so we
2870 * sometimes read more then requested, and handle the additional data.
2871 * It could be the rest of the current record (while fetching the
2872 * header) and/or some other records in the same datagram.
2873 */
2874
2875 /*
2876 * Move to the next record in the already read datagram if applicable
2877 */
2878 if( ssl->next_record_offset != 0 )
2879 {
2880 if( ssl->in_left < ssl->next_record_offset )
2881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2883 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002884 }
2885
2886 ssl->in_left -= ssl->next_record_offset;
2887
2888 if( ssl->in_left != 0 )
2889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002891 ssl->next_record_offset ) );
2892 memmove( ssl->in_hdr,
2893 ssl->in_hdr + ssl->next_record_offset,
2894 ssl->in_left );
2895 }
2896
2897 ssl->next_record_offset = 0;
2898 }
2899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002901 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002902
2903 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002904 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002905 */
2906 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002909 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002910 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002911
2912 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002913 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002914 * are not at the beginning of a new record, the caller did something
2915 * wrong.
2916 */
2917 if( ssl->in_left != 0 )
2918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2920 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002921 }
2922
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002923 /*
2924 * Don't even try to read if time's out already.
2925 * This avoids by-passing the timer when repeatedly receiving messages
2926 * that will end up being dropped.
2927 */
2928 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002929 {
2930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002931 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002932 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002933 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002934 {
Angus Grattond8213d02016-05-25 20:56:48 +10002935 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002938 timeout = ssl->handshake->retransmit_timeout;
2939 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002940 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002943
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002944 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002945 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2946 timeout );
2947 else
2948 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002951
2952 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002954 }
2955
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002956 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002959 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002962 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002963 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002966 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002967 }
2968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002972 return( ret );
2973 }
2974
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002975 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002976 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002978 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002980 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002981 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002984 return( ret );
2985 }
2986
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002987 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002988 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002990 }
2991
Paul Bakker5121ce52009-01-03 21:22:43 +00002992 if( ret < 0 )
2993 return( ret );
2994
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002995 ssl->in_left = ret;
2996 }
2997 else
2998#endif
2999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003001 ssl->in_left, nb_want ) );
3002
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003003 while( ssl->in_left < nb_want )
3004 {
3005 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003006
3007 if( ssl_check_timer( ssl ) != 0 )
3008 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3009 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003010 {
3011 if( ssl->f_recv_timeout != NULL )
3012 {
3013 ret = ssl->f_recv_timeout( ssl->p_bio,
3014 ssl->in_hdr + ssl->in_left, len,
3015 ssl->conf->read_timeout );
3016 }
3017 else
3018 {
3019 ret = ssl->f_recv( ssl->p_bio,
3020 ssl->in_hdr + ssl->in_left, len );
3021 }
3022 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003025 ssl->in_left, nb_want ) );
3026 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003027
3028 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003030
3031 if( ret < 0 )
3032 return( ret );
3033
mohammad160352aecb92018-03-28 23:41:40 -07003034 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003035 {
Darryl Green11999bb2018-03-13 15:22:58 +00003036 MBEDTLS_SSL_DEBUG_MSG( 1,
3037 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003038 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003039 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3040 }
3041
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003042 ssl->in_left += ret;
3043 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003044 }
3045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003047
3048 return( 0 );
3049}
3050
3051/*
3052 * Flush any data not yet written
3053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003055{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003056 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003057 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003060
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003061 if( ssl->f_send == NULL )
3062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003064 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003066 }
3067
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003068 /* Avoid incrementing counter if data is flushed */
3069 if( ssl->out_left == 0 )
3070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003072 return( 0 );
3073 }
3074
Paul Bakker5121ce52009-01-03 21:22:43 +00003075 while( ssl->out_left > 0 )
3076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3078 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003079
Hanno Becker2b1e3542018-08-06 11:19:13 +01003080 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003081 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003084
3085 if( ret <= 0 )
3086 return( ret );
3087
mohammad160352aecb92018-03-28 23:41:40 -07003088 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003089 {
Darryl Green11999bb2018-03-13 15:22:58 +00003090 MBEDTLS_SSL_DEBUG_MSG( 1,
3091 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003092 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003093 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3094 }
3095
Paul Bakker5121ce52009-01-03 21:22:43 +00003096 ssl->out_left -= ret;
3097 }
3098
Hanno Becker2b1e3542018-08-06 11:19:13 +01003099#if defined(MBEDTLS_SSL_PROTO_DTLS)
3100 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003101 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003102 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003103 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003104 else
3105#endif
3106 {
3107 ssl->out_hdr = ssl->out_buf + 8;
3108 }
3109 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003112
3113 return( 0 );
3114}
3115
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003116/*
3117 * Functions to handle the DTLS retransmission state machine
3118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003120/*
3121 * Append current handshake message to current outgoing flight
3122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003124{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3127 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3128 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003129
3130 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003131 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003132 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003135 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003136 }
3137
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003138 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003139 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003142 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003143 }
3144
3145 /* Copy current handshake message with headers */
3146 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3147 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003148 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003149 msg->next = NULL;
3150
3151 /* Append to the current flight */
3152 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003153 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003154 else
3155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003157 while( cur->next != NULL )
3158 cur = cur->next;
3159 cur->next = msg;
3160 }
3161
Hanno Becker3b235902018-08-06 09:54:53 +01003162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003163 return( 0 );
3164}
3165
3166/*
3167 * Free the current flight of handshake messages
3168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003170{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171 mbedtls_ssl_flight_item *cur = flight;
3172 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003173
3174 while( cur != NULL )
3175 {
3176 next = cur->next;
3177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178 mbedtls_free( cur->p );
3179 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003180
3181 cur = next;
3182 }
3183}
3184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3186static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003187#endif
3188
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003189/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003190 * Swap transform_out and out_ctr with the alternative ones
3191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003193{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003195 unsigned char tmp_out_ctr[8];
3196
3197 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003200 return;
3201 }
3202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003204
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003205 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003206 tmp_transform = ssl->transform_out;
3207 ssl->transform_out = ssl->handshake->alt_transform_out;
3208 ssl->handshake->alt_transform_out = tmp_transform;
3209
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003210 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003211 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3212 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003213 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003214
3215 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003216 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3219 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3224 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003225 }
3226 }
3227#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003228}
3229
3230/*
3231 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003232 */
3233int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3234{
3235 int ret = 0;
3236
3237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3238
3239 ret = mbedtls_ssl_flight_transmit( ssl );
3240
3241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3242
3243 return( ret );
3244}
3245
3246/*
3247 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003248 *
3249 * Need to remember the current message in case flush_output returns
3250 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003251 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003252 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003253int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003254{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003255 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003259 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003261
3262 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003263 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003264 ssl_swap_epochs( ssl );
3265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003267 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003268
3269 while( ssl->handshake->cur_msg != NULL )
3270 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003271 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003272 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003273
Hanno Beckere1dcb032018-08-17 16:47:58 +01003274 int const is_finished =
3275 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3276 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3277
Hanno Becker04da1892018-08-14 13:22:10 +01003278 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3279 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3280
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003281 /* Swap epochs before sending Finished: we can't do it after
3282 * sending ChangeCipherSpec, in case write returns WANT_READ.
3283 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003284 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003285 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003287 ssl_swap_epochs( ssl );
3288 }
3289
Hanno Becker67bc7c32018-08-06 11:33:50 +01003290 ret = ssl_get_remaining_payload_in_datagram( ssl );
3291 if( ret < 0 )
3292 return( ret );
3293 max_frag_len = (size_t) ret;
3294
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003295 /* CCS is copied as is, while HS messages may need fragmentation */
3296 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3297 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003298 if( max_frag_len == 0 )
3299 {
3300 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3301 return( ret );
3302
3303 continue;
3304 }
3305
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003306 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003307 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003308 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003309
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003310 /* Update position inside current message */
3311 ssl->handshake->cur_msg_p += cur->len;
3312 }
3313 else
3314 {
3315 const unsigned char * const p = ssl->handshake->cur_msg_p;
3316 const size_t hs_len = cur->len - 12;
3317 const size_t frag_off = p - ( cur->p + 12 );
3318 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003319 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003320
Hanno Beckere1dcb032018-08-17 16:47:58 +01003321 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003322 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003323 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003324 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003325
Hanno Becker67bc7c32018-08-06 11:33:50 +01003326 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3327 return( ret );
3328
3329 continue;
3330 }
3331 max_hs_frag_len = max_frag_len - 12;
3332
3333 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3334 max_hs_frag_len : rem_len;
3335
3336 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003337 {
3338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003339 (unsigned) cur_hs_frag_len,
3340 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003341 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003342
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003343 /* Messages are stored with handshake headers as if not fragmented,
3344 * copy beginning of headers then fill fragmentation fields.
3345 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3346 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003347
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003348 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3349 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3350 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3351
Hanno Becker67bc7c32018-08-06 11:33:50 +01003352 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3353 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3354 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003355
3356 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3357
Hanno Becker3f7b9732018-08-28 09:53:25 +01003358 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003359 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3360 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003361 ssl->out_msgtype = cur->type;
3362
3363 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003364 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003365 }
3366
3367 /* If done with the current message move to the next one if any */
3368 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3369 {
3370 if( cur->next != NULL )
3371 {
3372 ssl->handshake->cur_msg = cur->next;
3373 ssl->handshake->cur_msg_p = cur->next->p + 12;
3374 }
3375 else
3376 {
3377 ssl->handshake->cur_msg = NULL;
3378 ssl->handshake->cur_msg_p = NULL;
3379 }
3380 }
3381
3382 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003383 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003386 return( ret );
3387 }
3388 }
3389
Hanno Becker67bc7c32018-08-06 11:33:50 +01003390 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3391 return( ret );
3392
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003393 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3395 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003396 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003399 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3400 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003401
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003403
3404 return( 0 );
3405}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003406
3407/*
3408 * To be called when the last message of an incoming flight is received.
3409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003411{
3412 /* We won't need to resend that one any more */
3413 ssl_flight_free( ssl->handshake->flight );
3414 ssl->handshake->flight = NULL;
3415 ssl->handshake->cur_msg = NULL;
3416
3417 /* The next incoming flight will start with this msg_seq */
3418 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3419
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003420 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003421 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003422
Hanno Becker0271f962018-08-16 13:23:47 +01003423 /* Clear future message buffering structure. */
3424 ssl_buffering_free( ssl );
3425
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003426 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003427 ssl_set_timer( ssl, 0 );
3428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003429 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3430 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003433 }
3434 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003436}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003437
3438/*
3439 * To be called when the last message of an outgoing flight is send.
3440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003442{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003443 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003444 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3447 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003450 }
3451 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003455
Paul Bakker5121ce52009-01-03 21:22:43 +00003456/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003457 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003458 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003459
3460/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003461 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003462 *
3463 * - fill in handshake headers
3464 * - update handshake checksum
3465 * - DTLS: save message for resending
3466 * - then pass to the record layer
3467 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003468 * DTLS: except for HelloRequest, messages are only queued, and will only be
3469 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003470 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003471 * Inputs:
3472 * - ssl->out_msglen: 4 + actual handshake message len
3473 * (4 is the size of handshake headers for TLS)
3474 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3475 * - ssl->out_msg + 4: the handshake message body
3476 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003477 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003478 * - ssl->out_msglen: the length of the record contents
3479 * (including handshake headers but excluding record headers)
3480 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003481 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003482int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003483{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003484 int ret;
3485 const size_t hs_len = ssl->out_msglen - 4;
3486 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003487
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3489
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003490 /*
3491 * Sanity checks
3492 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003493 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003494 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3495 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003496 /* In SSLv3, the client might send a NoCertificate alert. */
3497#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3498 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3499 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3500 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3501#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3502 {
3503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3504 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3505 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003506 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003507
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003508 /* Whenever we send anything different from a
3509 * HelloRequest we should be in a handshake - double check. */
3510 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3511 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003512 ssl->handshake == NULL )
3513 {
3514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3515 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3516 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003519 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003520 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003522 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3524 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003525 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003526#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003527
Hanno Beckerb50a2532018-08-06 11:52:54 +01003528 /* Double-check that we did not exceed the bounds
3529 * of the outgoing record buffer.
3530 * This should never fail as the various message
3531 * writing functions must obey the bounds of the
3532 * outgoing record buffer, but better be safe.
3533 *
3534 * Note: We deliberately do not check for the MTU or MFL here.
3535 */
3536 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3537 {
3538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3539 "size %u, maximum %u",
3540 (unsigned) ssl->out_msglen,
3541 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3542 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3543 }
3544
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003545 /*
3546 * Fill handshake headers
3547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003549 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003550 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3551 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3552 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003553
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003554 /*
3555 * DTLS has additional fields in the Handshake layer,
3556 * between the length field and the actual payload:
3557 * uint16 message_seq;
3558 * uint24 fragment_offset;
3559 * uint24 fragment_length;
3560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003562 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003563 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003564 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003565 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003566 {
3567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3568 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003569 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003570 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003571 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3572 }
3573
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003574 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003575 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003576
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003577 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003578 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003579 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003580 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3581 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3582 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003583 }
3584 else
3585 {
3586 ssl->out_msg[4] = 0;
3587 ssl->out_msg[5] = 0;
3588 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003589
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003590 /* Handshake hashes are computed without fragmentation,
3591 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003592 memset( ssl->out_msg + 6, 0x00, 3 );
3593 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003594 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003596
Hanno Becker0207e532018-08-28 10:28:28 +01003597 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003598 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3599 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003600 }
3601
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003602 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003604 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003605 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3606 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003607 {
3608 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003611 return( ret );
3612 }
3613 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003614 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003615#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003616 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003617 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003618 {
3619 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3620 return( ret );
3621 }
3622 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003623
3624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3625
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003626 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003627}
3628
3629/*
3630 * Record layer functions
3631 */
3632
3633/*
3634 * Write current record.
3635 *
3636 * Uses:
3637 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3638 * - ssl->out_msglen: length of the record content (excl headers)
3639 * - ssl->out_msg: record content
3640 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003641int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003642{
3643 int ret, done = 0;
3644 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003645 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003646
3647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003650 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003652 {
3653 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003656 return( ret );
3657 }
3658
3659 len = ssl->out_msglen;
3660 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3664 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668 ret = mbedtls_ssl_hw_record_write( ssl );
3669 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3672 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003673 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003674
3675 if( ret == 0 )
3676 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003677 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003679 if( !done )
3680 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003681 unsigned i;
3682 size_t protected_record_size;
3683
Paul Bakker05ef8352012-05-08 09:17:57 +00003684 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003685 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003686 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003687
Hanno Becker19859472018-08-06 09:40:20 +01003688 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003689 ssl->out_len[0] = (unsigned char)( len >> 8 );
3690 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003691
Paul Bakker48916f92012-09-16 19:57:18 +00003692 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003693 {
3694 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003697 return( ret );
3698 }
3699
3700 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003701 ssl->out_len[0] = (unsigned char)( len >> 8 );
3702 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003703 }
3704
Hanno Becker2b1e3542018-08-06 11:19:13 +01003705 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3706
3707#if defined(MBEDTLS_SSL_PROTO_DTLS)
3708 /* In case of DTLS, double-check that we don't exceed
3709 * the remaining space in the datagram. */
3710 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3711 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003712 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003713 if( ret < 0 )
3714 return( ret );
3715
3716 if( protected_record_size > (size_t) ret )
3717 {
3718 /* Should never happen */
3719 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3720 }
3721 }
3722#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003725 "version = [%d:%d], msglen = %d",
3726 ssl->out_hdr[0], ssl->out_hdr[1],
3727 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003729 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003730 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003731
3732 ssl->out_left += protected_record_size;
3733 ssl->out_hdr += protected_record_size;
3734 ssl_update_out_pointers( ssl, ssl->transform_out );
3735
Hanno Becker04484622018-08-06 09:49:38 +01003736 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3737 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3738 break;
3739
3740 /* The loop goes to its end iff the counter is wrapping */
3741 if( i == ssl_ep_len( ssl ) )
3742 {
3743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3744 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3745 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003746 }
3747
Hanno Becker67bc7c32018-08-06 11:33:50 +01003748#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003749 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3750 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003751 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003752 size_t remaining;
3753 ret = ssl_get_remaining_payload_in_datagram( ssl );
3754 if( ret < 0 )
3755 {
3756 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3757 ret );
3758 return( ret );
3759 }
3760
3761 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003762 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003763 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003764 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003765 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003766 else
3767 {
Hanno Becker513815a2018-08-20 11:56:09 +01003768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003769 }
3770 }
3771#endif /* MBEDTLS_SSL_PROTO_DTLS */
3772
3773 if( ( flush == SSL_FORCE_FLUSH ) &&
3774 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003777 return( ret );
3778 }
3779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003781
3782 return( 0 );
3783}
3784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003785#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003786
3787static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3788{
3789 if( ssl->in_msglen < ssl->in_hslen ||
3790 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3791 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3792 {
3793 return( 1 );
3794 }
3795 return( 0 );
3796}
Hanno Becker44650b72018-08-16 12:51:11 +01003797
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003798static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003799{
3800 return( ( ssl->in_msg[9] << 16 ) |
3801 ( ssl->in_msg[10] << 8 ) |
3802 ssl->in_msg[11] );
3803}
3804
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003805static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003806{
3807 return( ( ssl->in_msg[6] << 16 ) |
3808 ( ssl->in_msg[7] << 8 ) |
3809 ssl->in_msg[8] );
3810}
3811
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003812static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003813{
3814 uint32_t msg_len, frag_off, frag_len;
3815
3816 msg_len = ssl_get_hs_total_len( ssl );
3817 frag_off = ssl_get_hs_frag_off( ssl );
3818 frag_len = ssl_get_hs_frag_len( ssl );
3819
3820 if( frag_off > msg_len )
3821 return( -1 );
3822
3823 if( frag_len > msg_len - frag_off )
3824 return( -1 );
3825
3826 if( frag_len + 12 > ssl->in_msglen )
3827 return( -1 );
3828
3829 return( 0 );
3830}
3831
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003832/*
3833 * Mark bits in bitmask (used for DTLS HS reassembly)
3834 */
3835static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3836{
3837 unsigned int start_bits, end_bits;
3838
3839 start_bits = 8 - ( offset % 8 );
3840 if( start_bits != 8 )
3841 {
3842 size_t first_byte_idx = offset / 8;
3843
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003844 /* Special case */
3845 if( len <= start_bits )
3846 {
3847 for( ; len != 0; len-- )
3848 mask[first_byte_idx] |= 1 << ( start_bits - len );
3849
3850 /* Avoid potential issues with offset or len becoming invalid */
3851 return;
3852 }
3853
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003854 offset += start_bits; /* Now offset % 8 == 0 */
3855 len -= start_bits;
3856
3857 for( ; start_bits != 0; start_bits-- )
3858 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3859 }
3860
3861 end_bits = len % 8;
3862 if( end_bits != 0 )
3863 {
3864 size_t last_byte_idx = ( offset + len ) / 8;
3865
3866 len -= end_bits; /* Now len % 8 == 0 */
3867
3868 for( ; end_bits != 0; end_bits-- )
3869 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3870 }
3871
3872 memset( mask + offset / 8, 0xFF, len / 8 );
3873}
3874
3875/*
3876 * Check that bitmask is full
3877 */
3878static int ssl_bitmask_check( unsigned char *mask, size_t len )
3879{
3880 size_t i;
3881
3882 for( i = 0; i < len / 8; i++ )
3883 if( mask[i] != 0xFF )
3884 return( -1 );
3885
3886 for( i = 0; i < len % 8; i++ )
3887 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3888 return( -1 );
3889
3890 return( 0 );
3891}
3892
Hanno Becker56e205e2018-08-16 09:06:12 +01003893/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003894static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003895 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003896{
Hanno Becker56e205e2018-08-16 09:06:12 +01003897 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003898
Hanno Becker56e205e2018-08-16 09:06:12 +01003899 alloc_len = 12; /* Handshake header */
3900 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003901
Hanno Beckerd07df862018-08-16 09:14:58 +01003902 if( add_bitmap )
3903 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003904
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003905 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003906}
Hanno Becker56e205e2018-08-16 09:06:12 +01003907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003909
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003910static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003911{
3912 return( ( ssl->in_msg[1] << 16 ) |
3913 ( ssl->in_msg[2] << 8 ) |
3914 ssl->in_msg[3] );
3915}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003916
Simon Butcher99000142016-10-13 17:21:01 +01003917int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003918{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003922 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003924 }
3925
Hanno Becker12555c62018-08-16 12:47:53 +01003926 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003929 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003930 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003932#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003933 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003934 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003935 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003936 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003937
Hanno Becker44650b72018-08-16 12:51:11 +01003938 if( ssl_check_hs_header( ssl ) != 0 )
3939 {
3940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3941 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3942 }
3943
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003944 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003945 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3946 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3947 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3948 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003949 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003950 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3951 {
3952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3953 recv_msg_seq,
3954 ssl->handshake->in_msg_seq ) );
3955 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3956 }
3957
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003958 /* Retransmit only on last message from previous flight, to avoid
3959 * too many retransmissions.
3960 * Besides, No sane server ever retransmits HelloVerifyRequest */
3961 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003965 "message_seq = %d, start_of_flight = %d",
3966 recv_msg_seq,
3967 ssl->handshake->in_flight_start_seq ) );
3968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003972 return( ret );
3973 }
3974 }
3975 else
3976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003978 "message_seq = %d, expected = %d",
3979 recv_msg_seq,
3980 ssl->handshake->in_msg_seq ) );
3981 }
3982
Hanno Becker90333da2017-10-10 11:27:13 +01003983 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003984 }
3985 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003986
Hanno Becker6d97ef52018-08-16 13:09:04 +01003987 /* Message reassembly is handled alongside buffering of future
3988 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003989 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003990 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003991 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003994 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003995 }
3996 }
3997 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003999 /* With TLS we don't handle fragmentation (for now) */
4000 if( ssl->in_msglen < ssl->in_hslen )
4001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4003 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004004 }
4005
Simon Butcher99000142016-10-13 17:21:01 +01004006 return( 0 );
4007}
4008
4009void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4010{
Hanno Becker0271f962018-08-16 13:23:47 +01004011 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004012
Hanno Becker0271f962018-08-16 13:23:47 +01004013 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004014 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004015 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004016 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004017
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004018 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004019#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004020 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004021 ssl->handshake != NULL )
4022 {
Hanno Becker0271f962018-08-16 13:23:47 +01004023 unsigned offset;
4024 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004025
Hanno Becker0271f962018-08-16 13:23:47 +01004026 /* Increment handshake sequence number */
4027 hs->in_msg_seq++;
4028
4029 /*
4030 * Clear up handshake buffering and reassembly structure.
4031 */
4032
4033 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004034 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004035
4036 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004037 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4038 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004039 offset++, hs_buf++ )
4040 {
4041 *hs_buf = *(hs_buf + 1);
4042 }
4043
4044 /* Create a fresh last entry */
4045 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004046 }
4047#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004048}
4049
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004050/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004051 * DTLS anti-replay: RFC 6347 4.1.2.6
4052 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004053 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4054 * Bit n is set iff record number in_window_top - n has been seen.
4055 *
4056 * Usually, in_window_top is the last record number seen and the lsb of
4057 * in_window is set. The only exception is the initial state (record number 0
4058 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4061static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004062{
4063 ssl->in_window_top = 0;
4064 ssl->in_window = 0;
4065}
4066
4067static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4068{
4069 return( ( (uint64_t) buf[0] << 40 ) |
4070 ( (uint64_t) buf[1] << 32 ) |
4071 ( (uint64_t) buf[2] << 24 ) |
4072 ( (uint64_t) buf[3] << 16 ) |
4073 ( (uint64_t) buf[4] << 8 ) |
4074 ( (uint64_t) buf[5] ) );
4075}
4076
4077/*
4078 * Return 0 if sequence number is acceptable, -1 otherwise
4079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004081{
4082 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4083 uint64_t bit;
4084
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004085 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004086 return( 0 );
4087
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004088 if( rec_seqnum > ssl->in_window_top )
4089 return( 0 );
4090
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004091 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004092
4093 if( bit >= 64 )
4094 return( -1 );
4095
4096 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4097 return( -1 );
4098
4099 return( 0 );
4100}
4101
4102/*
4103 * Update replay window on new validated record
4104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004106{
4107 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4108
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004109 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004110 return;
4111
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004112 if( rec_seqnum > ssl->in_window_top )
4113 {
4114 /* Update window_top and the contents of the window */
4115 uint64_t shift = rec_seqnum - ssl->in_window_top;
4116
4117 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004118 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004119 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004120 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004121 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004122 ssl->in_window |= 1;
4123 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004124
4125 ssl->in_window_top = rec_seqnum;
4126 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004127 else
4128 {
4129 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004130 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004131
4132 if( bit < 64 ) /* Always true, but be extra sure */
4133 ssl->in_window |= (uint64_t) 1 << bit;
4134 }
4135}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004136#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004137
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004138#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004139/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004140static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4141
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004142/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004143 * Without any SSL context, check if a datagram looks like a ClientHello with
4144 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004145 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004146 *
4147 * - if cookie is valid, return 0
4148 * - if ClientHello looks superficially valid but cookie is not,
4149 * fill obuf and set olen, then
4150 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4151 * - otherwise return a specific error code
4152 */
4153static int ssl_check_dtls_clihlo_cookie(
4154 mbedtls_ssl_cookie_write_t *f_cookie_write,
4155 mbedtls_ssl_cookie_check_t *f_cookie_check,
4156 void *p_cookie,
4157 const unsigned char *cli_id, size_t cli_id_len,
4158 const unsigned char *in, size_t in_len,
4159 unsigned char *obuf, size_t buf_len, size_t *olen )
4160{
4161 size_t sid_len, cookie_len;
4162 unsigned char *p;
4163
4164 if( f_cookie_write == NULL || f_cookie_check == NULL )
4165 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4166
4167 /*
4168 * Structure of ClientHello with record and handshake headers,
4169 * and expected values. We don't need to check a lot, more checks will be
4170 * done when actually parsing the ClientHello - skipping those checks
4171 * avoids code duplication and does not make cookie forging any easier.
4172 *
4173 * 0-0 ContentType type; copied, must be handshake
4174 * 1-2 ProtocolVersion version; copied
4175 * 3-4 uint16 epoch; copied, must be 0
4176 * 5-10 uint48 sequence_number; copied
4177 * 11-12 uint16 length; (ignored)
4178 *
4179 * 13-13 HandshakeType msg_type; (ignored)
4180 * 14-16 uint24 length; (ignored)
4181 * 17-18 uint16 message_seq; copied
4182 * 19-21 uint24 fragment_offset; copied, must be 0
4183 * 22-24 uint24 fragment_length; (ignored)
4184 *
4185 * 25-26 ProtocolVersion client_version; (ignored)
4186 * 27-58 Random random; (ignored)
4187 * 59-xx SessionID session_id; 1 byte len + sid_len content
4188 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4189 * ...
4190 *
4191 * Minimum length is 61 bytes.
4192 */
4193 if( in_len < 61 ||
4194 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4195 in[3] != 0 || in[4] != 0 ||
4196 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4197 {
4198 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4199 }
4200
4201 sid_len = in[59];
4202 if( sid_len > in_len - 61 )
4203 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4204
4205 cookie_len = in[60 + sid_len];
4206 if( cookie_len > in_len - 60 )
4207 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4208
4209 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4210 cli_id, cli_id_len ) == 0 )
4211 {
4212 /* Valid cookie */
4213 return( 0 );
4214 }
4215
4216 /*
4217 * If we get here, we've got an invalid cookie, let's prepare HVR.
4218 *
4219 * 0-0 ContentType type; copied
4220 * 1-2 ProtocolVersion version; copied
4221 * 3-4 uint16 epoch; copied
4222 * 5-10 uint48 sequence_number; copied
4223 * 11-12 uint16 length; olen - 13
4224 *
4225 * 13-13 HandshakeType msg_type; hello_verify_request
4226 * 14-16 uint24 length; olen - 25
4227 * 17-18 uint16 message_seq; copied
4228 * 19-21 uint24 fragment_offset; copied
4229 * 22-24 uint24 fragment_length; olen - 25
4230 *
4231 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4232 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4233 *
4234 * Minimum length is 28.
4235 */
4236 if( buf_len < 28 )
4237 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4238
4239 /* Copy most fields and adapt others */
4240 memcpy( obuf, in, 25 );
4241 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4242 obuf[25] = 0xfe;
4243 obuf[26] = 0xff;
4244
4245 /* Generate and write actual cookie */
4246 p = obuf + 28;
4247 if( f_cookie_write( p_cookie,
4248 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4249 {
4250 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4251 }
4252
4253 *olen = p - obuf;
4254
4255 /* Go back and fill length fields */
4256 obuf[27] = (unsigned char)( *olen - 28 );
4257
4258 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4259 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4260 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4261
4262 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4263 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4264
4265 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4266}
4267
4268/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004269 * Handle possible client reconnect with the same UDP quadruplet
4270 * (RFC 6347 Section 4.2.8).
4271 *
4272 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4273 * that looks like a ClientHello.
4274 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004275 * - if the input looks like a ClientHello without cookies,
4276 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004277 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004278 * - if the input looks like a ClientHello with a valid cookie,
4279 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004280 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004281 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004282 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004283 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004284 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4285 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004286 */
4287static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4288{
4289 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004290 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004291
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004292 ret = ssl_check_dtls_clihlo_cookie(
4293 ssl->conf->f_cookie_write,
4294 ssl->conf->f_cookie_check,
4295 ssl->conf->p_cookie,
4296 ssl->cli_id, ssl->cli_id_len,
4297 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004298 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004299
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004300 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4301
4302 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004303 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004304 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004305 * If the error is permanent we'll catch it later,
4306 * if it's not, then hopefully it'll work next time. */
4307 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4308
4309 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004310 }
4311
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004312 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004313 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004314 /* Got a valid cookie, partially reset context */
4315 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4316 {
4317 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4318 return( ret );
4319 }
4320
4321 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004322 }
4323
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004324 return( ret );
4325}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004326#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004327
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004328/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004329 * ContentType type;
4330 * ProtocolVersion version;
4331 * uint16 epoch; // DTLS only
4332 * uint48 sequence_number; // DTLS only
4333 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004334 *
4335 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004336 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004337 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4338 *
4339 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004340 * 1. proceed with the record if this function returns 0
4341 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4342 * 3. return CLIENT_RECONNECT if this function return that value
4343 * 4. drop the whole datagram if this function returns anything else.
4344 * Point 2 is needed when the peer is resending, and we have already received
4345 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004347static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004348{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004349 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004352
Paul Bakker5121ce52009-01-03 21:22:43 +00004353 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004354 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004355 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004357 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004358 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004359 ssl->in_msgtype,
4360 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004361
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004362 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004363 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4364 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4365 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4366 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004369
4370#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004371 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4372 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004373 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4374#endif /* MBEDTLS_SSL_PROTO_DTLS */
4375 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4376 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004378 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004379 }
4380
4381 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004382 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4385 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004386 }
4387
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004388 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4391 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004392 }
4393
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004394 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004395 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004396 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4399 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004400 }
4401
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004402 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004403 * DTLS-related tests.
4404 * Check epoch before checking length constraint because
4405 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4406 * message gets duplicated before the corresponding Finished message,
4407 * the second ChangeCipherSpec should be discarded because it belongs
4408 * to an old epoch, but not because its length is shorter than
4409 * the minimum record length for packets using the new record transform.
4410 * Note that these two kinds of failures are handled differently,
4411 * as an unexpected record is silently skipped but an invalid
4412 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004413 */
4414#if defined(MBEDTLS_SSL_PROTO_DTLS)
4415 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4416 {
4417 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4418
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004419 /* Check epoch (and sequence number) with DTLS */
4420 if( rec_epoch != ssl->in_epoch )
4421 {
4422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4423 "expected %d, received %d",
4424 ssl->in_epoch, rec_epoch ) );
4425
4426#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4427 /*
4428 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4429 * access the first byte of record content (handshake type), as we
4430 * have an active transform (possibly iv_len != 0), so use the
4431 * fact that the record header len is 13 instead.
4432 */
4433 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4434 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4435 rec_epoch == 0 &&
4436 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4437 ssl->in_left > 13 &&
4438 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4439 {
4440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4441 "from the same port" ) );
4442 return( ssl_handle_possible_reconnect( ssl ) );
4443 }
4444 else
4445#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004446 {
4447 /* Consider buffering the record. */
4448 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4449 {
4450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4451 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4452 }
4453
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004454 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004455 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004456 }
4457
4458#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4459 /* Replay detection only works for the current epoch */
4460 if( rec_epoch == ssl->in_epoch &&
4461 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4462 {
4463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4464 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4465 }
4466#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004467
Hanno Becker52c6dc62017-05-26 16:07:36 +01004468 /* Drop unexpected ApplicationData records,
4469 * except at the beginning of renegotiations */
4470 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4471 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4472#if defined(MBEDTLS_SSL_RENEGOTIATION)
4473 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4474 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4475#endif
4476 )
4477 {
4478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4479 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4480 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004481 }
4482#endif /* MBEDTLS_SSL_PROTO_DTLS */
4483
Hanno Becker52c6dc62017-05-26 16:07:36 +01004484
4485 /* Check length against bounds of the current transform and version */
4486 if( ssl->transform_in == NULL )
4487 {
4488 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004489 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004490 {
4491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4492 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4493 }
4494 }
4495 else
4496 {
4497 if( ssl->in_msglen < ssl->transform_in->minlen )
4498 {
4499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4500 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4501 }
4502
4503#if defined(MBEDTLS_SSL_PROTO_SSL3)
4504 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004505 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004506 {
4507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4508 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4509 }
4510#endif
4511#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4512 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4513 /*
4514 * TLS encrypted messages can have up to 256 bytes of padding
4515 */
4516 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4517 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004518 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004519 {
4520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4521 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4522 }
4523#endif
4524 }
4525
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004526 return( 0 );
4527}
Paul Bakker5121ce52009-01-03 21:22:43 +00004528
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004529/*
4530 * If applicable, decrypt (and decompress) record content
4531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004532static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004533{
4534 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004536 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4537 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4540 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004544 ret = mbedtls_ssl_hw_record_read( ssl );
4545 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4548 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004549 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004550
4551 if( ret == 0 )
4552 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004553 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004554#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004555 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004556 {
4557 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004559 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004560 return( ret );
4561 }
4562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004563 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004564 ssl->in_msg, ssl->in_msglen );
4565
Angus Grattond8213d02016-05-25 20:56:48 +10004566 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4569 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004570 }
4571 }
4572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004574 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004575 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004576 {
4577 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004579 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004580 return( ret );
4581 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004582 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004583#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004585#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004586 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004588 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004589 }
4590#endif
4591
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004592 return( 0 );
4593}
4594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004595static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004596
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004597/*
4598 * Read a record.
4599 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004600 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4601 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4602 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004603 */
Hanno Becker1097b342018-08-15 14:09:41 +01004604
4605/* Helper functions for mbedtls_ssl_read_record(). */
4606static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004607static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4608static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004609
Hanno Becker327c93b2018-08-15 13:56:18 +01004610int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004611 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004612{
4613 int ret;
4614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004616
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004617 if( ssl->keep_current_message == 0 )
4618 {
4619 do {
Simon Butcher99000142016-10-13 17:21:01 +01004620
Hanno Becker26994592018-08-15 14:14:59 +01004621 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004622 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004623 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004624
Hanno Beckere74d5562018-08-15 14:26:08 +01004625 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004626 {
Hanno Becker40f50842018-08-15 14:48:01 +01004627#if defined(MBEDTLS_SSL_PROTO_DTLS)
4628 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004629
Hanno Becker40f50842018-08-15 14:48:01 +01004630 /* We only check for buffered messages if the
4631 * current datagram is fully consumed. */
4632 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004633 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004634 {
Hanno Becker40f50842018-08-15 14:48:01 +01004635 if( ssl_load_buffered_message( ssl ) == 0 )
4636 have_buffered = 1;
4637 }
4638
4639 if( have_buffered == 0 )
4640#endif /* MBEDTLS_SSL_PROTO_DTLS */
4641 {
4642 ret = ssl_get_next_record( ssl );
4643 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4644 continue;
4645
4646 if( ret != 0 )
4647 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004648 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004649 return( ret );
4650 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004651 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004652 }
4653
4654 ret = mbedtls_ssl_handle_message_type( ssl );
4655
Hanno Becker40f50842018-08-15 14:48:01 +01004656#if defined(MBEDTLS_SSL_PROTO_DTLS)
4657 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4658 {
4659 /* Buffer future message */
4660 ret = ssl_buffer_message( ssl );
4661 if( ret != 0 )
4662 return( ret );
4663
4664 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4665 }
4666#endif /* MBEDTLS_SSL_PROTO_DTLS */
4667
Hanno Becker90333da2017-10-10 11:27:13 +01004668 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4669 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004670
4671 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004672 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004673 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004674 return( ret );
4675 }
4676
Hanno Becker327c93b2018-08-15 13:56:18 +01004677 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004678 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004679 {
4680 mbedtls_ssl_update_handshake_status( ssl );
4681 }
Simon Butcher99000142016-10-13 17:21:01 +01004682 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004683 else
Simon Butcher99000142016-10-13 17:21:01 +01004684 {
Hanno Becker02f59072018-08-15 14:00:24 +01004685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004686 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004687 }
4688
4689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4690
4691 return( 0 );
4692}
4693
Hanno Becker40f50842018-08-15 14:48:01 +01004694#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004695static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004696{
Hanno Becker40f50842018-08-15 14:48:01 +01004697 if( ssl->in_left > ssl->next_record_offset )
4698 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004699
Hanno Becker40f50842018-08-15 14:48:01 +01004700 return( 0 );
4701}
4702
4703static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4704{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004705 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004706 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004707 int ret = 0;
4708
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004709 if( hs == NULL )
4710 return( -1 );
4711
Hanno Beckere00ae372018-08-20 09:39:42 +01004712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4713
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004714 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4715 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4716 {
4717 /* Check if we have seen a ChangeCipherSpec before.
4718 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004719 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004720 {
4721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4722 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004723 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004724 }
4725
Hanno Becker39b8bc92018-08-28 17:17:13 +01004726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004727 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4728 ssl->in_msglen = 1;
4729 ssl->in_msg[0] = 1;
4730
4731 /* As long as they are equal, the exact value doesn't matter. */
4732 ssl->in_left = 0;
4733 ssl->next_record_offset = 0;
4734
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004735 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004736 goto exit;
4737 }
Hanno Becker37f95322018-08-16 13:55:32 +01004738
Hanno Beckerb8f50142018-08-28 10:01:34 +01004739#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004740 /* Debug only */
4741 {
4742 unsigned offset;
4743 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4744 {
4745 hs_buf = &hs->buffering.hs[offset];
4746 if( hs_buf->is_valid == 1 )
4747 {
4748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4749 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004750 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004751 }
4752 }
4753 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004754#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004755
4756 /* Check if we have buffered and/or fully reassembled the
4757 * next handshake message. */
4758 hs_buf = &hs->buffering.hs[0];
4759 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4760 {
4761 /* Synthesize a record containing the buffered HS message. */
4762 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4763 ( hs_buf->data[2] << 8 ) |
4764 hs_buf->data[3];
4765
4766 /* Double-check that we haven't accidentally buffered
4767 * a message that doesn't fit into the input buffer. */
4768 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4769 {
4770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4771 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4772 }
4773
4774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4775 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4776 hs_buf->data, msg_len + 12 );
4777
4778 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4779 ssl->in_hslen = msg_len + 12;
4780 ssl->in_msglen = msg_len + 12;
4781 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4782
4783 ret = 0;
4784 goto exit;
4785 }
4786 else
4787 {
4788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4789 hs->in_msg_seq ) );
4790 }
4791
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004792 ret = -1;
4793
4794exit:
4795
4796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4797 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004798}
4799
Hanno Beckera02b0b42018-08-21 17:20:27 +01004800static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4801 size_t desired )
4802{
4803 int offset;
4804 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4806 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004807
Hanno Becker01315ea2018-08-21 17:22:17 +01004808 /* Get rid of future records epoch first, if such exist. */
4809 ssl_free_buffered_record( ssl );
4810
4811 /* Check if we have enough space available now. */
4812 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4813 hs->buffering.total_bytes_buffered ) )
4814 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004816 return( 0 );
4817 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004818
Hanno Becker4f432ad2018-08-28 10:02:32 +01004819 /* We don't have enough space to buffer the next expected handshake
4820 * message. Remove buffers used for future messages to gain space,
4821 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004822 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4823 offset >= 0; offset-- )
4824 {
4825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4826 offset ) );
4827
Hanno Beckerb309b922018-08-23 13:18:05 +01004828 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004829
4830 /* Check if we have enough space available now. */
4831 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4832 hs->buffering.total_bytes_buffered ) )
4833 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004835 return( 0 );
4836 }
4837 }
4838
4839 return( -1 );
4840}
4841
Hanno Becker40f50842018-08-15 14:48:01 +01004842static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4843{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004844 int ret = 0;
4845 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4846
4847 if( hs == NULL )
4848 return( 0 );
4849
4850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4851
4852 switch( ssl->in_msgtype )
4853 {
4854 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004856
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004857 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004858 break;
4859
4860 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004861 {
4862 unsigned recv_msg_seq_offset;
4863 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4864 mbedtls_ssl_hs_buffer *hs_buf;
4865 size_t msg_len = ssl->in_hslen - 12;
4866
4867 /* We should never receive an old handshake
4868 * message - double-check nonetheless. */
4869 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4870 {
4871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4872 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4873 }
4874
4875 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4876 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4877 {
4878 /* Silently ignore -- message too far in the future */
4879 MBEDTLS_SSL_DEBUG_MSG( 2,
4880 ( "Ignore future HS message with sequence number %u, "
4881 "buffering window %u - %u",
4882 recv_msg_seq, ssl->handshake->in_msg_seq,
4883 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4884
4885 goto exit;
4886 }
4887
4888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4889 recv_msg_seq, recv_msg_seq_offset ) );
4890
4891 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4892
4893 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004894 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004895 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004896 size_t reassembly_buf_sz;
4897
Hanno Becker37f95322018-08-16 13:55:32 +01004898 hs_buf->is_fragmented =
4899 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4900
4901 /* We copy the message back into the input buffer
4902 * after reassembly, so check that it's not too large.
4903 * This is an implementation-specific limitation
4904 * and not one from the standard, hence it is not
4905 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004906 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004907 {
4908 /* Ignore message */
4909 goto exit;
4910 }
4911
Hanno Beckere0b150f2018-08-21 15:51:03 +01004912 /* Check if we have enough space to buffer the message. */
4913 if( hs->buffering.total_bytes_buffered >
4914 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4915 {
4916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4917 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4918 }
4919
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004920 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4921 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004922
4923 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4924 hs->buffering.total_bytes_buffered ) )
4925 {
4926 if( recv_msg_seq_offset > 0 )
4927 {
4928 /* If we can't buffer a future message because
4929 * of space limitations -- ignore. */
4930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
4931 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4932 (unsigned) hs->buffering.total_bytes_buffered ) );
4933 goto exit;
4934 }
Hanno Beckere1801392018-08-21 16:51:05 +01004935 else
4936 {
4937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- attempt to make space by freeing buffered future messages\n",
4938 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4939 (unsigned) hs->buffering.total_bytes_buffered ) );
4940 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004941
Hanno Beckera02b0b42018-08-21 17:20:27 +01004942 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004943 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u (%u with bitmap) would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n",
4945 (unsigned) msg_len,
4946 (unsigned) reassembly_buf_sz,
4947 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004948 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004949 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4950 goto exit;
4951 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004952 }
4953
4954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4955 msg_len ) );
4956
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004957 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4958 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004959 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004960 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004961 goto exit;
4962 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004963 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004964
4965 /* Prepare final header: copy msg_type, length and message_seq,
4966 * then add standardised fragment_offset and fragment_length */
4967 memcpy( hs_buf->data, ssl->in_msg, 6 );
4968 memset( hs_buf->data + 6, 0, 3 );
4969 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4970
4971 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004972
4973 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004974 }
4975 else
4976 {
4977 /* Make sure msg_type and length are consistent */
4978 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4979 {
4980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4981 /* Ignore */
4982 goto exit;
4983 }
4984 }
4985
Hanno Becker4422bbb2018-08-20 09:40:19 +01004986 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004987 {
4988 size_t frag_len, frag_off;
4989 unsigned char * const msg = hs_buf->data + 12;
4990
4991 /*
4992 * Check and copy current fragment
4993 */
4994
4995 /* Validation of header fields already done in
4996 * mbedtls_ssl_prepare_handshake_record(). */
4997 frag_off = ssl_get_hs_frag_off( ssl );
4998 frag_len = ssl_get_hs_frag_len( ssl );
4999
5000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5001 frag_off, frag_len ) );
5002 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5003
5004 if( hs_buf->is_fragmented )
5005 {
5006 unsigned char * const bitmask = msg + msg_len;
5007 ssl_bitmask_set( bitmask, frag_off, frag_len );
5008 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5009 msg_len ) == 0 );
5010 }
5011 else
5012 {
5013 hs_buf->is_complete = 1;
5014 }
5015
5016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5017 hs_buf->is_complete ? "" : "not yet " ) );
5018 }
5019
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005020 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005021 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005022
5023 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005024 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005025 break;
5026 }
5027
5028exit:
5029
5030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5031 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005032}
5033#endif /* MBEDTLS_SSL_PROTO_DTLS */
5034
Hanno Becker1097b342018-08-15 14:09:41 +01005035static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005036{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005037 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005038 * Consume last content-layer message and potentially
5039 * update in_msglen which keeps track of the contents'
5040 * consumption state.
5041 *
5042 * (1) Handshake messages:
5043 * Remove last handshake message, move content
5044 * and adapt in_msglen.
5045 *
5046 * (2) Alert messages:
5047 * Consume whole record content, in_msglen = 0.
5048 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005049 * (3) Change cipher spec:
5050 * Consume whole record content, in_msglen = 0.
5051 *
5052 * (4) Application data:
5053 * Don't do anything - the record layer provides
5054 * the application data as a stream transport
5055 * and consumes through mbedtls_ssl_read only.
5056 *
5057 */
5058
5059 /* Case (1): Handshake messages */
5060 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005061 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005062 /* Hard assertion to be sure that no application data
5063 * is in flight, as corrupting ssl->in_msglen during
5064 * ssl->in_offt != NULL is fatal. */
5065 if( ssl->in_offt != NULL )
5066 {
5067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5068 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5069 }
5070
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005071 /*
5072 * Get next Handshake message in the current record
5073 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005074
Hanno Becker4a810fb2017-05-24 16:27:30 +01005075 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005076 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005077 * current handshake content: If DTLS handshake
5078 * fragmentation is used, that's the fragment
5079 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005080 * size here is faulty and should be changed at
5081 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005082 * (2) While it doesn't seem to cause problems, one
5083 * has to be very careful not to assume that in_hslen
5084 * is always <= in_msglen in a sensible communication.
5085 * Again, it's wrong for DTLS handshake fragmentation.
5086 * The following check is therefore mandatory, and
5087 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005088 * Additionally, ssl->in_hslen might be arbitrarily out of
5089 * bounds after handling a DTLS message with an unexpected
5090 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005091 */
5092 if( ssl->in_hslen < ssl->in_msglen )
5093 {
5094 ssl->in_msglen -= ssl->in_hslen;
5095 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5096 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005097
Hanno Becker4a810fb2017-05-24 16:27:30 +01005098 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5099 ssl->in_msg, ssl->in_msglen );
5100 }
5101 else
5102 {
5103 ssl->in_msglen = 0;
5104 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005105
Hanno Becker4a810fb2017-05-24 16:27:30 +01005106 ssl->in_hslen = 0;
5107 }
5108 /* Case (4): Application data */
5109 else if( ssl->in_offt != NULL )
5110 {
5111 return( 0 );
5112 }
5113 /* Everything else (CCS & Alerts) */
5114 else
5115 {
5116 ssl->in_msglen = 0;
5117 }
5118
Hanno Becker1097b342018-08-15 14:09:41 +01005119 return( 0 );
5120}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005121
Hanno Beckere74d5562018-08-15 14:26:08 +01005122static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5123{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005124 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005125 return( 1 );
5126
5127 return( 0 );
5128}
5129
Hanno Becker5f066e72018-08-16 14:56:31 +01005130#if defined(MBEDTLS_SSL_PROTO_DTLS)
5131
5132static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5133{
5134 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5135 if( hs == NULL )
5136 return;
5137
Hanno Becker01315ea2018-08-21 17:22:17 +01005138 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005139 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005140 hs->buffering.total_bytes_buffered -=
5141 hs->buffering.future_record.len;
5142
5143 mbedtls_free( hs->buffering.future_record.data );
5144 hs->buffering.future_record.data = NULL;
5145 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005146}
5147
5148static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5149{
5150 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5151 unsigned char * rec;
5152 size_t rec_len;
5153 unsigned rec_epoch;
5154
5155 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5156 return( 0 );
5157
5158 if( hs == NULL )
5159 return( 0 );
5160
Hanno Becker5f066e72018-08-16 14:56:31 +01005161 rec = hs->buffering.future_record.data;
5162 rec_len = hs->buffering.future_record.len;
5163 rec_epoch = hs->buffering.future_record.epoch;
5164
5165 if( rec == NULL )
5166 return( 0 );
5167
Hanno Becker4cb782d2018-08-20 11:19:05 +01005168 /* Only consider loading future records if the
5169 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005170 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005171 return( 0 );
5172
Hanno Becker5f066e72018-08-16 14:56:31 +01005173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5174
5175 if( rec_epoch != ssl->in_epoch )
5176 {
5177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5178 goto exit;
5179 }
5180
5181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5182
5183 /* Double-check that the record is not too large */
5184 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5185 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5186 {
5187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5188 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5189 }
5190
5191 memcpy( ssl->in_hdr, rec, rec_len );
5192 ssl->in_left = rec_len;
5193 ssl->next_record_offset = 0;
5194
5195 ssl_free_buffered_record( ssl );
5196
5197exit:
5198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5199 return( 0 );
5200}
5201
5202static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5203{
5204 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5205 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005206 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005207
5208 /* Don't buffer future records outside handshakes. */
5209 if( hs == NULL )
5210 return( 0 );
5211
5212 /* Only buffer handshake records (we are only interested
5213 * in Finished messages). */
5214 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5215 return( 0 );
5216
5217 /* Don't buffer more than one future epoch record. */
5218 if( hs->buffering.future_record.data != NULL )
5219 return( 0 );
5220
Hanno Becker01315ea2018-08-21 17:22:17 +01005221 /* Don't buffer record if there's not enough buffering space remaining. */
5222 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5223 hs->buffering.total_bytes_buffered ) )
5224 {
5225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
5226 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5227 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005228 return( 0 );
5229 }
5230
Hanno Becker5f066e72018-08-16 14:56:31 +01005231 /* Buffer record */
5232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5233 ssl->in_epoch + 1 ) );
5234 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5235 rec_hdr_len + ssl->in_msglen );
5236
5237 /* ssl_parse_record_header() only considers records
5238 * of the next epoch as candidates for buffering. */
5239 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005240 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005241
5242 hs->buffering.future_record.data =
5243 mbedtls_calloc( 1, hs->buffering.future_record.len );
5244 if( hs->buffering.future_record.data == NULL )
5245 {
5246 /* If we run out of RAM trying to buffer a
5247 * record from the next epoch, just ignore. */
5248 return( 0 );
5249 }
5250
Hanno Becker01315ea2018-08-21 17:22:17 +01005251 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005252
Hanno Becker01315ea2018-08-21 17:22:17 +01005253 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005254 return( 0 );
5255}
5256
5257#endif /* MBEDTLS_SSL_PROTO_DTLS */
5258
Hanno Beckere74d5562018-08-15 14:26:08 +01005259static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005260{
5261 int ret;
5262
Hanno Becker5f066e72018-08-16 14:56:31 +01005263#if defined(MBEDTLS_SSL_PROTO_DTLS)
5264 /* We might have buffered a future record; if so,
5265 * and if the epoch matches now, load it.
5266 * On success, this call will set ssl->in_left to
5267 * the length of the buffered record, so that
5268 * the calls to ssl_fetch_input() below will
5269 * essentially be no-ops. */
5270 ret = ssl_load_buffered_record( ssl );
5271 if( ret != 0 )
5272 return( ret );
5273#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005275 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005278 return( ret );
5279 }
5280
5281 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005284 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5285 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005286 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005287 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5288 {
5289 ret = ssl_buffer_future_record( ssl );
5290 if( ret != 0 )
5291 return( ret );
5292
5293 /* Fall through to handling of unexpected records */
5294 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5295 }
5296
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005297 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5298 {
5299 /* Skip unexpected record (but not whole datagram) */
5300 ssl->next_record_offset = ssl->in_msglen
5301 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005302
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5304 "(header)" ) );
5305 }
5306 else
5307 {
5308 /* Skip invalid record and the rest of the datagram */
5309 ssl->next_record_offset = 0;
5310 ssl->in_left = 0;
5311
5312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5313 "(header)" ) );
5314 }
5315
5316 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005317 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005318 }
5319#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005320 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005321 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005322
5323 /*
5324 * Read and optionally decrypt the message contents
5325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005326 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5327 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005330 return( ret );
5331 }
5332
5333 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005334#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005335 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005338 if( ssl->next_record_offset < ssl->in_left )
5339 {
5340 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5341 }
5342 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005343 else
5344#endif
5345 ssl->in_left = 0;
5346
5347 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005349#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005350 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005351 {
5352 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5354 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005355 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005356 /* Except when waiting for Finished as a bad mac here
5357 * probably means something went wrong in the handshake
5358 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5359 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5360 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5361 {
5362#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5363 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5364 {
5365 mbedtls_ssl_send_alert_message( ssl,
5366 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5367 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5368 }
5369#endif
5370 return( ret );
5371 }
5372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005373#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005374 if( ssl->conf->badmac_limit != 0 &&
5375 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5378 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005379 }
5380#endif
5381
Hanno Becker4a810fb2017-05-24 16:27:30 +01005382 /* As above, invalid records cause
5383 * dismissal of the whole datagram. */
5384
5385 ssl->next_record_offset = 0;
5386 ssl->in_left = 0;
5387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005389 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005390 }
5391
5392 return( ret );
5393 }
5394 else
5395#endif
5396 {
5397 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005398#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5399 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401 mbedtls_ssl_send_alert_message( ssl,
5402 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5403 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005404 }
5405#endif
5406 return( ret );
5407 }
5408 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005409
Simon Butcher99000142016-10-13 17:21:01 +01005410 return( 0 );
5411}
5412
5413int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5414{
5415 int ret;
5416
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005417 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005418 * Handle particular types of records
5419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005420 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005421 {
Simon Butcher99000142016-10-13 17:21:01 +01005422 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5423 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005424 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005425 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005426 }
5427
Hanno Beckere678eaa2018-08-21 14:57:46 +01005428 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005429 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005430 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005431 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5433 ssl->in_msglen ) );
5434 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005435 }
5436
Hanno Beckere678eaa2018-08-21 14:57:46 +01005437 if( ssl->in_msg[0] != 1 )
5438 {
5439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5440 ssl->in_msg[0] ) );
5441 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5442 }
5443
5444#if defined(MBEDTLS_SSL_PROTO_DTLS)
5445 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5446 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5447 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5448 {
5449 if( ssl->handshake == NULL )
5450 {
5451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5452 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5453 }
5454
5455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5456 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5457 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005458#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005459 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005462 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005463 if( ssl->in_msglen != 2 )
5464 {
5465 /* Note: Standard allows for more than one 2 byte alert
5466 to be packed in a single message, but Mbed TLS doesn't
5467 currently support this. */
5468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5469 ssl->in_msglen ) );
5470 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5471 }
5472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005474 ssl->in_msg[0], ssl->in_msg[1] ) );
5475
5476 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005477 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005482 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005483 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005484 }
5485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005486 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5487 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5490 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005491 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005492
5493#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5494 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5495 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5496 {
Hanno Becker90333da2017-10-10 11:27:13 +01005497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005498 /* Will be handled when trying to parse ServerHello */
5499 return( 0 );
5500 }
5501#endif
5502
5503#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5504 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5505 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5506 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5507 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5508 {
5509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5510 /* Will be handled in mbedtls_ssl_parse_certificate() */
5511 return( 0 );
5512 }
5513#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5514
5515 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005516 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005517 }
5518
Hanno Beckerc76c6192017-06-06 10:03:17 +01005519#if defined(MBEDTLS_SSL_PROTO_DTLS)
5520 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5521 ssl->handshake != NULL &&
5522 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5523 {
5524 ssl_handshake_wrapup_free_hs_transform( ssl );
5525 }
5526#endif
5527
Paul Bakker5121ce52009-01-03 21:22:43 +00005528 return( 0 );
5529}
5530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005531int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005532{
5533 int ret;
5534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005535 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5536 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5537 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005538 {
5539 return( ret );
5540 }
5541
5542 return( 0 );
5543}
5544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005545int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005546 unsigned char level,
5547 unsigned char message )
5548{
5549 int ret;
5550
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005551 if( ssl == NULL || ssl->conf == NULL )
5552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005555 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005558 ssl->out_msglen = 2;
5559 ssl->out_msg[0] = level;
5560 ssl->out_msg[1] = message;
5561
Hanno Becker67bc7c32018-08-06 11:33:50 +01005562 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005565 return( ret );
5566 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005568
5569 return( 0 );
5570}
5571
Paul Bakker5121ce52009-01-03 21:22:43 +00005572/*
5573 * Handshake functions
5574 */
Hanno Becker21489932019-02-05 13:20:55 +00005575#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005576/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005577int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005578{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005582
Hanno Becker7177a882019-02-05 13:36:46 +00005583 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005586 ssl->state++;
5587 return( 0 );
5588 }
5589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5591 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005592}
5593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005595{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005596 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005599
Hanno Becker7177a882019-02-05 13:36:46 +00005600 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005603 ssl->state++;
5604 return( 0 );
5605 }
5606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5608 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005609}
Gilles Peskinef9828522017-05-03 12:28:43 +02005610
Hanno Becker21489932019-02-05 13:20:55 +00005611#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005612/* Some certificate support -> implement write and parse */
5613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005615{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005617 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 const mbedtls_x509_crt *crt;
5619 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005622
Hanno Becker7177a882019-02-05 13:36:46 +00005623 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005626 ssl->state++;
5627 return( 0 );
5628 }
5629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005630#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005631 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005632 {
5633 if( ssl->client_auth == 0 )
5634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005636 ssl->state++;
5637 return( 0 );
5638 }
5639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005641 /*
5642 * If using SSLv3 and got no cert, send an Alert message
5643 * (otherwise an empty Certificate message will be sent).
5644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5646 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005647 {
5648 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5650 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5651 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005654 goto write_msg;
5655 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005656#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005657 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658#endif /* MBEDTLS_SSL_CLI_C */
5659#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005660 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005662 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5665 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005666 }
5667 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005668#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005670 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005671
5672 /*
5673 * 0 . 0 handshake type
5674 * 1 . 3 handshake length
5675 * 4 . 6 length of all certs
5676 * 7 . 9 length of cert. 1
5677 * 10 . n-1 peer certificate
5678 * n . n+2 length of cert. 2
5679 * n+3 . ... upper level cert, etc.
5680 */
5681 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005683
Paul Bakker29087132010-03-21 21:03:34 +00005684 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005685 {
5686 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005687 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005690 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005692 }
5693
5694 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5695 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5696 ssl->out_msg[i + 2] = (unsigned char)( n );
5697
5698 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5699 i += n; crt = crt->next;
5700 }
5701
5702 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5703 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5704 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5705
5706 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5708 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005709
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005710#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005711write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005712#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005713
5714 ssl->state++;
5715
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005716 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005717 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005718 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005719 return( ret );
5720 }
5721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005723
Paul Bakkered27a042013-04-18 22:46:23 +02005724 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005725}
5726
Hanno Becker84879e32019-01-31 07:44:03 +00005727#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00005728
5729#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005730static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5731 unsigned char *crt_buf,
5732 size_t crt_buf_len )
5733{
5734 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5735
5736 if( peer_crt == NULL )
5737 return( -1 );
5738
5739 if( peer_crt->raw.len != crt_buf_len )
5740 return( -1 );
5741
Hanno Becker46f34d02019-02-08 14:00:04 +00005742 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005743}
Hanno Becker177475a2019-02-05 17:02:46 +00005744#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5745static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5746 unsigned char *crt_buf,
5747 size_t crt_buf_len )
5748{
5749 int ret;
5750 unsigned char const * const peer_cert_digest =
5751 ssl->session->peer_cert_digest;
5752 mbedtls_md_type_t const peer_cert_digest_type =
5753 ssl->session->peer_cert_digest_type;
5754 mbedtls_md_info_t const * const digest_info =
5755 mbedtls_md_info_from_type( peer_cert_digest_type );
5756 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5757 size_t digest_len;
5758
5759 if( peer_cert_digest == NULL || digest_info == NULL )
5760 return( -1 );
5761
5762 digest_len = mbedtls_md_get_size( digest_info );
5763 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5764 return( -1 );
5765
5766 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5767 if( ret != 0 )
5768 return( -1 );
5769
5770 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5771}
5772#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00005773#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005774
Hanno Becker1294a0b2019-02-05 12:38:15 +00005775static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5776{
5777 if( session->peer_cert != NULL )
5778 {
5779 mbedtls_x509_crt_free( session->peer_cert );
5780 mbedtls_free( session->peer_cert );
5781 session->peer_cert = NULL;
5782 }
Hanno Becker9198ad12019-02-05 17:00:50 +00005783
5784#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5785 if( session->peer_cert_digest != NULL )
5786 {
5787 /* Zeroization is not necessary. */
5788 mbedtls_free( session->peer_cert_digest );
5789 session->peer_cert_digest = NULL;
5790 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5791 session->peer_cert_digest_len = 0;
5792 }
5793#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker1294a0b2019-02-05 12:38:15 +00005794}
5795
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005796/*
5797 * Once the certificate message is read, parse it into a cert chain and
5798 * perform basic checks, but leave actual verification to the caller
5799 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005800static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5801 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00005802{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005803 int ret;
5804#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5805 int crt_cnt=0;
5806#endif
Paul Bakker23986e52011-04-24 08:57:21 +00005807 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005808 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005810 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005813 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5814 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005815 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005816 }
5817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5819 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005822 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5823 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005825 }
5826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005828
Paul Bakker5121ce52009-01-03 21:22:43 +00005829 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005830 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005831 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005832 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005833
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005834 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005835 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005838 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5839 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005840 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005841 }
5842
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005843 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5844 i += 3;
5845
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005846 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005847 while( i < ssl->in_hslen )
5848 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005849 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02005850 if ( i + 3 > ssl->in_hslen ) {
5851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005852 mbedtls_ssl_send_alert_message( ssl,
5853 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5854 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02005855 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5856 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005857 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5858 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005859 if( ssl->in_msg[i] != 0 )
5860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005862 mbedtls_ssl_send_alert_message( ssl,
5863 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5864 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005866 }
5867
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005868 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005869 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5870 | (unsigned int) ssl->in_msg[i + 2];
5871 i += 3;
5872
5873 if( n < 128 || i + n > ssl->in_hslen )
5874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005876 mbedtls_ssl_send_alert_message( ssl,
5877 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5878 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005880 }
5881
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005882 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005883#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5884 if( crt_cnt++ == 0 &&
5885 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5886 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005887 {
Hanno Becker46f34d02019-02-08 14:00:04 +00005888 /* During client-side renegotiation, check that the server's
5889 * end-CRTs hasn't changed compared to the initial handshake,
5890 * mitigating the triple handshake attack. On success, reuse
5891 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005892 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
5893 if( ssl_check_peer_crt_unchanged( ssl,
5894 &ssl->in_msg[i],
5895 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005896 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
5898 mbedtls_ssl_send_alert_message( ssl,
5899 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5900 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
5901 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005902 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005903
5904 /* Now we can safely free the original chain. */
5905 ssl_clear_peer_cert( ssl->session );
5906 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005907#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5908
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005909 /* Parse the next certificate in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005910 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005911 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005912 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005913 case 0: /*ok*/
5914 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5915 /* Ignore certificate with an unknown algorithm: maybe a
5916 prior certificate was already trusted. */
5917 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005918
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005919 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5920 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5921 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005922
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005923 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5924 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5925 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005926
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005927 default:
5928 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5929 crt_parse_der_failed:
5930 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
5931 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
5932 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005933 }
5934
5935 i += n;
5936 }
5937
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005938 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005939 return( 0 );
5940}
5941
Hanno Becker4a55f632019-02-05 12:49:06 +00005942#if defined(MBEDTLS_SSL_SRV_C)
5943static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
5944{
5945 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5946 return( -1 );
5947
5948#if defined(MBEDTLS_SSL_PROTO_SSL3)
5949 /*
5950 * Check if the client sent an empty certificate
5951 */
5952 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
5953 {
5954 if( ssl->in_msglen == 2 &&
5955 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5956 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5957 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5958 {
5959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
5960 return( 0 );
5961 }
5962
5963 return( -1 );
5964 }
5965#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5966
5967#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5968 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5969 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5970 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5971 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5972 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
5973 {
5974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
5975 return( 0 );
5976 }
5977
5978 return( -1 );
5979#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5980 MBEDTLS_SSL_PROTO_TLS1_2 */
5981}
5982#endif /* MBEDTLS_SSL_SRV_C */
5983
Hanno Becker28f2fcd2019-02-07 10:11:07 +00005984/* Check if a certificate message is expected.
5985 * Return either
5986 * - SSL_CERTIFICATE_EXPECTED, or
5987 * - SSL_CERTIFICATE_SKIP
5988 * indicating whether a Certificate message is expected or not.
5989 */
5990#define SSL_CERTIFICATE_EXPECTED 0
5991#define SSL_CERTIFICATE_SKIP 1
5992static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
5993 int authmode )
5994{
5995 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5996 ssl->transform_negotiate->ciphersuite_info;
5997
5998 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5999 return( SSL_CERTIFICATE_SKIP );
6000
6001#if defined(MBEDTLS_SSL_SRV_C)
6002 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6003 {
6004 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6005 return( SSL_CERTIFICATE_SKIP );
6006
6007 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6008 {
6009 /* NOTE: Is it intentional that we set verify_result
6010 * to SKIP_VERIFY on server-side only? */
6011 ssl->session_negotiate->verify_result =
6012 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6013 return( SSL_CERTIFICATE_SKIP );
6014 }
6015 }
6016#endif /* MBEDTLS_SSL_SRV_C */
6017
6018 return( SSL_CERTIFICATE_EXPECTED );
6019}
6020
Hanno Becker68636192019-02-05 14:36:34 +00006021static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6022 int authmode,
6023 mbedtls_x509_crt *chain,
6024 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006025{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006026 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006027 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6028 ssl->transform_negotiate->ciphersuite_info;
Hanno Becker68636192019-02-05 14:36:34 +00006029 mbedtls_x509_crt *ca_chain;
6030 mbedtls_x509_crl *ca_crl;
6031
6032 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6033 return( 0 );
6034
6035#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6036 if( ssl->handshake->sni_ca_chain != NULL )
6037 {
6038 ca_chain = ssl->handshake->sni_ca_chain;
6039 ca_crl = ssl->handshake->sni_ca_crl;
6040 }
6041 else
6042#endif
6043 {
6044 ca_chain = ssl->conf->ca_chain;
6045 ca_crl = ssl->conf->ca_crl;
6046 }
6047
6048 /*
6049 * Main check: verify certificate
6050 */
6051 ret = mbedtls_x509_crt_verify_restartable(
6052 chain,
6053 ca_chain, ca_crl,
6054 ssl->conf->cert_profile,
6055 ssl->hostname,
6056 &ssl->session_negotiate->verify_result,
6057 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6058
6059 if( ret != 0 )
6060 {
6061 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6062 }
6063
6064#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6065 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6066 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6067#endif
6068
6069 /*
6070 * Secondary checks: always done, but change 'ret' only if it was 0
6071 */
6072
6073#if defined(MBEDTLS_ECP_C)
6074 {
6075 const mbedtls_pk_context *pk = &chain->pk;
6076
6077 /* If certificate uses an EC key, make sure the curve is OK */
6078 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6079 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6080 {
6081 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6082
6083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6084 if( ret == 0 )
6085 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6086 }
6087 }
6088#endif /* MBEDTLS_ECP_C */
6089
6090 if( mbedtls_ssl_check_cert_usage( chain,
6091 ciphersuite_info,
6092 ! ssl->conf->endpoint,
6093 &ssl->session_negotiate->verify_result ) != 0 )
6094 {
6095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6096 if( ret == 0 )
6097 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6098 }
6099
6100 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6101 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6102 * with details encoded in the verification flags. All other kinds
6103 * of error codes, including those from the user provided f_vrfy
6104 * functions, are treated as fatal and lead to a failure of
6105 * ssl_parse_certificate even if verification was optional. */
6106 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6107 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6108 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6109 {
6110 ret = 0;
6111 }
6112
6113 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6114 {
6115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6116 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6117 }
6118
6119 if( ret != 0 )
6120 {
6121 uint8_t alert;
6122
6123 /* The certificate may have been rejected for several reasons.
6124 Pick one and send the corresponding alert. Which alert to send
6125 may be a subject of debate in some cases. */
6126 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6127 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6128 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6129 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6130 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6131 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6132 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6133 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6134 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6135 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6136 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6137 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6138 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6139 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6140 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6141 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6142 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6143 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6144 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6145 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6146 else
6147 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6148 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6149 alert );
6150 }
6151
6152#if defined(MBEDTLS_DEBUG_C)
6153 if( ssl->session_negotiate->verify_result != 0 )
6154 {
6155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6156 ssl->session_negotiate->verify_result ) );
6157 }
6158 else
6159 {
6160 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6161 }
6162#endif /* MBEDTLS_DEBUG_C */
6163
6164 return( ret );
6165}
6166
6167int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6168{
6169 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006170 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006171#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6172 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6173 ? ssl->handshake->sni_authmode
6174 : ssl->conf->authmode;
6175#else
6176 const int authmode = ssl->conf->authmode;
6177#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006178 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006179 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006180
6181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6182
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006183 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6184 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006185 {
6186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006187 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006188 }
6189
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006190#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6191 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006192 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006193 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006194 chain = ssl->handshake->ecrs_peer_cert;
6195 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006196 goto crt_verify;
6197 }
6198#endif
6199
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006200 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006201 {
6202 /* mbedtls_ssl_read_record may have sent an alert already. We
6203 let it decide whether to alert. */
6204 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006205 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006206 }
6207
Hanno Becker4a55f632019-02-05 12:49:06 +00006208#if defined(MBEDTLS_SSL_SRV_C)
6209 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6210 {
6211 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006212
6213 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006214 ret = 0;
6215 else
6216 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006217
Hanno Becker6bdfab22019-02-05 13:11:17 +00006218 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006219 }
6220#endif /* MBEDTLS_SSL_SRV_C */
6221
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006222 /* Clear existing peer CRT structure in case we tried to
6223 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006224 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006225
6226 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6227 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006228 {
6229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6230 sizeof( mbedtls_x509_crt ) ) );
6231 mbedtls_ssl_send_alert_message( ssl,
6232 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6233 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006234
Hanno Becker3dad3112019-02-05 17:19:52 +00006235 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6236 goto exit;
6237 }
6238 mbedtls_x509_crt_init( chain );
6239
6240 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006241 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006242 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006243
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006244#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6245 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006246 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006247
6248crt_verify:
6249 if( ssl->handshake->ecrs_enabled)
6250 rs_ctx = &ssl->handshake->ecrs_ctx;
6251#endif
6252
Hanno Becker68636192019-02-05 14:36:34 +00006253 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006254 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006255 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006256 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006257
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006258#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker3dad3112019-02-05 17:19:52 +00006259
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006260 /* Remember digest of the peer's end-CRT. */
6261 ssl->session_negotiate->peer_cert_digest =
6262 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6263 if( ssl->session_negotiate->peer_cert_digest == NULL )
6264 {
6265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6266 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6267 mbedtls_ssl_send_alert_message( ssl,
6268 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6269 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker3dad3112019-02-05 17:19:52 +00006270
6271 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6272 goto exit;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006273 }
6274 ret = mbedtls_md( mbedtls_md_info_from_type(
Hanno Becker3dad3112019-02-05 17:19:52 +00006275 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6276 chain->raw.p, chain->raw.len,
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006277 ssl->session_negotiate->peer_cert_digest );
6278 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006279 goto exit;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006280
6281 ssl->session_negotiate->peer_cert_digest_type =
6282 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6283 ssl->session_negotiate->peer_cert_digest_len =
6284 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6285#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6286
Hanno Beckera2747532019-02-06 16:19:04 +00006287#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6288 /* Make a copy of the peer's raw public key. */
6289 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6290 {
6291 unsigned char *p, *end;
6292 p = chain->pk_raw.p;
6293 end = p + chain->pk_raw.len;
6294 ret = mbedtls_pk_parse_subpubkey( &p, end,
6295 &ssl->handshake->peer_pubkey );
6296 if( ret != 0 )
6297 {
6298 /* We should have parsed the public key before. */
6299 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6300 goto exit;
6301 }
6302 }
6303#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6304
Hanno Becker3dad3112019-02-05 17:19:52 +00006305 ssl->session_negotiate->peer_cert = chain;
6306 chain = NULL;
6307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006309
Hanno Becker6bdfab22019-02-05 13:11:17 +00006310exit:
6311
Hanno Becker3dad3112019-02-05 17:19:52 +00006312 if( ret == 0 )
6313 ssl->state++;
6314
6315#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6316 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6317 {
6318 ssl->handshake->ecrs_peer_cert = chain;
6319 chain = NULL;
6320 }
6321#endif
6322
6323 if( chain != NULL )
6324 {
6325 mbedtls_x509_crt_free( chain );
6326 mbedtls_free( chain );
6327 }
6328
Paul Bakker5121ce52009-01-03 21:22:43 +00006329 return( ret );
6330}
Hanno Becker21489932019-02-05 13:20:55 +00006331#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006334{
6335 int ret;
6336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006339 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006340 ssl->out_msglen = 1;
6341 ssl->out_msg[0] = 1;
6342
Paul Bakker5121ce52009-01-03 21:22:43 +00006343 ssl->state++;
6344
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006345 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006346 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006347 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006348 return( ret );
6349 }
6350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006352
6353 return( 0 );
6354}
6355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006356int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006357{
6358 int ret;
6359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006361
Hanno Becker327c93b2018-08-15 13:56:18 +01006362 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006365 return( ret );
6366 }
6367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006368 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006371 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6372 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006373 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006374 }
6375
Hanno Beckere678eaa2018-08-21 14:57:46 +01006376 /* CCS records are only accepted if they have length 1 and content '1',
6377 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006378
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006379 /*
6380 * Switch to our negotiated transform and session parameters for inbound
6381 * data.
6382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006384 ssl->transform_in = ssl->transform_negotiate;
6385 ssl->session_in = ssl->session_negotiate;
6386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006387#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006388 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006390#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006391 ssl_dtls_replay_reset( ssl );
6392#endif
6393
6394 /* Increment epoch */
6395 if( ++ssl->in_epoch == 0 )
6396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006398 /* This is highly unlikely to happen for legitimate reasons, so
6399 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006401 }
6402 }
6403 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006404#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006405 memset( ssl->in_ctr, 0, 8 );
6406
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006407 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006409#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6410 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006412 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006414 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006415 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6416 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006417 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006418 }
6419 }
6420#endif
6421
Paul Bakker5121ce52009-01-03 21:22:43 +00006422 ssl->state++;
6423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006425
6426 return( 0 );
6427}
6428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006429void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6430 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006431{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006432 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6435 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6436 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006437 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006438 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006439#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006440#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6441#if defined(MBEDTLS_SHA512_C)
6442 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006443 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6444 else
6445#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006446#if defined(MBEDTLS_SHA256_C)
6447 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006448 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006449 else
6450#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006451#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006454 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006455 }
Paul Bakker380da532012-04-18 16:10:25 +00006456}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006458void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006459{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006460#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6461 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006462 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6463 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006464#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006465#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6466#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006467#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006468 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006469 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6470#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006471 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006472#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006473#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006475#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006476 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006477 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006478#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006479 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006480#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006481#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006482#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006483}
6484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006485static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006486 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006487{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006488#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6489 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006490 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6491 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006492#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6494#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006495#if defined(MBEDTLS_USE_PSA_CRYPTO)
6496 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6497#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006498 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006499#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006500#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006501#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006502#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006503 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006504#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006505 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006506#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006507#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006508#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006509}
6510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6512 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6513static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006514 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006515{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006516 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6517 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006518}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006519#endif
Paul Bakker380da532012-04-18 16:10:25 +00006520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006521#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6522#if defined(MBEDTLS_SHA256_C)
6523static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006524 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006525{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006526#if defined(MBEDTLS_USE_PSA_CRYPTO)
6527 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6528#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006529 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006530#endif
Paul Bakker380da532012-04-18 16:10:25 +00006531}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006532#endif
Paul Bakker380da532012-04-18 16:10:25 +00006533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006534#if defined(MBEDTLS_SHA512_C)
6535static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006536 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006537{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006538#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006539 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006540#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006541 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006542#endif
Paul Bakker380da532012-04-18 16:10:25 +00006543}
Paul Bakker769075d2012-11-24 11:26:46 +01006544#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006548static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006549 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006550{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006551 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006552 mbedtls_md5_context md5;
6553 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006554
Paul Bakker5121ce52009-01-03 21:22:43 +00006555 unsigned char padbuf[48];
6556 unsigned char md5sum[16];
6557 unsigned char sha1sum[20];
6558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006559 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006560 if( !session )
6561 session = ssl->session;
6562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006564
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006565 mbedtls_md5_init( &md5 );
6566 mbedtls_sha1_init( &sha1 );
6567
6568 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6569 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006570
6571 /*
6572 * SSLv3:
6573 * hash =
6574 * MD5( master + pad2 +
6575 * MD5( handshake + sender + master + pad1 ) )
6576 * + SHA1( master + pad2 +
6577 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006578 */
6579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006580#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006581 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6582 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006583#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006585#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006586 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6587 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006588#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006590 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006591 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006592
Paul Bakker1ef83d62012-04-11 12:09:53 +00006593 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006594
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006595 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6596 mbedtls_md5_update_ret( &md5, session->master, 48 );
6597 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6598 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006599
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006600 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6601 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6602 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6603 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006604
Paul Bakker1ef83d62012-04-11 12:09:53 +00006605 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006606
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006607 mbedtls_md5_starts_ret( &md5 );
6608 mbedtls_md5_update_ret( &md5, session->master, 48 );
6609 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6610 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6611 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006612
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006613 mbedtls_sha1_starts_ret( &sha1 );
6614 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6615 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6616 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6617 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006620
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006621 mbedtls_md5_free( &md5 );
6622 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006623
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006624 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6625 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6626 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006629}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006630#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006632#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006633static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006634 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006635{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006636 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006637 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006638 mbedtls_md5_context md5;
6639 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006640 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006642 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006643 if( !session )
6644 session = ssl->session;
6645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006647
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006648 mbedtls_md5_init( &md5 );
6649 mbedtls_sha1_init( &sha1 );
6650
6651 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6652 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006653
Paul Bakker1ef83d62012-04-11 12:09:53 +00006654 /*
6655 * TLSv1:
6656 * hash = PRF( master, finished_label,
6657 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6658 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006660#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006661 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6662 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006663#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006665#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006666 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6667 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006668#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006670 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006671 ? "client finished"
6672 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006673
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006674 mbedtls_md5_finish_ret( &md5, padbuf );
6675 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006676
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006677 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006678 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006680 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006681
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006682 mbedtls_md5_free( &md5 );
6683 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006684
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006685 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6692#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006693static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006695{
6696 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006697 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006698 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006699#if defined(MBEDTLS_USE_PSA_CRYPTO)
6700 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006701 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006702 psa_status_t status;
6703#else
6704 mbedtls_sha256_context sha256;
6705#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006708 if( !session )
6709 session = ssl->session;
6710
Andrzej Kurekeb342242019-01-29 09:14:33 -05006711 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6712 ? "client finished"
6713 : "server finished";
6714
6715#if defined(MBEDTLS_USE_PSA_CRYPTO)
6716 sha256_psa = psa_hash_operation_init();
6717
6718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6719
6720 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6721 if( status != PSA_SUCCESS )
6722 {
6723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6724 return;
6725 }
6726
6727 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6728 if( status != PSA_SUCCESS )
6729 {
6730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6731 return;
6732 }
6733 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6734#else
6735
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006736 mbedtls_sha256_init( &sha256 );
6737
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006739
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006740 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006741
6742 /*
6743 * TLSv1.2:
6744 * hash = PRF( master, finished_label,
6745 * Hash( handshake ) )[0.11]
6746 */
6747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748#if !defined(MBEDTLS_SHA256_ALT)
6749 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006750 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006751#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006752
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006753 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006754 mbedtls_sha256_free( &sha256 );
6755#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006756
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006757 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006758 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006761
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006762 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006765}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006769static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006770 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006771{
6772 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006773 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006774 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006775#if defined(MBEDTLS_USE_PSA_CRYPTO)
6776 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006777 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006778 psa_status_t status;
6779#else
6780 mbedtls_sha512_context sha512;
6781#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006784 if( !session )
6785 session = ssl->session;
6786
Andrzej Kurekeb342242019-01-29 09:14:33 -05006787 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6788 ? "client finished"
6789 : "server finished";
6790
6791#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006792 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05006793
6794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6795
Andrzej Kurek972fba52019-01-30 03:29:12 -05006796 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006797 if( status != PSA_SUCCESS )
6798 {
6799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6800 return;
6801 }
6802
Andrzej Kurek972fba52019-01-30 03:29:12 -05006803 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006804 if( status != PSA_SUCCESS )
6805 {
6806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6807 return;
6808 }
6809 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6810#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006811 mbedtls_sha512_init( &sha512 );
6812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006814
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006815 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006816
6817 /*
6818 * TLSv1.2:
6819 * hash = PRF( master, finished_label,
6820 * Hash( handshake ) )[0.11]
6821 */
6822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006824 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6825 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006826#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006827
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006828 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006829 mbedtls_sha512_free( &sha512 );
6830#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006831
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006832 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006833 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006835 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006836
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006837 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006840}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006841#endif /* MBEDTLS_SHA512_C */
6842#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006844static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006845{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006847
6848 /*
6849 * Free our handshake params
6850 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006851 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006853 ssl->handshake = NULL;
6854
6855 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006856 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006857 */
6858 if( ssl->transform )
6859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006860 mbedtls_ssl_transform_free( ssl->transform );
6861 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006862 }
6863 ssl->transform = ssl->transform_negotiate;
6864 ssl->transform_negotiate = NULL;
6865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006867}
6868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006869void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006870{
6871 int resume = ssl->handshake->resume;
6872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006873 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875#if defined(MBEDTLS_SSL_RENEGOTIATION)
6876 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006878 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006879 ssl->renego_records_seen = 0;
6880 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006881#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006882
6883 /*
6884 * Free the previous session and switch in the current one
6885 */
Paul Bakker0a597072012-09-25 21:55:46 +00006886 if( ssl->session )
6887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006888#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006889 /* RFC 7366 3.1: keep the EtM state */
6890 ssl->session_negotiate->encrypt_then_mac =
6891 ssl->session->encrypt_then_mac;
6892#endif
6893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006894 mbedtls_ssl_session_free( ssl->session );
6895 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006896 }
6897 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006898 ssl->session_negotiate = NULL;
6899
Paul Bakker0a597072012-09-25 21:55:46 +00006900 /*
6901 * Add cache entry
6902 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006903 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006904 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006905 resume == 0 )
6906 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006907 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006909 }
Paul Bakker0a597072012-09-25 21:55:46 +00006910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006911#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006912 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006913 ssl->handshake->flight != NULL )
6914 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006915 /* Cancel handshake timer */
6916 ssl_set_timer( ssl, 0 );
6917
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006918 /* Keep last flight around in case we need to resend it:
6919 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006921 }
6922 else
6923#endif
6924 ssl_handshake_wrapup_free_hs_transform( ssl );
6925
Paul Bakker48916f92012-09-16 19:57:18 +00006926 ssl->state++;
6927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006929}
6930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006931int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006932{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006933 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006936
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006937 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006938
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006939 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006940
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006941 /*
6942 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6943 * may define some other value. Currently (early 2016), no defined
6944 * ciphersuite does this (and this is unlikely to change as activity has
6945 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6946 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006949#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006950 ssl->verify_data_len = hash_len;
6951 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006952#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006953
Paul Bakker5121ce52009-01-03 21:22:43 +00006954 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6956 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006957
6958 /*
6959 * In case of session resuming, invert the client and server
6960 * ChangeCipherSpec messages order.
6961 */
Paul Bakker0a597072012-09-25 21:55:46 +00006962 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006965 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006967#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006969 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006971#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006972 }
6973 else
6974 ssl->state++;
6975
Paul Bakker48916f92012-09-16 19:57:18 +00006976 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006977 * Switch to our negotiated transform and session parameters for outbound
6978 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006980 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006983 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006984 {
6985 unsigned char i;
6986
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006987 /* Remember current epoch settings for resending */
6988 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006989 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006990
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006991 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006992 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006993
6994 /* Increment epoch */
6995 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006996 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006997 break;
6998
6999 /* The loop goes to its end iff the counter is wrapping */
7000 if( i == 0 )
7001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7003 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007004 }
7005 }
7006 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007008 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007009
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007010 ssl->transform_out = ssl->transform_negotiate;
7011 ssl->session_out = ssl->session_negotiate;
7012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7014 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007016 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7019 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007020 }
7021 }
7022#endif
7023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007025 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007026 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007027#endif
7028
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007029 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007030 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007031 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007032 return( ret );
7033 }
7034
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007035#if defined(MBEDTLS_SSL_PROTO_DTLS)
7036 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7037 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7038 {
7039 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7040 return( ret );
7041 }
7042#endif
7043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007045
7046 return( 0 );
7047}
7048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007049#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007050#define SSL_MAX_HASH_LEN 36
7051#else
7052#define SSL_MAX_HASH_LEN 12
7053#endif
7054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007055int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007056{
Paul Bakker23986e52011-04-24 08:57:21 +00007057 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007058 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007059 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007062
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007063 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007064
Hanno Becker327c93b2018-08-15 13:56:18 +01007065 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007067 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007068 return( ret );
7069 }
7070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007074 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7075 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007076 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007077 }
7078
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007079 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007080#if defined(MBEDTLS_SSL_PROTO_SSL3)
7081 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007082 hash_len = 36;
7083 else
7084#endif
7085 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007087 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7088 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007091 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7092 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007093 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007094 }
7095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007097 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007100 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7101 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007103 }
7104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007105#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007106 ssl->verify_data_len = hash_len;
7107 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007108#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007109
Paul Bakker0a597072012-09-25 21:55:46 +00007110 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007113 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007115#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007117 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007119#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007120 }
7121 else
7122 ssl->state++;
7123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007125 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007127#endif
7128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007130
7131 return( 0 );
7132}
7133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007135{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007136 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7139 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7140 mbedtls_md5_init( &handshake->fin_md5 );
7141 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007142 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7143 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007144#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7146#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007147#if defined(MBEDTLS_USE_PSA_CRYPTO)
7148 handshake->fin_sha256_psa = psa_hash_operation_init();
7149 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7150#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007152 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007153#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007154#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007156#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007157 handshake->fin_sha384_psa = psa_hash_operation_init();
7158 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007159#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007161 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007162#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007163#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007164#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007165
7166 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007167
7168#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7169 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7170 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7171#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173#if defined(MBEDTLS_DHM_C)
7174 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007175#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176#if defined(MBEDTLS_ECDH_C)
7177 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007178#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007179#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007180 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007181#if defined(MBEDTLS_SSL_CLI_C)
7182 handshake->ecjpake_cache = NULL;
7183 handshake->ecjpake_cache_len = 0;
7184#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007185#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007186
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007187#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007188 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007189#endif
7190
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007191#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7192 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7193#endif
Hanno Becker75173122019-02-06 16:18:31 +00007194
7195#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7196 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7197 mbedtls_pk_init( &handshake->peer_pubkey );
7198#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007199}
7200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007202{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7206 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208 mbedtls_md_init( &transform->md_ctx_enc );
7209 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007210}
7211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007213{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007215}
7216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007218{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007219 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007220 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007222 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007224 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007225 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007226
7227 /*
7228 * Either the pointers are now NULL or cleared properly and can be freed.
7229 * Now allocate missing structures.
7230 */
7231 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007232 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007233 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007234 }
Paul Bakker48916f92012-09-16 19:57:18 +00007235
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007236 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007237 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007238 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007239 }
Paul Bakker48916f92012-09-16 19:57:18 +00007240
Paul Bakker82788fb2014-10-20 13:59:19 +02007241 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007242 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007243 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007244 }
Paul Bakker48916f92012-09-16 19:57:18 +00007245
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007246 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007247 if( ssl->handshake == NULL ||
7248 ssl->transform_negotiate == NULL ||
7249 ssl->session_negotiate == NULL )
7250 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007253 mbedtls_free( ssl->handshake );
7254 mbedtls_free( ssl->transform_negotiate );
7255 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007256
7257 ssl->handshake = NULL;
7258 ssl->transform_negotiate = NULL;
7259 ssl->session_negotiate = NULL;
7260
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007261 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007262 }
7263
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007264 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007266 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007267 ssl_handshake_params_init( ssl->handshake );
7268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007269#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007270 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7271 {
7272 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007273
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007274 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7275 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7276 else
7277 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007278
7279 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007280 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007281#endif
7282
Paul Bakker48916f92012-09-16 19:57:18 +00007283 return( 0 );
7284}
7285
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007286#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007287/* Dummy cookie callbacks for defaults */
7288static int ssl_cookie_write_dummy( void *ctx,
7289 unsigned char **p, unsigned char *end,
7290 const unsigned char *cli_id, size_t cli_id_len )
7291{
7292 ((void) ctx);
7293 ((void) p);
7294 ((void) end);
7295 ((void) cli_id);
7296 ((void) cli_id_len);
7297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007298 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007299}
7300
7301static int ssl_cookie_check_dummy( void *ctx,
7302 const unsigned char *cookie, size_t cookie_len,
7303 const unsigned char *cli_id, size_t cli_id_len )
7304{
7305 ((void) ctx);
7306 ((void) cookie);
7307 ((void) cookie_len);
7308 ((void) cli_id);
7309 ((void) cli_id_len);
7310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007312}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007313#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007314
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007315/* Once ssl->out_hdr as the address of the beginning of the
7316 * next outgoing record is set, deduce the other pointers.
7317 *
7318 * Note: For TLS, we save the implicit record sequence number
7319 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7320 * and the caller has to make sure there's space for this.
7321 */
7322
7323static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7324 mbedtls_ssl_transform *transform )
7325{
7326#if defined(MBEDTLS_SSL_PROTO_DTLS)
7327 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7328 {
7329 ssl->out_ctr = ssl->out_hdr + 3;
7330 ssl->out_len = ssl->out_hdr + 11;
7331 ssl->out_iv = ssl->out_hdr + 13;
7332 }
7333 else
7334#endif
7335 {
7336 ssl->out_ctr = ssl->out_hdr - 8;
7337 ssl->out_len = ssl->out_hdr + 3;
7338 ssl->out_iv = ssl->out_hdr + 5;
7339 }
7340
7341 /* Adjust out_msg to make space for explicit IV, if used. */
7342 if( transform != NULL &&
7343 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7344 {
7345 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7346 }
7347 else
7348 ssl->out_msg = ssl->out_iv;
7349}
7350
7351/* Once ssl->in_hdr as the address of the beginning of the
7352 * next incoming record is set, deduce the other pointers.
7353 *
7354 * Note: For TLS, we save the implicit record sequence number
7355 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7356 * and the caller has to make sure there's space for this.
7357 */
7358
7359static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7360 mbedtls_ssl_transform *transform )
7361{
7362#if defined(MBEDTLS_SSL_PROTO_DTLS)
7363 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7364 {
7365 ssl->in_ctr = ssl->in_hdr + 3;
7366 ssl->in_len = ssl->in_hdr + 11;
7367 ssl->in_iv = ssl->in_hdr + 13;
7368 }
7369 else
7370#endif
7371 {
7372 ssl->in_ctr = ssl->in_hdr - 8;
7373 ssl->in_len = ssl->in_hdr + 3;
7374 ssl->in_iv = ssl->in_hdr + 5;
7375 }
7376
7377 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7378 if( transform != NULL &&
7379 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7380 {
7381 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7382 }
7383 else
7384 ssl->in_msg = ssl->in_iv;
7385}
7386
Paul Bakker5121ce52009-01-03 21:22:43 +00007387/*
7388 * Initialize an SSL context
7389 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007390void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7391{
7392 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7393}
7394
7395/*
7396 * Setup an SSL context
7397 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007398
7399static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7400{
7401 /* Set the incoming and outgoing record pointers. */
7402#if defined(MBEDTLS_SSL_PROTO_DTLS)
7403 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7404 {
7405 ssl->out_hdr = ssl->out_buf;
7406 ssl->in_hdr = ssl->in_buf;
7407 }
7408 else
7409#endif /* MBEDTLS_SSL_PROTO_DTLS */
7410 {
7411 ssl->out_hdr = ssl->out_buf + 8;
7412 ssl->in_hdr = ssl->in_buf + 8;
7413 }
7414
7415 /* Derive other internal pointers. */
7416 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7417 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7418}
7419
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007420int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007421 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007422{
Paul Bakker48916f92012-09-16 19:57:18 +00007423 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007424
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007425 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007426
7427 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007428 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007429 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007430
7431 /* Set to NULL in case of an error condition */
7432 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007433
Angus Grattond8213d02016-05-25 20:56:48 +10007434 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7435 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007436 {
Angus Grattond8213d02016-05-25 20:56:48 +10007437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007438 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007439 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007440 }
7441
7442 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7443 if( ssl->out_buf == NULL )
7444 {
7445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007446 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007447 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007448 }
7449
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007450 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007451
Paul Bakker48916f92012-09-16 19:57:18 +00007452 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007453 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007454
7455 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007456
7457error:
7458 mbedtls_free( ssl->in_buf );
7459 mbedtls_free( ssl->out_buf );
7460
7461 ssl->conf = NULL;
7462
7463 ssl->in_buf = NULL;
7464 ssl->out_buf = NULL;
7465
7466 ssl->in_hdr = NULL;
7467 ssl->in_ctr = NULL;
7468 ssl->in_len = NULL;
7469 ssl->in_iv = NULL;
7470 ssl->in_msg = NULL;
7471
7472 ssl->out_hdr = NULL;
7473 ssl->out_ctr = NULL;
7474 ssl->out_len = NULL;
7475 ssl->out_iv = NULL;
7476 ssl->out_msg = NULL;
7477
k-stachowiak9f7798e2018-07-31 16:52:32 +02007478 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007479}
7480
7481/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007482 * Reset an initialized and used SSL context for re-use while retaining
7483 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007484 *
7485 * If partial is non-zero, keep data in the input buffer and client ID.
7486 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007487 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007488static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007489{
Paul Bakker48916f92012-09-16 19:57:18 +00007490 int ret;
7491
Hanno Becker7e772132018-08-10 12:38:21 +01007492#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7493 !defined(MBEDTLS_SSL_SRV_C)
7494 ((void) partial);
7495#endif
7496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007497 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007498
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007499 /* Cancel any possibly running timer */
7500 ssl_set_timer( ssl, 0 );
7501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502#if defined(MBEDTLS_SSL_RENEGOTIATION)
7503 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007504 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007505
7506 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7508 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007509#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007511
Paul Bakker7eb013f2011-10-06 12:37:39 +00007512 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007513 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007514
7515 ssl->in_msgtype = 0;
7516 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007518 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007519 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007520#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007522 ssl_dtls_replay_reset( ssl );
7523#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007524
7525 ssl->in_hslen = 0;
7526 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007527
7528 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007529
7530 ssl->out_msgtype = 0;
7531 ssl->out_msglen = 0;
7532 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7534 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007535 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007536#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007537
Hanno Becker19859472018-08-06 09:40:20 +01007538 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7539
Paul Bakker48916f92012-09-16 19:57:18 +00007540 ssl->transform_in = NULL;
7541 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007542
Hanno Becker78640902018-08-13 16:35:15 +01007543 ssl->session_in = NULL;
7544 ssl->session_out = NULL;
7545
Angus Grattond8213d02016-05-25 20:56:48 +10007546 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007547
7548#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007549 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007550#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7551 {
7552 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007553 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007554 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7557 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7560 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007562 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7563 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007564 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007565 }
7566#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007567
Paul Bakker48916f92012-09-16 19:57:18 +00007568 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007570 mbedtls_ssl_transform_free( ssl->transform );
7571 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007572 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007573 }
Paul Bakker48916f92012-09-16 19:57:18 +00007574
Paul Bakkerc0463502013-02-14 11:19:38 +01007575 if( ssl->session )
7576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577 mbedtls_ssl_session_free( ssl->session );
7578 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007579 ssl->session = NULL;
7580 }
7581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007582#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007583 ssl->alpn_chosen = NULL;
7584#endif
7585
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007586#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007587#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007588 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007589#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007590 {
7591 mbedtls_free( ssl->cli_id );
7592 ssl->cli_id = NULL;
7593 ssl->cli_id_len = 0;
7594 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007595#endif
7596
Paul Bakker48916f92012-09-16 19:57:18 +00007597 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7598 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007599
7600 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007601}
7602
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007603/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007604 * Reset an initialized and used SSL context for re-use while retaining
7605 * all application-set variables, function pointers and data.
7606 */
7607int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7608{
7609 return( ssl_session_reset_int( ssl, 0 ) );
7610}
7611
7612/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007613 * SSL set accessors
7614 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007615void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007616{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007617 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007618}
7619
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007620void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007621{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007622 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007623}
7624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007626void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007627{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007628 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007629}
7630#endif
7631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007633void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007634{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007635 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007636}
7637#endif
7638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007640
Hanno Becker1841b0a2018-08-24 11:13:57 +01007641void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7642 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007643{
7644 ssl->disable_datagram_packing = !allow_packing;
7645}
7646
7647void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7648 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007649{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007650 conf->hs_timeout_min = min;
7651 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007652}
7653#endif
7654
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007655void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007656{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007657 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007658}
7659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007660#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007661void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007662 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007663 void *p_vrfy )
7664{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007665 conf->f_vrfy = f_vrfy;
7666 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007669
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007670void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007671 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007672 void *p_rng )
7673{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007674 conf->f_rng = f_rng;
7675 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007676}
7677
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007678void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007679 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007680 void *p_dbg )
7681{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007682 conf->f_dbg = f_dbg;
7683 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007684}
7685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007686void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007687 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007688 mbedtls_ssl_send_t *f_send,
7689 mbedtls_ssl_recv_t *f_recv,
7690 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007691{
7692 ssl->p_bio = p_bio;
7693 ssl->f_send = f_send;
7694 ssl->f_recv = f_recv;
7695 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007696}
7697
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007698#if defined(MBEDTLS_SSL_PROTO_DTLS)
7699void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7700{
7701 ssl->mtu = mtu;
7702}
7703#endif
7704
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007705void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007706{
7707 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007708}
7709
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007710void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7711 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007712 mbedtls_ssl_set_timer_t *f_set_timer,
7713 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007714{
7715 ssl->p_timer = p_timer;
7716 ssl->f_set_timer = f_set_timer;
7717 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007718
7719 /* Make sure we start with no timer running */
7720 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007721}
7722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007723#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007724void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007725 void *p_cache,
7726 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7727 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007728{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007729 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007730 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007731 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007732}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007733#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735#if defined(MBEDTLS_SSL_CLI_C)
7736int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007737{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007738 int ret;
7739
7740 if( ssl == NULL ||
7741 session == NULL ||
7742 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007743 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007746 }
7747
Hanno Becker52055ae2019-02-06 14:30:46 +00007748 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
7749 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007750 return( ret );
7751
Paul Bakker0a597072012-09-25 21:55:46 +00007752 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007753
7754 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007755}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007756#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007757
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007758void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007759 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007760{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007761 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7762 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7763 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7764 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007765}
7766
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007767void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007768 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007769 int major, int minor )
7770{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007772 return;
7773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007775 return;
7776
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007777 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007778}
7779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007780#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007781void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007782 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007783{
7784 conf->cert_profile = profile;
7785}
7786
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007787/* Append a new keycert entry to a (possibly empty) list */
7788static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7789 mbedtls_x509_crt *cert,
7790 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007791{
niisato8ee24222018-06-25 19:05:48 +09007792 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007793
niisato8ee24222018-06-25 19:05:48 +09007794 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7795 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007796 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007797
niisato8ee24222018-06-25 19:05:48 +09007798 new_cert->cert = cert;
7799 new_cert->key = key;
7800 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007801
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007802 /* Update head is the list was null, else add to the end */
7803 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007804 {
niisato8ee24222018-06-25 19:05:48 +09007805 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007806 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007807 else
7808 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007809 mbedtls_ssl_key_cert *cur = *head;
7810 while( cur->next != NULL )
7811 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007812 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007813 }
7814
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007815 return( 0 );
7816}
7817
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007818int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007819 mbedtls_x509_crt *own_cert,
7820 mbedtls_pk_context *pk_key )
7821{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007822 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007823}
7824
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007825void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007826 mbedtls_x509_crt *ca_chain,
7827 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007828{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007829 conf->ca_chain = ca_chain;
7830 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007831}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007833
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007834#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7835int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7836 mbedtls_x509_crt *own_cert,
7837 mbedtls_pk_context *pk_key )
7838{
7839 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7840 own_cert, pk_key ) );
7841}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007842
7843void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7844 mbedtls_x509_crt *ca_chain,
7845 mbedtls_x509_crl *ca_crl )
7846{
7847 ssl->handshake->sni_ca_chain = ca_chain;
7848 ssl->handshake->sni_ca_crl = ca_crl;
7849}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007850
7851void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7852 int authmode )
7853{
7854 ssl->handshake->sni_authmode = authmode;
7855}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007856#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7857
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007858#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007859/*
7860 * Set EC J-PAKE password for current handshake
7861 */
7862int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7863 const unsigned char *pw,
7864 size_t pw_len )
7865{
7866 mbedtls_ecjpake_role role;
7867
Janos Follath8eb64132016-06-03 15:40:57 +01007868 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007869 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7870
7871 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7872 role = MBEDTLS_ECJPAKE_SERVER;
7873 else
7874 role = MBEDTLS_ECJPAKE_CLIENT;
7875
7876 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7877 role,
7878 MBEDTLS_MD_SHA256,
7879 MBEDTLS_ECP_DP_SECP256R1,
7880 pw, pw_len ) );
7881}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007882#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007885
7886static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
7887{
7888 /* Remove reference to existing PSK, if any. */
7889#if defined(MBEDTLS_USE_PSA_CRYPTO)
7890 if( conf->psk_opaque != 0 )
7891 {
7892 /* The maintenance of the PSK key slot is the
7893 * user's responsibility. */
7894 conf->psk_opaque = 0;
7895 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00007896 /* This and the following branch should never
7897 * be taken simultaenously as we maintain the
7898 * invariant that raw and opaque PSKs are never
7899 * configured simultaneously. As a safeguard,
7900 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007901#endif /* MBEDTLS_USE_PSA_CRYPTO */
7902 if( conf->psk != NULL )
7903 {
7904 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
7905
7906 mbedtls_free( conf->psk );
7907 conf->psk = NULL;
7908 conf->psk_len = 0;
7909 }
7910
7911 /* Remove reference to PSK identity, if any. */
7912 if( conf->psk_identity != NULL )
7913 {
7914 mbedtls_free( conf->psk_identity );
7915 conf->psk_identity = NULL;
7916 conf->psk_identity_len = 0;
7917 }
7918}
7919
Hanno Becker7390c712018-11-15 13:33:04 +00007920/* This function assumes that PSK identity in the SSL config is unset.
7921 * It checks that the provided identity is well-formed and attempts
7922 * to make a copy of it in the SSL config.
7923 * On failure, the PSK identity in the config remains unset. */
7924static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
7925 unsigned char const *psk_identity,
7926 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007927{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007928 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00007929 if( psk_identity == NULL ||
7930 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007931 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007932 {
7933 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7934 }
7935
Hanno Becker7390c712018-11-15 13:33:04 +00007936 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
7937 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007938 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02007939
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007940 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007941 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02007942
7943 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02007944}
7945
Hanno Becker7390c712018-11-15 13:33:04 +00007946int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
7947 const unsigned char *psk, size_t psk_len,
7948 const unsigned char *psk_identity, size_t psk_identity_len )
7949{
7950 int ret;
7951 /* Remove opaque/raw PSK + PSK Identity */
7952 ssl_conf_remove_psk( conf );
7953
7954 /* Check and set raw PSK */
7955 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
7956 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7957 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
7958 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7959 conf->psk_len = psk_len;
7960 memcpy( conf->psk, psk, conf->psk_len );
7961
7962 /* Check and set PSK Identity */
7963 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
7964 if( ret != 0 )
7965 ssl_conf_remove_psk( conf );
7966
7967 return( ret );
7968}
7969
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007970static void ssl_remove_psk( mbedtls_ssl_context *ssl )
7971{
7972#if defined(MBEDTLS_USE_PSA_CRYPTO)
7973 if( ssl->handshake->psk_opaque != 0 )
7974 {
7975 ssl->handshake->psk_opaque = 0;
7976 }
7977 else
7978#endif /* MBEDTLS_USE_PSA_CRYPTO */
7979 if( ssl->handshake->psk != NULL )
7980 {
7981 mbedtls_platform_zeroize( ssl->handshake->psk,
7982 ssl->handshake->psk_len );
7983 mbedtls_free( ssl->handshake->psk );
7984 ssl->handshake->psk_len = 0;
7985 }
7986}
7987
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007988int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7989 const unsigned char *psk, size_t psk_len )
7990{
7991 if( psk == NULL || ssl->handshake == NULL )
7992 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7993
7994 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7995 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7996
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007997 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007998
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007999 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008000 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008001
8002 ssl->handshake->psk_len = psk_len;
8003 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8004
8005 return( 0 );
8006}
8007
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008008#if defined(MBEDTLS_USE_PSA_CRYPTO)
8009int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008010 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008011 const unsigned char *psk_identity,
8012 size_t psk_identity_len )
8013{
Hanno Becker7390c712018-11-15 13:33:04 +00008014 int ret;
8015 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008016 ssl_conf_remove_psk( conf );
8017
Hanno Becker7390c712018-11-15 13:33:04 +00008018 /* Check and set opaque PSK */
8019 if( psk_slot == 0 )
8020 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008021 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008022
8023 /* Check and set PSK Identity */
8024 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8025 psk_identity_len );
8026 if( ret != 0 )
8027 ssl_conf_remove_psk( conf );
8028
8029 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008030}
8031
8032int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008033 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008034{
8035 if( psk_slot == 0 || ssl->handshake == NULL )
8036 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8037
8038 ssl_remove_psk( ssl );
8039 ssl->handshake->psk_opaque = psk_slot;
8040 return( 0 );
8041}
8042#endif /* MBEDTLS_USE_PSA_CRYPTO */
8043
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008044void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008045 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008046 size_t),
8047 void *p_psk )
8048{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008049 conf->f_psk = f_psk;
8050 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008051}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008052#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008053
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008054#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008055
8056#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008057int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008058{
8059 int ret;
8060
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008061 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8062 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8063 {
8064 mbedtls_mpi_free( &conf->dhm_P );
8065 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008066 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008067 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008068
8069 return( 0 );
8070}
Hanno Becker470a8c42017-10-04 15:28:46 +01008071#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008072
Hanno Beckera90658f2017-10-04 15:29:08 +01008073int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8074 const unsigned char *dhm_P, size_t P_len,
8075 const unsigned char *dhm_G, size_t G_len )
8076{
8077 int ret;
8078
8079 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8080 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8081 {
8082 mbedtls_mpi_free( &conf->dhm_P );
8083 mbedtls_mpi_free( &conf->dhm_G );
8084 return( ret );
8085 }
8086
8087 return( 0 );
8088}
Paul Bakker5121ce52009-01-03 21:22:43 +00008089
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008090int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008091{
8092 int ret;
8093
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008094 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8095 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8096 {
8097 mbedtls_mpi_free( &conf->dhm_P );
8098 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008099 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008100 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008101
8102 return( 0 );
8103}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008104#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008105
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008106#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8107/*
8108 * Set the minimum length for Diffie-Hellman parameters
8109 */
8110void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8111 unsigned int bitlen )
8112{
8113 conf->dhm_min_bitlen = bitlen;
8114}
8115#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8116
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008117#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008118/*
8119 * Set allowed/preferred hashes for handshake signatures
8120 */
8121void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8122 const int *hashes )
8123{
8124 conf->sig_hashes = hashes;
8125}
Hanno Becker947194e2017-04-07 13:25:49 +01008126#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008127
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008128#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008129/*
8130 * Set the allowed elliptic curves
8131 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008132void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008133 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008134{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008135 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008136}
Hanno Becker947194e2017-04-07 13:25:49 +01008137#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008138
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008139#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008141{
Hanno Becker947194e2017-04-07 13:25:49 +01008142 /* Initialize to suppress unnecessary compiler warning */
8143 size_t hostname_len = 0;
8144
8145 /* Check if new hostname is valid before
8146 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008147 if( hostname != NULL )
8148 {
8149 hostname_len = strlen( hostname );
8150
8151 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8152 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8153 }
8154
8155 /* Now it's clear that we will overwrite the old hostname,
8156 * so we can free it safely */
8157
8158 if( ssl->hostname != NULL )
8159 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008160 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008161 mbedtls_free( ssl->hostname );
8162 }
8163
8164 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008165
Paul Bakker5121ce52009-01-03 21:22:43 +00008166 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008167 {
8168 ssl->hostname = NULL;
8169 }
8170 else
8171 {
8172 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008173 if( ssl->hostname == NULL )
8174 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008175
Hanno Becker947194e2017-04-07 13:25:49 +01008176 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008177
Hanno Becker947194e2017-04-07 13:25:49 +01008178 ssl->hostname[hostname_len] = '\0';
8179 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008180
8181 return( 0 );
8182}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008183#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008184
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008185#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008186void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008187 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008188 const unsigned char *, size_t),
8189 void *p_sni )
8190{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008191 conf->f_sni = f_sni;
8192 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008193}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008197int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008198{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008199 size_t cur_len, tot_len;
8200 const char **p;
8201
8202 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008203 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8204 * MUST NOT be truncated."
8205 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008206 */
8207 tot_len = 0;
8208 for( p = protos; *p != NULL; p++ )
8209 {
8210 cur_len = strlen( *p );
8211 tot_len += cur_len;
8212
8213 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008215 }
8216
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008217 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008218
8219 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008220}
8221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008222const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008223{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008224 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008225}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008226#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008227
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008228void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008229{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008230 conf->max_major_ver = major;
8231 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008232}
8233
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008234void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008235{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008236 conf->min_major_ver = major;
8237 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008238}
8239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008240#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008241void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008242{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008243 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008244}
8245#endif
8246
Janos Follath088ce432017-04-10 12:42:31 +01008247#if defined(MBEDTLS_SSL_SRV_C)
8248void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8249 char cert_req_ca_list )
8250{
8251 conf->cert_req_ca_list = cert_req_ca_list;
8252}
8253#endif
8254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008255#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008256void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008257{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008258 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008259}
8260#endif
8261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008262#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008263void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008264{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008265 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008266}
8267#endif
8268
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008269#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008270void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008271{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008272 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008273}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008274#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008277int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008278{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008279 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008280 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008283 }
8284
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008285 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008286
8287 return( 0 );
8288}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008289#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008291#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008292void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008293{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008294 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008295}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008296#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008298#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008299void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008300{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008301 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008302}
8303#endif
8304
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008305void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008306{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008307 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008308}
8309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008311void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008312{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008313 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008314}
8315
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008316void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008317{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008318 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008319}
8320
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008321void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008322 const unsigned char period[8] )
8323{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008324 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008325}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008329#if defined(MBEDTLS_SSL_CLI_C)
8330void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008331{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008332 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008333}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008334#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008335
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008336#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008337void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8338 mbedtls_ssl_ticket_write_t *f_ticket_write,
8339 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8340 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008341{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008342 conf->f_ticket_write = f_ticket_write;
8343 conf->f_ticket_parse = f_ticket_parse;
8344 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008345}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008346#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008348
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008349#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8350void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8351 mbedtls_ssl_export_keys_t *f_export_keys,
8352 void *p_export_keys )
8353{
8354 conf->f_export_keys = f_export_keys;
8355 conf->p_export_keys = p_export_keys;
8356}
8357#endif
8358
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008359#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008360void mbedtls_ssl_conf_async_private_cb(
8361 mbedtls_ssl_config *conf,
8362 mbedtls_ssl_async_sign_t *f_async_sign,
8363 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8364 mbedtls_ssl_async_resume_t *f_async_resume,
8365 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008366 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008367{
8368 conf->f_async_sign_start = f_async_sign;
8369 conf->f_async_decrypt_start = f_async_decrypt;
8370 conf->f_async_resume = f_async_resume;
8371 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008372 conf->p_async_config_data = async_config_data;
8373}
8374
Gilles Peskine8f97af72018-04-26 11:46:10 +02008375void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8376{
8377 return( conf->p_async_config_data );
8378}
8379
Gilles Peskine1febfef2018-04-30 11:54:39 +02008380void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008381{
8382 if( ssl->handshake == NULL )
8383 return( NULL );
8384 else
8385 return( ssl->handshake->user_async_ctx );
8386}
8387
Gilles Peskine1febfef2018-04-30 11:54:39 +02008388void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008389 void *ctx )
8390{
8391 if( ssl->handshake != NULL )
8392 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008393}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008394#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008395
Paul Bakker5121ce52009-01-03 21:22:43 +00008396/*
8397 * SSL get accessors
8398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008400{
8401 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8402}
8403
Hanno Becker8b170a02017-10-10 11:51:19 +01008404int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8405{
8406 /*
8407 * Case A: We're currently holding back
8408 * a message for further processing.
8409 */
8410
8411 if( ssl->keep_current_message == 1 )
8412 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008413 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008414 return( 1 );
8415 }
8416
8417 /*
8418 * Case B: Further records are pending in the current datagram.
8419 */
8420
8421#if defined(MBEDTLS_SSL_PROTO_DTLS)
8422 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8423 ssl->in_left > ssl->next_record_offset )
8424 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008425 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008426 return( 1 );
8427 }
8428#endif /* MBEDTLS_SSL_PROTO_DTLS */
8429
8430 /*
8431 * Case C: A handshake message is being processed.
8432 */
8433
Hanno Becker8b170a02017-10-10 11:51:19 +01008434 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8435 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008436 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008437 return( 1 );
8438 }
8439
8440 /*
8441 * Case D: An application data message is being processed
8442 */
8443 if( ssl->in_offt != NULL )
8444 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008446 return( 1 );
8447 }
8448
8449 /*
8450 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008451 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008452 * we implement support for multiple alerts in single records.
8453 */
8454
8455 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8456 return( 0 );
8457}
8458
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008459uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008460{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008461 if( ssl->session != NULL )
8462 return( ssl->session->verify_result );
8463
8464 if( ssl->session_negotiate != NULL )
8465 return( ssl->session_negotiate->verify_result );
8466
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008467 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008468}
8469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008470const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008471{
Paul Bakker926c8e42013-03-06 10:23:34 +01008472 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008473 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008475 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008476}
8477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008478const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008479{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008480#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008481 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008482 {
8483 switch( ssl->minor_ver )
8484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008485 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008486 return( "DTLSv1.0" );
8487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008488 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008489 return( "DTLSv1.2" );
8490
8491 default:
8492 return( "unknown (DTLS)" );
8493 }
8494 }
8495#endif
8496
Paul Bakker43ca69c2011-01-15 17:35:19 +00008497 switch( ssl->minor_ver )
8498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008499 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008500 return( "SSLv3.0" );
8501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008502 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008503 return( "TLSv1.0" );
8504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008505 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008506 return( "TLSv1.1" );
8507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008508 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008509 return( "TLSv1.2" );
8510
Paul Bakker43ca69c2011-01-15 17:35:19 +00008511 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008512 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008513 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008514}
8515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008516int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008517{
Hanno Becker3136ede2018-08-17 15:28:19 +01008518 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008519 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008520 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008521
Hanno Becker78640902018-08-13 16:35:15 +01008522 if( transform == NULL )
8523 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008525#if defined(MBEDTLS_ZLIB_SUPPORT)
8526 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8527 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008528#endif
8529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008530 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008532 case MBEDTLS_MODE_GCM:
8533 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008534 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008535 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008536 transform_expansion = transform->minlen;
8537 break;
8538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008539 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008540
8541 block_size = mbedtls_cipher_get_block_size(
8542 &transform->cipher_ctx_enc );
8543
Hanno Becker3136ede2018-08-17 15:28:19 +01008544 /* Expansion due to the addition of the MAC. */
8545 transform_expansion += transform->maclen;
8546
8547 /* Expansion due to the addition of CBC padding;
8548 * Theoretically up to 256 bytes, but we never use
8549 * more than the block size of the underlying cipher. */
8550 transform_expansion += block_size;
8551
8552 /* For TLS 1.1 or higher, an explicit IV is added
8553 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008554#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8555 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008556 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008557#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008558
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008559 break;
8560
8561 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008564 }
8565
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008566 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008567}
8568
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008569#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8570size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8571{
8572 size_t max_len;
8573
8574 /*
8575 * Assume mfl_code is correct since it was checked when set
8576 */
Angus Grattond8213d02016-05-25 20:56:48 +10008577 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008578
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008579 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008580 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008581 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008582 {
Angus Grattond8213d02016-05-25 20:56:48 +10008583 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008584 }
8585
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008586 /* During a handshake, use the value being negotiated */
8587 if( ssl->session_negotiate != NULL &&
8588 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8589 {
8590 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8591 }
8592
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008593 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008594}
8595#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8596
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008597#if defined(MBEDTLS_SSL_PROTO_DTLS)
8598static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8599{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008600 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8601 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8602 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8603 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8604 return ( 0 );
8605
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008606 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8607 return( ssl->mtu );
8608
8609 if( ssl->mtu == 0 )
8610 return( ssl->handshake->mtu );
8611
8612 return( ssl->mtu < ssl->handshake->mtu ?
8613 ssl->mtu : ssl->handshake->mtu );
8614}
8615#endif /* MBEDTLS_SSL_PROTO_DTLS */
8616
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008617int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8618{
8619 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8620
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008621#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8622 !defined(MBEDTLS_SSL_PROTO_DTLS)
8623 (void) ssl;
8624#endif
8625
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008626#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8627 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8628
8629 if( max_len > mfl )
8630 max_len = mfl;
8631#endif
8632
8633#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008634 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008635 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008636 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008637 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8638 const size_t overhead = (size_t) ret;
8639
8640 if( ret < 0 )
8641 return( ret );
8642
8643 if( mtu <= overhead )
8644 {
8645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8646 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8647 }
8648
8649 if( max_len > mtu - overhead )
8650 max_len = mtu - overhead;
8651 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008652#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008653
Hanno Becker0defedb2018-08-10 12:35:02 +01008654#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8655 !defined(MBEDTLS_SSL_PROTO_DTLS)
8656 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008657#endif
8658
8659 return( (int) max_len );
8660}
8661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008662#if defined(MBEDTLS_X509_CRT_PARSE_C)
8663const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008664{
8665 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008666 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008667
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008668 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008669}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008670#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008672#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00008673int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8674 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008675{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008676 if( ssl == NULL ||
8677 dst == NULL ||
8678 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008679 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008681 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008682 }
8683
Hanno Becker52055ae2019-02-06 14:30:46 +00008684 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008685}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008687
Paul Bakker5121ce52009-01-03 21:22:43 +00008688/*
Paul Bakker1961b702013-01-25 14:49:24 +01008689 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008690 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008691int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008692{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008693 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008694
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008695 if( ssl == NULL || ssl->conf == NULL )
8696 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008698#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008699 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008700 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008701#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008702#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008703 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008704 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008705#endif
8706
Paul Bakker1961b702013-01-25 14:49:24 +01008707 return( ret );
8708}
8709
8710/*
8711 * Perform the SSL handshake
8712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008713int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008714{
8715 int ret = 0;
8716
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008717 if( ssl == NULL || ssl->conf == NULL )
8718 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008722 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008724 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008725
8726 if( ret != 0 )
8727 break;
8728 }
8729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008731
8732 return( ret );
8733}
8734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008735#if defined(MBEDTLS_SSL_RENEGOTIATION)
8736#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008737/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008738 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008740static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008741{
8742 int ret;
8743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008745
8746 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008747 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8748 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008749
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008750 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008751 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008752 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008753 return( ret );
8754 }
8755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008757
8758 return( 0 );
8759}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008760#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008761
8762/*
8763 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008764 * - any side: calling mbedtls_ssl_renegotiate(),
8765 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8766 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008767 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008768 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008770 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008771static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008772{
8773 int ret;
8774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008776
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008777 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8778 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008779
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008780 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8781 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008782#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008783 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008784 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008785 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008786 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008787 ssl->handshake->out_msg_seq = 1;
8788 else
8789 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008790 }
8791#endif
8792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008793 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8794 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008796 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008798 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008799 return( ret );
8800 }
8801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008803
8804 return( 0 );
8805}
8806
8807/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008808 * Renegotiate current connection on client,
8809 * or request renegotiation on server
8810 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008811int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008812{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008813 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008814
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008815 if( ssl == NULL || ssl->conf == NULL )
8816 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008818#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008819 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008820 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008822 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8823 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008825 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008826
8827 /* Did we already try/start sending HelloRequest? */
8828 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008829 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008830
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008831 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008832 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008833#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008835#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008836 /*
8837 * On client, either start the renegotiation process or,
8838 * if already in progress, continue the handshake
8839 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008840 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008842 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8843 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008844
8845 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008847 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008848 return( ret );
8849 }
8850 }
8851 else
8852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008853 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008855 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008856 return( ret );
8857 }
8858 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008859#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008860
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008861 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008862}
8863
8864/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008865 * Check record counters and renegotiate if they're above the limit.
8866 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008867static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008868{
Andres AG2196c7f2016-12-15 17:01:16 +00008869 size_t ep_len = ssl_ep_len( ssl );
8870 int in_ctr_cmp;
8871 int out_ctr_cmp;
8872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008873 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8874 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008875 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008876 {
8877 return( 0 );
8878 }
8879
Andres AG2196c7f2016-12-15 17:01:16 +00008880 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8881 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008882 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008883 ssl->conf->renego_period + ep_len, 8 - ep_len );
8884
8885 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008886 {
8887 return( 0 );
8888 }
8889
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008891 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008892}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008893#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008894
8895/*
8896 * Receive application data decrypted from the SSL layer
8897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008898int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008899{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008900 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008901 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008902
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008903 if( ssl == NULL || ssl->conf == NULL )
8904 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008908#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008909 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008911 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008912 return( ret );
8913
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008914 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008915 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008916 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008917 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008918 return( ret );
8919 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008920 }
8921#endif
8922
Hanno Becker4a810fb2017-05-24 16:27:30 +01008923 /*
8924 * Check if renegotiation is necessary and/or handshake is
8925 * in process. If yes, perform/continue, and fall through
8926 * if an unexpected packet is received while the client
8927 * is waiting for the ServerHello.
8928 *
8929 * (There is no equivalent to the last condition on
8930 * the server-side as it is not treated as within
8931 * a handshake while waiting for the ClientHello
8932 * after a renegotiation request.)
8933 */
8934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008935#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008936 ret = ssl_check_ctr_renegotiate( ssl );
8937 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8938 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008940 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008941 return( ret );
8942 }
8943#endif
8944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008945 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008947 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008948 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8949 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008951 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008952 return( ret );
8953 }
8954 }
8955
Hanno Beckere41158b2017-10-23 13:30:32 +01008956 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008957 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008958 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008959 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008960 if( ssl->f_get_timer != NULL &&
8961 ssl->f_get_timer( ssl->p_timer ) == -1 )
8962 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008963 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008964 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008965
Hanno Becker327c93b2018-08-15 13:56:18 +01008966 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008967 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008968 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8969 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008970
Hanno Becker4a810fb2017-05-24 16:27:30 +01008971 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8972 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008973 }
8974
8975 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008976 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008977 {
8978 /*
8979 * OpenSSL sends empty messages to randomize the IV
8980 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008981 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008983 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008984 return( 0 );
8985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008986 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008987 return( ret );
8988 }
8989 }
8990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008991 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008994
Hanno Becker4a810fb2017-05-24 16:27:30 +01008995 /*
8996 * - For client-side, expect SERVER_HELLO_REQUEST.
8997 * - For server-side, expect CLIENT_HELLO.
8998 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8999 */
9000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009001#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009002 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009003 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009004 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009007
9008 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009009#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009010 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009011 {
9012 continue;
9013 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009014#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009015 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009016 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009017#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009018
Hanno Becker4a810fb2017-05-24 16:27:30 +01009019#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009020 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009021 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009024
9025 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009026#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009027 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009028 {
9029 continue;
9030 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009031#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009032 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009033 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009034#endif /* MBEDTLS_SSL_SRV_C */
9035
Hanno Becker21df7f92017-10-17 11:03:26 +01009036#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009037 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009038 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9039 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9040 ssl->conf->allow_legacy_renegotiation ==
9041 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9042 {
9043 /*
9044 * Accept renegotiation request
9045 */
Paul Bakker48916f92012-09-16 19:57:18 +00009046
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009047 /* DTLS clients need to know renego is server-initiated */
9048#if defined(MBEDTLS_SSL_PROTO_DTLS)
9049 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9050 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9051 {
9052 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9053 }
9054#endif
9055 ret = ssl_start_renegotiation( ssl );
9056 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9057 ret != 0 )
9058 {
9059 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9060 return( ret );
9061 }
9062 }
9063 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009064#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009065 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009066 /*
9067 * Refuse renegotiation
9068 */
9069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009070 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009072#if defined(MBEDTLS_SSL_PROTO_SSL3)
9073 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009074 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009075 /* SSLv3 does not have a "no_renegotiation" warning, so
9076 we send a fatal alert and abort the connection. */
9077 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9078 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9079 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009080 }
9081 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009082#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9083#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9084 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9085 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009087 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9088 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9089 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009090 {
9091 return( ret );
9092 }
Paul Bakker48916f92012-09-16 19:57:18 +00009093 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009094 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009095#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9096 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9099 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009100 }
Paul Bakker48916f92012-09-16 19:57:18 +00009101 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009102
Hanno Becker90333da2017-10-10 11:27:13 +01009103 /* At this point, we don't know whether the renegotiation has been
9104 * completed or not. The cases to consider are the following:
9105 * 1) The renegotiation is complete. In this case, no new record
9106 * has been read yet.
9107 * 2) The renegotiation is incomplete because the client received
9108 * an application data record while awaiting the ServerHello.
9109 * 3) The renegotiation is incomplete because the client received
9110 * a non-handshake, non-application data message while awaiting
9111 * the ServerHello.
9112 * In each of these case, looping will be the proper action:
9113 * - For 1), the next iteration will read a new record and check
9114 * if it's application data.
9115 * - For 2), the loop condition isn't satisfied as application data
9116 * is present, hence continue is the same as break
9117 * - For 3), the loop condition is satisfied and read_record
9118 * will re-deliver the message that was held back by the client
9119 * when expecting the ServerHello.
9120 */
9121 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009122 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009123#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009124 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009125 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009126 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009127 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009128 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009131 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009132 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009133 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009134 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009135 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009136#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009138 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9139 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009142 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009143 }
9144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009145 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9148 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009149 }
9150
9151 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009152
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009153 /* We're going to return something now, cancel timer,
9154 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009155 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009156 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009157
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009158#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009159 /* If we requested renego but received AppData, resend HelloRequest.
9160 * Do it now, after setting in_offt, to avoid taking this branch
9161 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009162#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009163 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009164 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009165 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009166 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009168 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009169 return( ret );
9170 }
9171 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009172#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009173#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009174 }
9175
9176 n = ( len < ssl->in_msglen )
9177 ? len : ssl->in_msglen;
9178
9179 memcpy( buf, ssl->in_offt, n );
9180 ssl->in_msglen -= n;
9181
9182 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009183 {
9184 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009185 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009186 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009187 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009188 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009189 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009190 /* more data available */
9191 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009192 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009195
Paul Bakker23986e52011-04-24 08:57:21 +00009196 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009197}
9198
9199/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009200 * Send application data to be encrypted by the SSL layer, taking care of max
9201 * fragment length and buffer size.
9202 *
9203 * According to RFC 5246 Section 6.2.1:
9204 *
9205 * Zero-length fragments of Application data MAY be sent as they are
9206 * potentially useful as a traffic analysis countermeasure.
9207 *
9208 * Therefore, it is possible that the input message length is 0 and the
9209 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009210 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009211static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009212 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009213{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009214 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9215 const size_t max_len = (size_t) ret;
9216
9217 if( ret < 0 )
9218 {
9219 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9220 return( ret );
9221 }
9222
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009223 if( len > max_len )
9224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009225#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009226 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009229 "maximum fragment length: %d > %d",
9230 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009232 }
9233 else
9234#endif
9235 len = max_len;
9236 }
Paul Bakker887bd502011-06-08 13:10:54 +00009237
Paul Bakker5121ce52009-01-03 21:22:43 +00009238 if( ssl->out_left != 0 )
9239 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009240 /*
9241 * The user has previously tried to send the data and
9242 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9243 * written. In this case, we expect the high-level write function
9244 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009246 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009248 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009249 return( ret );
9250 }
9251 }
Paul Bakker887bd502011-06-08 13:10:54 +00009252 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009253 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009254 /*
9255 * The user is trying to send a message the first time, so we need to
9256 * copy the data into the internal buffers and setup the data structure
9257 * to keep track of partial writes
9258 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009259 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009260 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009261 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009262
Hanno Becker67bc7c32018-08-06 11:33:50 +01009263 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009265 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009266 return( ret );
9267 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009268 }
9269
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009270 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009271}
9272
9273/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009274 * Write application data, doing 1/n-1 splitting if necessary.
9275 *
9276 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009277 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009278 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009280#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009281static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009282 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009283{
9284 int ret;
9285
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009286 if( ssl->conf->cbc_record_splitting ==
9287 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009288 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009289 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9290 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9291 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009292 {
9293 return( ssl_write_real( ssl, buf, len ) );
9294 }
9295
9296 if( ssl->split_done == 0 )
9297 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009298 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009299 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009300 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009301 }
9302
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009303 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9304 return( ret );
9305 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009306
9307 return( ret + 1 );
9308}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009309#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009310
9311/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009312 * Write application data (public-facing wrapper)
9313 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009314int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009315{
9316 int ret;
9317
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009319
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009320 if( ssl == NULL || ssl->conf == NULL )
9321 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9322
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009323#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009324 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9325 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009326 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009327 return( ret );
9328 }
9329#endif
9330
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009331 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009332 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009333 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009334 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009336 return( ret );
9337 }
9338 }
9339
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009340#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009341 ret = ssl_write_split( ssl, buf, len );
9342#else
9343 ret = ssl_write_real( ssl, buf, len );
9344#endif
9345
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009347
9348 return( ret );
9349}
9350
9351/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009352 * Notify the peer that the connection is being closed
9353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009354int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009355{
9356 int ret;
9357
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009358 if( ssl == NULL || ssl->conf == NULL )
9359 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009362
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009363 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009364 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009366 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009368 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9369 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9370 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009372 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009373 return( ret );
9374 }
9375 }
9376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009378
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009379 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009380}
9381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009382void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009383{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009384 if( transform == NULL )
9385 return;
9386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009387#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009388 deflateEnd( &transform->ctx_deflate );
9389 inflateEnd( &transform->ctx_inflate );
9390#endif
9391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009392 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9393 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009395 mbedtls_md_free( &transform->md_ctx_enc );
9396 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009397
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009398 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009399}
9400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009401#if defined(MBEDTLS_X509_CRT_PARSE_C)
9402static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009403{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009404 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009405
9406 while( cur != NULL )
9407 {
9408 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009409 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009410 cur = next;
9411 }
9412}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009413#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009414
Hanno Becker0271f962018-08-16 13:23:47 +01009415#if defined(MBEDTLS_SSL_PROTO_DTLS)
9416
9417static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9418{
9419 unsigned offset;
9420 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9421
9422 if( hs == NULL )
9423 return;
9424
Hanno Becker283f5ef2018-08-24 09:34:47 +01009425 ssl_free_buffered_record( ssl );
9426
Hanno Becker0271f962018-08-16 13:23:47 +01009427 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009428 ssl_buffering_free_slot( ssl, offset );
9429}
9430
9431static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9432 uint8_t slot )
9433{
9434 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9435 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009436
9437 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9438 return;
9439
Hanno Beckere605b192018-08-21 15:59:07 +01009440 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009441 {
Hanno Beckere605b192018-08-21 15:59:07 +01009442 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009443 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009444 mbedtls_free( hs_buf->data );
9445 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009446 }
9447}
9448
9449#endif /* MBEDTLS_SSL_PROTO_DTLS */
9450
Gilles Peskine9b562d52018-04-25 20:32:43 +02009451void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009452{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009453 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9454
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009455 if( handshake == NULL )
9456 return;
9457
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009458#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9459 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9460 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009461 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009462 handshake->async_in_progress = 0;
9463 }
9464#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9465
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009466#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9467 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9468 mbedtls_md5_free( &handshake->fin_md5 );
9469 mbedtls_sha1_free( &handshake->fin_sha1 );
9470#endif
9471#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9472#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009473#if defined(MBEDTLS_USE_PSA_CRYPTO)
9474 psa_hash_abort( &handshake->fin_sha256_psa );
9475#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009476 mbedtls_sha256_free( &handshake->fin_sha256 );
9477#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009478#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009479#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009480#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009481 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009482#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009483 mbedtls_sha512_free( &handshake->fin_sha512 );
9484#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009485#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009486#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009488#if defined(MBEDTLS_DHM_C)
9489 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009490#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009491#if defined(MBEDTLS_ECDH_C)
9492 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009493#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009494#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009495 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009496#if defined(MBEDTLS_SSL_CLI_C)
9497 mbedtls_free( handshake->ecjpake_cache );
9498 handshake->ecjpake_cache = NULL;
9499 handshake->ecjpake_cache_len = 0;
9500#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009501#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009502
Janos Follath4ae5c292016-02-10 11:27:43 +00009503#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9504 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009505 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009507#endif
9508
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009509#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9510 if( handshake->psk != NULL )
9511 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009512 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009513 mbedtls_free( handshake->psk );
9514 }
9515#endif
9516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9518 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009519 /*
9520 * Free only the linked list wrapper, not the keys themselves
9521 * since the belong to the SNI callback
9522 */
9523 if( handshake->sni_key_cert != NULL )
9524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009525 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009526
9527 while( cur != NULL )
9528 {
9529 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009531 cur = next;
9532 }
9533 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009535
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009536#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009537 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00009538 if( handshake->ecrs_peer_cert != NULL )
9539 {
9540 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
9541 mbedtls_free( handshake->ecrs_peer_cert );
9542 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009543#endif
9544
Hanno Becker75173122019-02-06 16:18:31 +00009545#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9546 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9547 mbedtls_pk_free( &handshake->peer_pubkey );
9548#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009550#if defined(MBEDTLS_SSL_PROTO_DTLS)
9551 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009552 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009553 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009554#endif
9555
Hanno Becker4a63ed42019-01-08 11:39:35 +00009556#if defined(MBEDTLS_ECDH_C) && \
9557 defined(MBEDTLS_USE_PSA_CRYPTO)
9558 psa_destroy_key( handshake->ecdh_psa_privkey );
9559#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9560
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009561 mbedtls_platform_zeroize( handshake,
9562 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009563}
9564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009566{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009567 if( session == NULL )
9568 return;
9569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009570#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009571 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009572#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009573
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009574#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009575 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009576#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009577
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009578 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009579}
9580
Paul Bakker5121ce52009-01-03 21:22:43 +00009581/*
9582 * Free an SSL context
9583 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009584void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009585{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009586 if( ssl == NULL )
9587 return;
9588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009590
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009591 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009592 {
Angus Grattond8213d02016-05-25 20:56:48 +10009593 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009594 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009595 }
9596
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009597 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009598 {
Angus Grattond8213d02016-05-25 20:56:48 +10009599 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009600 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009601 }
9602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009604 if( ssl->compress_buf != NULL )
9605 {
Angus Grattond8213d02016-05-25 20:56:48 +10009606 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009607 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009608 }
9609#endif
9610
Paul Bakker48916f92012-09-16 19:57:18 +00009611 if( ssl->transform )
9612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009613 mbedtls_ssl_transform_free( ssl->transform );
9614 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009615 }
9616
9617 if( ssl->handshake )
9618 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009619 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009620 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9621 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623 mbedtls_free( ssl->handshake );
9624 mbedtls_free( ssl->transform_negotiate );
9625 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009626 }
9627
Paul Bakkerc0463502013-02-14 11:19:38 +01009628 if( ssl->session )
9629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630 mbedtls_ssl_session_free( ssl->session );
9631 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009632 }
9633
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009634#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009635 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009636 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009637 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009638 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009639 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009640#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009642#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9643 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009645 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9646 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009647 }
9648#endif
9649
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009650#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009651 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009652#endif
9653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009655
Paul Bakker86f04f42013-02-14 11:20:09 +01009656 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009657 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009658}
9659
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009660/*
9661 * Initialze mbedtls_ssl_config
9662 */
9663void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9664{
9665 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9666}
9667
Simon Butcherc97b6972015-12-27 23:48:17 +00009668#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009669static int ssl_preset_default_hashes[] = {
9670#if defined(MBEDTLS_SHA512_C)
9671 MBEDTLS_MD_SHA512,
9672 MBEDTLS_MD_SHA384,
9673#endif
9674#if defined(MBEDTLS_SHA256_C)
9675 MBEDTLS_MD_SHA256,
9676 MBEDTLS_MD_SHA224,
9677#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009678#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009679 MBEDTLS_MD_SHA1,
9680#endif
9681 MBEDTLS_MD_NONE
9682};
Simon Butcherc97b6972015-12-27 23:48:17 +00009683#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009684
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009685static int ssl_preset_suiteb_ciphersuites[] = {
9686 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9687 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9688 0
9689};
9690
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009691#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009692static int ssl_preset_suiteb_hashes[] = {
9693 MBEDTLS_MD_SHA256,
9694 MBEDTLS_MD_SHA384,
9695 MBEDTLS_MD_NONE
9696};
9697#endif
9698
9699#if defined(MBEDTLS_ECP_C)
9700static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9701 MBEDTLS_ECP_DP_SECP256R1,
9702 MBEDTLS_ECP_DP_SECP384R1,
9703 MBEDTLS_ECP_DP_NONE
9704};
9705#endif
9706
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009707/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009708 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009709 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009710int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009711 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009712{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009713#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009714 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009715#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009716
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009717 /* Use the functions here so that they are covered in tests,
9718 * but otherwise access member directly for efficiency */
9719 mbedtls_ssl_conf_endpoint( conf, endpoint );
9720 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009721
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009722 /*
9723 * Things that are common to all presets
9724 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009725#if defined(MBEDTLS_SSL_CLI_C)
9726 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9727 {
9728 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9729#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9730 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9731#endif
9732 }
9733#endif
9734
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009735#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009736 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009737#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009738
9739#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9740 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9741#endif
9742
9743#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9744 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9745#endif
9746
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009747#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9748 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9749#endif
9750
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009751#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009752 conf->f_cookie_write = ssl_cookie_write_dummy;
9753 conf->f_cookie_check = ssl_cookie_check_dummy;
9754#endif
9755
9756#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9757 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9758#endif
9759
Janos Follath088ce432017-04-10 12:42:31 +01009760#if defined(MBEDTLS_SSL_SRV_C)
9761 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9762#endif
9763
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009764#if defined(MBEDTLS_SSL_PROTO_DTLS)
9765 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9766 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9767#endif
9768
9769#if defined(MBEDTLS_SSL_RENEGOTIATION)
9770 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009771 memset( conf->renego_period, 0x00, 2 );
9772 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009773#endif
9774
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009775#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9776 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9777 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009778 const unsigned char dhm_p[] =
9779 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9780 const unsigned char dhm_g[] =
9781 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9782
Hanno Beckera90658f2017-10-04 15:29:08 +01009783 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9784 dhm_p, sizeof( dhm_p ),
9785 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009786 {
9787 return( ret );
9788 }
9789 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009790#endif
9791
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009792 /*
9793 * Preset-specific defaults
9794 */
9795 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009796 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009797 /*
9798 * NSA Suite B
9799 */
9800 case MBEDTLS_SSL_PRESET_SUITEB:
9801 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9802 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9803 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9804 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9805
9806 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9807 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9808 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9809 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9810 ssl_preset_suiteb_ciphersuites;
9811
9812#if defined(MBEDTLS_X509_CRT_PARSE_C)
9813 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009814#endif
9815
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009816#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009817 conf->sig_hashes = ssl_preset_suiteb_hashes;
9818#endif
9819
9820#if defined(MBEDTLS_ECP_C)
9821 conf->curve_list = ssl_preset_suiteb_curves;
9822#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009823 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009824
9825 /*
9826 * Default
9827 */
9828 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009829 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9830 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9831 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9832 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9833 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9834 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9835 MBEDTLS_SSL_MIN_MINOR_VERSION :
9836 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009837 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9838 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9839
9840#if defined(MBEDTLS_SSL_PROTO_DTLS)
9841 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9842 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9843#endif
9844
9845 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9846 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9847 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9848 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9849 mbedtls_ssl_list_ciphersuites();
9850
9851#if defined(MBEDTLS_X509_CRT_PARSE_C)
9852 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9853#endif
9854
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009855#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009856 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009857#endif
9858
9859#if defined(MBEDTLS_ECP_C)
9860 conf->curve_list = mbedtls_ecp_grp_id_list();
9861#endif
9862
9863#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9864 conf->dhm_min_bitlen = 1024;
9865#endif
9866 }
9867
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009868 return( 0 );
9869}
9870
9871/*
9872 * Free mbedtls_ssl_config
9873 */
9874void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9875{
9876#if defined(MBEDTLS_DHM_C)
9877 mbedtls_mpi_free( &conf->dhm_P );
9878 mbedtls_mpi_free( &conf->dhm_G );
9879#endif
9880
9881#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9882 if( conf->psk != NULL )
9883 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009884 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009885 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009886 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009887 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009888 }
9889
9890 if( conf->psk_identity != NULL )
9891 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009892 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009893 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009894 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009895 conf->psk_identity_len = 0;
9896 }
9897#endif
9898
9899#if defined(MBEDTLS_X509_CRT_PARSE_C)
9900 ssl_key_cert_free( conf->key_cert );
9901#endif
9902
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009903 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009904}
9905
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009906#if defined(MBEDTLS_PK_C) && \
9907 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009908/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009909 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009910 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009911unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009912{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009913#if defined(MBEDTLS_RSA_C)
9914 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9915 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009916#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009917#if defined(MBEDTLS_ECDSA_C)
9918 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9919 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009920#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009921 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009922}
9923
Hanno Becker7e5437a2017-04-28 17:15:26 +01009924unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9925{
9926 switch( type ) {
9927 case MBEDTLS_PK_RSA:
9928 return( MBEDTLS_SSL_SIG_RSA );
9929 case MBEDTLS_PK_ECDSA:
9930 case MBEDTLS_PK_ECKEY:
9931 return( MBEDTLS_SSL_SIG_ECDSA );
9932 default:
9933 return( MBEDTLS_SSL_SIG_ANON );
9934 }
9935}
9936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009937mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009938{
9939 switch( sig )
9940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009941#if defined(MBEDTLS_RSA_C)
9942 case MBEDTLS_SSL_SIG_RSA:
9943 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009944#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009945#if defined(MBEDTLS_ECDSA_C)
9946 case MBEDTLS_SSL_SIG_ECDSA:
9947 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009948#endif
9949 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009950 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009951 }
9952}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009953#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009954
Hanno Becker7e5437a2017-04-28 17:15:26 +01009955#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9956 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9957
9958/* Find an entry in a signature-hash set matching a given hash algorithm. */
9959mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9960 mbedtls_pk_type_t sig_alg )
9961{
9962 switch( sig_alg )
9963 {
9964 case MBEDTLS_PK_RSA:
9965 return( set->rsa );
9966 case MBEDTLS_PK_ECDSA:
9967 return( set->ecdsa );
9968 default:
9969 return( MBEDTLS_MD_NONE );
9970 }
9971}
9972
9973/* Add a signature-hash-pair to a signature-hash set */
9974void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9975 mbedtls_pk_type_t sig_alg,
9976 mbedtls_md_type_t md_alg )
9977{
9978 switch( sig_alg )
9979 {
9980 case MBEDTLS_PK_RSA:
9981 if( set->rsa == MBEDTLS_MD_NONE )
9982 set->rsa = md_alg;
9983 break;
9984
9985 case MBEDTLS_PK_ECDSA:
9986 if( set->ecdsa == MBEDTLS_MD_NONE )
9987 set->ecdsa = md_alg;
9988 break;
9989
9990 default:
9991 break;
9992 }
9993}
9994
9995/* Allow exactly one hash algorithm for each signature. */
9996void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9997 mbedtls_md_type_t md_alg )
9998{
9999 set->rsa = md_alg;
10000 set->ecdsa = md_alg;
10001}
10002
10003#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10004 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10005
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010006/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010007 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010009mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010010{
10011 switch( hash )
10012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010013#if defined(MBEDTLS_MD5_C)
10014 case MBEDTLS_SSL_HASH_MD5:
10015 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010016#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010017#if defined(MBEDTLS_SHA1_C)
10018 case MBEDTLS_SSL_HASH_SHA1:
10019 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010020#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010021#if defined(MBEDTLS_SHA256_C)
10022 case MBEDTLS_SSL_HASH_SHA224:
10023 return( MBEDTLS_MD_SHA224 );
10024 case MBEDTLS_SSL_HASH_SHA256:
10025 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010026#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010027#if defined(MBEDTLS_SHA512_C)
10028 case MBEDTLS_SSL_HASH_SHA384:
10029 return( MBEDTLS_MD_SHA384 );
10030 case MBEDTLS_SSL_HASH_SHA512:
10031 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010032#endif
10033 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010034 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010035 }
10036}
10037
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010038/*
10039 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10040 */
10041unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10042{
10043 switch( md )
10044 {
10045#if defined(MBEDTLS_MD5_C)
10046 case MBEDTLS_MD_MD5:
10047 return( MBEDTLS_SSL_HASH_MD5 );
10048#endif
10049#if defined(MBEDTLS_SHA1_C)
10050 case MBEDTLS_MD_SHA1:
10051 return( MBEDTLS_SSL_HASH_SHA1 );
10052#endif
10053#if defined(MBEDTLS_SHA256_C)
10054 case MBEDTLS_MD_SHA224:
10055 return( MBEDTLS_SSL_HASH_SHA224 );
10056 case MBEDTLS_MD_SHA256:
10057 return( MBEDTLS_SSL_HASH_SHA256 );
10058#endif
10059#if defined(MBEDTLS_SHA512_C)
10060 case MBEDTLS_MD_SHA384:
10061 return( MBEDTLS_SSL_HASH_SHA384 );
10062 case MBEDTLS_MD_SHA512:
10063 return( MBEDTLS_SSL_HASH_SHA512 );
10064#endif
10065 default:
10066 return( MBEDTLS_SSL_HASH_NONE );
10067 }
10068}
10069
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010070#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010071/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010072 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010073 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010074 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010075int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010077 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010078
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010079 if( ssl->conf->curve_list == NULL )
10080 return( -1 );
10081
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010082 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010083 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010084 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010085
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010086 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010087}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010088#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010089
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010090#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010091/*
10092 * Check if a hash proposed by the peer is in our list.
10093 * Return 0 if we're willing to use it, -1 otherwise.
10094 */
10095int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10096 mbedtls_md_type_t md )
10097{
10098 const int *cur;
10099
10100 if( ssl->conf->sig_hashes == NULL )
10101 return( -1 );
10102
10103 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10104 if( *cur == (int) md )
10105 return( 0 );
10106
10107 return( -1 );
10108}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010109#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010111#if defined(MBEDTLS_X509_CRT_PARSE_C)
10112int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10113 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010114 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010115 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010116{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010117 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010118#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010119 int usage = 0;
10120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010121#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010122 const char *ext_oid;
10123 size_t ext_len;
10124#endif
10125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010126#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10127 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010128 ((void) cert);
10129 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010130 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010131#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010133#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10134 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010135 {
10136 /* Server part of the key exchange */
10137 switch( ciphersuite->key_exchange )
10138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010139 case MBEDTLS_KEY_EXCHANGE_RSA:
10140 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010141 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010142 break;
10143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010144 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10145 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10146 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10147 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010148 break;
10149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010150 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10151 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010152 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010153 break;
10154
10155 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010156 case MBEDTLS_KEY_EXCHANGE_NONE:
10157 case MBEDTLS_KEY_EXCHANGE_PSK:
10158 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10159 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010160 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010161 usage = 0;
10162 }
10163 }
10164 else
10165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010166 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10167 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010168 }
10169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010170 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010171 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010172 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010173 ret = -1;
10174 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010175#else
10176 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010177#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010179#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10180 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010182 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10183 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010184 }
10185 else
10186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010187 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10188 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010189 }
10190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010191 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010192 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010193 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010194 ret = -1;
10195 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010196#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010197
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010198 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010199}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010200#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010201
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010202/*
10203 * Convert version numbers to/from wire format
10204 * and, for DTLS, to/from TLS equivalent.
10205 *
10206 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010207 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010208 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10209 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010211void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010212 unsigned char ver[2] )
10213{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010214#if defined(MBEDTLS_SSL_PROTO_DTLS)
10215 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010217 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010218 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10219
10220 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10221 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10222 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010223 else
10224#else
10225 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010226#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010227 {
10228 ver[0] = (unsigned char) major;
10229 ver[1] = (unsigned char) minor;
10230 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010231}
10232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010233void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010234 const unsigned char ver[2] )
10235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010236#if defined(MBEDTLS_SSL_PROTO_DTLS)
10237 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010238 {
10239 *major = 255 - ver[0] + 2;
10240 *minor = 255 - ver[1] + 1;
10241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010242 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010243 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10244 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010245 else
10246#else
10247 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010248#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010249 {
10250 *major = ver[0];
10251 *minor = ver[1];
10252 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010253}
10254
Simon Butcher99000142016-10-13 17:21:01 +010010255int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10256{
10257#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10258 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10259 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10260
10261 switch( md )
10262 {
10263#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10264#if defined(MBEDTLS_MD5_C)
10265 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010266 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010267#endif
10268#if defined(MBEDTLS_SHA1_C)
10269 case MBEDTLS_SSL_HASH_SHA1:
10270 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10271 break;
10272#endif
10273#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10274#if defined(MBEDTLS_SHA512_C)
10275 case MBEDTLS_SSL_HASH_SHA384:
10276 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10277 break;
10278#endif
10279#if defined(MBEDTLS_SHA256_C)
10280 case MBEDTLS_SSL_HASH_SHA256:
10281 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10282 break;
10283#endif
10284 default:
10285 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10286 }
10287
10288 return 0;
10289#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10290 (void) ssl;
10291 (void) md;
10292
10293 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10294#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10295}
10296
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010297#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10298 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10299int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10300 unsigned char *output,
10301 unsigned char *data, size_t data_len )
10302{
10303 int ret = 0;
10304 mbedtls_md5_context mbedtls_md5;
10305 mbedtls_sha1_context mbedtls_sha1;
10306
10307 mbedtls_md5_init( &mbedtls_md5 );
10308 mbedtls_sha1_init( &mbedtls_sha1 );
10309
10310 /*
10311 * digitally-signed struct {
10312 * opaque md5_hash[16];
10313 * opaque sha_hash[20];
10314 * };
10315 *
10316 * md5_hash
10317 * MD5(ClientHello.random + ServerHello.random
10318 * + ServerParams);
10319 * sha_hash
10320 * SHA(ClientHello.random + ServerHello.random
10321 * + ServerParams);
10322 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010323 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010324 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010325 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010326 goto exit;
10327 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010328 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010329 ssl->handshake->randbytes, 64 ) ) != 0 )
10330 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010331 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010332 goto exit;
10333 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010334 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010335 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010336 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010337 goto exit;
10338 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010339 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010340 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010341 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010342 goto exit;
10343 }
10344
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010345 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010346 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010347 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010348 goto exit;
10349 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010350 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010351 ssl->handshake->randbytes, 64 ) ) != 0 )
10352 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010353 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010354 goto exit;
10355 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010356 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010357 data_len ) ) != 0 )
10358 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010360 goto exit;
10361 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010362 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010363 output + 16 ) ) != 0 )
10364 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010365 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010366 goto exit;
10367 }
10368
10369exit:
10370 mbedtls_md5_free( &mbedtls_md5 );
10371 mbedtls_sha1_free( &mbedtls_sha1 );
10372
10373 if( ret != 0 )
10374 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10375 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10376
10377 return( ret );
10378
10379}
10380#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10381 MBEDTLS_SSL_PROTO_TLS1_1 */
10382
10383#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10384 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010385
10386#if defined(MBEDTLS_USE_PSA_CRYPTO)
10387int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10388 unsigned char *hash, size_t *hashlen,
10389 unsigned char *data, size_t data_len,
10390 mbedtls_md_type_t md_alg )
10391{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010392 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010393 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010394 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10395
Andrzej Kurek5615dab2019-01-16 05:26:25 -050010396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010397
10398 if( ( status = psa_hash_setup( &hash_operation,
10399 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010400 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010401 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010402 goto exit;
10403 }
10404
Andrzej Kurek814feff2019-01-14 04:35:19 -050010405 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10406 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010407 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010408 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010409 goto exit;
10410 }
10411
Andrzej Kurek814feff2019-01-14 04:35:19 -050010412 if( ( status = psa_hash_update( &hash_operation,
10413 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010414 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010415 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010416 goto exit;
10417 }
10418
Andrzej Kurek814feff2019-01-14 04:35:19 -050010419 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10420 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010421 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010422 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010423 goto exit;
10424 }
10425
10426exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010427 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010428 {
10429 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10430 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010431 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010432 {
10433 case PSA_ERROR_NOT_SUPPORTED:
10434 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010435 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010436 case PSA_ERROR_BUFFER_TOO_SMALL:
10437 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10438 case PSA_ERROR_INSUFFICIENT_MEMORY:
10439 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10440 default:
10441 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10442 }
10443 }
10444 return( 0 );
10445}
10446
10447#else
10448
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010449int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010450 unsigned char *hash, size_t *hashlen,
10451 unsigned char *data, size_t data_len,
10452 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010453{
10454 int ret = 0;
10455 mbedtls_md_context_t ctx;
10456 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010457 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010458
Andrzej Kurek5615dab2019-01-16 05:26:25 -050010459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010460
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010461 mbedtls_md_init( &ctx );
10462
10463 /*
10464 * digitally-signed struct {
10465 * opaque client_random[32];
10466 * opaque server_random[32];
10467 * ServerDHParams params;
10468 * };
10469 */
10470 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10471 {
10472 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10473 goto exit;
10474 }
10475 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10476 {
10477 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10478 goto exit;
10479 }
10480 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10481 {
10482 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10483 goto exit;
10484 }
10485 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10486 {
10487 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10488 goto exit;
10489 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010490 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010491 {
10492 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10493 goto exit;
10494 }
10495
10496exit:
10497 mbedtls_md_free( &ctx );
10498
10499 if( ret != 0 )
10500 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10501 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10502
10503 return( ret );
10504}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010505#endif /* MBEDTLS_USE_PSA_CRYPTO */
10506
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010507#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10508 MBEDTLS_SSL_PROTO_TLS1_2 */
10509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010510#endif /* MBEDTLS_SSL_TLS_C */