blob: d863f7d0231b227c5ab9a6feef4b04e9c16faac4 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Andrzej Kurekc929a822019-01-14 03:51:11 -050062#if defined(MBEDTLS_USE_PSA_CRYPTO)
63#include "mbedtls/psa_util.h"
64#endif
65
Hanno Becker2a43f6f2018-08-10 11:12:52 +010066static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010067static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010068
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081/*
82 * Start a timer.
83 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020087 if( ssl->f_set_timer == NULL )
88 return;
89
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
91 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020099 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200100 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200101
102 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 {
104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200106 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
108 return( 0 );
109}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200110
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100111static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
112 mbedtls_ssl_transform *transform );
113static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
114 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100115
116#define SSL_DONT_FORCE_FLUSH 0
117#define SSL_FORCE_FLUSH 1
118
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200119#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100120
Hanno Beckerd5847772018-08-28 10:09:23 +0100121/* Forward declarations for functions related to message buffering. */
122static void ssl_buffering_free( mbedtls_ssl_context *ssl );
123static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
124 uint8_t slot );
125static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
126static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
127static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
128static int ssl_buffer_message( mbedtls_ssl_context *ssl );
129static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100130static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100131
Hanno Beckera67dee22018-08-22 10:05:20 +0100132static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100133static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100134{
Hanno Becker11682cc2018-08-22 14:41:02 +0100135 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100136
137 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100138 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100139
140 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
141}
142
Hanno Becker67bc7c32018-08-06 11:33:50 +0100143static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
144{
Hanno Becker11682cc2018-08-22 14:41:02 +0100145 size_t const bytes_written = ssl->out_left;
146 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100147
148 /* Double-check that the write-index hasn't gone
149 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100150 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100151 {
152 /* Should never happen... */
153 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
154 }
155
156 return( (int) ( mtu - bytes_written ) );
157}
158
159static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
160{
161 int ret;
162 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400163 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100164
165#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
166 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
167
168 if( max_len > mfl )
169 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100170
171 /* By the standard (RFC 6066 Sect. 4), the MFL extension
172 * only limits the maximum record payload size, so in theory
173 * we would be allowed to pack multiple records of payload size
174 * MFL into a single datagram. However, this would mean that there's
175 * no way to explicitly communicate MTU restrictions to the peer.
176 *
177 * The following reduction of max_len makes sure that we never
178 * write datagrams larger than MFL + Record Expansion Overhead.
179 */
180 if( max_len <= ssl->out_left )
181 return( 0 );
182
183 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100184#endif
185
186 ret = ssl_get_remaining_space_in_datagram( ssl );
187 if( ret < 0 )
188 return( ret );
189 remaining = (size_t) ret;
190
191 ret = mbedtls_ssl_get_record_expansion( ssl );
192 if( ret < 0 )
193 return( ret );
194 expansion = (size_t) ret;
195
196 if( remaining <= expansion )
197 return( 0 );
198
199 remaining -= expansion;
200 if( remaining >= max_len )
201 remaining = max_len;
202
203 return( (int) remaining );
204}
205
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200206/*
207 * Double the retransmit timeout value, within the allowed range,
208 * returning -1 if the maximum value has already been reached.
209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200211{
212 uint32_t new_timeout;
213
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200214 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200215 return( -1 );
216
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200217 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
218 * in the following way: after the initial transmission and a first
219 * retransmission, back off to a temporary estimated MTU of 508 bytes.
220 * This value is guaranteed to be deliverable (if not guaranteed to be
221 * delivered) of any compliant IPv4 (and IPv6) network, and should work
222 * on most non-IP stacks too. */
223 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400224 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200225 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
227 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200228
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200229 new_timeout = 2 * ssl->handshake->retransmit_timeout;
230
231 /* Avoid arithmetic overflow and range overflow */
232 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200233 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200234 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200235 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200236 }
237
238 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200240 ssl->handshake->retransmit_timeout ) );
241
242 return( 0 );
243}
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200246{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200247 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200249 ssl->handshake->retransmit_timeout ) );
250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200254/*
255 * Convert max_fragment_length codes to length.
256 * RFC 6066 says:
257 * enum{
258 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
259 * } MaxFragmentLength;
260 * and we add 0 -> extension unused
261 */
Angus Grattond8213d02016-05-25 20:56:48 +1000262static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200263{
Angus Grattond8213d02016-05-25 20:56:48 +1000264 switch( mfl )
265 {
266 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
267 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
268 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
269 return 512;
270 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
271 return 1024;
272 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
273 return 2048;
274 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
275 return 4096;
276 default:
277 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
278 }
279}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200281
Hanno Becker52055ae2019-02-06 14:30:46 +0000282int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
283 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_ssl_session_free( dst );
286 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000289
290#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200291 if( src->peer_cert != NULL )
292 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200293 int ret;
294
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200295 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200296 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200297 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200302 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200305 dst->peer_cert = NULL;
306 return( ret );
307 }
308 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000309#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000310 if( src->peer_cert_digest != NULL )
311 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000312 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000313 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000314 if( dst->peer_cert_digest == NULL )
315 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
316
317 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
318 src->peer_cert_digest_len );
319 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000320 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000321 }
322#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200325
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200326#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200327 if( src->ticket != NULL )
328 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200329 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200330 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200331 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200332
333 memcpy( dst->ticket, src->ticket, src->ticket_len );
334 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200335#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200336
337 return( 0 );
338}
339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
341int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200342 const unsigned char *key_enc, const unsigned char *key_dec,
343 size_t keylen,
344 const unsigned char *iv_enc, const unsigned char *iv_dec,
345 size_t ivlen,
346 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200347 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
349int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
350int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
351int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
352int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
353#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000354
Paul Bakker5121ce52009-01-03 21:22:43 +0000355/*
356 * Key material generation
357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200359static int ssl3_prf( const unsigned char *secret, size_t slen,
360 const char *label,
361 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000362 unsigned char *dstbuf, size_t dlen )
363{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100364 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000365 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200366 mbedtls_md5_context md5;
367 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000368 unsigned char padding[16];
369 unsigned char sha1sum[20];
370 ((void)label);
371
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200372 mbedtls_md5_init( &md5 );
373 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200374
Paul Bakker5f70b252012-09-13 14:23:06 +0000375 /*
376 * SSLv3:
377 * block =
378 * MD5( secret + SHA1( 'A' + secret + random ) ) +
379 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
380 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
381 * ...
382 */
383 for( i = 0; i < dlen / 16; i++ )
384 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000386
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100387 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100388 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100389 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100390 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100391 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100392 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100393 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100394 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100395 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100396 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000397
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100398 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100399 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100400 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100401 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100402 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100403 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100404 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100405 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000406 }
407
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100408exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200409 mbedtls_md5_free( &md5 );
410 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000411
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500412 mbedtls_platform_zeroize( padding, sizeof( padding ) );
413 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000414
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100415 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000416}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200420static int tls1_prf( const unsigned char *secret, size_t slen,
421 const char *label,
422 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000423 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000424{
Paul Bakker23986e52011-04-24 08:57:21 +0000425 size_t nb, hs;
426 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200427 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 unsigned char tmp[128];
429 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 const mbedtls_md_info_t *md_info;
431 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100432 int ret;
433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
439 hs = ( slen + 1 ) / 2;
440 S1 = secret;
441 S2 = secret + slen - hs;
442
443 nb = strlen( label );
444 memcpy( tmp + 20, label, nb );
445 memcpy( tmp + 20 + nb, random, rlen );
446 nb += rlen;
447
448 /*
449 * First compute P_md5(secret,label+random)[0..dlen]
450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
452 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100455 return( ret );
456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
458 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
459 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461 for( i = 0; i < dlen; i += 16 )
462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_md_hmac_reset ( &md_ctx );
464 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
465 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_md_hmac_reset ( &md_ctx );
468 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
469 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
471 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
472
473 for( j = 0; j < k; j++ )
474 dstbuf[i + j] = h_i[j];
475 }
476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100478
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 /*
480 * XOR out with P_sha1(secret,label+random)[0..dlen]
481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
483 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100486 return( ret );
487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
489 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
490 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 for( i = 0; i < dlen; i += 20 )
493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_md_hmac_reset ( &md_ctx );
495 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
496 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_md_hmac_reset ( &md_ctx );
499 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
500 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
503
504 for( j = 0; j < k; j++ )
505 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
506 }
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100509
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500510 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
511 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
513 return( 0 );
514}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500518#if defined(MBEDTLS_USE_PSA_CRYPTO)
519static int tls_prf_generic( mbedtls_md_type_t md_type,
520 const unsigned char *secret, size_t slen,
521 const char *label,
522 const unsigned char *random, size_t rlen,
523 unsigned char *dstbuf, size_t dlen )
524{
525 psa_status_t status;
526 psa_algorithm_t alg;
527 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500528 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500529 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
530
Andrzej Kurek2f760752019-01-28 08:08:15 -0500531 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500532 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500533
Andrzej Kurekc929a822019-01-14 03:51:11 -0500534 if( md_type == MBEDTLS_MD_SHA384 )
535 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
536 else
537 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
538
Andrzej Kurek2f760752019-01-28 08:08:15 -0500539 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500540 psa_key_policy_set_usage( &policy,
541 PSA_KEY_USAGE_DERIVE,
542 alg );
543 status = psa_set_key_policy( master_slot, &policy );
544 if( status != PSA_SUCCESS )
545 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
546
547 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
548 if( status != PSA_SUCCESS )
549 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
550
551 status = psa_key_derivation( &generator,
552 master_slot, alg,
553 random, rlen,
554 (unsigned char const *) label,
555 (size_t) strlen( label ),
556 dlen );
557 if( status != PSA_SUCCESS )
558 {
559 psa_generator_abort( &generator );
560 psa_destroy_key( master_slot );
561 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
562 }
563
564 status = psa_generator_read( &generator, dstbuf, dlen );
565 if( status != PSA_SUCCESS )
566 {
567 psa_generator_abort( &generator );
568 psa_destroy_key( master_slot );
569 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
570 }
571
572 status = psa_generator_abort( &generator );
573 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500574 {
575 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500576 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500577 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500578
579 status = psa_destroy_key( master_slot );
580 if( status != PSA_SUCCESS )
581 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
582
Andrzej Kurek33171262019-01-15 03:25:18 -0500583 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500584}
585
586#else /* MBEDTLS_USE_PSA_CRYPTO */
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100589 const unsigned char *secret, size_t slen,
590 const char *label,
591 const unsigned char *random, size_t rlen,
592 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000593{
594 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100595 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000596 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
598 const mbedtls_md_info_t *md_info;
599 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100600 int ret;
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
605 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100608
609 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000611
612 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100613 memcpy( tmp + md_len, label, nb );
614 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000615 nb += rlen;
616
617 /*
618 * Compute P_<hash>(secret, label + random)[0..dlen]
619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100621 return( ret );
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
624 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
625 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100626
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100627 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_md_hmac_reset ( &md_ctx );
630 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
631 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_md_hmac_reset ( &md_ctx );
634 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
635 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000636
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100637 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000638
639 for( j = 0; j < k; j++ )
640 dstbuf[i + j] = h_i[j];
641 }
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100644
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500645 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
646 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000647
648 return( 0 );
649}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500650#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100652static int tls_prf_sha256( const unsigned char *secret, size_t slen,
653 const char *label,
654 const unsigned char *random, size_t rlen,
655 unsigned char *dstbuf, size_t dlen )
656{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100658 label, random, rlen, dstbuf, dlen ) );
659}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200663static int tls_prf_sha384( const unsigned char *secret, size_t slen,
664 const char *label,
665 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000666 unsigned char *dstbuf, size_t dlen )
667{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100669 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000670}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671#endif /* MBEDTLS_SHA512_C */
672#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
677 defined(MBEDTLS_SSL_PROTO_TLS1_1)
678static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200679#endif
Paul Bakker380da532012-04-18 16:10:25 +0000680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#if defined(MBEDTLS_SSL_PROTO_SSL3)
682static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
683static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200684#endif
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
687static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
688static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200689#endif
690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
692#if defined(MBEDTLS_SHA256_C)
693static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
694static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
695static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200696#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698#if defined(MBEDTLS_SHA512_C)
699static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
700static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
701static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100702#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000704
Hanno Becker7d0a5692018-10-23 15:26:22 +0100705#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
706 defined(MBEDTLS_USE_PSA_CRYPTO)
707static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
708{
709 if( ssl->conf->f_psk != NULL )
710 {
711 /* If we've used a callback to select the PSK,
712 * the static configuration is irrelevant. */
713 if( ssl->handshake->psk_opaque != 0 )
714 return( 1 );
715
716 return( 0 );
717 }
718
719 if( ssl->conf->psk_opaque != 0 )
720 return( 1 );
721
722 return( 0 );
723}
724#endif /* MBEDTLS_USE_PSA_CRYPTO &&
725 MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000728{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200729 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000730#if defined(MBEDTLS_USE_PSA_CRYPTO)
731 int psa_fallthrough;
732#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000734 unsigned char keyblk[256];
735 unsigned char *key1;
736 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100737 unsigned char *mac_enc;
738 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000739 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200740 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000741 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000742 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 const mbedtls_cipher_info_t *cipher_info;
744 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100745
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000746 /* cf. RFC 5246, Section 8.1:
747 * "The master secret is always exactly 48 bytes in length." */
748 size_t const master_secret_len = 48;
749
Hanno Becker35b23c72018-10-23 12:10:41 +0100750#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
751 unsigned char session_hash[48];
752#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_ssl_session *session = ssl->session_negotiate;
755 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
756 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000760#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
761 transform->encrypt_then_mac = session->encrypt_then_mac;
762#endif
763 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000764
765 ciphersuite_info = handshake->ciphersuite_info;
766 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100767 if( cipher_info == NULL )
768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000770 ciphersuite_info->cipher ) );
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
Hanno Beckere694c3e2017-12-27 21:34:08 +0000774 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100775 if( md_info == NULL )
776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000778 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100780 }
781
Paul Bakker5121ce52009-01-03 21:22:43 +0000782 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000783 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000784 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#if defined(MBEDTLS_SSL_PROTO_SSL3)
786 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000787 {
Paul Bakker48916f92012-09-16 19:57:18 +0000788 handshake->tls_prf = ssl3_prf;
789 handshake->calc_verify = ssl_calc_verify_ssl;
790 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000791 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200792 else
793#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
795 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000796 {
Paul Bakker48916f92012-09-16 19:57:18 +0000797 handshake->tls_prf = tls1_prf;
798 handshake->calc_verify = ssl_calc_verify_tls;
799 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000800 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200801 else
802#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
804#if defined(MBEDTLS_SHA512_C)
805 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +0000806 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000807 {
Paul Bakker48916f92012-09-16 19:57:18 +0000808 handshake->tls_prf = tls_prf_sha384;
809 handshake->calc_verify = ssl_calc_verify_tls_sha384;
810 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000811 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000812 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200813#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814#if defined(MBEDTLS_SHA256_C)
815 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000816 {
Paul Bakker48916f92012-09-16 19:57:18 +0000817 handshake->tls_prf = tls_prf_sha256;
818 handshake->calc_verify = ssl_calc_verify_tls_sha256;
819 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000820 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200821 else
822#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
826 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200827 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000828
829 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000830 * SSLv3:
831 * master =
832 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
833 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
834 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200835 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200836 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 * master = PRF( premaster, "master secret", randbytes )[0..47]
838 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100839 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000840 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100841 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
842 }
843 else
844 {
845 /* The label for the KDF used for key expansion.
846 * This is either "master secret" or "extended master secret"
847 * depending on whether the Extended Master Secret extension
848 * is used. */
849 char const *lbl = "master secret";
850
851 /* The salt for the KDF used for key expansion.
852 * - If the Extended Master Secret extension is not used,
853 * this is ClientHello.Random + ServerHello.Random
854 * (see Sect. 8.1 in RFC 5246).
855 * - If the Extended Master Secret extension is used,
856 * this is the transcript of the handshake so far.
857 * (see Sect. 4 in RFC 7627). */
858 unsigned char const *salt = handshake->randbytes;
859 size_t salt_len = 64;
860
861#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200865
Hanno Becker35b23c72018-10-23 12:10:41 +0100866 lbl = "extended master secret";
867 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200868 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
870 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +0000873 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
874 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100875 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000876 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200877 else
Hanno Becker35b23c72018-10-23 12:10:41 +0100878#endif /* MBEDTLS_SHA512_C */
879 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200880 }
881 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100883 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200884
Hanno Becker35b23c72018-10-23 12:10:41 +0100885 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200886 }
Hanno Becker35b23c72018-10-23 12:10:41 +0100887#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
888
Hanno Becker7d0a5692018-10-23 15:26:22 +0100889#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
890 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
891 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
892 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
893 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100894 {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100895 /* Perform PSK-to-MS expansion in a single step. */
896 psa_status_t status;
897 psa_algorithm_t alg;
898 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500899 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100900
901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
902
903 psk = ssl->conf->psk_opaque;
904 if( ssl->handshake->psk_opaque != 0 )
905 psk = ssl->handshake->psk_opaque;
906
907 if( md_type == MBEDTLS_MD_SHA384 )
908 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
909 else
910 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
911
912 status = psa_key_derivation( &generator, psk, alg,
913 salt, salt_len,
914 (unsigned char const *) lbl,
915 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000916 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100917 if( status != PSA_SUCCESS )
918 {
919 psa_generator_abort( &generator );
920 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
921 }
922
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000923 status = psa_generator_read( &generator, session->master,
924 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100925 if( status != PSA_SUCCESS )
926 {
927 psa_generator_abort( &generator );
928 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
929 }
930
931 status = psa_generator_abort( &generator );
932 if( status != PSA_SUCCESS )
933 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100934 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100935 else
936#endif
937 {
938 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
939 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000940 session->master,
941 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100942 if( ret != 0 )
943 {
944 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
945 return( ret );
946 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200947
Hanno Becker7d0a5692018-10-23 15:26:22 +0100948 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
949 handshake->premaster,
950 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +0100951
Hanno Becker7d0a5692018-10-23 15:26:22 +0100952 mbedtls_platform_zeroize( handshake->premaster,
953 sizeof(handshake->premaster) );
954 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000956
957 /*
958 * Swap the client and server random values.
959 */
Paul Bakker48916f92012-09-16 19:57:18 +0000960 memcpy( tmp, handshake->randbytes, 64 );
961 memcpy( handshake->randbytes, tmp + 32, 32 );
962 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500963 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000964
965 /*
966 * SSLv3:
967 * key block =
968 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
969 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
970 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
971 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
972 * ...
973 *
974 * TLSv1:
975 * key block = PRF( master, "key expansion", randbytes )
976 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100977 ret = handshake->tls_prf( session->master, 48, "key expansion",
978 handshake->randbytes, 64, keyblk, 256 );
979 if( ret != 0 )
980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100982 return( ret );
983 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
986 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
987 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
988 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
989 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000990
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500991 mbedtls_platform_zeroize( handshake->randbytes,
992 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000993
994 /*
995 * Determine the appropriate key, IV and MAC length.
996 */
Paul Bakker68884e32013-01-07 18:20:04 +0100997
Hanno Becker88aaf652017-12-27 08:17:40 +0000998 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200999
Hanno Becker8031d062018-01-03 15:32:31 +00001000#if defined(MBEDTLS_GCM_C) || \
1001 defined(MBEDTLS_CCM_C) || \
1002 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001004 cipher_info->mode == MBEDTLS_MODE_CCM ||
1005 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001006 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001007 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001008
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001009 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001010 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001011 transform->taglen =
1012 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001013
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001014 /* All modes haves 96-bit IVs;
1015 * GCM and CCM has 4 implicit and 8 explicit bytes
1016 * ChachaPoly has all 12 bytes implicit
1017 */
Paul Bakker68884e32013-01-07 18:20:04 +01001018 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001019 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1020 transform->fixed_ivlen = 12;
1021 else
1022 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001023
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001024 /* Minimum length of encrypted record */
1025 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001026 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001027 }
1028 else
Hanno Becker8031d062018-01-03 15:32:31 +00001029#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1030#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1031 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1032 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001033 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001034 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1036 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001039 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001040 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001042 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001043 mac_key_len = mbedtls_md_get_size( md_info );
1044 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001047 /*
1048 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1049 * (rfc 6066 page 13 or rfc 2104 section 4),
1050 * so we only need to adjust the length here.
1051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001055
1056#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1057 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001058 * HMAC implementation which also truncates the key
1059 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001060 mac_key_len = transform->maclen;
1061#endif
1062 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001064
1065 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001066 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001067
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001068 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001070 transform->minlen = transform->maclen;
1071 else
Paul Bakker68884e32013-01-07 18:20:04 +01001072 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001073 /*
1074 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001075 * 1. if EtM is in use: one block plus MAC
1076 * otherwise: * first multiple of blocklen greater than maclen
1077 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1080 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001081 {
1082 transform->minlen = transform->maclen
1083 + cipher_info->block_size;
1084 }
1085 else
1086#endif
1087 {
1088 transform->minlen = transform->maclen
1089 + cipher_info->block_size
1090 - transform->maclen % cipher_info->block_size;
1091 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1094 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1095 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001096 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001097 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001098#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1100 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1101 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001102 {
1103 transform->minlen += transform->ivlen;
1104 }
1105 else
1106#endif
1107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1109 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001110 }
Paul Bakker68884e32013-01-07 18:20:04 +01001111 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 }
Hanno Becker8031d062018-01-03 15:32:31 +00001113 else
1114#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1115 {
1116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1117 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1118 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001119
Hanno Becker88aaf652017-12-27 08:17:40 +00001120 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1121 (unsigned) keylen,
1122 (unsigned) transform->minlen,
1123 (unsigned) transform->ivlen,
1124 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
1126 /*
1127 * Finally setup the cipher contexts, IVs and MAC secrets.
1128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001130 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001131 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001132 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001133 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001134
Paul Bakker68884e32013-01-07 18:20:04 +01001135 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001136 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001138 /*
1139 * This is not used in TLS v1.1.
1140 */
Paul Bakker48916f92012-09-16 19:57:18 +00001141 iv_copy_len = ( transform->fixed_ivlen ) ?
1142 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001143 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1144 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001145 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001146 }
1147 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148#endif /* MBEDTLS_SSL_CLI_C */
1149#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001150 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001151 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001152 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001153 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001154
Hanno Becker81c7b182017-11-09 18:39:33 +00001155 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001156 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001157
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001158 /*
1159 * This is not used in TLS v1.1.
1160 */
Paul Bakker48916f92012-09-16 19:57:18 +00001161 iv_copy_len = ( transform->fixed_ivlen ) ?
1162 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001163 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1164 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001165 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001166 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001167 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1171 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001172 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174#if defined(MBEDTLS_SSL_PROTO_SSL3)
1175 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001176 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001177 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1180 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001181 }
1182
Hanno Becker81c7b182017-11-09 18:39:33 +00001183 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1184 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001185 }
1186 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1188#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1189 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1190 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001191 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001192 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1193 For AEAD-based ciphersuites, there is nothing to do here. */
1194 if( mac_key_len != 0 )
1195 {
1196 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1197 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1198 }
Paul Bakker68884e32013-01-07 18:20:04 +01001199 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001200 else
1201#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001205 }
Paul Bakker68884e32013-01-07 18:20:04 +01001206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1208 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001209 {
1210 int ret = 0;
1211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001213
Hanno Becker88aaf652017-12-27 08:17:40 +00001214 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001215 transform->iv_enc, transform->iv_dec,
1216 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001217 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001218 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1221 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001222 }
1223 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001225
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001226#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1227 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001228 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001229 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1230 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001231 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001232 iv_copy_len );
1233 }
1234#endif
1235
Hanno Beckerf704bef2018-11-16 15:21:18 +00001236#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001237
1238 /* Only use PSA-based ciphers for TLS-1.2.
1239 * That's relevant at least for TLS-1.0, where
1240 * we assume that mbedtls_cipher_crypt() updates
1241 * the structure field for the IV, which the PSA-based
1242 * implementation currently doesn't. */
1243#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1244 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001245 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001246 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
1247 cipher_info, taglen );
1248 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1249 {
1250 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1251 return( ret );
1252 }
1253
1254 if( ret == 0 )
1255 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001256 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001257 psa_fallthrough = 0;
1258 }
1259 else
1260 {
1261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1262 psa_fallthrough = 1;
1263 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001264 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001265 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001266 psa_fallthrough = 1;
1267#else
1268 psa_fallthrough = 1;
1269#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001270
Hanno Beckercb1cc802018-11-17 22:27:38 +00001271 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001272#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001273 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001274 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001275 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001276 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001277 return( ret );
1278 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001279
Hanno Beckerf704bef2018-11-16 15:21:18 +00001280#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001281 /* Only use PSA-based ciphers for TLS-1.2.
1282 * That's relevant at least for TLS-1.0, where
1283 * we assume that mbedtls_cipher_crypt() updates
1284 * the structure field for the IV, which the PSA-based
1285 * implementation currently doesn't. */
1286#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1287 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001288 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001289 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
1290 cipher_info, taglen );
1291 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1292 {
1293 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1294 return( ret );
1295 }
1296
1297 if( ret == 0 )
1298 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001299 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001300 psa_fallthrough = 0;
1301 }
1302 else
1303 {
1304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1305 psa_fallthrough = 1;
1306 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001307 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001308 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001309 psa_fallthrough = 1;
1310#else
1311 psa_fallthrough = 1;
1312#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001313
Hanno Beckercb1cc802018-11-17 22:27:38 +00001314 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001315#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001316 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001317 cipher_info ) ) != 0 )
1318 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", 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( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001324 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001328 return( ret );
1329 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001332 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001336 return( ret );
1337 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#if defined(MBEDTLS_CIPHER_MODE_CBC)
1340 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1343 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001346 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001347 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1350 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001353 return( ret );
1354 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001355 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001357
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001358 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001361 // Initialize compression
1362 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001364 {
Paul Bakker16770332013-10-11 09:59:44 +02001365 if( ssl->compress_buf == NULL )
1366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001368 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001369 if( ssl->compress_buf == NULL )
1370 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001372 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001373 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001374 }
1375 }
1376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001378
Paul Bakker48916f92012-09-16 19:57:18 +00001379 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1380 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001381
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001382 if( deflateInit( &transform->ctx_deflate,
1383 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001384 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1387 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001388 }
1389 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001393
1394 return( 0 );
1395}
1396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#if defined(MBEDTLS_SSL_PROTO_SSL3)
1398void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001399{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001400 mbedtls_md5_context md5;
1401 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001402 unsigned char pad_1[48];
1403 unsigned char pad_2[48];
1404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001407 mbedtls_md5_init( &md5 );
1408 mbedtls_sha1_init( &sha1 );
1409
1410 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1411 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001412
Paul Bakker380da532012-04-18 16:10:25 +00001413 memset( pad_1, 0x36, 48 );
1414 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001415
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001416 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1417 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1418 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001419
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001420 mbedtls_md5_starts_ret( &md5 );
1421 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1422 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1423 mbedtls_md5_update_ret( &md5, hash, 16 );
1424 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001426 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1427 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1428 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001429
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001430 mbedtls_sha1_starts_ret( &sha1 );
1431 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1432 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1433 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1434 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001438
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001439 mbedtls_md5_free( &md5 );
1440 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001441
Paul Bakker380da532012-04-18 16:10:25 +00001442 return;
1443}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1447void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001448{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001449 mbedtls_md5_context md5;
1450 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001453
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001454 mbedtls_md5_init( &md5 );
1455 mbedtls_sha1_init( &sha1 );
1456
1457 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1458 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001459
Andrzej Kurekeb342242019-01-29 09:14:33 -05001460 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001461 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001465
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001466 mbedtls_md5_free( &md5 );
1467 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001468
Paul Bakker380da532012-04-18 16:10:25 +00001469 return;
1470}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1474#if defined(MBEDTLS_SHA256_C)
1475void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001476{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001477#if defined(MBEDTLS_USE_PSA_CRYPTO)
1478 size_t hash_size;
1479 psa_status_t status;
1480 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1481
1482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1483 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1484 if( status != PSA_SUCCESS )
1485 {
1486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1487 return;
1488 }
1489
1490 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1491 if( status != PSA_SUCCESS )
1492 {
1493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1494 return;
1495 }
1496 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1498#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001499 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001500
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001501 mbedtls_sha256_init( &sha256 );
1502
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001504
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001505 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001506 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001510
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001511 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001512#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001513 return;
1514}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517#if defined(MBEDTLS_SHA512_C)
1518void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001519{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001520#if defined(MBEDTLS_USE_PSA_CRYPTO)
1521 size_t hash_size;
1522 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001523 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001524
1525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001526 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001527 if( status != PSA_SUCCESS )
1528 {
1529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1530 return;
1531 }
1532
Andrzej Kurek972fba52019-01-30 03:29:12 -05001533 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001534 if( status != PSA_SUCCESS )
1535 {
1536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1537 return;
1538 }
1539 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1541#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001542 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001543
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001544 mbedtls_sha512_init( &sha512 );
1545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001547
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001548 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001549 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001553
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001554 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001555#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001556 return;
1557}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558#endif /* MBEDTLS_SHA512_C */
1559#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1562int 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 +02001563{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001564 unsigned char *p = ssl->handshake->premaster;
1565 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001566 const unsigned char *psk = ssl->conf->psk;
1567 size_t psk_len = ssl->conf->psk_len;
1568
1569 /* If the psk callback was called, use its result */
1570 if( ssl->handshake->psk != NULL )
1571 {
1572 psk = ssl->handshake->psk;
1573 psk_len = ssl->handshake->psk_len;
1574 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001575
1576 /*
1577 * PMS = struct {
1578 * opaque other_secret<0..2^16-1>;
1579 * opaque psk<0..2^16-1>;
1580 * };
1581 * with "other_secret" depending on the particular key exchange
1582 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1584 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001585 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001586 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001588
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001589 *(p++) = (unsigned char)( psk_len >> 8 );
1590 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001591
1592 if( end < p || (size_t)( end - p ) < psk_len )
1593 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1594
1595 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001596 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001597 }
1598 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1600#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1601 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001602 {
1603 /*
1604 * other_secret already set by the ClientKeyExchange message,
1605 * and is 48 bytes long
1606 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001607 if( end - p < 2 )
1608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1609
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001610 *p++ = 0;
1611 *p++ = 48;
1612 p += 48;
1613 }
1614 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1616#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1617 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001618 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001619 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001620 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001621
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001622 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001624 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001625 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001628 return( ret );
1629 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001630 *(p++) = (unsigned char)( len >> 8 );
1631 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001632 p += len;
1633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001635 }
1636 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1638#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1639 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001640 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001641 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001642 size_t zlen;
1643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001645 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001646 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001649 return( ret );
1650 }
1651
1652 *(p++) = (unsigned char)( zlen >> 8 );
1653 *(p++) = (unsigned char)( zlen );
1654 p += zlen;
1655
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001656 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1657 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001658 }
1659 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1663 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001664 }
1665
1666 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001667 if( end - p < 2 )
1668 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001669
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001670 *(p++) = (unsigned char)( psk_len >> 8 );
1671 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001672
1673 if( end < p || (size_t)( end - p ) < psk_len )
1674 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1675
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001676 memcpy( p, psk, psk_len );
1677 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001678
1679 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1680
1681 return( 0 );
1682}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001686/*
1687 * SSLv3.0 MAC functions
1688 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001689#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001690static void ssl_mac( mbedtls_md_context_t *md_ctx,
1691 const unsigned char *secret,
1692 const unsigned char *buf, size_t len,
1693 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001694 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001695{
1696 unsigned char header[11];
1697 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001698 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1700 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001701
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001702 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001704 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001705 else
Paul Bakker68884e32013-01-07 18:20:04 +01001706 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001707
1708 memcpy( header, ctr, 8 );
1709 header[ 8] = (unsigned char) type;
1710 header[ 9] = (unsigned char)( len >> 8 );
1711 header[10] = (unsigned char)( len );
1712
Paul Bakker68884e32013-01-07 18:20:04 +01001713 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 mbedtls_md_starts( md_ctx );
1715 mbedtls_md_update( md_ctx, secret, md_size );
1716 mbedtls_md_update( md_ctx, padding, padlen );
1717 mbedtls_md_update( md_ctx, header, 11 );
1718 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001719 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001720
Paul Bakker68884e32013-01-07 18:20:04 +01001721 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722 mbedtls_md_starts( md_ctx );
1723 mbedtls_md_update( md_ctx, secret, md_size );
1724 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001725 mbedtls_md_update( md_ctx, out, md_size );
1726 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001727}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001729
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001730/* The function below is only used in the Lucky 13 counter-measure in
1731 * ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001732#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001733 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1734 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1735 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1736/* This function makes sure every byte in the memory region is accessed
1737 * (in ascending addresses order) */
1738static void ssl_read_memory( unsigned char *p, size_t len )
1739{
1740 unsigned char acc = 0;
1741 volatile unsigned char force;
1742
1743 for( ; len != 0; p++, len-- )
1744 acc ^= *p;
1745
1746 force = acc;
1747 (void) force;
1748}
1749#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1750
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001751/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001752 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001753 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001754
1755static void ssl_extract_add_data_from_record( unsigned char* add_data,
1756 mbedtls_record *rec )
1757{
1758 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1759 add_data[8] = rec->type;
1760 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
1761 add_data[11] = ( rec->data_len >> 8 ) & 0xFF;
1762 add_data[12] = rec->data_len & 0xFF;
1763}
1764
1765static int ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1766 mbedtls_ssl_transform *transform,
1767 mbedtls_record *rec,
1768 int (*f_rng)(void *, unsigned char *, size_t),
1769 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001770{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001772 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001773 unsigned char * data;
1774 unsigned char add_data[13];
1775 size_t post_avail;
1776
1777 /* The SSL context is only used for debugging purposes! */
1778#if !defined(MBEDTLS_SSL_DEBUG_C)
1779 ((void) ssl);
1780#endif
1781
1782 /* The PRNG is used for dynamic IV generation that's used
1783 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1784#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1785 ( defined(MBEDTLS_AES_C) || \
1786 defined(MBEDTLS_ARIA_C) || \
1787 defined(MBEDTLS_CAMELLIA_C) ) && \
1788 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1789 ((void) f_rng);
1790 ((void) p_rng);
1791#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001794
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001795 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001796 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1798 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1799 }
1800 if( rec == NULL ||
1801 rec->buf == NULL ||
1802 rec->buf_len < rec->data_offset ||
1803 rec->buf_len - rec->data_offset < rec->data_len )
1804 {
1805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001807 }
1808
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001809 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1810 data = rec->buf + rec->data_offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001812 data, rec->data_len );
1813
1814 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1815
1816 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1817 {
1818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1819 (unsigned) rec->data_len,
1820 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1821 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1822 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001823
Paul Bakker5121ce52009-01-03 21:22:43 +00001824 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001825 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001826 */
Hanno Becker52344c22018-01-03 15:24:20 +00001827#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 if( mode == MBEDTLS_MODE_STREAM ||
1829 ( mode == MBEDTLS_MODE_CBC
1830#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001831 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001832#endif
1833 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001834 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001835 if( post_avail < transform->maclen )
1836 {
1837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1838 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1839 }
1840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001842 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001843 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001844 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001845 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1846 data, rec->data_len, rec->ctr, rec->type, mac );
1847 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001848 }
1849 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001850#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1852 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001853 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001854 {
Hanno Becker992b6872017-11-09 18:57:39 +00001855 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1856
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001857 ssl_extract_add_data_from_record( add_data, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001858
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001859 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
1860 sizeof( add_data ) );
1861 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1862 data, rec->data_len );
1863 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1864 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1865
1866 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001867 }
1868 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001869#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1872 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001873 }
1874
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001875 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1876 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001877
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001878 rec->data_len += transform->maclen;
1879 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001880 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001881 }
Hanno Becker52344c22018-01-03 15:24:20 +00001882#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001883
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001884 /*
1885 * Encrypt
1886 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1888 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001889 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001890 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001891 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001893 "including %d bytes of padding",
1894 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001895
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001896 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1897 transform->iv_enc, transform->ivlen,
1898 data, rec->data_len,
1899 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001902 return( ret );
1903 }
1904
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001905 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1908 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001909 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 }
Paul Bakker68884e32013-01-07 18:20:04 +01001911 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001913
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001914#if defined(MBEDTLS_GCM_C) || \
1915 defined(MBEDTLS_CCM_C) || \
1916 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001918 mode == MBEDTLS_MODE_CCM ||
1919 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001920 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001921 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001922 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001923 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001924
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001925 /* Check that there's space for both the authentication tag
1926 * and the explicit IV before and after the record content. */
1927 if( post_avail < transform->taglen ||
1928 rec->data_offset < explicit_iv_len )
1929 {
1930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1931 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1932 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001933
Paul Bakker68884e32013-01-07 18:20:04 +01001934 /*
1935 * Generate IV
1936 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001937 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1938 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001939 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001940 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001941 memcpy( iv + transform->fixed_ivlen, rec->ctr,
1942 explicit_iv_len );
1943 /* Prefix record content with explicit IV. */
1944 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001945 }
1946 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1947 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001948 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001949 unsigned char i;
1950
1951 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1952
1953 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001954 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001955 }
1956 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001957 {
1958 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1960 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001961 }
1962
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001963 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1964 iv, transform->ivlen );
1965 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001966 data - explicit_iv_len, explicit_iv_len );
1967 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1968 add_data, 13 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001970 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001971 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001972
Paul Bakker68884e32013-01-07 18:20:04 +01001973 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001974 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001975 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001976
1977 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001978 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001979 iv, transform->ivlen,
1980 add_data, 13, /* add data */
1981 data, rec->data_len, /* source */
1982 data, &rec->data_len, /* destination */
1983 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001986 return( ret );
1987 }
1988
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001989 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
1990 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001991
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001992 rec->data_len += transform->taglen + explicit_iv_len;
1993 rec->data_offset -= explicit_iv_len;
1994 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001995 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001996 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001997 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1999#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002000 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002002 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002003 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002004 size_t padlen, i;
2005 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002006
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002007 /* Currently we're always using minimal padding
2008 * (up to 255 bytes would be allowed). */
2009 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2010 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002011 padlen = 0;
2012
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002013 /* Check there's enough space in the buffer for the padding. */
2014 if( post_avail < padlen + 1 )
2015 {
2016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2017 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2018 }
2019
Paul Bakker5121ce52009-01-03 21:22:43 +00002020 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002021 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002022
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002023 rec->data_len += padlen + 1;
2024 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002027 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002028 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2029 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002030 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002031 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002032 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002033 if( f_rng == NULL )
2034 {
2035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2036 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2037 }
2038
2039 if( rec->data_offset < transform->ivlen )
2040 {
2041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2042 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2043 }
2044
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002045 /*
2046 * Generate IV
2047 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002048 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002049 if( ret != 0 )
2050 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002051
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002052 memcpy( data - transform->ivlen, transform->iv_enc,
2053 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002054
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002055 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002059 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002060 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002061 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002062
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002063 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2064 transform->iv_enc,
2065 transform->ivlen,
2066 data, rec->data_len,
2067 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002070 return( ret );
2071 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002072
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002073 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2076 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002077 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002080 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002081 {
2082 /*
2083 * Save IV in SSL3 and TLS1
2084 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002085 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2086 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002088 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002089#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002090 {
2091 data -= transform->ivlen;
2092 rec->data_offset -= transform->ivlen;
2093 rec->data_len += transform->ivlen;
2094 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002097 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002098 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002099 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2100
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002101 /*
2102 * MAC(MAC_write_key, seq_num +
2103 * TLSCipherText.type +
2104 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002105 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002106 * IV + // except for TLS 1.0
2107 * ENC(content + padding + padding_length));
2108 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002109
2110 if( post_avail < transform->maclen)
2111 {
2112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2113 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2114 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002117 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2118 sizeof( add_data ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002119
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002120 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002121
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002122 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2123 sizeof( add_data ) );
2124 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2125 data, rec->data_len );
2126 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2127 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002128
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002129 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002130
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002131 rec->data_len += transform->maclen;
2132 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002133 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002134 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002137 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002139 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2142 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002143 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002144
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002145 /* Make extra sure authentication was performed, exactly once */
2146 if( auth_done != 1 )
2147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2149 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002150 }
2151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002153
2154 return( 0 );
2155}
2156
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002157static int ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2158 mbedtls_ssl_transform *transform,
2159 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002160{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002161 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002163 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002164#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002165 size_t padlen = 0, correct = 1;
2166#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002167 unsigned char* data;
2168 unsigned char add_data[13];
2169
2170#if !defined(MBEDTLS_SSL_DEBUG_C)
2171 ((void) ssl);
2172#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002175 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002176 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2178 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2179 }
2180 if( rec == NULL ||
2181 rec->buf == NULL ||
2182 rec->buf_len < rec->data_offset ||
2183 rec->buf_len - rec->data_offset < rec->data_len )
2184 {
2185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002187 }
2188
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002189 data = rec->buf + rec->data_offset;
2190 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2193 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002194 {
2195 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002196 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2197 transform->iv_dec,
2198 transform->ivlen,
2199 data, rec->data_len,
2200 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002203 return( ret );
2204 }
2205
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002206 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2209 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002210 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002211 }
Paul Bakker68884e32013-01-07 18:20:04 +01002212 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002214#if defined(MBEDTLS_GCM_C) || \
2215 defined(MBEDTLS_CCM_C) || \
2216 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002218 mode == MBEDTLS_MODE_CCM ||
2219 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002220 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002221 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002222 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002223
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002224 /*
2225 * Compute and update sizes
2226 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002227 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002230 "+ taglen (%d)", rec->data_len,
2231 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002233 }
Paul Bakker68884e32013-01-07 18:20:04 +01002234
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002235 /*
2236 * Prepare IV
2237 */
2238 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2239 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002240 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002241 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002242 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002243
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002244 }
2245 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2246 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002247 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002248 unsigned char i;
2249
2250 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2251
2252 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002253 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002254 }
2255 else
2256 {
2257 /* Reminder if we ever add an AEAD mode with a different size */
2258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2259 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2260 }
2261
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002262 data += explicit_iv_len;
2263 rec->data_offset += explicit_iv_len;
2264 rec->data_len -= explicit_iv_len + transform->taglen;
2265
2266 ssl_extract_add_data_from_record( add_data, rec );
2267 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2268 add_data, 13 );
2269
2270 memcpy( transform->iv_dec + transform->fixed_ivlen,
2271 data - explicit_iv_len, explicit_iv_len );
2272
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002273 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002274 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002275 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002276
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002277
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002278 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002279 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002280 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002281 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2282 iv, transform->ivlen,
2283 add_data, 13,
2284 data, rec->data_len,
2285 data, &olen,
2286 data + rec->data_len,
2287 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2292 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002293
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002294 return( ret );
2295 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002296 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002297
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002298 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2301 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002302 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002303 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002304 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2306#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002307 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002309 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002310 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002311
Paul Bakker5121ce52009-01-03 21:22:43 +00002312 /*
Paul Bakker45829992013-01-03 14:52:21 +01002313 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002316 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2317 {
2318 /* The ciphertext is prefixed with the CBC IV. */
2319 minlen += transform->ivlen;
2320 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002321#endif
Paul Bakker45829992013-01-03 14:52:21 +01002322
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002323 /* Size considerations:
2324 *
2325 * - The CBC cipher text must not be empty and hence
2326 * at least of size transform->ivlen.
2327 *
2328 * Together with the potential IV-prefix, this explains
2329 * the first of the two checks below.
2330 *
2331 * - The record must contain a MAC, either in plain or
2332 * encrypted, depending on whether Encrypt-then-MAC
2333 * is used or not.
2334 * - If it is, the message contains the IV-prefix,
2335 * the CBC ciphertext, and the MAC.
2336 * - If it is not, the padded plaintext, and hence
2337 * the CBC ciphertext, has at least length maclen + 1
2338 * because there is at least the padding length byte.
2339 *
2340 * As the CBC ciphertext is not empty, both cases give the
2341 * lower bound minlen + maclen + 1 on the record size, which
2342 * we test for in the second check below.
2343 */
2344 if( rec->data_len < minlen + transform->ivlen ||
2345 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002348 "+ 1 ) ( + expl IV )", rec->data_len,
2349 transform->ivlen,
2350 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002352 }
2353
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002354 /*
2355 * Authenticate before decrypt if enabled
2356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002358 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002359 {
Hanno Becker992b6872017-11-09 18:57:39 +00002360 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002363
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002364 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2365 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002366
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002367 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002368
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002369 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, 13 );
2370 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2371 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2372 data, rec->data_len );
2373 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2374 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002375
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002376 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2377 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002378 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002379 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002380
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002381 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2382 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002386 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002387 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002388 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002390
2391 /*
2392 * Check length sanity
2393 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002394 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002397 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002399 }
2400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002402 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002403 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002404 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002405 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002406 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002407 /* This is safe because data_len >= minlen + maclen + 1 initially,
2408 * and at this point we have at most subtracted maclen (note that
2409 * minlen == transform->ivlen here). */
2410 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002411
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002412 data += transform->ivlen;
2413 rec->data_offset += transform->ivlen;
2414 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002415 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002417
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002418 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2419 transform->iv_dec, transform->ivlen,
2420 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002423 return( ret );
2424 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002425
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002426 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2429 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002430 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002433 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002434 {
2435 /*
2436 * Save IV in SSL3 and TLS1
2437 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002438 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2439 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002440 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002441#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002442
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002443 /* Safe since data_len >= minlen + maclen + 1, so after having
2444 * subtracted at most minlen and maclen up to this point,
2445 * data_len > 0. */
2446 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002447
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002448 if( auth_done == 1 )
2449 {
2450 correct *= ( rec->data_len >= padlen + 1 );
2451 padlen *= ( rec->data_len >= padlen + 1 );
2452 }
2453 else
Paul Bakker45829992013-01-03 14:52:21 +01002454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002456 if( rec->data_len < transform->maclen + padlen + 1 )
2457 {
2458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2459 rec->data_len,
2460 transform->maclen,
2461 padlen + 1 ) );
2462 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002463#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002464
2465 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2466 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002467 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002469 padlen++;
2470
2471 /* Regardless of the validity of the padding,
2472 * we have data_len >= padlen here. */
2473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002475 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002476 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002477 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479#if defined(MBEDTLS_SSL_DEBUG_ALL)
2480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002481 "should be no more than %d",
2482 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002483#endif
Paul Bakker45829992013-01-03 14:52:21 +01002484 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002485 }
2486 }
2487 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2489#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2490 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002491 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002493 /* The padding check involves a series of up to 256
2494 * consecutive memory reads at the end of the record
2495 * plaintext buffer. In order to hide the length and
2496 * validity of the padding, always perform exactly
2497 * `min(256,plaintext_len)` reads (but take into account
2498 * only the last `padlen` bytes for the padding check). */
2499 size_t pad_count = 0;
2500 size_t real_count = 0;
2501 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002502
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002503 /* Index of first padding byte; it has been ensured above
2504 * that the subtraction is safe. */
2505 size_t const padding_idx = rec->data_len - padlen;
2506 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2507 size_t const start_idx = rec->data_len - num_checks;
2508 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002509
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002510 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002511 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002512 real_count |= ( idx >= padding_idx );
2513 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002514 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002515 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002518 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002520#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002521 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002523 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2525 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2528 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002529 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002530
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002531 /* If the padding was found to be invalid, padlen == 0
2532 * and the subtraction is safe. If the padding was found valid,
2533 * padlen hasn't been changed and the previous assertion
2534 * data_len >= padlen still holds. */
2535 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002537 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002539 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2542 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002543 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002544
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002545#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002547 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002548#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002549
2550 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002551 * Authenticate if not done yet.
2552 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002553 */
Hanno Becker52344c22018-01-03 15:24:20 +00002554#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002555 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002556 {
Hanno Becker992b6872017-11-09 18:57:39 +00002557 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002558
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002559 /* If the initial value of padlen was such that
2560 * data_len < maclen + padlen + 1, then padlen
2561 * got reset to 1, and the initial check
2562 * data_len >= minlen + maclen + 1
2563 * guarantees that at this point we still
2564 * have at least data_len >= maclen.
2565 *
2566 * If the initial value of padlen was such that
2567 * data_len >= maclen + padlen + 1, then we have
2568 * subtracted either padlen + 1 (if the padding was correct)
2569 * or 0 (if the padding was incorrect) since then,
2570 * hence data_len >= maclen in any case.
2571 */
2572 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002574 ssl_extract_add_data_from_record( add_data, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002577 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002578 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002579 ssl_mac( &transform->md_ctx_dec,
2580 transform->mac_dec,
2581 data, rec->data_len,
2582 rec->ctr, rec->type,
2583 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002584 }
2585 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2587#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2588 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002589 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002590 {
2591 /*
2592 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002593 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002594 *
2595 * Known timing attacks:
2596 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2597 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002598 * To compensate for different timings for the MAC calculation
2599 * depending on how much padding was removed (which is determined
2600 * by padlen), process extra_run more blocks through the hash
2601 * function.
2602 *
2603 * The formula in the paper is
2604 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2605 * where L1 is the size of the header plus the decrypted message
2606 * plus CBC padding and L2 is the size of the header plus the
2607 * decrypted message. This is for an underlying hash function
2608 * with 64-byte blocks.
2609 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2610 * correctly. We round down instead of up, so -56 is the correct
2611 * value for our calculations instead of -55.
2612 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002613 * Repeat the formula rather than defining a block_size variable.
2614 * This avoids requiring division by a variable at runtime
2615 * (which would be marginally less efficient and would require
2616 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002617 */
2618 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002619 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002620
2621 /*
2622 * The next two sizes are the minimum and maximum values of
2623 * in_msglen over all padlen values.
2624 *
2625 * They're independent of padlen, since we previously did
2626 * in_msglen -= padlen.
2627 *
2628 * Note that max_len + maclen is never more than the buffer
2629 * length, as we previously did in_msglen -= maclen too.
2630 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002631 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002632 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2633
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002634 memset( tmp, 0, sizeof( tmp ) );
2635
2636 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002637 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002638#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2639 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002640 case MBEDTLS_MD_MD5:
2641 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002642 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002643 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002644 extra_run = ( 13 + rec->data_len + padlen + 8 ) / 64 -
2645 ( 13 + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002646 break;
2647#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002648#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002649 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002650 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002651 extra_run = ( 13 + rec->data_len + padlen + 16 ) / 128 -
2652 ( 13 + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002653 break;
2654#endif
2655 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002657 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2658 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002659
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002660 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002661
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002662 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2663 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2664 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002665 /* Make sure we access everything even when padlen > 0. This
2666 * makes the synchronisation requirements for just-in-time
2667 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002668 ssl_read_memory( data + rec->data_len, padlen );
2669 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002670
2671 /* Call mbedtls_md_process at least once due to cache attacks
2672 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002673 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002674 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002675
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002676 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002677
2678 /* Make sure we access all the memory that could contain the MAC,
2679 * before we check it in the next code block. This makes the
2680 * synchronisation requirements for just-in-time Prime+Probe
2681 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002682 ssl_read_memory( data + min_len,
2683 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002684 }
2685 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2687 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2690 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002691 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002693#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002694 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2695 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002696#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002698 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2699 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701#if defined(MBEDTLS_SSL_DEBUG_ALL)
2702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002703#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002704 correct = 0;
2705 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002706 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002707 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002708
2709 /*
2710 * Finally check the correct flag
2711 */
2712 if( correct == 0 )
2713 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00002714#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002715
2716 /* Make extra sure authentication was performed, exactly once */
2717 if( auth_done != 1 )
2718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2720 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002721 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002724
2725 return( 0 );
2726}
2727
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002728#undef MAC_NONE
2729#undef MAC_PLAINTEXT
2730#undef MAC_CIPHERTEXT
2731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002733/*
2734 * Compression/decompression functions
2735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002737{
2738 int ret;
2739 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002740 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002741 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002742 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002745
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002746 if( len_pre == 0 )
2747 return( 0 );
2748
Paul Bakker2770fbd2012-07-03 13:30:23 +00002749 memcpy( msg_pre, ssl->out_msg, len_pre );
2750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002752 ssl->out_msglen ) );
2753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002755 ssl->out_msg, ssl->out_msglen );
2756
Paul Bakker48916f92012-09-16 19:57:18 +00002757 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2758 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2759 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002760 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002761
Paul Bakker48916f92012-09-16 19:57:18 +00002762 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002763 if( ret != Z_OK )
2764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2766 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002767 }
2768
Angus Grattond8213d02016-05-25 20:56:48 +10002769 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002770 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002773 ssl->out_msglen ) );
2774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002776 ssl->out_msg, ssl->out_msglen );
2777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002779
2780 return( 0 );
2781}
2782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002784{
2785 int ret;
2786 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002787 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002788 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002789 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002792
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002793 if( len_pre == 0 )
2794 return( 0 );
2795
Paul Bakker2770fbd2012-07-03 13:30:23 +00002796 memcpy( msg_pre, ssl->in_msg, len_pre );
2797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002799 ssl->in_msglen ) );
2800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002802 ssl->in_msg, ssl->in_msglen );
2803
Paul Bakker48916f92012-09-16 19:57:18 +00002804 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2805 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2806 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002807 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002808 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002809
Paul Bakker48916f92012-09-16 19:57:18 +00002810 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002811 if( ret != Z_OK )
2812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2814 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002815 }
2816
Angus Grattond8213d02016-05-25 20:56:48 +10002817 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002818 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002821 ssl->in_msglen ) );
2822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002824 ssl->in_msg, ssl->in_msglen );
2825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002827
2828 return( 0 );
2829}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2833static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835#if defined(MBEDTLS_SSL_PROTO_DTLS)
2836static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002837{
2838 /* If renegotiation is not enforced, retransmit until we would reach max
2839 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002840 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002841 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002842 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002843 unsigned char doublings = 1;
2844
2845 while( ratio != 0 )
2846 {
2847 ++doublings;
2848 ratio >>= 1;
2849 }
2850
2851 if( ++ssl->renego_records_seen > doublings )
2852 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002854 return( 0 );
2855 }
2856 }
2857
2858 return( ssl_write_hello_request( ssl ) );
2859}
2860#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002862
Paul Bakker5121ce52009-01-03 21:22:43 +00002863/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002864 * Fill the input message buffer by appending data to it.
2865 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002866 *
2867 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2868 * available (from this read and/or a previous one). Otherwise, an error code
2869 * is returned (possibly EOF or WANT_READ).
2870 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002871 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2872 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2873 * since we always read a whole datagram at once.
2874 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002875 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002876 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002879{
Paul Bakker23986e52011-04-24 08:57:21 +00002880 int ret;
2881 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002884
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002885 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002888 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002890 }
2891
Angus Grattond8213d02016-05-25 20:56:48 +10002892 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2895 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002896 }
2897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002899 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002900 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002901 uint32_t timeout;
2902
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002903 /* Just to be sure */
2904 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2905 {
2906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2907 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2908 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2909 }
2910
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002911 /*
2912 * The point is, we need to always read a full datagram at once, so we
2913 * sometimes read more then requested, and handle the additional data.
2914 * It could be the rest of the current record (while fetching the
2915 * header) and/or some other records in the same datagram.
2916 */
2917
2918 /*
2919 * Move to the next record in the already read datagram if applicable
2920 */
2921 if( ssl->next_record_offset != 0 )
2922 {
2923 if( ssl->in_left < ssl->next_record_offset )
2924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2926 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002927 }
2928
2929 ssl->in_left -= ssl->next_record_offset;
2930
2931 if( ssl->in_left != 0 )
2932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002934 ssl->next_record_offset ) );
2935 memmove( ssl->in_hdr,
2936 ssl->in_hdr + ssl->next_record_offset,
2937 ssl->in_left );
2938 }
2939
2940 ssl->next_record_offset = 0;
2941 }
2942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002944 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002945
2946 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002947 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002948 */
2949 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002952 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002953 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002954
2955 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002956 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002957 * are not at the beginning of a new record, the caller did something
2958 * wrong.
2959 */
2960 if( ssl->in_left != 0 )
2961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2963 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002964 }
2965
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002966 /*
2967 * Don't even try to read if time's out already.
2968 * This avoids by-passing the timer when repeatedly receiving messages
2969 * that will end up being dropped.
2970 */
2971 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002972 {
2973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002974 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002975 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002976 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002977 {
Angus Grattond8213d02016-05-25 20:56:48 +10002978 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002981 timeout = ssl->handshake->retransmit_timeout;
2982 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002983 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002986
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002987 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002988 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2989 timeout );
2990 else
2991 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002994
2995 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002997 }
2998
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002999 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003002 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003005 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003006 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003009 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003010 }
3011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003015 return( ret );
3016 }
3017
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003018 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003019 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003020#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003021 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003023 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003024 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003027 return( ret );
3028 }
3029
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003030 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003031 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003033 }
3034
Paul Bakker5121ce52009-01-03 21:22:43 +00003035 if( ret < 0 )
3036 return( ret );
3037
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003038 ssl->in_left = ret;
3039 }
3040 else
3041#endif
3042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003044 ssl->in_left, nb_want ) );
3045
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003046 while( ssl->in_left < nb_want )
3047 {
3048 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003049
3050 if( ssl_check_timer( ssl ) != 0 )
3051 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3052 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003053 {
3054 if( ssl->f_recv_timeout != NULL )
3055 {
3056 ret = ssl->f_recv_timeout( ssl->p_bio,
3057 ssl->in_hdr + ssl->in_left, len,
3058 ssl->conf->read_timeout );
3059 }
3060 else
3061 {
3062 ret = ssl->f_recv( ssl->p_bio,
3063 ssl->in_hdr + ssl->in_left, len );
3064 }
3065 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003068 ssl->in_left, nb_want ) );
3069 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003070
3071 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003073
3074 if( ret < 0 )
3075 return( ret );
3076
mohammad160352aecb92018-03-28 23:41:40 -07003077 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003078 {
Darryl Green11999bb2018-03-13 15:22:58 +00003079 MBEDTLS_SSL_DEBUG_MSG( 1,
3080 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003081 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003082 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3083 }
3084
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003085 ssl->in_left += ret;
3086 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003087 }
3088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003090
3091 return( 0 );
3092}
3093
3094/*
3095 * Flush any data not yet written
3096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003098{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003099 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003100 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003103
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003104 if( ssl->f_send == NULL )
3105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003107 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003109 }
3110
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003111 /* Avoid incrementing counter if data is flushed */
3112 if( ssl->out_left == 0 )
3113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003115 return( 0 );
3116 }
3117
Paul Bakker5121ce52009-01-03 21:22:43 +00003118 while( ssl->out_left > 0 )
3119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3121 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003122
Hanno Becker2b1e3542018-08-06 11:19:13 +01003123 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003124 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003127
3128 if( ret <= 0 )
3129 return( ret );
3130
mohammad160352aecb92018-03-28 23:41:40 -07003131 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003132 {
Darryl Green11999bb2018-03-13 15:22:58 +00003133 MBEDTLS_SSL_DEBUG_MSG( 1,
3134 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003135 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3137 }
3138
Paul Bakker5121ce52009-01-03 21:22:43 +00003139 ssl->out_left -= ret;
3140 }
3141
Hanno Becker2b1e3542018-08-06 11:19:13 +01003142#if defined(MBEDTLS_SSL_PROTO_DTLS)
3143 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003144 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003145 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003146 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003147 else
3148#endif
3149 {
3150 ssl->out_hdr = ssl->out_buf + 8;
3151 }
3152 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003155
3156 return( 0 );
3157}
3158
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003159/*
3160 * Functions to handle the DTLS retransmission state machine
3161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003163/*
3164 * Append current handshake message to current outgoing flight
3165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003167{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3170 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3171 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003172
3173 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003174 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003175 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003178 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003179 }
3180
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003181 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003182 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003185 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003186 }
3187
3188 /* Copy current handshake message with headers */
3189 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3190 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003191 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003192 msg->next = NULL;
3193
3194 /* Append to the current flight */
3195 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003196 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003197 else
3198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003200 while( cur->next != NULL )
3201 cur = cur->next;
3202 cur->next = msg;
3203 }
3204
Hanno Becker3b235902018-08-06 09:54:53 +01003205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003206 return( 0 );
3207}
3208
3209/*
3210 * Free the current flight of handshake messages
3211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003212static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003213{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 mbedtls_ssl_flight_item *cur = flight;
3215 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003216
3217 while( cur != NULL )
3218 {
3219 next = cur->next;
3220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 mbedtls_free( cur->p );
3222 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003223
3224 cur = next;
3225 }
3226}
3227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3229static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003230#endif
3231
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003232/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003233 * Swap transform_out and out_ctr with the alternative ones
3234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003236{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003238 unsigned char tmp_out_ctr[8];
3239
3240 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003243 return;
3244 }
3245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003247
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003248 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003249 tmp_transform = ssl->transform_out;
3250 ssl->transform_out = ssl->handshake->alt_transform_out;
3251 ssl->handshake->alt_transform_out = tmp_transform;
3252
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003253 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003254 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3255 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003256 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003257
3258 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003259 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003261#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3262 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3267 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003268 }
3269 }
3270#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003271}
3272
3273/*
3274 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003275 */
3276int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3277{
3278 int ret = 0;
3279
3280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3281
3282 ret = mbedtls_ssl_flight_transmit( ssl );
3283
3284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3285
3286 return( ret );
3287}
3288
3289/*
3290 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003291 *
3292 * Need to remember the current message in case flush_output returns
3293 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003294 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003295 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003296int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003297{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003298 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003302 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003304
3305 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003306 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003307 ssl_swap_epochs( ssl );
3308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003310 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003311
3312 while( ssl->handshake->cur_msg != NULL )
3313 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003314 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003315 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003316
Hanno Beckere1dcb032018-08-17 16:47:58 +01003317 int const is_finished =
3318 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3319 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3320
Hanno Becker04da1892018-08-14 13:22:10 +01003321 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3322 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3323
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003324 /* Swap epochs before sending Finished: we can't do it after
3325 * sending ChangeCipherSpec, in case write returns WANT_READ.
3326 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003327 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003328 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003330 ssl_swap_epochs( ssl );
3331 }
3332
Hanno Becker67bc7c32018-08-06 11:33:50 +01003333 ret = ssl_get_remaining_payload_in_datagram( ssl );
3334 if( ret < 0 )
3335 return( ret );
3336 max_frag_len = (size_t) ret;
3337
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003338 /* CCS is copied as is, while HS messages may need fragmentation */
3339 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3340 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003341 if( max_frag_len == 0 )
3342 {
3343 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3344 return( ret );
3345
3346 continue;
3347 }
3348
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003349 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003350 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003351 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003352
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003353 /* Update position inside current message */
3354 ssl->handshake->cur_msg_p += cur->len;
3355 }
3356 else
3357 {
3358 const unsigned char * const p = ssl->handshake->cur_msg_p;
3359 const size_t hs_len = cur->len - 12;
3360 const size_t frag_off = p - ( cur->p + 12 );
3361 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003362 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003363
Hanno Beckere1dcb032018-08-17 16:47:58 +01003364 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003365 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003366 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003367 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003368
Hanno Becker67bc7c32018-08-06 11:33:50 +01003369 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3370 return( ret );
3371
3372 continue;
3373 }
3374 max_hs_frag_len = max_frag_len - 12;
3375
3376 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3377 max_hs_frag_len : rem_len;
3378
3379 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003380 {
3381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003382 (unsigned) cur_hs_frag_len,
3383 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003384 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003385
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003386 /* Messages are stored with handshake headers as if not fragmented,
3387 * copy beginning of headers then fill fragmentation fields.
3388 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3389 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003390
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003391 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3392 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3393 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3394
Hanno Becker67bc7c32018-08-06 11:33:50 +01003395 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3396 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3397 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003398
3399 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3400
Hanno Becker3f7b9732018-08-28 09:53:25 +01003401 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003402 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3403 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003404 ssl->out_msgtype = cur->type;
3405
3406 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003407 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003408 }
3409
3410 /* If done with the current message move to the next one if any */
3411 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3412 {
3413 if( cur->next != NULL )
3414 {
3415 ssl->handshake->cur_msg = cur->next;
3416 ssl->handshake->cur_msg_p = cur->next->p + 12;
3417 }
3418 else
3419 {
3420 ssl->handshake->cur_msg = NULL;
3421 ssl->handshake->cur_msg_p = NULL;
3422 }
3423 }
3424
3425 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003426 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003429 return( ret );
3430 }
3431 }
3432
Hanno Becker67bc7c32018-08-06 11:33:50 +01003433 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3434 return( ret );
3435
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003436 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3438 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003439 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003442 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3443 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003444
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003446
3447 return( 0 );
3448}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003449
3450/*
3451 * To be called when the last message of an incoming flight is received.
3452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003454{
3455 /* We won't need to resend that one any more */
3456 ssl_flight_free( ssl->handshake->flight );
3457 ssl->handshake->flight = NULL;
3458 ssl->handshake->cur_msg = NULL;
3459
3460 /* The next incoming flight will start with this msg_seq */
3461 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3462
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003463 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003464 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003465
Hanno Becker0271f962018-08-16 13:23:47 +01003466 /* Clear future message buffering structure. */
3467 ssl_buffering_free( ssl );
3468
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003469 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003470 ssl_set_timer( ssl, 0 );
3471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3473 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003475 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003476 }
3477 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003479}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003480
3481/*
3482 * To be called when the last message of an outgoing flight is send.
3483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003485{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003486 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003487 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3490 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003493 }
3494 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003496}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003498
Paul Bakker5121ce52009-01-03 21:22:43 +00003499/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003500 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003501 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003502
3503/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003504 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003505 *
3506 * - fill in handshake headers
3507 * - update handshake checksum
3508 * - DTLS: save message for resending
3509 * - then pass to the record layer
3510 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003511 * DTLS: except for HelloRequest, messages are only queued, and will only be
3512 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003513 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003514 * Inputs:
3515 * - ssl->out_msglen: 4 + actual handshake message len
3516 * (4 is the size of handshake headers for TLS)
3517 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3518 * - ssl->out_msg + 4: the handshake message body
3519 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003520 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003521 * - ssl->out_msglen: the length of the record contents
3522 * (including handshake headers but excluding record headers)
3523 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003524 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003525int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003526{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003527 int ret;
3528 const size_t hs_len = ssl->out_msglen - 4;
3529 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003530
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3532
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003533 /*
3534 * Sanity checks
3535 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003536 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003537 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3538 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003539 /* In SSLv3, the client might send a NoCertificate alert. */
3540#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3541 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3542 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3543 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3544#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3545 {
3546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3547 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3548 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003549 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003550
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003551 /* Whenever we send anything different from a
3552 * HelloRequest we should be in a handshake - double check. */
3553 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3554 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003555 ssl->handshake == NULL )
3556 {
3557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3558 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3559 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003560
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é-Gonnardffa67be2014-09-19 11:18:57 +02003563 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003565 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3567 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003568 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003569#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003570
Hanno Beckerb50a2532018-08-06 11:52:54 +01003571 /* Double-check that we did not exceed the bounds
3572 * of the outgoing record buffer.
3573 * This should never fail as the various message
3574 * writing functions must obey the bounds of the
3575 * outgoing record buffer, but better be safe.
3576 *
3577 * Note: We deliberately do not check for the MTU or MFL here.
3578 */
3579 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3580 {
3581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3582 "size %u, maximum %u",
3583 (unsigned) ssl->out_msglen,
3584 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3585 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3586 }
3587
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003588 /*
3589 * Fill handshake headers
3590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003591 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003592 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003593 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3594 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3595 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003596
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003597 /*
3598 * DTLS has additional fields in the Handshake layer,
3599 * between the length field and the actual payload:
3600 * uint16 message_seq;
3601 * uint24 fragment_offset;
3602 * uint24 fragment_length;
3603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003605 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003606 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003607 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003608 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003609 {
3610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3611 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003612 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003613 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003614 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3615 }
3616
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003617 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003618 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003619
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003620 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003621 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003622 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003623 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3624 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3625 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003626 }
3627 else
3628 {
3629 ssl->out_msg[4] = 0;
3630 ssl->out_msg[5] = 0;
3631 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003632
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003633 /* Handshake hashes are computed without fragmentation,
3634 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003635 memset( ssl->out_msg + 6, 0x00, 3 );
3636 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003637 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003639
Hanno Becker0207e532018-08-28 10:28:28 +01003640 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003641 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3642 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003643 }
3644
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003645 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003647 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003648 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3649 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003650 {
3651 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003654 return( ret );
3655 }
3656 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003657 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003658#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003659 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003660 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003661 {
3662 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3663 return( ret );
3664 }
3665 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003666
3667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3668
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003669 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003670}
3671
3672/*
3673 * Record layer functions
3674 */
3675
3676/*
3677 * Write current record.
3678 *
3679 * Uses:
3680 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3681 * - ssl->out_msglen: length of the record content (excl headers)
3682 * - ssl->out_msg: record content
3683 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003684int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003685{
3686 int ret, done = 0;
3687 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003688 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003689
3690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003692#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003693 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003695 {
3696 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003699 return( ret );
3700 }
3701
3702 len = ssl->out_msglen;
3703 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3707 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711 ret = mbedtls_ssl_hw_record_write( ssl );
3712 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3715 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003716 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003717
3718 if( ret == 0 )
3719 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003720 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003722 if( !done )
3723 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003724 unsigned i;
3725 size_t protected_record_size;
3726
Paul Bakker05ef8352012-05-08 09:17:57 +00003727 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003729 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003730
Hanno Becker19859472018-08-06 09:40:20 +01003731 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003732 ssl->out_len[0] = (unsigned char)( len >> 8 );
3733 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003734
Paul Bakker48916f92012-09-16 19:57:18 +00003735 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003736 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003737 mbedtls_record rec;
3738
3739 rec.buf = ssl->out_iv;
3740 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3741 ( ssl->out_iv - ssl->out_buf );
3742 rec.data_len = ssl->out_msglen;
3743 rec.data_offset = ssl->out_msg - rec.buf;
3744
3745 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3746 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3747 ssl->conf->transport, rec.ver );
3748 rec.type = ssl->out_msgtype;
3749
3750 if( ( ret = ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
3751 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003753 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003754 return( ret );
3755 }
3756
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003757 if( rec.data_offset != 0 )
3758 {
3759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3760 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3761 }
3762
3763 ssl->out_msglen = rec.data_len;
3764 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3765 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003766 }
3767
Hanno Becker2b1e3542018-08-06 11:19:13 +01003768 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3769
3770#if defined(MBEDTLS_SSL_PROTO_DTLS)
3771 /* In case of DTLS, double-check that we don't exceed
3772 * the remaining space in the datagram. */
3773 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3774 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003775 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003776 if( ret < 0 )
3777 return( ret );
3778
3779 if( protected_record_size > (size_t) ret )
3780 {
3781 /* Should never happen */
3782 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3783 }
3784 }
3785#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003788 "version = [%d:%d], msglen = %d",
3789 ssl->out_hdr[0], ssl->out_hdr[1],
3790 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003793 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003794
3795 ssl->out_left += protected_record_size;
3796 ssl->out_hdr += protected_record_size;
3797 ssl_update_out_pointers( ssl, ssl->transform_out );
3798
Hanno Becker04484622018-08-06 09:49:38 +01003799 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3800 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3801 break;
3802
3803 /* The loop goes to its end iff the counter is wrapping */
3804 if( i == ssl_ep_len( ssl ) )
3805 {
3806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3807 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3808 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003809 }
3810
Hanno Becker67bc7c32018-08-06 11:33:50 +01003811#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003812 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3813 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003814 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003815 size_t remaining;
3816 ret = ssl_get_remaining_payload_in_datagram( ssl );
3817 if( ret < 0 )
3818 {
3819 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3820 ret );
3821 return( ret );
3822 }
3823
3824 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003825 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003826 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003827 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003828 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003829 else
3830 {
Hanno Becker513815a2018-08-20 11:56:09 +01003831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003832 }
3833 }
3834#endif /* MBEDTLS_SSL_PROTO_DTLS */
3835
3836 if( ( flush == SSL_FORCE_FLUSH ) &&
3837 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003839 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003840 return( ret );
3841 }
3842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003844
3845 return( 0 );
3846}
3847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003849
3850static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3851{
3852 if( ssl->in_msglen < ssl->in_hslen ||
3853 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3854 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3855 {
3856 return( 1 );
3857 }
3858 return( 0 );
3859}
Hanno Becker44650b72018-08-16 12:51:11 +01003860
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003861static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003862{
3863 return( ( ssl->in_msg[9] << 16 ) |
3864 ( ssl->in_msg[10] << 8 ) |
3865 ssl->in_msg[11] );
3866}
3867
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003868static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003869{
3870 return( ( ssl->in_msg[6] << 16 ) |
3871 ( ssl->in_msg[7] << 8 ) |
3872 ssl->in_msg[8] );
3873}
3874
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003875static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003876{
3877 uint32_t msg_len, frag_off, frag_len;
3878
3879 msg_len = ssl_get_hs_total_len( ssl );
3880 frag_off = ssl_get_hs_frag_off( ssl );
3881 frag_len = ssl_get_hs_frag_len( ssl );
3882
3883 if( frag_off > msg_len )
3884 return( -1 );
3885
3886 if( frag_len > msg_len - frag_off )
3887 return( -1 );
3888
3889 if( frag_len + 12 > ssl->in_msglen )
3890 return( -1 );
3891
3892 return( 0 );
3893}
3894
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003895/*
3896 * Mark bits in bitmask (used for DTLS HS reassembly)
3897 */
3898static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3899{
3900 unsigned int start_bits, end_bits;
3901
3902 start_bits = 8 - ( offset % 8 );
3903 if( start_bits != 8 )
3904 {
3905 size_t first_byte_idx = offset / 8;
3906
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003907 /* Special case */
3908 if( len <= start_bits )
3909 {
3910 for( ; len != 0; len-- )
3911 mask[first_byte_idx] |= 1 << ( start_bits - len );
3912
3913 /* Avoid potential issues with offset or len becoming invalid */
3914 return;
3915 }
3916
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003917 offset += start_bits; /* Now offset % 8 == 0 */
3918 len -= start_bits;
3919
3920 for( ; start_bits != 0; start_bits-- )
3921 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3922 }
3923
3924 end_bits = len % 8;
3925 if( end_bits != 0 )
3926 {
3927 size_t last_byte_idx = ( offset + len ) / 8;
3928
3929 len -= end_bits; /* Now len % 8 == 0 */
3930
3931 for( ; end_bits != 0; end_bits-- )
3932 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3933 }
3934
3935 memset( mask + offset / 8, 0xFF, len / 8 );
3936}
3937
3938/*
3939 * Check that bitmask is full
3940 */
3941static int ssl_bitmask_check( unsigned char *mask, size_t len )
3942{
3943 size_t i;
3944
3945 for( i = 0; i < len / 8; i++ )
3946 if( mask[i] != 0xFF )
3947 return( -1 );
3948
3949 for( i = 0; i < len % 8; i++ )
3950 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3951 return( -1 );
3952
3953 return( 0 );
3954}
3955
Hanno Becker56e205e2018-08-16 09:06:12 +01003956/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003957static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003958 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003959{
Hanno Becker56e205e2018-08-16 09:06:12 +01003960 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003961
Hanno Becker56e205e2018-08-16 09:06:12 +01003962 alloc_len = 12; /* Handshake header */
3963 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003964
Hanno Beckerd07df862018-08-16 09:14:58 +01003965 if( add_bitmap )
3966 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003967
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003968 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003969}
Hanno Becker56e205e2018-08-16 09:06:12 +01003970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003972
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003973static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003974{
3975 return( ( ssl->in_msg[1] << 16 ) |
3976 ( ssl->in_msg[2] << 8 ) |
3977 ssl->in_msg[3] );
3978}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003979
Simon Butcher99000142016-10-13 17:21:01 +01003980int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003981{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003985 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003986 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003987 }
3988
Hanno Becker12555c62018-08-16 12:47:53 +01003989 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003991 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003992 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003993 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003995#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003996 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003997 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003998 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003999 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004000
Hanno Becker44650b72018-08-16 12:51:11 +01004001 if( ssl_check_hs_header( ssl ) != 0 )
4002 {
4003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4004 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4005 }
4006
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004007 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004008 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4009 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4010 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4011 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004012 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004013 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4014 {
4015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4016 recv_msg_seq,
4017 ssl->handshake->in_msg_seq ) );
4018 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4019 }
4020
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004021 /* Retransmit only on last message from previous flight, to avoid
4022 * too many retransmissions.
4023 * Besides, No sane server ever retransmits HelloVerifyRequest */
4024 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004025 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004028 "message_seq = %d, start_of_flight = %d",
4029 recv_msg_seq,
4030 ssl->handshake->in_flight_start_seq ) );
4031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004032 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004034 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004035 return( ret );
4036 }
4037 }
4038 else
4039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004041 "message_seq = %d, expected = %d",
4042 recv_msg_seq,
4043 ssl->handshake->in_msg_seq ) );
4044 }
4045
Hanno Becker90333da2017-10-10 11:27:13 +01004046 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004047 }
4048 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004049
Hanno Becker6d97ef52018-08-16 13:09:04 +01004050 /* Message reassembly is handled alongside buffering of future
4051 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004052 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004053 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004054 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004057 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004058 }
4059 }
4060 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004061#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004062 /* With TLS we don't handle fragmentation (for now) */
4063 if( ssl->in_msglen < ssl->in_hslen )
4064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4066 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004067 }
4068
Simon Butcher99000142016-10-13 17:21:01 +01004069 return( 0 );
4070}
4071
4072void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4073{
Hanno Becker0271f962018-08-16 13:23:47 +01004074 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004075
Hanno Becker0271f962018-08-16 13:23:47 +01004076 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004077 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004078 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004079 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004080
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004081 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004082#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004083 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004084 ssl->handshake != NULL )
4085 {
Hanno Becker0271f962018-08-16 13:23:47 +01004086 unsigned offset;
4087 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004088
Hanno Becker0271f962018-08-16 13:23:47 +01004089 /* Increment handshake sequence number */
4090 hs->in_msg_seq++;
4091
4092 /*
4093 * Clear up handshake buffering and reassembly structure.
4094 */
4095
4096 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004097 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004098
4099 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004100 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4101 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004102 offset++, hs_buf++ )
4103 {
4104 *hs_buf = *(hs_buf + 1);
4105 }
4106
4107 /* Create a fresh last entry */
4108 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004109 }
4110#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004111}
4112
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004113/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004114 * DTLS anti-replay: RFC 6347 4.1.2.6
4115 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004116 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4117 * Bit n is set iff record number in_window_top - n has been seen.
4118 *
4119 * Usually, in_window_top is the last record number seen and the lsb of
4120 * in_window is set. The only exception is the initial state (record number 0
4121 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4124static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004125{
4126 ssl->in_window_top = 0;
4127 ssl->in_window = 0;
4128}
4129
4130static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4131{
4132 return( ( (uint64_t) buf[0] << 40 ) |
4133 ( (uint64_t) buf[1] << 32 ) |
4134 ( (uint64_t) buf[2] << 24 ) |
4135 ( (uint64_t) buf[3] << 16 ) |
4136 ( (uint64_t) buf[4] << 8 ) |
4137 ( (uint64_t) buf[5] ) );
4138}
4139
4140/*
4141 * Return 0 if sequence number is acceptable, -1 otherwise
4142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004144{
4145 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4146 uint64_t bit;
4147
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004148 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004149 return( 0 );
4150
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004151 if( rec_seqnum > ssl->in_window_top )
4152 return( 0 );
4153
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004154 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004155
4156 if( bit >= 64 )
4157 return( -1 );
4158
4159 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4160 return( -1 );
4161
4162 return( 0 );
4163}
4164
4165/*
4166 * Update replay window on new validated record
4167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004168void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004169{
4170 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4171
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004172 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004173 return;
4174
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004175 if( rec_seqnum > ssl->in_window_top )
4176 {
4177 /* Update window_top and the contents of the window */
4178 uint64_t shift = rec_seqnum - ssl->in_window_top;
4179
4180 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004181 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004182 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004183 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004184 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004185 ssl->in_window |= 1;
4186 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004187
4188 ssl->in_window_top = rec_seqnum;
4189 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004190 else
4191 {
4192 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004193 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004194
4195 if( bit < 64 ) /* Always true, but be extra sure */
4196 ssl->in_window |= (uint64_t) 1 << bit;
4197 }
4198}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004199#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004200
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004201#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004202/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004203static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4204
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004205/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004206 * Without any SSL context, check if a datagram looks like a ClientHello with
4207 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004208 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004209 *
4210 * - if cookie is valid, return 0
4211 * - if ClientHello looks superficially valid but cookie is not,
4212 * fill obuf and set olen, then
4213 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4214 * - otherwise return a specific error code
4215 */
4216static int ssl_check_dtls_clihlo_cookie(
4217 mbedtls_ssl_cookie_write_t *f_cookie_write,
4218 mbedtls_ssl_cookie_check_t *f_cookie_check,
4219 void *p_cookie,
4220 const unsigned char *cli_id, size_t cli_id_len,
4221 const unsigned char *in, size_t in_len,
4222 unsigned char *obuf, size_t buf_len, size_t *olen )
4223{
4224 size_t sid_len, cookie_len;
4225 unsigned char *p;
4226
4227 if( f_cookie_write == NULL || f_cookie_check == NULL )
4228 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4229
4230 /*
4231 * Structure of ClientHello with record and handshake headers,
4232 * and expected values. We don't need to check a lot, more checks will be
4233 * done when actually parsing the ClientHello - skipping those checks
4234 * avoids code duplication and does not make cookie forging any easier.
4235 *
4236 * 0-0 ContentType type; copied, must be handshake
4237 * 1-2 ProtocolVersion version; copied
4238 * 3-4 uint16 epoch; copied, must be 0
4239 * 5-10 uint48 sequence_number; copied
4240 * 11-12 uint16 length; (ignored)
4241 *
4242 * 13-13 HandshakeType msg_type; (ignored)
4243 * 14-16 uint24 length; (ignored)
4244 * 17-18 uint16 message_seq; copied
4245 * 19-21 uint24 fragment_offset; copied, must be 0
4246 * 22-24 uint24 fragment_length; (ignored)
4247 *
4248 * 25-26 ProtocolVersion client_version; (ignored)
4249 * 27-58 Random random; (ignored)
4250 * 59-xx SessionID session_id; 1 byte len + sid_len content
4251 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4252 * ...
4253 *
4254 * Minimum length is 61 bytes.
4255 */
4256 if( in_len < 61 ||
4257 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4258 in[3] != 0 || in[4] != 0 ||
4259 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4260 {
4261 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4262 }
4263
4264 sid_len = in[59];
4265 if( sid_len > in_len - 61 )
4266 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4267
4268 cookie_len = in[60 + sid_len];
4269 if( cookie_len > in_len - 60 )
4270 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4271
4272 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4273 cli_id, cli_id_len ) == 0 )
4274 {
4275 /* Valid cookie */
4276 return( 0 );
4277 }
4278
4279 /*
4280 * If we get here, we've got an invalid cookie, let's prepare HVR.
4281 *
4282 * 0-0 ContentType type; copied
4283 * 1-2 ProtocolVersion version; copied
4284 * 3-4 uint16 epoch; copied
4285 * 5-10 uint48 sequence_number; copied
4286 * 11-12 uint16 length; olen - 13
4287 *
4288 * 13-13 HandshakeType msg_type; hello_verify_request
4289 * 14-16 uint24 length; olen - 25
4290 * 17-18 uint16 message_seq; copied
4291 * 19-21 uint24 fragment_offset; copied
4292 * 22-24 uint24 fragment_length; olen - 25
4293 *
4294 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4295 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4296 *
4297 * Minimum length is 28.
4298 */
4299 if( buf_len < 28 )
4300 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4301
4302 /* Copy most fields and adapt others */
4303 memcpy( obuf, in, 25 );
4304 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4305 obuf[25] = 0xfe;
4306 obuf[26] = 0xff;
4307
4308 /* Generate and write actual cookie */
4309 p = obuf + 28;
4310 if( f_cookie_write( p_cookie,
4311 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4312 {
4313 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4314 }
4315
4316 *olen = p - obuf;
4317
4318 /* Go back and fill length fields */
4319 obuf[27] = (unsigned char)( *olen - 28 );
4320
4321 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4322 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4323 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4324
4325 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4326 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4327
4328 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4329}
4330
4331/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004332 * Handle possible client reconnect with the same UDP quadruplet
4333 * (RFC 6347 Section 4.2.8).
4334 *
4335 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4336 * that looks like a ClientHello.
4337 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004338 * - if the input looks like a ClientHello without cookies,
4339 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004340 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004341 * - if the input looks like a ClientHello with a valid cookie,
4342 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004343 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004344 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004345 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004346 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004347 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4348 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004349 */
4350static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4351{
4352 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004353 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004354
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004355 ret = ssl_check_dtls_clihlo_cookie(
4356 ssl->conf->f_cookie_write,
4357 ssl->conf->f_cookie_check,
4358 ssl->conf->p_cookie,
4359 ssl->cli_id, ssl->cli_id_len,
4360 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004361 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004362
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004363 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4364
4365 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004366 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004367 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004368 * If the error is permanent we'll catch it later,
4369 * if it's not, then hopefully it'll work next time. */
4370 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4371
4372 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004373 }
4374
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004375 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004376 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004377 /* Got a valid cookie, partially reset context */
4378 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4379 {
4380 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4381 return( ret );
4382 }
4383
4384 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004385 }
4386
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004387 return( ret );
4388}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004389#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004390
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004391/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004392 * ContentType type;
4393 * ProtocolVersion version;
4394 * uint16 epoch; // DTLS only
4395 * uint48 sequence_number; // DTLS only
4396 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004397 *
4398 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004399 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004400 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4401 *
4402 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004403 * 1. proceed with the record if this function returns 0
4404 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4405 * 3. return CLIENT_RECONNECT if this function return that value
4406 * 4. drop the whole datagram if this function returns anything else.
4407 * Point 2 is needed when the peer is resending, and we have already received
4408 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004410static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004411{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004412 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004414 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 +02004415
Paul Bakker5121ce52009-01-03 21:22:43 +00004416 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004417 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004418 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004420 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004421 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004422 ssl->in_msgtype,
4423 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004424
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004425 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004426 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4427 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4428 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4429 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004432
4433#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004434 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4435 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004436 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4437#endif /* MBEDTLS_SSL_PROTO_DTLS */
4438 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4439 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004441 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004442 }
4443
4444 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004445 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4448 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004449 }
4450
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004451 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4454 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004455 }
4456
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004457 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004458 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004459 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4462 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004463 }
4464
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004465 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004466 * DTLS-related tests.
4467 * Check epoch before checking length constraint because
4468 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4469 * message gets duplicated before the corresponding Finished message,
4470 * the second ChangeCipherSpec should be discarded because it belongs
4471 * to an old epoch, but not because its length is shorter than
4472 * the minimum record length for packets using the new record transform.
4473 * Note that these two kinds of failures are handled differently,
4474 * as an unexpected record is silently skipped but an invalid
4475 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004476 */
4477#if defined(MBEDTLS_SSL_PROTO_DTLS)
4478 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4479 {
4480 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4481
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004482 /* Check epoch (and sequence number) with DTLS */
4483 if( rec_epoch != ssl->in_epoch )
4484 {
4485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4486 "expected %d, received %d",
4487 ssl->in_epoch, rec_epoch ) );
4488
4489#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4490 /*
4491 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4492 * access the first byte of record content (handshake type), as we
4493 * have an active transform (possibly iv_len != 0), so use the
4494 * fact that the record header len is 13 instead.
4495 */
4496 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4497 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4498 rec_epoch == 0 &&
4499 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4500 ssl->in_left > 13 &&
4501 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4502 {
4503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4504 "from the same port" ) );
4505 return( ssl_handle_possible_reconnect( ssl ) );
4506 }
4507 else
4508#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004509 {
4510 /* Consider buffering the record. */
4511 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4512 {
4513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4514 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4515 }
4516
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004517 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004518 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004519 }
4520
4521#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4522 /* Replay detection only works for the current epoch */
4523 if( rec_epoch == ssl->in_epoch &&
4524 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4525 {
4526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4527 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4528 }
4529#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004530
Hanno Becker52c6dc62017-05-26 16:07:36 +01004531 /* Drop unexpected ApplicationData records,
4532 * except at the beginning of renegotiations */
4533 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4534 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4535#if defined(MBEDTLS_SSL_RENEGOTIATION)
4536 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4537 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4538#endif
4539 )
4540 {
4541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4542 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4543 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004544 }
4545#endif /* MBEDTLS_SSL_PROTO_DTLS */
4546
Hanno Becker52c6dc62017-05-26 16:07:36 +01004547
4548 /* Check length against bounds of the current transform and version */
4549 if( ssl->transform_in == NULL )
4550 {
4551 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004552 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004553 {
4554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4555 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4556 }
4557 }
4558 else
4559 {
4560 if( ssl->in_msglen < ssl->transform_in->minlen )
4561 {
4562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4563 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4564 }
4565
4566#if defined(MBEDTLS_SSL_PROTO_SSL3)
4567 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004568 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004569 {
4570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4571 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4572 }
4573#endif
4574#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4575 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4576 /*
4577 * TLS encrypted messages can have up to 256 bytes of padding
4578 */
4579 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4580 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004581 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004582 {
4583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4584 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4585 }
4586#endif
4587 }
4588
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004589 return( 0 );
4590}
Paul Bakker5121ce52009-01-03 21:22:43 +00004591
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004592/*
4593 * If applicable, decrypt (and decompress) record content
4594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004595static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004596{
4597 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004599 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4600 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004602#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4603 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004607 ret = mbedtls_ssl_hw_record_read( ssl );
4608 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004610 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4611 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004612 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004613
4614 if( ret == 0 )
4615 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004616 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004617#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004618 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004619 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004620 mbedtls_record rec;
4621
4622 rec.buf = ssl->in_iv;
4623 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4624 - ( ssl->in_iv - ssl->in_buf );
4625 rec.data_len = ssl->in_msglen;
4626 rec.data_offset = 0;
4627
4628 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4629 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4630 ssl->conf->transport, rec.ver );
4631 rec.type = ssl->in_msgtype;
4632 if( ( ret = ssl_decrypt_buf( ssl, ssl->transform_in, &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004634 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004635 return( ret );
4636 }
4637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004638 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004639 ssl->in_msg, ssl->in_msglen );
4640
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004641 ssl->in_msglen = rec.data_len;
4642 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4643 ssl->in_len[1] = (unsigned char)( rec.data_len );
4644
Angus Grattond8213d02016-05-25 20:56:48 +10004645 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4648 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004649 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004650 else if( ssl->in_msglen == 0 )
4651 {
4652#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4653 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4654 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4655 {
4656 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4658 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4659 }
4660#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4661
4662 ssl->nb_zero++;
4663
4664 /*
4665 * Three or more empty messages may be a DoS attack
4666 * (excessive CPU consumption).
4667 */
4668 if( ssl->nb_zero > 3 )
4669 {
4670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
4671 "messages, possible DoS attack" ) );
4672 /* Q: Is that the right error code? */
4673 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4674 }
4675 }
4676 else
4677 ssl->nb_zero = 0;
4678
4679#if defined(MBEDTLS_SSL_PROTO_DTLS)
4680 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4681 {
4682 ; /* in_ctr read from peer, not maintained internally */
4683 }
4684 else
4685#endif
4686 {
4687 unsigned i;
4688 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4689 if( ++ssl->in_ctr[i - 1] != 0 )
4690 break;
4691
4692 /* The loop goes to its end iff the counter is wrapping */
4693 if( i == ssl_ep_len( ssl ) )
4694 {
4695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4696 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4697 }
4698 }
4699
Paul Bakker5121ce52009-01-03 21:22:43 +00004700 }
4701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004702#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004703 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004704 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004705 {
4706 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004708 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004709 return( ret );
4710 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004711 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004714#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004715 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004717 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004718 }
4719#endif
4720
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004721 return( 0 );
4722}
4723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004724static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004725
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004726/*
4727 * Read a record.
4728 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004729 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4730 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4731 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004732 */
Hanno Becker1097b342018-08-15 14:09:41 +01004733
4734/* Helper functions for mbedtls_ssl_read_record(). */
4735static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004736static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4737static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004738
Hanno Becker327c93b2018-08-15 13:56:18 +01004739int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004740 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004741{
4742 int ret;
4743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004745
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004746 if( ssl->keep_current_message == 0 )
4747 {
4748 do {
Simon Butcher99000142016-10-13 17:21:01 +01004749
Hanno Becker26994592018-08-15 14:14:59 +01004750 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004751 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004752 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004753
Hanno Beckere74d5562018-08-15 14:26:08 +01004754 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004755 {
Hanno Becker40f50842018-08-15 14:48:01 +01004756#if defined(MBEDTLS_SSL_PROTO_DTLS)
4757 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004758
Hanno Becker40f50842018-08-15 14:48:01 +01004759 /* We only check for buffered messages if the
4760 * current datagram is fully consumed. */
4761 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004762 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004763 {
Hanno Becker40f50842018-08-15 14:48:01 +01004764 if( ssl_load_buffered_message( ssl ) == 0 )
4765 have_buffered = 1;
4766 }
4767
4768 if( have_buffered == 0 )
4769#endif /* MBEDTLS_SSL_PROTO_DTLS */
4770 {
4771 ret = ssl_get_next_record( ssl );
4772 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4773 continue;
4774
4775 if( ret != 0 )
4776 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004777 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004778 return( ret );
4779 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004780 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004781 }
4782
4783 ret = mbedtls_ssl_handle_message_type( ssl );
4784
Hanno Becker40f50842018-08-15 14:48:01 +01004785#if defined(MBEDTLS_SSL_PROTO_DTLS)
4786 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4787 {
4788 /* Buffer future message */
4789 ret = ssl_buffer_message( ssl );
4790 if( ret != 0 )
4791 return( ret );
4792
4793 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4794 }
4795#endif /* MBEDTLS_SSL_PROTO_DTLS */
4796
Hanno Becker90333da2017-10-10 11:27:13 +01004797 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4798 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004799
4800 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004801 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004802 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004803 return( ret );
4804 }
4805
Hanno Becker327c93b2018-08-15 13:56:18 +01004806 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004807 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004808 {
4809 mbedtls_ssl_update_handshake_status( ssl );
4810 }
Simon Butcher99000142016-10-13 17:21:01 +01004811 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004812 else
Simon Butcher99000142016-10-13 17:21:01 +01004813 {
Hanno Becker02f59072018-08-15 14:00:24 +01004814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004815 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004816 }
4817
4818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4819
4820 return( 0 );
4821}
4822
Hanno Becker40f50842018-08-15 14:48:01 +01004823#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004824static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004825{
Hanno Becker40f50842018-08-15 14:48:01 +01004826 if( ssl->in_left > ssl->next_record_offset )
4827 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004828
Hanno Becker40f50842018-08-15 14:48:01 +01004829 return( 0 );
4830}
4831
4832static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4833{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004834 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004835 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004836 int ret = 0;
4837
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004838 if( hs == NULL )
4839 return( -1 );
4840
Hanno Beckere00ae372018-08-20 09:39:42 +01004841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4842
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004843 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4844 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4845 {
4846 /* Check if we have seen a ChangeCipherSpec before.
4847 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004848 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004849 {
4850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4851 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004852 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004853 }
4854
Hanno Becker39b8bc92018-08-28 17:17:13 +01004855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004856 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4857 ssl->in_msglen = 1;
4858 ssl->in_msg[0] = 1;
4859
4860 /* As long as they are equal, the exact value doesn't matter. */
4861 ssl->in_left = 0;
4862 ssl->next_record_offset = 0;
4863
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004864 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004865 goto exit;
4866 }
Hanno Becker37f95322018-08-16 13:55:32 +01004867
Hanno Beckerb8f50142018-08-28 10:01:34 +01004868#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004869 /* Debug only */
4870 {
4871 unsigned offset;
4872 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4873 {
4874 hs_buf = &hs->buffering.hs[offset];
4875 if( hs_buf->is_valid == 1 )
4876 {
4877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4878 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004879 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004880 }
4881 }
4882 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004883#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004884
4885 /* Check if we have buffered and/or fully reassembled the
4886 * next handshake message. */
4887 hs_buf = &hs->buffering.hs[0];
4888 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4889 {
4890 /* Synthesize a record containing the buffered HS message. */
4891 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4892 ( hs_buf->data[2] << 8 ) |
4893 hs_buf->data[3];
4894
4895 /* Double-check that we haven't accidentally buffered
4896 * a message that doesn't fit into the input buffer. */
4897 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4898 {
4899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4900 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4901 }
4902
4903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4904 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4905 hs_buf->data, msg_len + 12 );
4906
4907 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4908 ssl->in_hslen = msg_len + 12;
4909 ssl->in_msglen = msg_len + 12;
4910 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4911
4912 ret = 0;
4913 goto exit;
4914 }
4915 else
4916 {
4917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4918 hs->in_msg_seq ) );
4919 }
4920
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004921 ret = -1;
4922
4923exit:
4924
4925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4926 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004927}
4928
Hanno Beckera02b0b42018-08-21 17:20:27 +01004929static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4930 size_t desired )
4931{
4932 int offset;
4933 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4935 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004936
Hanno Becker01315ea2018-08-21 17:22:17 +01004937 /* Get rid of future records epoch first, if such exist. */
4938 ssl_free_buffered_record( ssl );
4939
4940 /* Check if we have enough space available now. */
4941 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4942 hs->buffering.total_bytes_buffered ) )
4943 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004945 return( 0 );
4946 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004947
Hanno Becker4f432ad2018-08-28 10:02:32 +01004948 /* We don't have enough space to buffer the next expected handshake
4949 * message. Remove buffers used for future messages to gain space,
4950 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004951 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4952 offset >= 0; offset-- )
4953 {
4954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4955 offset ) );
4956
Hanno Beckerb309b922018-08-23 13:18:05 +01004957 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004958
4959 /* Check if we have enough space available now. */
4960 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4961 hs->buffering.total_bytes_buffered ) )
4962 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004964 return( 0 );
4965 }
4966 }
4967
4968 return( -1 );
4969}
4970
Hanno Becker40f50842018-08-15 14:48:01 +01004971static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4972{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004973 int ret = 0;
4974 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4975
4976 if( hs == NULL )
4977 return( 0 );
4978
4979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4980
4981 switch( ssl->in_msgtype )
4982 {
4983 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004985
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004986 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004987 break;
4988
4989 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004990 {
4991 unsigned recv_msg_seq_offset;
4992 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4993 mbedtls_ssl_hs_buffer *hs_buf;
4994 size_t msg_len = ssl->in_hslen - 12;
4995
4996 /* We should never receive an old handshake
4997 * message - double-check nonetheless. */
4998 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4999 {
5000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5001 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5002 }
5003
5004 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5005 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5006 {
5007 /* Silently ignore -- message too far in the future */
5008 MBEDTLS_SSL_DEBUG_MSG( 2,
5009 ( "Ignore future HS message with sequence number %u, "
5010 "buffering window %u - %u",
5011 recv_msg_seq, ssl->handshake->in_msg_seq,
5012 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5013
5014 goto exit;
5015 }
5016
5017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5018 recv_msg_seq, recv_msg_seq_offset ) );
5019
5020 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5021
5022 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005023 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005024 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005025 size_t reassembly_buf_sz;
5026
Hanno Becker37f95322018-08-16 13:55:32 +01005027 hs_buf->is_fragmented =
5028 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5029
5030 /* We copy the message back into the input buffer
5031 * after reassembly, so check that it's not too large.
5032 * This is an implementation-specific limitation
5033 * and not one from the standard, hence it is not
5034 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005035 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005036 {
5037 /* Ignore message */
5038 goto exit;
5039 }
5040
Hanno Beckere0b150f2018-08-21 15:51:03 +01005041 /* Check if we have enough space to buffer the message. */
5042 if( hs->buffering.total_bytes_buffered >
5043 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5044 {
5045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5046 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5047 }
5048
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005049 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5050 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005051
5052 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5053 hs->buffering.total_bytes_buffered ) )
5054 {
5055 if( recv_msg_seq_offset > 0 )
5056 {
5057 /* If we can't buffer a future message because
5058 * of space limitations -- ignore. */
5059 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",
5060 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5061 (unsigned) hs->buffering.total_bytes_buffered ) );
5062 goto exit;
5063 }
Hanno Beckere1801392018-08-21 16:51:05 +01005064 else
5065 {
5066 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",
5067 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5068 (unsigned) hs->buffering.total_bytes_buffered ) );
5069 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005070
Hanno Beckera02b0b42018-08-21 17:20:27 +01005071 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005072 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005073 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",
5074 (unsigned) msg_len,
5075 (unsigned) reassembly_buf_sz,
5076 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005077 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005078 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5079 goto exit;
5080 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005081 }
5082
5083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5084 msg_len ) );
5085
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005086 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5087 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005088 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005089 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005090 goto exit;
5091 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005092 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005093
5094 /* Prepare final header: copy msg_type, length and message_seq,
5095 * then add standardised fragment_offset and fragment_length */
5096 memcpy( hs_buf->data, ssl->in_msg, 6 );
5097 memset( hs_buf->data + 6, 0, 3 );
5098 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5099
5100 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005101
5102 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005103 }
5104 else
5105 {
5106 /* Make sure msg_type and length are consistent */
5107 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5108 {
5109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5110 /* Ignore */
5111 goto exit;
5112 }
5113 }
5114
Hanno Becker4422bbb2018-08-20 09:40:19 +01005115 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005116 {
5117 size_t frag_len, frag_off;
5118 unsigned char * const msg = hs_buf->data + 12;
5119
5120 /*
5121 * Check and copy current fragment
5122 */
5123
5124 /* Validation of header fields already done in
5125 * mbedtls_ssl_prepare_handshake_record(). */
5126 frag_off = ssl_get_hs_frag_off( ssl );
5127 frag_len = ssl_get_hs_frag_len( ssl );
5128
5129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5130 frag_off, frag_len ) );
5131 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5132
5133 if( hs_buf->is_fragmented )
5134 {
5135 unsigned char * const bitmask = msg + msg_len;
5136 ssl_bitmask_set( bitmask, frag_off, frag_len );
5137 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5138 msg_len ) == 0 );
5139 }
5140 else
5141 {
5142 hs_buf->is_complete = 1;
5143 }
5144
5145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5146 hs_buf->is_complete ? "" : "not yet " ) );
5147 }
5148
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005149 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005150 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005151
5152 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005153 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005154 break;
5155 }
5156
5157exit:
5158
5159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5160 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005161}
5162#endif /* MBEDTLS_SSL_PROTO_DTLS */
5163
Hanno Becker1097b342018-08-15 14:09:41 +01005164static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005165{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005166 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005167 * Consume last content-layer message and potentially
5168 * update in_msglen which keeps track of the contents'
5169 * consumption state.
5170 *
5171 * (1) Handshake messages:
5172 * Remove last handshake message, move content
5173 * and adapt in_msglen.
5174 *
5175 * (2) Alert messages:
5176 * Consume whole record content, in_msglen = 0.
5177 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005178 * (3) Change cipher spec:
5179 * Consume whole record content, in_msglen = 0.
5180 *
5181 * (4) Application data:
5182 * Don't do anything - the record layer provides
5183 * the application data as a stream transport
5184 * and consumes through mbedtls_ssl_read only.
5185 *
5186 */
5187
5188 /* Case (1): Handshake messages */
5189 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005190 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005191 /* Hard assertion to be sure that no application data
5192 * is in flight, as corrupting ssl->in_msglen during
5193 * ssl->in_offt != NULL is fatal. */
5194 if( ssl->in_offt != NULL )
5195 {
5196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5197 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5198 }
5199
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005200 /*
5201 * Get next Handshake message in the current record
5202 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005203
Hanno Becker4a810fb2017-05-24 16:27:30 +01005204 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005205 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005206 * current handshake content: If DTLS handshake
5207 * fragmentation is used, that's the fragment
5208 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005209 * size here is faulty and should be changed at
5210 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005211 * (2) While it doesn't seem to cause problems, one
5212 * has to be very careful not to assume that in_hslen
5213 * is always <= in_msglen in a sensible communication.
5214 * Again, it's wrong for DTLS handshake fragmentation.
5215 * The following check is therefore mandatory, and
5216 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005217 * Additionally, ssl->in_hslen might be arbitrarily out of
5218 * bounds after handling a DTLS message with an unexpected
5219 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005220 */
5221 if( ssl->in_hslen < ssl->in_msglen )
5222 {
5223 ssl->in_msglen -= ssl->in_hslen;
5224 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5225 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005226
Hanno Becker4a810fb2017-05-24 16:27:30 +01005227 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5228 ssl->in_msg, ssl->in_msglen );
5229 }
5230 else
5231 {
5232 ssl->in_msglen = 0;
5233 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005234
Hanno Becker4a810fb2017-05-24 16:27:30 +01005235 ssl->in_hslen = 0;
5236 }
5237 /* Case (4): Application data */
5238 else if( ssl->in_offt != NULL )
5239 {
5240 return( 0 );
5241 }
5242 /* Everything else (CCS & Alerts) */
5243 else
5244 {
5245 ssl->in_msglen = 0;
5246 }
5247
Hanno Becker1097b342018-08-15 14:09:41 +01005248 return( 0 );
5249}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005250
Hanno Beckere74d5562018-08-15 14:26:08 +01005251static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5252{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005253 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005254 return( 1 );
5255
5256 return( 0 );
5257}
5258
Hanno Becker5f066e72018-08-16 14:56:31 +01005259#if defined(MBEDTLS_SSL_PROTO_DTLS)
5260
5261static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5262{
5263 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5264 if( hs == NULL )
5265 return;
5266
Hanno Becker01315ea2018-08-21 17:22:17 +01005267 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005268 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005269 hs->buffering.total_bytes_buffered -=
5270 hs->buffering.future_record.len;
5271
5272 mbedtls_free( hs->buffering.future_record.data );
5273 hs->buffering.future_record.data = NULL;
5274 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005275}
5276
5277static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5278{
5279 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5280 unsigned char * rec;
5281 size_t rec_len;
5282 unsigned rec_epoch;
5283
5284 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5285 return( 0 );
5286
5287 if( hs == NULL )
5288 return( 0 );
5289
Hanno Becker5f066e72018-08-16 14:56:31 +01005290 rec = hs->buffering.future_record.data;
5291 rec_len = hs->buffering.future_record.len;
5292 rec_epoch = hs->buffering.future_record.epoch;
5293
5294 if( rec == NULL )
5295 return( 0 );
5296
Hanno Becker4cb782d2018-08-20 11:19:05 +01005297 /* Only consider loading future records if the
5298 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005299 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005300 return( 0 );
5301
Hanno Becker5f066e72018-08-16 14:56:31 +01005302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5303
5304 if( rec_epoch != ssl->in_epoch )
5305 {
5306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5307 goto exit;
5308 }
5309
5310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5311
5312 /* Double-check that the record is not too large */
5313 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5314 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5315 {
5316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5317 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5318 }
5319
5320 memcpy( ssl->in_hdr, rec, rec_len );
5321 ssl->in_left = rec_len;
5322 ssl->next_record_offset = 0;
5323
5324 ssl_free_buffered_record( ssl );
5325
5326exit:
5327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5328 return( 0 );
5329}
5330
5331static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5332{
5333 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5334 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005335 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005336
5337 /* Don't buffer future records outside handshakes. */
5338 if( hs == NULL )
5339 return( 0 );
5340
5341 /* Only buffer handshake records (we are only interested
5342 * in Finished messages). */
5343 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5344 return( 0 );
5345
5346 /* Don't buffer more than one future epoch record. */
5347 if( hs->buffering.future_record.data != NULL )
5348 return( 0 );
5349
Hanno Becker01315ea2018-08-21 17:22:17 +01005350 /* Don't buffer record if there's not enough buffering space remaining. */
5351 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5352 hs->buffering.total_bytes_buffered ) )
5353 {
5354 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",
5355 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5356 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005357 return( 0 );
5358 }
5359
Hanno Becker5f066e72018-08-16 14:56:31 +01005360 /* Buffer record */
5361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5362 ssl->in_epoch + 1 ) );
5363 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5364 rec_hdr_len + ssl->in_msglen );
5365
5366 /* ssl_parse_record_header() only considers records
5367 * of the next epoch as candidates for buffering. */
5368 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005369 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005370
5371 hs->buffering.future_record.data =
5372 mbedtls_calloc( 1, hs->buffering.future_record.len );
5373 if( hs->buffering.future_record.data == NULL )
5374 {
5375 /* If we run out of RAM trying to buffer a
5376 * record from the next epoch, just ignore. */
5377 return( 0 );
5378 }
5379
Hanno Becker01315ea2018-08-21 17:22:17 +01005380 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005381
Hanno Becker01315ea2018-08-21 17:22:17 +01005382 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005383 return( 0 );
5384}
5385
5386#endif /* MBEDTLS_SSL_PROTO_DTLS */
5387
Hanno Beckere74d5562018-08-15 14:26:08 +01005388static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005389{
5390 int ret;
5391
Hanno Becker5f066e72018-08-16 14:56:31 +01005392#if defined(MBEDTLS_SSL_PROTO_DTLS)
5393 /* We might have buffered a future record; if so,
5394 * and if the epoch matches now, load it.
5395 * On success, this call will set ssl->in_left to
5396 * the length of the buffered record, so that
5397 * the calls to ssl_fetch_input() below will
5398 * essentially be no-ops. */
5399 ret = ssl_load_buffered_record( ssl );
5400 if( ret != 0 )
5401 return( ret );
5402#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005404 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005406 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005407 return( ret );
5408 }
5409
5410 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005412#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005413 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5414 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005415 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005416 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5417 {
5418 ret = ssl_buffer_future_record( ssl );
5419 if( ret != 0 )
5420 return( ret );
5421
5422 /* Fall through to handling of unexpected records */
5423 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5424 }
5425
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005426 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5427 {
5428 /* Skip unexpected record (but not whole datagram) */
5429 ssl->next_record_offset = ssl->in_msglen
5430 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005431
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5433 "(header)" ) );
5434 }
5435 else
5436 {
5437 /* Skip invalid record and the rest of the datagram */
5438 ssl->next_record_offset = 0;
5439 ssl->in_left = 0;
5440
5441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5442 "(header)" ) );
5443 }
5444
5445 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005446 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005447 }
5448#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005449 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005450 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005451
5452 /*
5453 * Read and optionally decrypt the message contents
5454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5456 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005459 return( ret );
5460 }
5461
5462 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005463#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005464 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005467 if( ssl->next_record_offset < ssl->in_left )
5468 {
5469 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5470 }
5471 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005472 else
5473#endif
5474 ssl->in_left = 0;
5475
5476 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005479 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005480 {
5481 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5483 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005484 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005485 /* Except when waiting for Finished as a bad mac here
5486 * probably means something went wrong in the handshake
5487 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5488 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5489 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5490 {
5491#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5492 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5493 {
5494 mbedtls_ssl_send_alert_message( ssl,
5495 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5496 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5497 }
5498#endif
5499 return( ret );
5500 }
5501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005502#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005503 if( ssl->conf->badmac_limit != 0 &&
5504 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5507 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005508 }
5509#endif
5510
Hanno Becker4a810fb2017-05-24 16:27:30 +01005511 /* As above, invalid records cause
5512 * dismissal of the whole datagram. */
5513
5514 ssl->next_record_offset = 0;
5515 ssl->in_left = 0;
5516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005518 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005519 }
5520
5521 return( ret );
5522 }
5523 else
5524#endif
5525 {
5526 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005527#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5528 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005530 mbedtls_ssl_send_alert_message( ssl,
5531 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5532 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005533 }
5534#endif
5535 return( ret );
5536 }
5537 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005538
Simon Butcher99000142016-10-13 17:21:01 +01005539 return( 0 );
5540}
5541
5542int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5543{
5544 int ret;
5545
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005546 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005547 * Handle particular types of records
5548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005550 {
Simon Butcher99000142016-10-13 17:21:01 +01005551 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5552 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005553 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005554 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005555 }
5556
Hanno Beckere678eaa2018-08-21 14:57:46 +01005557 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005558 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005559 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005560 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5562 ssl->in_msglen ) );
5563 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005564 }
5565
Hanno Beckere678eaa2018-08-21 14:57:46 +01005566 if( ssl->in_msg[0] != 1 )
5567 {
5568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5569 ssl->in_msg[0] ) );
5570 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5571 }
5572
5573#if defined(MBEDTLS_SSL_PROTO_DTLS)
5574 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5575 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5576 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5577 {
5578 if( ssl->handshake == NULL )
5579 {
5580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5581 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5582 }
5583
5584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5585 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5586 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005587#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005588 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005590 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005591 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005592 if( ssl->in_msglen != 2 )
5593 {
5594 /* Note: Standard allows for more than one 2 byte alert
5595 to be packed in a single message, but Mbed TLS doesn't
5596 currently support this. */
5597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5598 ssl->in_msglen ) );
5599 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5600 }
5601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005603 ssl->in_msg[0], ssl->in_msg[1] ) );
5604
5605 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005606 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005611 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005612 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005613 }
5614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5616 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5619 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005620 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005621
5622#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5623 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5624 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5625 {
Hanno Becker90333da2017-10-10 11:27:13 +01005626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005627 /* Will be handled when trying to parse ServerHello */
5628 return( 0 );
5629 }
5630#endif
5631
5632#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5633 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5634 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5635 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5636 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5637 {
5638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5639 /* Will be handled in mbedtls_ssl_parse_certificate() */
5640 return( 0 );
5641 }
5642#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5643
5644 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005645 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005646 }
5647
Hanno Beckerc76c6192017-06-06 10:03:17 +01005648#if defined(MBEDTLS_SSL_PROTO_DTLS)
5649 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5650 ssl->handshake != NULL &&
5651 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5652 {
5653 ssl_handshake_wrapup_free_hs_transform( ssl );
5654 }
5655#endif
5656
Paul Bakker5121ce52009-01-03 21:22:43 +00005657 return( 0 );
5658}
5659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005660int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005661{
5662 int ret;
5663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005664 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5665 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5666 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005667 {
5668 return( ret );
5669 }
5670
5671 return( 0 );
5672}
5673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005674int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005675 unsigned char level,
5676 unsigned char message )
5677{
5678 int ret;
5679
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005680 if( ssl == NULL || ssl->conf == NULL )
5681 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005684 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005686 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005687 ssl->out_msglen = 2;
5688 ssl->out_msg[0] = level;
5689 ssl->out_msg[1] = message;
5690
Hanno Becker67bc7c32018-08-06 11:33:50 +01005691 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005693 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005694 return( ret );
5695 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005697
5698 return( 0 );
5699}
5700
Hanno Beckerb9d44792019-02-08 07:19:04 +00005701#if defined(MBEDTLS_X509_CRT_PARSE_C)
5702static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5703{
5704#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5705 if( session->peer_cert != NULL )
5706 {
5707 mbedtls_x509_crt_free( session->peer_cert );
5708 mbedtls_free( session->peer_cert );
5709 session->peer_cert = NULL;
5710 }
5711#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5712 if( session->peer_cert_digest != NULL )
5713 {
5714 /* Zeroization is not necessary. */
5715 mbedtls_free( session->peer_cert_digest );
5716 session->peer_cert_digest = NULL;
5717 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5718 session->peer_cert_digest_len = 0;
5719 }
5720#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5721}
5722#endif /* MBEDTLS_X509_CRT_PARSE_C */
5723
Paul Bakker5121ce52009-01-03 21:22:43 +00005724/*
5725 * Handshake functions
5726 */
Hanno Becker21489932019-02-05 13:20:55 +00005727#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005728/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005729int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005730{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005731 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5732 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005735
Hanno Becker7177a882019-02-05 13:36:46 +00005736 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005739 ssl->state++;
5740 return( 0 );
5741 }
5742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5744 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005745}
5746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005747int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005748{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005749 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5750 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005753
Hanno Becker7177a882019-02-05 13:36:46 +00005754 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005757 ssl->state++;
5758 return( 0 );
5759 }
5760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5762 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005763}
Gilles Peskinef9828522017-05-03 12:28:43 +02005764
Hanno Becker21489932019-02-05 13:20:55 +00005765#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005766/* Some certificate support -> implement write and parse */
5767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005768int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005769{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005770 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005771 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005772 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00005773 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5774 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005777
Hanno Becker7177a882019-02-05 13:36:46 +00005778 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005781 ssl->state++;
5782 return( 0 );
5783 }
5784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005786 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005787 {
5788 if( ssl->client_auth == 0 )
5789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005791 ssl->state++;
5792 return( 0 );
5793 }
5794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005795#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005796 /*
5797 * If using SSLv3 and got no cert, send an Alert message
5798 * (otherwise an empty Certificate message will be sent).
5799 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005800 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5801 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005802 {
5803 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5805 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5806 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005809 goto write_msg;
5810 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005812 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005813#endif /* MBEDTLS_SSL_CLI_C */
5814#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005815 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5820 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005821 }
5822 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005823#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005825 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005826
5827 /*
5828 * 0 . 0 handshake type
5829 * 1 . 3 handshake length
5830 * 4 . 6 length of all certs
5831 * 7 . 9 length of cert. 1
5832 * 10 . n-1 peer certificate
5833 * n . n+2 length of cert. 2
5834 * n+3 . ... upper level cert, etc.
5835 */
5836 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005837 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005838
Paul Bakker29087132010-03-21 21:03:34 +00005839 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005840 {
5841 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005842 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005845 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005846 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005847 }
5848
5849 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5850 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5851 ssl->out_msg[i + 2] = (unsigned char)( n );
5852
5853 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5854 i += n; crt = crt->next;
5855 }
5856
5857 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5858 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5859 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5860
5861 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005862 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5863 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005864
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005865#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005866write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005867#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005868
5869 ssl->state++;
5870
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005871 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005872 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005873 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005874 return( ret );
5875 }
5876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005878
Paul Bakkered27a042013-04-18 22:46:23 +02005879 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005880}
5881
Hanno Becker84879e32019-01-31 07:44:03 +00005882#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00005883
5884#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005885static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5886 unsigned char *crt_buf,
5887 size_t crt_buf_len )
5888{
5889 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5890
5891 if( peer_crt == NULL )
5892 return( -1 );
5893
5894 if( peer_crt->raw.len != crt_buf_len )
5895 return( -1 );
5896
Hanno Becker46f34d02019-02-08 14:00:04 +00005897 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005898}
Hanno Becker177475a2019-02-05 17:02:46 +00005899#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5900static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5901 unsigned char *crt_buf,
5902 size_t crt_buf_len )
5903{
5904 int ret;
5905 unsigned char const * const peer_cert_digest =
5906 ssl->session->peer_cert_digest;
5907 mbedtls_md_type_t const peer_cert_digest_type =
5908 ssl->session->peer_cert_digest_type;
5909 mbedtls_md_info_t const * const digest_info =
5910 mbedtls_md_info_from_type( peer_cert_digest_type );
5911 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5912 size_t digest_len;
5913
5914 if( peer_cert_digest == NULL || digest_info == NULL )
5915 return( -1 );
5916
5917 digest_len = mbedtls_md_get_size( digest_info );
5918 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5919 return( -1 );
5920
5921 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5922 if( ret != 0 )
5923 return( -1 );
5924
5925 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5926}
5927#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00005928#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005929
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005930/*
5931 * Once the certificate message is read, parse it into a cert chain and
5932 * perform basic checks, but leave actual verification to the caller
5933 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005934static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5935 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00005936{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005937 int ret;
5938#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5939 int crt_cnt=0;
5940#endif
Paul Bakker23986e52011-04-24 08:57:21 +00005941 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005942 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005944 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005947 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5948 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005949 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005950 }
5951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005952 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5953 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005956 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5957 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005959 }
5960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005961 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005962
Paul Bakker5121ce52009-01-03 21:22:43 +00005963 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005964 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005965 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005966 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005967
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005968 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005969 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005972 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5973 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005974 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005975 }
5976
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005977 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5978 i += 3;
5979
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005980 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005981 while( i < ssl->in_hslen )
5982 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005983 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02005984 if ( i + 3 > ssl->in_hslen ) {
5985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005986 mbedtls_ssl_send_alert_message( ssl,
5987 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5988 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02005989 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5990 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005991 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5992 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005993 if( ssl->in_msg[i] != 0 )
5994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005996 mbedtls_ssl_send_alert_message( ssl,
5997 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5998 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005999 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006000 }
6001
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006002 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006003 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6004 | (unsigned int) ssl->in_msg[i + 2];
6005 i += 3;
6006
6007 if( n < 128 || i + n > ssl->in_hslen )
6008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006010 mbedtls_ssl_send_alert_message( ssl,
6011 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6012 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006014 }
6015
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006016 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006017#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6018 if( crt_cnt++ == 0 &&
6019 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6020 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006021 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006022 /* During client-side renegotiation, check that the server's
6023 * end-CRTs hasn't changed compared to the initial handshake,
6024 * mitigating the triple handshake attack. On success, reuse
6025 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006026 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6027 if( ssl_check_peer_crt_unchanged( ssl,
6028 &ssl->in_msg[i],
6029 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006030 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6032 mbedtls_ssl_send_alert_message( ssl,
6033 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6034 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6035 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006036 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006037
6038 /* Now we can safely free the original chain. */
6039 ssl_clear_peer_cert( ssl->session );
6040 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006041#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6042
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006043 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006044#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006045 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006046#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006047 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006048 * it in-place from the input buffer instead of making a copy. */
6049 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6050#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006051 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006052 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006053 case 0: /*ok*/
6054 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6055 /* Ignore certificate with an unknown algorithm: maybe a
6056 prior certificate was already trusted. */
6057 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006058
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006059 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6060 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6061 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006062
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006063 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6064 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6065 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006066
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006067 default:
6068 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6069 crt_parse_der_failed:
6070 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6071 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6072 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006073 }
6074
6075 i += n;
6076 }
6077
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006078 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006079 return( 0 );
6080}
6081
Hanno Becker4a55f632019-02-05 12:49:06 +00006082#if defined(MBEDTLS_SSL_SRV_C)
6083static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6084{
6085 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6086 return( -1 );
6087
6088#if defined(MBEDTLS_SSL_PROTO_SSL3)
6089 /*
6090 * Check if the client sent an empty certificate
6091 */
6092 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6093 {
6094 if( ssl->in_msglen == 2 &&
6095 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6096 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6097 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6098 {
6099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6100 return( 0 );
6101 }
6102
6103 return( -1 );
6104 }
6105#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6106
6107#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6108 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6109 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6110 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6111 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6112 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6113 {
6114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6115 return( 0 );
6116 }
6117
6118 return( -1 );
6119#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6120 MBEDTLS_SSL_PROTO_TLS1_2 */
6121}
6122#endif /* MBEDTLS_SSL_SRV_C */
6123
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006124/* Check if a certificate message is expected.
6125 * Return either
6126 * - SSL_CERTIFICATE_EXPECTED, or
6127 * - SSL_CERTIFICATE_SKIP
6128 * indicating whether a Certificate message is expected or not.
6129 */
6130#define SSL_CERTIFICATE_EXPECTED 0
6131#define SSL_CERTIFICATE_SKIP 1
6132static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6133 int authmode )
6134{
6135 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006136 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006137
6138 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6139 return( SSL_CERTIFICATE_SKIP );
6140
6141#if defined(MBEDTLS_SSL_SRV_C)
6142 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6143 {
6144 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6145 return( SSL_CERTIFICATE_SKIP );
6146
6147 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6148 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006149 ssl->session_negotiate->verify_result =
6150 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6151 return( SSL_CERTIFICATE_SKIP );
6152 }
6153 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006154#else
6155 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006156#endif /* MBEDTLS_SSL_SRV_C */
6157
6158 return( SSL_CERTIFICATE_EXPECTED );
6159}
6160
Hanno Becker68636192019-02-05 14:36:34 +00006161static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6162 int authmode,
6163 mbedtls_x509_crt *chain,
6164 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006165{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006166 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006167 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006168 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006169 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006170
Hanno Becker8927c832019-04-03 12:52:50 +01006171 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6172 void *p_vrfy;
6173
Hanno Becker68636192019-02-05 14:36:34 +00006174 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6175 return( 0 );
6176
Hanno Becker8927c832019-04-03 12:52:50 +01006177 if( ssl->f_vrfy != NULL )
6178 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006179 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006180 f_vrfy = ssl->f_vrfy;
6181 p_vrfy = ssl->p_vrfy;
6182 }
6183 else
6184 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006185 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006186 f_vrfy = ssl->conf->f_vrfy;
6187 p_vrfy = ssl->conf->p_vrfy;
6188 }
6189
Hanno Becker68636192019-02-05 14:36:34 +00006190 /*
6191 * Main check: verify certificate
6192 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006193#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6194 if( ssl->conf->f_ca_cb != NULL )
6195 {
6196 ((void) rs_ctx);
6197 have_ca_chain = 1;
6198
6199 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006200 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006201 chain,
6202 ssl->conf->f_ca_cb,
6203 ssl->conf->p_ca_cb,
6204 ssl->conf->cert_profile,
6205 ssl->hostname,
6206 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006207 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006208 }
6209 else
6210#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6211 {
6212 mbedtls_x509_crt *ca_chain;
6213 mbedtls_x509_crl *ca_crl;
6214
6215#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6216 if( ssl->handshake->sni_ca_chain != NULL )
6217 {
6218 ca_chain = ssl->handshake->sni_ca_chain;
6219 ca_crl = ssl->handshake->sni_ca_crl;
6220 }
6221 else
6222#endif
6223 {
6224 ca_chain = ssl->conf->ca_chain;
6225 ca_crl = ssl->conf->ca_crl;
6226 }
6227
6228 if( ca_chain != NULL )
6229 have_ca_chain = 1;
6230
6231 ret = mbedtls_x509_crt_verify_restartable(
6232 chain,
6233 ca_chain, ca_crl,
6234 ssl->conf->cert_profile,
6235 ssl->hostname,
6236 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006237 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006238 }
Hanno Becker68636192019-02-05 14:36:34 +00006239
6240 if( ret != 0 )
6241 {
6242 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6243 }
6244
6245#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6246 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6247 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6248#endif
6249
6250 /*
6251 * Secondary checks: always done, but change 'ret' only if it was 0
6252 */
6253
6254#if defined(MBEDTLS_ECP_C)
6255 {
6256 const mbedtls_pk_context *pk = &chain->pk;
6257
6258 /* If certificate uses an EC key, make sure the curve is OK */
6259 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6260 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6261 {
6262 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6263
6264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6265 if( ret == 0 )
6266 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6267 }
6268 }
6269#endif /* MBEDTLS_ECP_C */
6270
6271 if( mbedtls_ssl_check_cert_usage( chain,
6272 ciphersuite_info,
6273 ! ssl->conf->endpoint,
6274 &ssl->session_negotiate->verify_result ) != 0 )
6275 {
6276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6277 if( ret == 0 )
6278 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6279 }
6280
6281 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6282 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6283 * with details encoded in the verification flags. All other kinds
6284 * of error codes, including those from the user provided f_vrfy
6285 * functions, are treated as fatal and lead to a failure of
6286 * ssl_parse_certificate even if verification was optional. */
6287 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6288 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6289 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6290 {
6291 ret = 0;
6292 }
6293
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006294 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006295 {
6296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6297 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6298 }
6299
6300 if( ret != 0 )
6301 {
6302 uint8_t alert;
6303
6304 /* The certificate may have been rejected for several reasons.
6305 Pick one and send the corresponding alert. Which alert to send
6306 may be a subject of debate in some cases. */
6307 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6308 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6309 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6310 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6311 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6312 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6313 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6314 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6315 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6316 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6317 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6318 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6319 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6320 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6321 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6322 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6323 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6324 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6325 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6326 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6327 else
6328 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6329 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6330 alert );
6331 }
6332
6333#if defined(MBEDTLS_DEBUG_C)
6334 if( ssl->session_negotiate->verify_result != 0 )
6335 {
6336 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6337 ssl->session_negotiate->verify_result ) );
6338 }
6339 else
6340 {
6341 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6342 }
6343#endif /* MBEDTLS_DEBUG_C */
6344
6345 return( ret );
6346}
6347
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006348#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6349static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6350 unsigned char *start, size_t len )
6351{
6352 int ret;
6353 /* Remember digest of the peer's end-CRT. */
6354 ssl->session_negotiate->peer_cert_digest =
6355 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6356 if( ssl->session_negotiate->peer_cert_digest == NULL )
6357 {
6358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6359 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6360 mbedtls_ssl_send_alert_message( ssl,
6361 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6362 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6363
6364 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6365 }
6366
6367 ret = mbedtls_md( mbedtls_md_info_from_type(
6368 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6369 start, len,
6370 ssl->session_negotiate->peer_cert_digest );
6371
6372 ssl->session_negotiate->peer_cert_digest_type =
6373 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6374 ssl->session_negotiate->peer_cert_digest_len =
6375 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6376
6377 return( ret );
6378}
6379
6380static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6381 unsigned char *start, size_t len )
6382{
6383 unsigned char *end = start + len;
6384 int ret;
6385
6386 /* Make a copy of the peer's raw public key. */
6387 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6388 ret = mbedtls_pk_parse_subpubkey( &start, end,
6389 &ssl->handshake->peer_pubkey );
6390 if( ret != 0 )
6391 {
6392 /* We should have parsed the public key before. */
6393 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6394 }
6395
6396 return( 0 );
6397}
6398#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6399
Hanno Becker68636192019-02-05 14:36:34 +00006400int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6401{
6402 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006403 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006404#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6405 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6406 ? ssl->handshake->sni_authmode
6407 : ssl->conf->authmode;
6408#else
6409 const int authmode = ssl->conf->authmode;
6410#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006411 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006412 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006413
6414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6415
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006416 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6417 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006418 {
6419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006420 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006421 }
6422
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006423#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6424 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006425 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006426 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006427 chain = ssl->handshake->ecrs_peer_cert;
6428 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006429 goto crt_verify;
6430 }
6431#endif
6432
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006433 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006434 {
6435 /* mbedtls_ssl_read_record may have sent an alert already. We
6436 let it decide whether to alert. */
6437 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006438 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006439 }
6440
Hanno Becker4a55f632019-02-05 12:49:06 +00006441#if defined(MBEDTLS_SSL_SRV_C)
6442 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6443 {
6444 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006445
6446 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006447 ret = 0;
6448 else
6449 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006450
Hanno Becker6bdfab22019-02-05 13:11:17 +00006451 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006452 }
6453#endif /* MBEDTLS_SSL_SRV_C */
6454
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006455 /* Clear existing peer CRT structure in case we tried to
6456 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006457 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006458
6459 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6460 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006461 {
6462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6463 sizeof( mbedtls_x509_crt ) ) );
6464 mbedtls_ssl_send_alert_message( ssl,
6465 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6466 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006467
Hanno Becker3dad3112019-02-05 17:19:52 +00006468 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6469 goto exit;
6470 }
6471 mbedtls_x509_crt_init( chain );
6472
6473 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006474 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006475 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006476
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006477#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6478 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006479 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006480
6481crt_verify:
6482 if( ssl->handshake->ecrs_enabled)
6483 rs_ctx = &ssl->handshake->ecrs_ctx;
6484#endif
6485
Hanno Becker68636192019-02-05 14:36:34 +00006486 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006487 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006488 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006489 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006490
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006491#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006492 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006493 unsigned char *crt_start, *pk_start;
6494 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006495
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006496 /* We parse the CRT chain without copying, so
6497 * these pointers point into the input buffer,
6498 * and are hence still valid after freeing the
6499 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006500
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006501 crt_start = chain->raw.p;
6502 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006503
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006504 pk_start = chain->pk_raw.p;
6505 pk_len = chain->pk_raw.len;
6506
6507 /* Free the CRT structures before computing
6508 * digest and copying the peer's public key. */
6509 mbedtls_x509_crt_free( chain );
6510 mbedtls_free( chain );
6511 chain = NULL;
6512
6513 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006514 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006515 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006516
6517 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6518 if( ret != 0 )
6519 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006520 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006521#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6522 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006523 ssl->session_negotiate->peer_cert = chain;
6524 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006525#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006528
Hanno Becker6bdfab22019-02-05 13:11:17 +00006529exit:
6530
Hanno Becker3dad3112019-02-05 17:19:52 +00006531 if( ret == 0 )
6532 ssl->state++;
6533
6534#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6535 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6536 {
6537 ssl->handshake->ecrs_peer_cert = chain;
6538 chain = NULL;
6539 }
6540#endif
6541
6542 if( chain != NULL )
6543 {
6544 mbedtls_x509_crt_free( chain );
6545 mbedtls_free( chain );
6546 }
6547
Paul Bakker5121ce52009-01-03 21:22:43 +00006548 return( ret );
6549}
Hanno Becker21489932019-02-05 13:20:55 +00006550#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006553{
6554 int ret;
6555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006559 ssl->out_msglen = 1;
6560 ssl->out_msg[0] = 1;
6561
Paul Bakker5121ce52009-01-03 21:22:43 +00006562 ssl->state++;
6563
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006564 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006565 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006566 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006567 return( ret );
6568 }
6569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006571
6572 return( 0 );
6573}
6574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006575int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006576{
6577 int ret;
6578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006580
Hanno Becker327c93b2018-08-15 13:56:18 +01006581 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006583 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006584 return( ret );
6585 }
6586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006587 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006590 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6591 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006592 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006593 }
6594
Hanno Beckere678eaa2018-08-21 14:57:46 +01006595 /* CCS records are only accepted if they have length 1 and content '1',
6596 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006597
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006598 /*
6599 * Switch to our negotiated transform and session parameters for inbound
6600 * data.
6601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006603 ssl->transform_in = ssl->transform_negotiate;
6604 ssl->session_in = ssl->session_negotiate;
6605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006607 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006609#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006610 ssl_dtls_replay_reset( ssl );
6611#endif
6612
6613 /* Increment epoch */
6614 if( ++ssl->in_epoch == 0 )
6615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006617 /* This is highly unlikely to happen for legitimate reasons, so
6618 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006620 }
6621 }
6622 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006623#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006624 memset( ssl->in_ctr, 0, 8 );
6625
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006626 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6629 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006631 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006633 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006634 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6635 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006636 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006637 }
6638 }
6639#endif
6640
Paul Bakker5121ce52009-01-03 21:22:43 +00006641 ssl->state++;
6642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006644
6645 return( 0 );
6646}
6647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6649 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006650{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006651 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006653#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6654 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6655 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006656 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006657 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006658#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006659#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6660#if defined(MBEDTLS_SHA512_C)
6661 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006662 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6663 else
6664#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006665#if defined(MBEDTLS_SHA256_C)
6666 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006667 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006668 else
6669#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006670#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006673 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006674 }
Paul Bakker380da532012-04-18 16:10:25 +00006675}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006677void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006678{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006679#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6680 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006681 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6682 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006683#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006684#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6685#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006686#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006687 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006688 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6689#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006690 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006691#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006692#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006693#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006694#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006695 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006696 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006697#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006698 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006699#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006700#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006702}
6703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006705 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006706{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6708 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006709 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6710 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006711#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006712#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6713#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006714#if defined(MBEDTLS_USE_PSA_CRYPTO)
6715 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6716#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006717 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006718#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006719#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006720#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006721#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006722 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006723#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006724 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006725#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006726#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006728}
6729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6731 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6732static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006733 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006734{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006735 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6736 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006737}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006738#endif
Paul Bakker380da532012-04-18 16:10:25 +00006739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6741#if defined(MBEDTLS_SHA256_C)
6742static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006743 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006744{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006745#if defined(MBEDTLS_USE_PSA_CRYPTO)
6746 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6747#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006748 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006749#endif
Paul Bakker380da532012-04-18 16:10:25 +00006750}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006751#endif
Paul Bakker380da532012-04-18 16:10:25 +00006752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753#if defined(MBEDTLS_SHA512_C)
6754static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006755 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006756{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006757#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006758 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006759#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006760 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006761#endif
Paul Bakker380da532012-04-18 16:10:25 +00006762}
Paul Bakker769075d2012-11-24 11:26:46 +01006763#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006767static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006769{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006770 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006771 mbedtls_md5_context md5;
6772 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006773
Paul Bakker5121ce52009-01-03 21:22:43 +00006774 unsigned char padbuf[48];
6775 unsigned char md5sum[16];
6776 unsigned char sha1sum[20];
6777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006779 if( !session )
6780 session = ssl->session;
6781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006783
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006784 mbedtls_md5_init( &md5 );
6785 mbedtls_sha1_init( &sha1 );
6786
6787 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6788 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006789
6790 /*
6791 * SSLv3:
6792 * hash =
6793 * MD5( master + pad2 +
6794 * MD5( handshake + sender + master + pad1 ) )
6795 * + SHA1( master + pad2 +
6796 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006797 */
6798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006800 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6801 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006802#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006805 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6806 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006807#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006810 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006811
Paul Bakker1ef83d62012-04-11 12:09:53 +00006812 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006813
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006814 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6815 mbedtls_md5_update_ret( &md5, session->master, 48 );
6816 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6817 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006818
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006819 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6820 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6821 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6822 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006823
Paul Bakker1ef83d62012-04-11 12:09:53 +00006824 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006825
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006826 mbedtls_md5_starts_ret( &md5 );
6827 mbedtls_md5_update_ret( &md5, session->master, 48 );
6828 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6829 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6830 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006831
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006832 mbedtls_sha1_starts_ret( &sha1 );
6833 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6834 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6835 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6836 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006839
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006840 mbedtls_md5_free( &md5 );
6841 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006842
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006843 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6844 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6845 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006848}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006849#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006852static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006853 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006854{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006855 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006856 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006857 mbedtls_md5_context md5;
6858 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006859 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006862 if( !session )
6863 session = ssl->session;
6864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006866
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006867 mbedtls_md5_init( &md5 );
6868 mbedtls_sha1_init( &sha1 );
6869
6870 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6871 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006872
Paul Bakker1ef83d62012-04-11 12:09:53 +00006873 /*
6874 * TLSv1:
6875 * hash = PRF( master, finished_label,
6876 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6877 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006879#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006880 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6881 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006882#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006884#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006885 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6886 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006887#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006889 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006890 ? "client finished"
6891 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006892
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006893 mbedtls_md5_finish_ret( &md5, padbuf );
6894 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006895
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006896 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006897 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006899 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006900
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006901 mbedtls_md5_free( &md5 );
6902 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006903
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006904 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006907}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6911#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006912static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006913 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006914{
6915 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006916 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006917 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006918#if defined(MBEDTLS_USE_PSA_CRYPTO)
6919 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006920 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006921 psa_status_t status;
6922#else
6923 mbedtls_sha256_context sha256;
6924#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006926 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006927 if( !session )
6928 session = ssl->session;
6929
Andrzej Kurekeb342242019-01-29 09:14:33 -05006930 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6931 ? "client finished"
6932 : "server finished";
6933
6934#if defined(MBEDTLS_USE_PSA_CRYPTO)
6935 sha256_psa = psa_hash_operation_init();
6936
6937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6938
6939 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6940 if( status != PSA_SUCCESS )
6941 {
6942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6943 return;
6944 }
6945
6946 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6947 if( status != PSA_SUCCESS )
6948 {
6949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6950 return;
6951 }
6952 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6953#else
6954
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006955 mbedtls_sha256_init( &sha256 );
6956
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006958
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006959 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006960
6961 /*
6962 * TLSv1.2:
6963 * hash = PRF( master, finished_label,
6964 * Hash( handshake ) )[0.11]
6965 */
6966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006967#if !defined(MBEDTLS_SHA256_ALT)
6968 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006969 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006970#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006971
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006972 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006973 mbedtls_sha256_free( &sha256 );
6974#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006975
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006976 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006977 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006980
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006981 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006984}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006988static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006990{
6991 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006992 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006993 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006994#if defined(MBEDTLS_USE_PSA_CRYPTO)
6995 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006996 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006997 psa_status_t status;
6998#else
6999 mbedtls_sha512_context sha512;
7000#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007003 if( !session )
7004 session = ssl->session;
7005
Andrzej Kurekeb342242019-01-29 09:14:33 -05007006 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7007 ? "client finished"
7008 : "server finished";
7009
7010#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007011 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007012
7013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7014
Andrzej Kurek972fba52019-01-30 03:29:12 -05007015 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007016 if( status != PSA_SUCCESS )
7017 {
7018 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7019 return;
7020 }
7021
Andrzej Kurek972fba52019-01-30 03:29:12 -05007022 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007023 if( status != PSA_SUCCESS )
7024 {
7025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7026 return;
7027 }
7028 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7029#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007030 mbedtls_sha512_init( &sha512 );
7031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007033
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007034 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007035
7036 /*
7037 * TLSv1.2:
7038 * hash = PRF( master, finished_label,
7039 * Hash( handshake ) )[0.11]
7040 */
7041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007042#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007043 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7044 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007045#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007046
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007047 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007048 mbedtls_sha512_free( &sha512 );
7049#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007050
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007051 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007052 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007054 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007055
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007056 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007060#endif /* MBEDTLS_SHA512_C */
7061#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007063static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007064{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007065 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007066
7067 /*
7068 * Free our handshake params
7069 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007070 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007072 ssl->handshake = NULL;
7073
7074 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007075 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007076 */
7077 if( ssl->transform )
7078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079 mbedtls_ssl_transform_free( ssl->transform );
7080 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007081 }
7082 ssl->transform = ssl->transform_negotiate;
7083 ssl->transform_negotiate = NULL;
7084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007085 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007086}
7087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007089{
7090 int resume = ssl->handshake->resume;
7091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007092 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094#if defined(MBEDTLS_SSL_RENEGOTIATION)
7095 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007098 ssl->renego_records_seen = 0;
7099 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007100#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007101
7102 /*
7103 * Free the previous session and switch in the current one
7104 */
Paul Bakker0a597072012-09-25 21:55:46 +00007105 if( ssl->session )
7106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007108 /* RFC 7366 3.1: keep the EtM state */
7109 ssl->session_negotiate->encrypt_then_mac =
7110 ssl->session->encrypt_then_mac;
7111#endif
7112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113 mbedtls_ssl_session_free( ssl->session );
7114 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007115 }
7116 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007117 ssl->session_negotiate = NULL;
7118
Paul Bakker0a597072012-09-25 21:55:46 +00007119 /*
7120 * Add cache entry
7121 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007122 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007123 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007124 resume == 0 )
7125 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007126 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007128 }
Paul Bakker0a597072012-09-25 21:55:46 +00007129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007131 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007132 ssl->handshake->flight != NULL )
7133 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007134 /* Cancel handshake timer */
7135 ssl_set_timer( ssl, 0 );
7136
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007137 /* Keep last flight around in case we need to resend it:
7138 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007140 }
7141 else
7142#endif
7143 ssl_handshake_wrapup_free_hs_transform( ssl );
7144
Paul Bakker48916f92012-09-16 19:57:18 +00007145 ssl->state++;
7146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007148}
7149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007150int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007151{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007152 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007155
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007156 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007157
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007158 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007159
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007160 /*
7161 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7162 * may define some other value. Currently (early 2016), no defined
7163 * ciphersuite does this (and this is unlikely to change as activity has
7164 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007169 ssl->verify_data_len = hash_len;
7170 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007171#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007172
Paul Bakker5121ce52009-01-03 21:22:43 +00007173 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7175 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007176
7177 /*
7178 * In case of session resuming, invert the client and server
7179 * ChangeCipherSpec messages order.
7180 */
Paul Bakker0a597072012-09-25 21:55:46 +00007181 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007183#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007184 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007186#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007188 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007190#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007191 }
7192 else
7193 ssl->state++;
7194
Paul Bakker48916f92012-09-16 19:57:18 +00007195 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007196 * Switch to our negotiated transform and session parameters for outbound
7197 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007202 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007203 {
7204 unsigned char i;
7205
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007206 /* Remember current epoch settings for resending */
7207 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007208 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007209
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007210 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007211 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007212
7213 /* Increment epoch */
7214 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007215 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007216 break;
7217
7218 /* The loop goes to its end iff the counter is wrapping */
7219 if( i == 0 )
7220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7222 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007223 }
7224 }
7225 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007226#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007227 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007228
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007229 ssl->transform_out = ssl->transform_negotiate;
7230 ssl->session_out = ssl->session_negotiate;
7231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7233 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007237 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7238 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007239 }
7240 }
7241#endif
7242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007244 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007245 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007246#endif
7247
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007248 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007249 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007250 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007251 return( ret );
7252 }
7253
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007254#if defined(MBEDTLS_SSL_PROTO_DTLS)
7255 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7256 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7257 {
7258 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7259 return( ret );
7260 }
7261#endif
7262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007264
7265 return( 0 );
7266}
7267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007269#define SSL_MAX_HASH_LEN 36
7270#else
7271#define SSL_MAX_HASH_LEN 12
7272#endif
7273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007275{
Paul Bakker23986e52011-04-24 08:57:21 +00007276 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007277 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007278 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007281
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007282 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007283
Hanno Becker327c93b2018-08-15 13:56:18 +01007284 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007287 return( ret );
7288 }
7289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007290 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007293 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7294 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007296 }
7297
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007298 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299#if defined(MBEDTLS_SSL_PROTO_SSL3)
7300 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007301 hash_len = 36;
7302 else
7303#endif
7304 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7307 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007310 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7311 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007312 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007313 }
7314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007315 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007316 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007319 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7320 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007321 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007322 }
7323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007325 ssl->verify_data_len = hash_len;
7326 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007327#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007328
Paul Bakker0a597072012-09-25 21:55:46 +00007329 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007332 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007334#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007335#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007336 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007337 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007338#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007339 }
7340 else
7341 ssl->state++;
7342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007344 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007345 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007346#endif
7347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007349
7350 return( 0 );
7351}
7352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007353static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007354{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007355 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7358 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7359 mbedtls_md5_init( &handshake->fin_md5 );
7360 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007361 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7362 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007363#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7365#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007366#if defined(MBEDTLS_USE_PSA_CRYPTO)
7367 handshake->fin_sha256_psa = psa_hash_operation_init();
7368 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7369#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007371 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007372#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007373#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007375#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007376 handshake->fin_sha384_psa = psa_hash_operation_init();
7377 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007378#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007380 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007381#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007382#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007384
7385 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007386
7387#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7388 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7389 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7390#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392#if defined(MBEDTLS_DHM_C)
7393 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007394#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395#if defined(MBEDTLS_ECDH_C)
7396 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007397#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007398#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007399 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007400#if defined(MBEDTLS_SSL_CLI_C)
7401 handshake->ecjpake_cache = NULL;
7402 handshake->ecjpake_cache_len = 0;
7403#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007404#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007405
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007406#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007407 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007408#endif
7409
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007410#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7411 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7412#endif
Hanno Becker75173122019-02-06 16:18:31 +00007413
7414#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7415 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7416 mbedtls_pk_init( &handshake->peer_pubkey );
7417#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007418}
7419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007421{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007424 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7425 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427 mbedtls_md_init( &transform->md_ctx_enc );
7428 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007429}
7430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007432{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007434}
7435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007436static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007437{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007438 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007439 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007441 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007443 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007444 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007445
7446 /*
7447 * Either the pointers are now NULL or cleared properly and can be freed.
7448 * Now allocate missing structures.
7449 */
7450 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007451 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007452 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007453 }
Paul Bakker48916f92012-09-16 19:57:18 +00007454
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007455 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007456 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007457 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007458 }
Paul Bakker48916f92012-09-16 19:57:18 +00007459
Paul Bakker82788fb2014-10-20 13:59:19 +02007460 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007461 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007462 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007463 }
Paul Bakker48916f92012-09-16 19:57:18 +00007464
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007465 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007466 if( ssl->handshake == NULL ||
7467 ssl->transform_negotiate == NULL ||
7468 ssl->session_negotiate == NULL )
7469 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472 mbedtls_free( ssl->handshake );
7473 mbedtls_free( ssl->transform_negotiate );
7474 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007475
7476 ssl->handshake = NULL;
7477 ssl->transform_negotiate = NULL;
7478 ssl->session_negotiate = NULL;
7479
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007480 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007481 }
7482
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007483 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007485 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007486 ssl_handshake_params_init( ssl->handshake );
7487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007489 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7490 {
7491 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007492
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007493 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7494 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7495 else
7496 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007497
7498 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007499 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007500#endif
7501
Paul Bakker48916f92012-09-16 19:57:18 +00007502 return( 0 );
7503}
7504
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007505#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007506/* Dummy cookie callbacks for defaults */
7507static int ssl_cookie_write_dummy( void *ctx,
7508 unsigned char **p, unsigned char *end,
7509 const unsigned char *cli_id, size_t cli_id_len )
7510{
7511 ((void) ctx);
7512 ((void) p);
7513 ((void) end);
7514 ((void) cli_id);
7515 ((void) cli_id_len);
7516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007518}
7519
7520static int ssl_cookie_check_dummy( void *ctx,
7521 const unsigned char *cookie, size_t cookie_len,
7522 const unsigned char *cli_id, size_t cli_id_len )
7523{
7524 ((void) ctx);
7525 ((void) cookie);
7526 ((void) cookie_len);
7527 ((void) cli_id);
7528 ((void) cli_id_len);
7529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007531}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007532#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007533
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007534/* Once ssl->out_hdr as the address of the beginning of the
7535 * next outgoing record is set, deduce the other pointers.
7536 *
7537 * Note: For TLS, we save the implicit record sequence number
7538 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7539 * and the caller has to make sure there's space for this.
7540 */
7541
7542static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7543 mbedtls_ssl_transform *transform )
7544{
7545#if defined(MBEDTLS_SSL_PROTO_DTLS)
7546 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7547 {
7548 ssl->out_ctr = ssl->out_hdr + 3;
7549 ssl->out_len = ssl->out_hdr + 11;
7550 ssl->out_iv = ssl->out_hdr + 13;
7551 }
7552 else
7553#endif
7554 {
7555 ssl->out_ctr = ssl->out_hdr - 8;
7556 ssl->out_len = ssl->out_hdr + 3;
7557 ssl->out_iv = ssl->out_hdr + 5;
7558 }
7559
7560 /* Adjust out_msg to make space for explicit IV, if used. */
7561 if( transform != NULL &&
7562 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7563 {
7564 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7565 }
7566 else
7567 ssl->out_msg = ssl->out_iv;
7568}
7569
7570/* Once ssl->in_hdr as the address of the beginning of the
7571 * next incoming record is set, deduce the other pointers.
7572 *
7573 * Note: For TLS, we save the implicit record sequence number
7574 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7575 * and the caller has to make sure there's space for this.
7576 */
7577
7578static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7579 mbedtls_ssl_transform *transform )
7580{
7581#if defined(MBEDTLS_SSL_PROTO_DTLS)
7582 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7583 {
7584 ssl->in_ctr = ssl->in_hdr + 3;
7585 ssl->in_len = ssl->in_hdr + 11;
7586 ssl->in_iv = ssl->in_hdr + 13;
7587 }
7588 else
7589#endif
7590 {
7591 ssl->in_ctr = ssl->in_hdr - 8;
7592 ssl->in_len = ssl->in_hdr + 3;
7593 ssl->in_iv = ssl->in_hdr + 5;
7594 }
7595
7596 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7597 if( transform != NULL &&
7598 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7599 {
7600 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7601 }
7602 else
7603 ssl->in_msg = ssl->in_iv;
7604}
7605
Paul Bakker5121ce52009-01-03 21:22:43 +00007606/*
7607 * Initialize an SSL context
7608 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007609void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7610{
7611 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7612}
7613
7614/*
7615 * Setup an SSL context
7616 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007617
7618static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7619{
7620 /* Set the incoming and outgoing record pointers. */
7621#if defined(MBEDTLS_SSL_PROTO_DTLS)
7622 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7623 {
7624 ssl->out_hdr = ssl->out_buf;
7625 ssl->in_hdr = ssl->in_buf;
7626 }
7627 else
7628#endif /* MBEDTLS_SSL_PROTO_DTLS */
7629 {
7630 ssl->out_hdr = ssl->out_buf + 8;
7631 ssl->in_hdr = ssl->in_buf + 8;
7632 }
7633
7634 /* Derive other internal pointers. */
7635 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7636 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7637}
7638
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007639int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007640 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007641{
Paul Bakker48916f92012-09-16 19:57:18 +00007642 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007643
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007644 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007645
7646 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007647 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007648 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007649
7650 /* Set to NULL in case of an error condition */
7651 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007652
Angus Grattond8213d02016-05-25 20:56:48 +10007653 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7654 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007655 {
Angus Grattond8213d02016-05-25 20:56:48 +10007656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007657 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007658 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007659 }
7660
7661 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7662 if( ssl->out_buf == NULL )
7663 {
7664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007665 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007666 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007667 }
7668
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007669 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007670
Paul Bakker48916f92012-09-16 19:57:18 +00007671 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007672 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007673
7674 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007675
7676error:
7677 mbedtls_free( ssl->in_buf );
7678 mbedtls_free( ssl->out_buf );
7679
7680 ssl->conf = NULL;
7681
7682 ssl->in_buf = NULL;
7683 ssl->out_buf = NULL;
7684
7685 ssl->in_hdr = NULL;
7686 ssl->in_ctr = NULL;
7687 ssl->in_len = NULL;
7688 ssl->in_iv = NULL;
7689 ssl->in_msg = NULL;
7690
7691 ssl->out_hdr = NULL;
7692 ssl->out_ctr = NULL;
7693 ssl->out_len = NULL;
7694 ssl->out_iv = NULL;
7695 ssl->out_msg = NULL;
7696
k-stachowiak9f7798e2018-07-31 16:52:32 +02007697 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007698}
7699
7700/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007701 * Reset an initialized and used SSL context for re-use while retaining
7702 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007703 *
7704 * If partial is non-zero, keep data in the input buffer and client ID.
7705 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007706 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007707static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007708{
Paul Bakker48916f92012-09-16 19:57:18 +00007709 int ret;
7710
Hanno Becker7e772132018-08-10 12:38:21 +01007711#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7712 !defined(MBEDTLS_SSL_SRV_C)
7713 ((void) partial);
7714#endif
7715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007717
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007718 /* Cancel any possibly running timer */
7719 ssl_set_timer( ssl, 0 );
7720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007721#if defined(MBEDTLS_SSL_RENEGOTIATION)
7722 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007723 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007724
7725 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7727 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007728#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007730
Paul Bakker7eb013f2011-10-06 12:37:39 +00007731 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007732 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007733
7734 ssl->in_msgtype = 0;
7735 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007737 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007738 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007739#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007741 ssl_dtls_replay_reset( ssl );
7742#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007743
7744 ssl->in_hslen = 0;
7745 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007746
7747 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007748
7749 ssl->out_msgtype = 0;
7750 ssl->out_msglen = 0;
7751 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7753 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007754 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007755#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007756
Hanno Becker19859472018-08-06 09:40:20 +01007757 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7758
Paul Bakker48916f92012-09-16 19:57:18 +00007759 ssl->transform_in = NULL;
7760 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007761
Hanno Becker78640902018-08-13 16:35:15 +01007762 ssl->session_in = NULL;
7763 ssl->session_out = NULL;
7764
Angus Grattond8213d02016-05-25 20:56:48 +10007765 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007766
7767#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007768 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007769#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7770 {
7771 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007772 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007773 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007775#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7776 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7779 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007781 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7782 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007783 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007784 }
7785#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007786
Paul Bakker48916f92012-09-16 19:57:18 +00007787 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007789 mbedtls_ssl_transform_free( ssl->transform );
7790 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007791 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007792 }
Paul Bakker48916f92012-09-16 19:57:18 +00007793
Paul Bakkerc0463502013-02-14 11:19:38 +01007794 if( ssl->session )
7795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796 mbedtls_ssl_session_free( ssl->session );
7797 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007798 ssl->session = NULL;
7799 }
7800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007801#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007802 ssl->alpn_chosen = NULL;
7803#endif
7804
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007805#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007806#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007807 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007808#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007809 {
7810 mbedtls_free( ssl->cli_id );
7811 ssl->cli_id = NULL;
7812 ssl->cli_id_len = 0;
7813 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007814#endif
7815
Paul Bakker48916f92012-09-16 19:57:18 +00007816 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7817 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007818
7819 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007820}
7821
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007822/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007823 * Reset an initialized and used SSL context for re-use while retaining
7824 * all application-set variables, function pointers and data.
7825 */
7826int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7827{
7828 return( ssl_session_reset_int( ssl, 0 ) );
7829}
7830
7831/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007832 * SSL set accessors
7833 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007834void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007835{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007836 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007837}
7838
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007839void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007840{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007841 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007842}
7843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007845void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007846{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007847 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007848}
7849#endif
7850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007851#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007852void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007853{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007854 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007855}
7856#endif
7857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007859
Hanno Becker1841b0a2018-08-24 11:13:57 +01007860void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7861 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007862{
7863 ssl->disable_datagram_packing = !allow_packing;
7864}
7865
7866void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7867 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007868{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007869 conf->hs_timeout_min = min;
7870 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007871}
7872#endif
7873
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007874void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007875{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007876 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007877}
7878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007879#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007880void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007881 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007882 void *p_vrfy )
7883{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007884 conf->f_vrfy = f_vrfy;
7885 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007886}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007888
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007889void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007890 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007891 void *p_rng )
7892{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007893 conf->f_rng = f_rng;
7894 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007895}
7896
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007897void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007898 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007899 void *p_dbg )
7900{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007901 conf->f_dbg = f_dbg;
7902 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007903}
7904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007906 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007907 mbedtls_ssl_send_t *f_send,
7908 mbedtls_ssl_recv_t *f_recv,
7909 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007910{
7911 ssl->p_bio = p_bio;
7912 ssl->f_send = f_send;
7913 ssl->f_recv = f_recv;
7914 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007915}
7916
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007917#if defined(MBEDTLS_SSL_PROTO_DTLS)
7918void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7919{
7920 ssl->mtu = mtu;
7921}
7922#endif
7923
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007924void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007925{
7926 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007927}
7928
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007929void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7930 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007931 mbedtls_ssl_set_timer_t *f_set_timer,
7932 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007933{
7934 ssl->p_timer = p_timer;
7935 ssl->f_set_timer = f_set_timer;
7936 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007937
7938 /* Make sure we start with no timer running */
7939 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007940}
7941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007942#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007943void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007944 void *p_cache,
7945 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7946 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007947{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007948 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007949 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007950 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007951}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007952#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007954#if defined(MBEDTLS_SSL_CLI_C)
7955int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007956{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007957 int ret;
7958
7959 if( ssl == NULL ||
7960 session == NULL ||
7961 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007962 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007965 }
7966
Hanno Becker52055ae2019-02-06 14:30:46 +00007967 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
7968 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007969 return( ret );
7970
Paul Bakker0a597072012-09-25 21:55:46 +00007971 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007972
7973 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007974}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007976
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007977void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007978 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007979{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007980 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7981 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7982 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7983 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007984}
7985
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007986void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007987 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007988 int major, int minor )
7989{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007990 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007991 return;
7992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007994 return;
7995
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007996 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007997}
7998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008000void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008001 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008002{
8003 conf->cert_profile = profile;
8004}
8005
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008006/* Append a new keycert entry to a (possibly empty) list */
8007static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8008 mbedtls_x509_crt *cert,
8009 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008010{
niisato8ee24222018-06-25 19:05:48 +09008011 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008012
niisato8ee24222018-06-25 19:05:48 +09008013 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8014 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008015 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008016
niisato8ee24222018-06-25 19:05:48 +09008017 new_cert->cert = cert;
8018 new_cert->key = key;
8019 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008020
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008021 /* Update head is the list was null, else add to the end */
8022 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008023 {
niisato8ee24222018-06-25 19:05:48 +09008024 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008025 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008026 else
8027 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008028 mbedtls_ssl_key_cert *cur = *head;
8029 while( cur->next != NULL )
8030 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008031 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008032 }
8033
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008034 return( 0 );
8035}
8036
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008037int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008038 mbedtls_x509_crt *own_cert,
8039 mbedtls_pk_context *pk_key )
8040{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008041 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008042}
8043
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008044void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008045 mbedtls_x509_crt *ca_chain,
8046 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008047{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008048 conf->ca_chain = ca_chain;
8049 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008050
8051#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8052 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8053 * cannot be used together. */
8054 conf->f_ca_cb = NULL;
8055 conf->p_ca_cb = NULL;
8056#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008057}
Hanno Becker5adaad92019-03-27 16:54:37 +00008058
8059#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8060void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008061 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008062 void *p_ca_cb )
8063{
8064 conf->f_ca_cb = f_ca_cb;
8065 conf->p_ca_cb = p_ca_cb;
8066
8067 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8068 * cannot be used together. */
8069 conf->ca_chain = NULL;
8070 conf->ca_crl = NULL;
8071}
8072#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008073#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008074
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008075#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8076int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8077 mbedtls_x509_crt *own_cert,
8078 mbedtls_pk_context *pk_key )
8079{
8080 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8081 own_cert, pk_key ) );
8082}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008083
8084void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8085 mbedtls_x509_crt *ca_chain,
8086 mbedtls_x509_crl *ca_crl )
8087{
8088 ssl->handshake->sni_ca_chain = ca_chain;
8089 ssl->handshake->sni_ca_crl = ca_crl;
8090}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008091
8092void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8093 int authmode )
8094{
8095 ssl->handshake->sni_authmode = authmode;
8096}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008097#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8098
Hanno Becker8927c832019-04-03 12:52:50 +01008099#if defined(MBEDTLS_X509_CRT_PARSE_C)
8100void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8101 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8102 void *p_vrfy )
8103{
8104 ssl->f_vrfy = f_vrfy;
8105 ssl->p_vrfy = p_vrfy;
8106}
8107#endif
8108
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008109#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008110/*
8111 * Set EC J-PAKE password for current handshake
8112 */
8113int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8114 const unsigned char *pw,
8115 size_t pw_len )
8116{
8117 mbedtls_ecjpake_role role;
8118
Janos Follath8eb64132016-06-03 15:40:57 +01008119 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8121
8122 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8123 role = MBEDTLS_ECJPAKE_SERVER;
8124 else
8125 role = MBEDTLS_ECJPAKE_CLIENT;
8126
8127 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8128 role,
8129 MBEDTLS_MD_SHA256,
8130 MBEDTLS_ECP_DP_SECP256R1,
8131 pw, pw_len ) );
8132}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008133#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008136
8137static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8138{
8139 /* Remove reference to existing PSK, if any. */
8140#if defined(MBEDTLS_USE_PSA_CRYPTO)
8141 if( conf->psk_opaque != 0 )
8142 {
8143 /* The maintenance of the PSK key slot is the
8144 * user's responsibility. */
8145 conf->psk_opaque = 0;
8146 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008147 /* This and the following branch should never
8148 * be taken simultaenously as we maintain the
8149 * invariant that raw and opaque PSKs are never
8150 * configured simultaneously. As a safeguard,
8151 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008152#endif /* MBEDTLS_USE_PSA_CRYPTO */
8153 if( conf->psk != NULL )
8154 {
8155 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8156
8157 mbedtls_free( conf->psk );
8158 conf->psk = NULL;
8159 conf->psk_len = 0;
8160 }
8161
8162 /* Remove reference to PSK identity, if any. */
8163 if( conf->psk_identity != NULL )
8164 {
8165 mbedtls_free( conf->psk_identity );
8166 conf->psk_identity = NULL;
8167 conf->psk_identity_len = 0;
8168 }
8169}
8170
Hanno Becker7390c712018-11-15 13:33:04 +00008171/* This function assumes that PSK identity in the SSL config is unset.
8172 * It checks that the provided identity is well-formed and attempts
8173 * to make a copy of it in the SSL config.
8174 * On failure, the PSK identity in the config remains unset. */
8175static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8176 unsigned char const *psk_identity,
8177 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008178{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008179 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008180 if( psk_identity == NULL ||
8181 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008182 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008183 {
8184 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8185 }
8186
Hanno Becker7390c712018-11-15 13:33:04 +00008187 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8188 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008189 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008190
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008191 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008192 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008193
8194 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008195}
8196
Hanno Becker7390c712018-11-15 13:33:04 +00008197int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8198 const unsigned char *psk, size_t psk_len,
8199 const unsigned char *psk_identity, size_t psk_identity_len )
8200{
8201 int ret;
8202 /* Remove opaque/raw PSK + PSK Identity */
8203 ssl_conf_remove_psk( conf );
8204
8205 /* Check and set raw PSK */
8206 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8207 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8208 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8209 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8210 conf->psk_len = psk_len;
8211 memcpy( conf->psk, psk, conf->psk_len );
8212
8213 /* Check and set PSK Identity */
8214 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8215 if( ret != 0 )
8216 ssl_conf_remove_psk( conf );
8217
8218 return( ret );
8219}
8220
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008221static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8222{
8223#if defined(MBEDTLS_USE_PSA_CRYPTO)
8224 if( ssl->handshake->psk_opaque != 0 )
8225 {
8226 ssl->handshake->psk_opaque = 0;
8227 }
8228 else
8229#endif /* MBEDTLS_USE_PSA_CRYPTO */
8230 if( ssl->handshake->psk != NULL )
8231 {
8232 mbedtls_platform_zeroize( ssl->handshake->psk,
8233 ssl->handshake->psk_len );
8234 mbedtls_free( ssl->handshake->psk );
8235 ssl->handshake->psk_len = 0;
8236 }
8237}
8238
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008239int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8240 const unsigned char *psk, size_t psk_len )
8241{
8242 if( psk == NULL || ssl->handshake == NULL )
8243 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8244
8245 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8246 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8247
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008248 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008249
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008250 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008251 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008252
8253 ssl->handshake->psk_len = psk_len;
8254 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8255
8256 return( 0 );
8257}
8258
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008259#if defined(MBEDTLS_USE_PSA_CRYPTO)
8260int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008261 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008262 const unsigned char *psk_identity,
8263 size_t psk_identity_len )
8264{
Hanno Becker7390c712018-11-15 13:33:04 +00008265 int ret;
8266 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008267 ssl_conf_remove_psk( conf );
8268
Hanno Becker7390c712018-11-15 13:33:04 +00008269 /* Check and set opaque PSK */
8270 if( psk_slot == 0 )
8271 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008272 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008273
8274 /* Check and set PSK Identity */
8275 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8276 psk_identity_len );
8277 if( ret != 0 )
8278 ssl_conf_remove_psk( conf );
8279
8280 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008281}
8282
8283int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008284 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008285{
8286 if( psk_slot == 0 || ssl->handshake == NULL )
8287 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8288
8289 ssl_remove_psk( ssl );
8290 ssl->handshake->psk_opaque = psk_slot;
8291 return( 0 );
8292}
8293#endif /* MBEDTLS_USE_PSA_CRYPTO */
8294
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008295void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008296 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008297 size_t),
8298 void *p_psk )
8299{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008300 conf->f_psk = f_psk;
8301 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008302}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008303#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008304
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008305#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008306
8307#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008308int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008309{
8310 int ret;
8311
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008312 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8313 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8314 {
8315 mbedtls_mpi_free( &conf->dhm_P );
8316 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008317 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008318 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008319
8320 return( 0 );
8321}
Hanno Becker470a8c42017-10-04 15:28:46 +01008322#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008323
Hanno Beckera90658f2017-10-04 15:29:08 +01008324int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8325 const unsigned char *dhm_P, size_t P_len,
8326 const unsigned char *dhm_G, size_t G_len )
8327{
8328 int ret;
8329
8330 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8331 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8332 {
8333 mbedtls_mpi_free( &conf->dhm_P );
8334 mbedtls_mpi_free( &conf->dhm_G );
8335 return( ret );
8336 }
8337
8338 return( 0 );
8339}
Paul Bakker5121ce52009-01-03 21:22:43 +00008340
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008341int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008342{
8343 int ret;
8344
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008345 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8346 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8347 {
8348 mbedtls_mpi_free( &conf->dhm_P );
8349 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008350 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008351 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008352
8353 return( 0 );
8354}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008355#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008356
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008357#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8358/*
8359 * Set the minimum length for Diffie-Hellman parameters
8360 */
8361void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8362 unsigned int bitlen )
8363{
8364 conf->dhm_min_bitlen = bitlen;
8365}
8366#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8367
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008368#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008369/*
8370 * Set allowed/preferred hashes for handshake signatures
8371 */
8372void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8373 const int *hashes )
8374{
8375 conf->sig_hashes = hashes;
8376}
Hanno Becker947194e2017-04-07 13:25:49 +01008377#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008378
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008379#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008380/*
8381 * Set the allowed elliptic curves
8382 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008383void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008384 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008385{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008386 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008387}
Hanno Becker947194e2017-04-07 13:25:49 +01008388#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008389
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008390#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008392{
Hanno Becker947194e2017-04-07 13:25:49 +01008393 /* Initialize to suppress unnecessary compiler warning */
8394 size_t hostname_len = 0;
8395
8396 /* Check if new hostname is valid before
8397 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008398 if( hostname != NULL )
8399 {
8400 hostname_len = strlen( hostname );
8401
8402 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8403 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8404 }
8405
8406 /* Now it's clear that we will overwrite the old hostname,
8407 * so we can free it safely */
8408
8409 if( ssl->hostname != NULL )
8410 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008411 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008412 mbedtls_free( ssl->hostname );
8413 }
8414
8415 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008416
Paul Bakker5121ce52009-01-03 21:22:43 +00008417 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008418 {
8419 ssl->hostname = NULL;
8420 }
8421 else
8422 {
8423 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008424 if( ssl->hostname == NULL )
8425 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008426
Hanno Becker947194e2017-04-07 13:25:49 +01008427 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008428
Hanno Becker947194e2017-04-07 13:25:49 +01008429 ssl->hostname[hostname_len] = '\0';
8430 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008431
8432 return( 0 );
8433}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008434#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008435
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008436#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008437void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008438 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008439 const unsigned char *, size_t),
8440 void *p_sni )
8441{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008442 conf->f_sni = f_sni;
8443 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008444}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008445#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008447#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008448int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008449{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008450 size_t cur_len, tot_len;
8451 const char **p;
8452
8453 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008454 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8455 * MUST NOT be truncated."
8456 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008457 */
8458 tot_len = 0;
8459 for( p = protos; *p != NULL; p++ )
8460 {
8461 cur_len = strlen( *p );
8462 tot_len += cur_len;
8463
8464 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008465 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008466 }
8467
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008468 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008469
8470 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008471}
8472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008473const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008474{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008475 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008476}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008477#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008478
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008479void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008480{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008481 conf->max_major_ver = major;
8482 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008483}
8484
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008485void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008486{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008487 conf->min_major_ver = major;
8488 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008489}
8490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008491#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008492void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008493{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008494 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008495}
8496#endif
8497
Janos Follath088ce432017-04-10 12:42:31 +01008498#if defined(MBEDTLS_SSL_SRV_C)
8499void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8500 char cert_req_ca_list )
8501{
8502 conf->cert_req_ca_list = cert_req_ca_list;
8503}
8504#endif
8505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008506#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008507void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008508{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008509 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008510}
8511#endif
8512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008513#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008514void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008515{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008516 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008517}
8518#endif
8519
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008520#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008521void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008522{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008523 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008524}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008525#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008527#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008528int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008529{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008530 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008531 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008533 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008534 }
8535
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008536 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008537
8538 return( 0 );
8539}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008540#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008542#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008543void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008544{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008545 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008546}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008547#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008549#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008550void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008551{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008552 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008553}
8554#endif
8555
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008556void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008557{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008558 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008559}
8560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008561#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008562void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008563{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008564 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008565}
8566
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008567void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008568{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008569 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008570}
8571
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008572void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008573 const unsigned char period[8] )
8574{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008575 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008576}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008577#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008579#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008580#if defined(MBEDTLS_SSL_CLI_C)
8581void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008582{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008583 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008584}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008585#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008586
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008587#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008588void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8589 mbedtls_ssl_ticket_write_t *f_ticket_write,
8590 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8591 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008592{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008593 conf->f_ticket_write = f_ticket_write;
8594 conf->f_ticket_parse = f_ticket_parse;
8595 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008596}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008597#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008598#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008599
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008600#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8601void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8602 mbedtls_ssl_export_keys_t *f_export_keys,
8603 void *p_export_keys )
8604{
8605 conf->f_export_keys = f_export_keys;
8606 conf->p_export_keys = p_export_keys;
8607}
8608#endif
8609
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008610#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008611void mbedtls_ssl_conf_async_private_cb(
8612 mbedtls_ssl_config *conf,
8613 mbedtls_ssl_async_sign_t *f_async_sign,
8614 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8615 mbedtls_ssl_async_resume_t *f_async_resume,
8616 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008617 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008618{
8619 conf->f_async_sign_start = f_async_sign;
8620 conf->f_async_decrypt_start = f_async_decrypt;
8621 conf->f_async_resume = f_async_resume;
8622 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008623 conf->p_async_config_data = async_config_data;
8624}
8625
Gilles Peskine8f97af72018-04-26 11:46:10 +02008626void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8627{
8628 return( conf->p_async_config_data );
8629}
8630
Gilles Peskine1febfef2018-04-30 11:54:39 +02008631void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008632{
8633 if( ssl->handshake == NULL )
8634 return( NULL );
8635 else
8636 return( ssl->handshake->user_async_ctx );
8637}
8638
Gilles Peskine1febfef2018-04-30 11:54:39 +02008639void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008640 void *ctx )
8641{
8642 if( ssl->handshake != NULL )
8643 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008644}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008645#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008646
Paul Bakker5121ce52009-01-03 21:22:43 +00008647/*
8648 * SSL get accessors
8649 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008650size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008651{
8652 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8653}
8654
Hanno Becker8b170a02017-10-10 11:51:19 +01008655int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8656{
8657 /*
8658 * Case A: We're currently holding back
8659 * a message for further processing.
8660 */
8661
8662 if( ssl->keep_current_message == 1 )
8663 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008664 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008665 return( 1 );
8666 }
8667
8668 /*
8669 * Case B: Further records are pending in the current datagram.
8670 */
8671
8672#if defined(MBEDTLS_SSL_PROTO_DTLS)
8673 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8674 ssl->in_left > ssl->next_record_offset )
8675 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008676 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008677 return( 1 );
8678 }
8679#endif /* MBEDTLS_SSL_PROTO_DTLS */
8680
8681 /*
8682 * Case C: A handshake message is being processed.
8683 */
8684
Hanno Becker8b170a02017-10-10 11:51:19 +01008685 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8686 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008687 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008688 return( 1 );
8689 }
8690
8691 /*
8692 * Case D: An application data message is being processed
8693 */
8694 if( ssl->in_offt != NULL )
8695 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008696 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008697 return( 1 );
8698 }
8699
8700 /*
8701 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008702 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008703 * we implement support for multiple alerts in single records.
8704 */
8705
8706 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8707 return( 0 );
8708}
8709
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008710uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008711{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008712 if( ssl->session != NULL )
8713 return( ssl->session->verify_result );
8714
8715 if( ssl->session_negotiate != NULL )
8716 return( ssl->session_negotiate->verify_result );
8717
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008718 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008719}
8720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008721const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008722{
Paul Bakker926c8e42013-03-06 10:23:34 +01008723 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008724 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008726 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008727}
8728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008729const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008730{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008731#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008732 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008733 {
8734 switch( ssl->minor_ver )
8735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008736 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008737 return( "DTLSv1.0" );
8738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008739 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008740 return( "DTLSv1.2" );
8741
8742 default:
8743 return( "unknown (DTLS)" );
8744 }
8745 }
8746#endif
8747
Paul Bakker43ca69c2011-01-15 17:35:19 +00008748 switch( ssl->minor_ver )
8749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008750 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008751 return( "SSLv3.0" );
8752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008753 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008754 return( "TLSv1.0" );
8755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008756 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008757 return( "TLSv1.1" );
8758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008759 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008760 return( "TLSv1.2" );
8761
Paul Bakker43ca69c2011-01-15 17:35:19 +00008762 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008763 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008764 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008765}
8766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008767int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008768{
Hanno Becker3136ede2018-08-17 15:28:19 +01008769 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008771 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008772
Hanno Becker78640902018-08-13 16:35:15 +01008773 if( transform == NULL )
8774 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008776#if defined(MBEDTLS_ZLIB_SUPPORT)
8777 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8778 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008779#endif
8780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008783 case MBEDTLS_MODE_GCM:
8784 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008785 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008786 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008787 transform_expansion = transform->minlen;
8788 break;
8789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008790 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008791
8792 block_size = mbedtls_cipher_get_block_size(
8793 &transform->cipher_ctx_enc );
8794
Hanno Becker3136ede2018-08-17 15:28:19 +01008795 /* Expansion due to the addition of the MAC. */
8796 transform_expansion += transform->maclen;
8797
8798 /* Expansion due to the addition of CBC padding;
8799 * Theoretically up to 256 bytes, but we never use
8800 * more than the block size of the underlying cipher. */
8801 transform_expansion += block_size;
8802
8803 /* For TLS 1.1 or higher, an explicit IV is added
8804 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008805#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8806 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008807 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008808#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008809
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008810 break;
8811
8812 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008814 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008815 }
8816
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008817 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008818}
8819
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008820#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8821size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8822{
8823 size_t max_len;
8824
8825 /*
8826 * Assume mfl_code is correct since it was checked when set
8827 */
Angus Grattond8213d02016-05-25 20:56:48 +10008828 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008829
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008830 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008831 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008832 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008833 {
Angus Grattond8213d02016-05-25 20:56:48 +10008834 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008835 }
8836
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008837 /* During a handshake, use the value being negotiated */
8838 if( ssl->session_negotiate != NULL &&
8839 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8840 {
8841 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8842 }
8843
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008844 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008845}
8846#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8847
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008848#if defined(MBEDTLS_SSL_PROTO_DTLS)
8849static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8850{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008851 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8852 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8853 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8854 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8855 return ( 0 );
8856
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008857 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8858 return( ssl->mtu );
8859
8860 if( ssl->mtu == 0 )
8861 return( ssl->handshake->mtu );
8862
8863 return( ssl->mtu < ssl->handshake->mtu ?
8864 ssl->mtu : ssl->handshake->mtu );
8865}
8866#endif /* MBEDTLS_SSL_PROTO_DTLS */
8867
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008868int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8869{
8870 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8871
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008872#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8873 !defined(MBEDTLS_SSL_PROTO_DTLS)
8874 (void) ssl;
8875#endif
8876
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008877#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8878 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8879
8880 if( max_len > mfl )
8881 max_len = mfl;
8882#endif
8883
8884#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008885 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008886 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008887 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008888 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8889 const size_t overhead = (size_t) ret;
8890
8891 if( ret < 0 )
8892 return( ret );
8893
8894 if( mtu <= overhead )
8895 {
8896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8897 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8898 }
8899
8900 if( max_len > mtu - overhead )
8901 max_len = mtu - overhead;
8902 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008903#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008904
Hanno Becker0defedb2018-08-10 12:35:02 +01008905#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8906 !defined(MBEDTLS_SSL_PROTO_DTLS)
8907 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008908#endif
8909
8910 return( (int) max_len );
8911}
8912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008913#if defined(MBEDTLS_X509_CRT_PARSE_C)
8914const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008915{
8916 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008917 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008918
Hanno Beckere6824572019-02-07 13:18:46 +00008919#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008920 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00008921#else
8922 return( NULL );
8923#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008924}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008925#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008927#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00008928int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8929 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008930{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008931 if( ssl == NULL ||
8932 dst == NULL ||
8933 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008934 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008936 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008937 }
8938
Hanno Becker52055ae2019-02-06 14:30:46 +00008939 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008940}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008941#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008942
Paul Bakker5121ce52009-01-03 21:22:43 +00008943/*
Paul Bakker1961b702013-01-25 14:49:24 +01008944 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008946int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008947{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008948 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008949
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008950 if( ssl == NULL || ssl->conf == NULL )
8951 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008953#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008954 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008955 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008956#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008957#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008958 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008959 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008960#endif
8961
Paul Bakker1961b702013-01-25 14:49:24 +01008962 return( ret );
8963}
8964
8965/*
8966 * Perform the SSL handshake
8967 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008968int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008969{
8970 int ret = 0;
8971
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008972 if( ssl == NULL || ssl->conf == NULL )
8973 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008977 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008979 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008980
8981 if( ret != 0 )
8982 break;
8983 }
8984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008986
8987 return( ret );
8988}
8989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008990#if defined(MBEDTLS_SSL_RENEGOTIATION)
8991#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008992/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008993 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008994 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008995static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008996{
8997 int ret;
8998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009000
9001 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009002 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9003 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009004
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009005 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009006 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009007 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009008 return( ret );
9009 }
9010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009012
9013 return( 0 );
9014}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009015#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009016
9017/*
9018 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009019 * - any side: calling mbedtls_ssl_renegotiate(),
9020 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9021 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009022 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009023 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009024 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009026static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009027{
9028 int ret;
9029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009031
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009032 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9033 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009034
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009035 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9036 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009037#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009038 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009039 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009040 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009041 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009042 ssl->handshake->out_msg_seq = 1;
9043 else
9044 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009045 }
9046#endif
9047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009048 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9049 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009051 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009053 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009054 return( ret );
9055 }
9056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009058
9059 return( 0 );
9060}
9061
9062/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009063 * Renegotiate current connection on client,
9064 * or request renegotiation on server
9065 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009066int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009068 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009069
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009070 if( ssl == NULL || ssl->conf == NULL )
9071 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009073#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009074 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009075 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009080 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009081
9082 /* Did we already try/start sending HelloRequest? */
9083 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009084 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009085
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009086 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009087 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009088#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009090#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009091 /*
9092 * On client, either start the renegotiation process or,
9093 * if already in progress, continue the handshake
9094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009095 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009097 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009099
9100 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009102 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009103 return( ret );
9104 }
9105 }
9106 else
9107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009108 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009110 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009111 return( ret );
9112 }
9113 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009114#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009115
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009116 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009117}
9118
9119/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009120 * Check record counters and renegotiate if they're above the limit.
9121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009122static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009123{
Andres AG2196c7f2016-12-15 17:01:16 +00009124 size_t ep_len = ssl_ep_len( ssl );
9125 int in_ctr_cmp;
9126 int out_ctr_cmp;
9127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009128 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9129 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009130 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009131 {
9132 return( 0 );
9133 }
9134
Andres AG2196c7f2016-12-15 17:01:16 +00009135 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9136 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009137 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009138 ssl->conf->renego_period + ep_len, 8 - ep_len );
9139
9140 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009141 {
9142 return( 0 );
9143 }
9144
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009146 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009147}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009148#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009149
9150/*
9151 * Receive application data decrypted from the SSL layer
9152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009153int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009154{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009155 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009156 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009157
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009158 if( ssl == NULL || ssl->conf == NULL )
9159 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009163#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009164 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009166 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009167 return( ret );
9168
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009169 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009170 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009171 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009172 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009173 return( ret );
9174 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009175 }
9176#endif
9177
Hanno Becker4a810fb2017-05-24 16:27:30 +01009178 /*
9179 * Check if renegotiation is necessary and/or handshake is
9180 * in process. If yes, perform/continue, and fall through
9181 * if an unexpected packet is received while the client
9182 * is waiting for the ServerHello.
9183 *
9184 * (There is no equivalent to the last condition on
9185 * the server-side as it is not treated as within
9186 * a handshake while waiting for the ClientHello
9187 * after a renegotiation request.)
9188 */
9189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009190#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009191 ret = ssl_check_ctr_renegotiate( ssl );
9192 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9193 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009195 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009196 return( ret );
9197 }
9198#endif
9199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009200 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009202 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009203 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9204 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009206 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009207 return( ret );
9208 }
9209 }
9210
Hanno Beckere41158b2017-10-23 13:30:32 +01009211 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009212 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009213 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009214 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009215 if( ssl->f_get_timer != NULL &&
9216 ssl->f_get_timer( ssl->p_timer ) == -1 )
9217 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009218 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009219 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009220
Hanno Becker327c93b2018-08-15 13:56:18 +01009221 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009222 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009223 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9224 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009225
Hanno Becker4a810fb2017-05-24 16:27:30 +01009226 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9227 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009228 }
9229
9230 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009231 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009232 {
9233 /*
9234 * OpenSSL sends empty messages to randomize the IV
9235 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009236 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009238 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009239 return( 0 );
9240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009241 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009242 return( ret );
9243 }
9244 }
9245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009246 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009249
Hanno Becker4a810fb2017-05-24 16:27:30 +01009250 /*
9251 * - For client-side, expect SERVER_HELLO_REQUEST.
9252 * - For server-side, expect CLIENT_HELLO.
9253 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9254 */
9255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009256#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009257 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009258 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009259 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009262
9263 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009264#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009265 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009266 {
9267 continue;
9268 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009269#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009270 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009271 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009272#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009273
Hanno Becker4a810fb2017-05-24 16:27:30 +01009274#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009275 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009276 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009279
9280 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009281#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009282 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009283 {
9284 continue;
9285 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009286#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009287 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009288 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009289#endif /* MBEDTLS_SSL_SRV_C */
9290
Hanno Becker21df7f92017-10-17 11:03:26 +01009291#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009292 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009293 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9294 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9295 ssl->conf->allow_legacy_renegotiation ==
9296 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9297 {
9298 /*
9299 * Accept renegotiation request
9300 */
Paul Bakker48916f92012-09-16 19:57:18 +00009301
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009302 /* DTLS clients need to know renego is server-initiated */
9303#if defined(MBEDTLS_SSL_PROTO_DTLS)
9304 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9305 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9306 {
9307 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9308 }
9309#endif
9310 ret = ssl_start_renegotiation( ssl );
9311 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9312 ret != 0 )
9313 {
9314 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9315 return( ret );
9316 }
9317 }
9318 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009319#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009320 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009321 /*
9322 * Refuse renegotiation
9323 */
9324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009325 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009327#if defined(MBEDTLS_SSL_PROTO_SSL3)
9328 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009329 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009330 /* SSLv3 does not have a "no_renegotiation" warning, so
9331 we send a fatal alert and abort the connection. */
9332 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9333 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9334 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009335 }
9336 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9338#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9339 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9340 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009342 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9343 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9344 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009345 {
9346 return( ret );
9347 }
Paul Bakker48916f92012-09-16 19:57:18 +00009348 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009349 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9351 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9354 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009355 }
Paul Bakker48916f92012-09-16 19:57:18 +00009356 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009357
Hanno Becker90333da2017-10-10 11:27:13 +01009358 /* At this point, we don't know whether the renegotiation has been
9359 * completed or not. The cases to consider are the following:
9360 * 1) The renegotiation is complete. In this case, no new record
9361 * has been read yet.
9362 * 2) The renegotiation is incomplete because the client received
9363 * an application data record while awaiting the ServerHello.
9364 * 3) The renegotiation is incomplete because the client received
9365 * a non-handshake, non-application data message while awaiting
9366 * the ServerHello.
9367 * In each of these case, looping will be the proper action:
9368 * - For 1), the next iteration will read a new record and check
9369 * if it's application data.
9370 * - For 2), the loop condition isn't satisfied as application data
9371 * is present, hence continue is the same as break
9372 * - For 3), the loop condition is satisfied and read_record
9373 * will re-deliver the message that was held back by the client
9374 * when expecting the ServerHello.
9375 */
9376 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009377 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009378#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009379 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009380 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009381 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009382 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009383 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009386 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009387 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009388 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009389 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009390 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009391#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009393 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9394 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009397 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009398 }
9399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009400 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9403 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009404 }
9405
9406 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009407
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009408 /* We're going to return something now, cancel timer,
9409 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009410 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009411 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009412
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009413#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009414 /* If we requested renego but received AppData, resend HelloRequest.
9415 * Do it now, after setting in_offt, to avoid taking this branch
9416 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009417#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009418 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009420 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009421 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009423 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009424 return( ret );
9425 }
9426 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009427#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009428#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009429 }
9430
9431 n = ( len < ssl->in_msglen )
9432 ? len : ssl->in_msglen;
9433
9434 memcpy( buf, ssl->in_offt, n );
9435 ssl->in_msglen -= n;
9436
9437 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009438 {
9439 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009440 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009441 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009442 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009443 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009444 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009445 /* more data available */
9446 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009447 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009450
Paul Bakker23986e52011-04-24 08:57:21 +00009451 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009452}
9453
9454/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009455 * Send application data to be encrypted by the SSL layer, taking care of max
9456 * fragment length and buffer size.
9457 *
9458 * According to RFC 5246 Section 6.2.1:
9459 *
9460 * Zero-length fragments of Application data MAY be sent as they are
9461 * potentially useful as a traffic analysis countermeasure.
9462 *
9463 * Therefore, it is possible that the input message length is 0 and the
9464 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009465 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009466static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009467 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009468{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009469 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9470 const size_t max_len = (size_t) ret;
9471
9472 if( ret < 0 )
9473 {
9474 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9475 return( ret );
9476 }
9477
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009478 if( len > max_len )
9479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009480#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009481 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009484 "maximum fragment length: %d > %d",
9485 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009487 }
9488 else
9489#endif
9490 len = max_len;
9491 }
Paul Bakker887bd502011-06-08 13:10:54 +00009492
Paul Bakker5121ce52009-01-03 21:22:43 +00009493 if( ssl->out_left != 0 )
9494 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009495 /*
9496 * The user has previously tried to send the data and
9497 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9498 * written. In this case, we expect the high-level write function
9499 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009504 return( ret );
9505 }
9506 }
Paul Bakker887bd502011-06-08 13:10:54 +00009507 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009508 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009509 /*
9510 * The user is trying to send a message the first time, so we need to
9511 * copy the data into the internal buffers and setup the data structure
9512 * to keep track of partial writes
9513 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009514 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009515 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009516 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009517
Hanno Becker67bc7c32018-08-06 11:33:50 +01009518 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009521 return( ret );
9522 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009523 }
9524
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009525 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009526}
9527
9528/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009529 * Write application data, doing 1/n-1 splitting if necessary.
9530 *
9531 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009532 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009533 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009536static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009537 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009538{
9539 int ret;
9540
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009541 if( ssl->conf->cbc_record_splitting ==
9542 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009543 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009544 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9545 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9546 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009547 {
9548 return( ssl_write_real( ssl, buf, len ) );
9549 }
9550
9551 if( ssl->split_done == 0 )
9552 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009553 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009554 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009555 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009556 }
9557
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009558 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9559 return( ret );
9560 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009561
9562 return( ret + 1 );
9563}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009564#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009565
9566/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009567 * Write application data (public-facing wrapper)
9568 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009569int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009570{
9571 int ret;
9572
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009574
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009575 if( ssl == NULL || ssl->conf == NULL )
9576 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9577
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009578#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009579 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9580 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009581 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009582 return( ret );
9583 }
9584#endif
9585
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009586 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009587 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009588 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009589 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009590 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009591 return( ret );
9592 }
9593 }
9594
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009595#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009596 ret = ssl_write_split( ssl, buf, len );
9597#else
9598 ret = ssl_write_real( ssl, buf, len );
9599#endif
9600
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009602
9603 return( ret );
9604}
9605
9606/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009607 * Notify the peer that the connection is being closed
9608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009609int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009610{
9611 int ret;
9612
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009613 if( ssl == NULL || ssl->conf == NULL )
9614 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009617
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009618 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009619 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009621 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9624 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9625 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009627 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009628 return( ret );
9629 }
9630 }
9631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009633
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009634 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009635}
9636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009637void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009638{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009639 if( transform == NULL )
9640 return;
9641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009642#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009643 deflateEnd( &transform->ctx_deflate );
9644 inflateEnd( &transform->ctx_inflate );
9645#endif
9646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009647 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9648 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009650 mbedtls_md_free( &transform->md_ctx_enc );
9651 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009652
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009653 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009654}
9655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009656#if defined(MBEDTLS_X509_CRT_PARSE_C)
9657static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009658{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009659 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009660
9661 while( cur != NULL )
9662 {
9663 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009664 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009665 cur = next;
9666 }
9667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009668#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009669
Hanno Becker0271f962018-08-16 13:23:47 +01009670#if defined(MBEDTLS_SSL_PROTO_DTLS)
9671
9672static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9673{
9674 unsigned offset;
9675 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9676
9677 if( hs == NULL )
9678 return;
9679
Hanno Becker283f5ef2018-08-24 09:34:47 +01009680 ssl_free_buffered_record( ssl );
9681
Hanno Becker0271f962018-08-16 13:23:47 +01009682 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009683 ssl_buffering_free_slot( ssl, offset );
9684}
9685
9686static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9687 uint8_t slot )
9688{
9689 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9690 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009691
9692 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9693 return;
9694
Hanno Beckere605b192018-08-21 15:59:07 +01009695 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009696 {
Hanno Beckere605b192018-08-21 15:59:07 +01009697 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009698 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009699 mbedtls_free( hs_buf->data );
9700 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009701 }
9702}
9703
9704#endif /* MBEDTLS_SSL_PROTO_DTLS */
9705
Gilles Peskine9b562d52018-04-25 20:32:43 +02009706void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009707{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009708 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9709
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009710 if( handshake == NULL )
9711 return;
9712
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009713#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9714 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9715 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009716 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009717 handshake->async_in_progress = 0;
9718 }
9719#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9720
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009721#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9722 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9723 mbedtls_md5_free( &handshake->fin_md5 );
9724 mbedtls_sha1_free( &handshake->fin_sha1 );
9725#endif
9726#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9727#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009728#if defined(MBEDTLS_USE_PSA_CRYPTO)
9729 psa_hash_abort( &handshake->fin_sha256_psa );
9730#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009731 mbedtls_sha256_free( &handshake->fin_sha256 );
9732#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009733#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009734#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009735#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009736 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009737#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009738 mbedtls_sha512_free( &handshake->fin_sha512 );
9739#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009740#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009741#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009743#if defined(MBEDTLS_DHM_C)
9744 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009745#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009746#if defined(MBEDTLS_ECDH_C)
9747 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009748#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009749#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009750 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009751#if defined(MBEDTLS_SSL_CLI_C)
9752 mbedtls_free( handshake->ecjpake_cache );
9753 handshake->ecjpake_cache = NULL;
9754 handshake->ecjpake_cache_len = 0;
9755#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009756#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009757
Janos Follath4ae5c292016-02-10 11:27:43 +00009758#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9759 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009760 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009761 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009762#endif
9763
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009764#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9765 if( handshake->psk != NULL )
9766 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009767 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009768 mbedtls_free( handshake->psk );
9769 }
9770#endif
9771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009772#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9773 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009774 /*
9775 * Free only the linked list wrapper, not the keys themselves
9776 * since the belong to the SNI callback
9777 */
9778 if( handshake->sni_key_cert != NULL )
9779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009780 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009781
9782 while( cur != NULL )
9783 {
9784 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009785 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009786 cur = next;
9787 }
9788 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009789#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009790
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009791#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009792 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00009793 if( handshake->ecrs_peer_cert != NULL )
9794 {
9795 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
9796 mbedtls_free( handshake->ecrs_peer_cert );
9797 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009798#endif
9799
Hanno Becker75173122019-02-06 16:18:31 +00009800#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9801 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9802 mbedtls_pk_free( &handshake->peer_pubkey );
9803#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009805#if defined(MBEDTLS_SSL_PROTO_DTLS)
9806 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009807 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009808 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009809#endif
9810
Hanno Becker4a63ed42019-01-08 11:39:35 +00009811#if defined(MBEDTLS_ECDH_C) && \
9812 defined(MBEDTLS_USE_PSA_CRYPTO)
9813 psa_destroy_key( handshake->ecdh_psa_privkey );
9814#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9815
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009816 mbedtls_platform_zeroize( handshake,
9817 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009818}
9819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009821{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009822 if( session == NULL )
9823 return;
9824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009826 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009827#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009828
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009829#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009831#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009832
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009833 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009834}
9835
Paul Bakker5121ce52009-01-03 21:22:43 +00009836/*
9837 * Free an SSL context
9838 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009840{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009841 if( ssl == NULL )
9842 return;
9843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009845
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009846 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009847 {
Angus Grattond8213d02016-05-25 20:56:48 +10009848 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009850 }
9851
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009852 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009853 {
Angus Grattond8213d02016-05-25 20:56:48 +10009854 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009856 }
9857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009858#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009859 if( ssl->compress_buf != NULL )
9860 {
Angus Grattond8213d02016-05-25 20:56:48 +10009861 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009863 }
9864#endif
9865
Paul Bakker48916f92012-09-16 19:57:18 +00009866 if( ssl->transform )
9867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009868 mbedtls_ssl_transform_free( ssl->transform );
9869 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009870 }
9871
9872 if( ssl->handshake )
9873 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009874 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009875 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9876 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009878 mbedtls_free( ssl->handshake );
9879 mbedtls_free( ssl->transform_negotiate );
9880 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009881 }
9882
Paul Bakkerc0463502013-02-14 11:19:38 +01009883 if( ssl->session )
9884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009885 mbedtls_ssl_session_free( ssl->session );
9886 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009887 }
9888
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009889#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009890 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009891 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009892 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009893 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009894 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009895#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009897#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9898 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9901 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009902 }
9903#endif
9904
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009905#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009906 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009907#endif
9908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009910
Paul Bakker86f04f42013-02-14 11:20:09 +01009911 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009912 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009913}
9914
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009915/*
9916 * Initialze mbedtls_ssl_config
9917 */
9918void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9919{
9920 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9921}
9922
Simon Butcherc97b6972015-12-27 23:48:17 +00009923#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009924static int ssl_preset_default_hashes[] = {
9925#if defined(MBEDTLS_SHA512_C)
9926 MBEDTLS_MD_SHA512,
9927 MBEDTLS_MD_SHA384,
9928#endif
9929#if defined(MBEDTLS_SHA256_C)
9930 MBEDTLS_MD_SHA256,
9931 MBEDTLS_MD_SHA224,
9932#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009933#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009934 MBEDTLS_MD_SHA1,
9935#endif
9936 MBEDTLS_MD_NONE
9937};
Simon Butcherc97b6972015-12-27 23:48:17 +00009938#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009939
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009940static int ssl_preset_suiteb_ciphersuites[] = {
9941 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9942 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9943 0
9944};
9945
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009946#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009947static int ssl_preset_suiteb_hashes[] = {
9948 MBEDTLS_MD_SHA256,
9949 MBEDTLS_MD_SHA384,
9950 MBEDTLS_MD_NONE
9951};
9952#endif
9953
9954#if defined(MBEDTLS_ECP_C)
9955static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9956 MBEDTLS_ECP_DP_SECP256R1,
9957 MBEDTLS_ECP_DP_SECP384R1,
9958 MBEDTLS_ECP_DP_NONE
9959};
9960#endif
9961
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009962/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009963 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009964 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009965int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009966 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009967{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009968#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009969 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009970#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009971
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009972 /* Use the functions here so that they are covered in tests,
9973 * but otherwise access member directly for efficiency */
9974 mbedtls_ssl_conf_endpoint( conf, endpoint );
9975 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009976
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009977 /*
9978 * Things that are common to all presets
9979 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009980#if defined(MBEDTLS_SSL_CLI_C)
9981 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9982 {
9983 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9984#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9985 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9986#endif
9987 }
9988#endif
9989
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009990#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009991 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009992#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009993
9994#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9995 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9996#endif
9997
9998#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9999 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10000#endif
10001
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010002#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10003 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10004#endif
10005
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010006#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010007 conf->f_cookie_write = ssl_cookie_write_dummy;
10008 conf->f_cookie_check = ssl_cookie_check_dummy;
10009#endif
10010
10011#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10012 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10013#endif
10014
Janos Follath088ce432017-04-10 12:42:31 +010010015#if defined(MBEDTLS_SSL_SRV_C)
10016 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10017#endif
10018
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010019#if defined(MBEDTLS_SSL_PROTO_DTLS)
10020 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10021 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10022#endif
10023
10024#if defined(MBEDTLS_SSL_RENEGOTIATION)
10025 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010026 memset( conf->renego_period, 0x00, 2 );
10027 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010028#endif
10029
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010030#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10031 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10032 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010033 const unsigned char dhm_p[] =
10034 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10035 const unsigned char dhm_g[] =
10036 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10037
Hanno Beckera90658f2017-10-04 15:29:08 +010010038 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10039 dhm_p, sizeof( dhm_p ),
10040 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010041 {
10042 return( ret );
10043 }
10044 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010045#endif
10046
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010047 /*
10048 * Preset-specific defaults
10049 */
10050 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010051 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010052 /*
10053 * NSA Suite B
10054 */
10055 case MBEDTLS_SSL_PRESET_SUITEB:
10056 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10057 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10058 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10059 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10060
10061 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10062 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10063 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10064 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10065 ssl_preset_suiteb_ciphersuites;
10066
10067#if defined(MBEDTLS_X509_CRT_PARSE_C)
10068 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010069#endif
10070
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010071#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010072 conf->sig_hashes = ssl_preset_suiteb_hashes;
10073#endif
10074
10075#if defined(MBEDTLS_ECP_C)
10076 conf->curve_list = ssl_preset_suiteb_curves;
10077#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010078 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010079
10080 /*
10081 * Default
10082 */
10083 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010084 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10085 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10086 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10087 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10088 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10089 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10090 MBEDTLS_SSL_MIN_MINOR_VERSION :
10091 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010092 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10093 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10094
10095#if defined(MBEDTLS_SSL_PROTO_DTLS)
10096 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10097 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10098#endif
10099
10100 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10101 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10102 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10103 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10104 mbedtls_ssl_list_ciphersuites();
10105
10106#if defined(MBEDTLS_X509_CRT_PARSE_C)
10107 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10108#endif
10109
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010110#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010111 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010112#endif
10113
10114#if defined(MBEDTLS_ECP_C)
10115 conf->curve_list = mbedtls_ecp_grp_id_list();
10116#endif
10117
10118#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10119 conf->dhm_min_bitlen = 1024;
10120#endif
10121 }
10122
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010123 return( 0 );
10124}
10125
10126/*
10127 * Free mbedtls_ssl_config
10128 */
10129void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10130{
10131#if defined(MBEDTLS_DHM_C)
10132 mbedtls_mpi_free( &conf->dhm_P );
10133 mbedtls_mpi_free( &conf->dhm_G );
10134#endif
10135
10136#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10137 if( conf->psk != NULL )
10138 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010139 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010140 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010141 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010142 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010143 }
10144
10145 if( conf->psk_identity != NULL )
10146 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010147 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010148 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010149 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010150 conf->psk_identity_len = 0;
10151 }
10152#endif
10153
10154#if defined(MBEDTLS_X509_CRT_PARSE_C)
10155 ssl_key_cert_free( conf->key_cert );
10156#endif
10157
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010158 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010159}
10160
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010161#if defined(MBEDTLS_PK_C) && \
10162 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010163/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010164 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010166unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010167{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010168#if defined(MBEDTLS_RSA_C)
10169 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10170 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010171#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010172#if defined(MBEDTLS_ECDSA_C)
10173 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10174 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010175#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010176 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010177}
10178
Hanno Becker7e5437a2017-04-28 17:15:26 +010010179unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10180{
10181 switch( type ) {
10182 case MBEDTLS_PK_RSA:
10183 return( MBEDTLS_SSL_SIG_RSA );
10184 case MBEDTLS_PK_ECDSA:
10185 case MBEDTLS_PK_ECKEY:
10186 return( MBEDTLS_SSL_SIG_ECDSA );
10187 default:
10188 return( MBEDTLS_SSL_SIG_ANON );
10189 }
10190}
10191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010192mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010193{
10194 switch( sig )
10195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010196#if defined(MBEDTLS_RSA_C)
10197 case MBEDTLS_SSL_SIG_RSA:
10198 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010199#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010200#if defined(MBEDTLS_ECDSA_C)
10201 case MBEDTLS_SSL_SIG_ECDSA:
10202 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010203#endif
10204 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010205 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010206 }
10207}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010208#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010209
Hanno Becker7e5437a2017-04-28 17:15:26 +010010210#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10211 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10212
10213/* Find an entry in a signature-hash set matching a given hash algorithm. */
10214mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10215 mbedtls_pk_type_t sig_alg )
10216{
10217 switch( sig_alg )
10218 {
10219 case MBEDTLS_PK_RSA:
10220 return( set->rsa );
10221 case MBEDTLS_PK_ECDSA:
10222 return( set->ecdsa );
10223 default:
10224 return( MBEDTLS_MD_NONE );
10225 }
10226}
10227
10228/* Add a signature-hash-pair to a signature-hash set */
10229void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10230 mbedtls_pk_type_t sig_alg,
10231 mbedtls_md_type_t md_alg )
10232{
10233 switch( sig_alg )
10234 {
10235 case MBEDTLS_PK_RSA:
10236 if( set->rsa == MBEDTLS_MD_NONE )
10237 set->rsa = md_alg;
10238 break;
10239
10240 case MBEDTLS_PK_ECDSA:
10241 if( set->ecdsa == MBEDTLS_MD_NONE )
10242 set->ecdsa = md_alg;
10243 break;
10244
10245 default:
10246 break;
10247 }
10248}
10249
10250/* Allow exactly one hash algorithm for each signature. */
10251void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10252 mbedtls_md_type_t md_alg )
10253{
10254 set->rsa = md_alg;
10255 set->ecdsa = md_alg;
10256}
10257
10258#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10259 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10260
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010261/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010262 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010264mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010265{
10266 switch( hash )
10267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010268#if defined(MBEDTLS_MD5_C)
10269 case MBEDTLS_SSL_HASH_MD5:
10270 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010271#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010272#if defined(MBEDTLS_SHA1_C)
10273 case MBEDTLS_SSL_HASH_SHA1:
10274 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010275#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010276#if defined(MBEDTLS_SHA256_C)
10277 case MBEDTLS_SSL_HASH_SHA224:
10278 return( MBEDTLS_MD_SHA224 );
10279 case MBEDTLS_SSL_HASH_SHA256:
10280 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010281#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010282#if defined(MBEDTLS_SHA512_C)
10283 case MBEDTLS_SSL_HASH_SHA384:
10284 return( MBEDTLS_MD_SHA384 );
10285 case MBEDTLS_SSL_HASH_SHA512:
10286 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010287#endif
10288 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010289 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010290 }
10291}
10292
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010293/*
10294 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10295 */
10296unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10297{
10298 switch( md )
10299 {
10300#if defined(MBEDTLS_MD5_C)
10301 case MBEDTLS_MD_MD5:
10302 return( MBEDTLS_SSL_HASH_MD5 );
10303#endif
10304#if defined(MBEDTLS_SHA1_C)
10305 case MBEDTLS_MD_SHA1:
10306 return( MBEDTLS_SSL_HASH_SHA1 );
10307#endif
10308#if defined(MBEDTLS_SHA256_C)
10309 case MBEDTLS_MD_SHA224:
10310 return( MBEDTLS_SSL_HASH_SHA224 );
10311 case MBEDTLS_MD_SHA256:
10312 return( MBEDTLS_SSL_HASH_SHA256 );
10313#endif
10314#if defined(MBEDTLS_SHA512_C)
10315 case MBEDTLS_MD_SHA384:
10316 return( MBEDTLS_SSL_HASH_SHA384 );
10317 case MBEDTLS_MD_SHA512:
10318 return( MBEDTLS_SSL_HASH_SHA512 );
10319#endif
10320 default:
10321 return( MBEDTLS_SSL_HASH_NONE );
10322 }
10323}
10324
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010325#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010326/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010327 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010328 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010329 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010330int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010331{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010332 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010333
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010334 if( ssl->conf->curve_list == NULL )
10335 return( -1 );
10336
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010337 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010338 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010339 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010340
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010341 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010342}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010343#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010344
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010345#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010346/*
10347 * Check if a hash proposed by the peer is in our list.
10348 * Return 0 if we're willing to use it, -1 otherwise.
10349 */
10350int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10351 mbedtls_md_type_t md )
10352{
10353 const int *cur;
10354
10355 if( ssl->conf->sig_hashes == NULL )
10356 return( -1 );
10357
10358 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10359 if( *cur == (int) md )
10360 return( 0 );
10361
10362 return( -1 );
10363}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010364#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010366#if defined(MBEDTLS_X509_CRT_PARSE_C)
10367int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10368 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010369 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010370 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010371{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010372 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010373#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010374 int usage = 0;
10375#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010376#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010377 const char *ext_oid;
10378 size_t ext_len;
10379#endif
10380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010381#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10382 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010383 ((void) cert);
10384 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010385 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010386#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010388#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10389 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010390 {
10391 /* Server part of the key exchange */
10392 switch( ciphersuite->key_exchange )
10393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010394 case MBEDTLS_KEY_EXCHANGE_RSA:
10395 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010396 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010397 break;
10398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010399 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10400 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10401 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10402 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010403 break;
10404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010405 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10406 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010407 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010408 break;
10409
10410 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010411 case MBEDTLS_KEY_EXCHANGE_NONE:
10412 case MBEDTLS_KEY_EXCHANGE_PSK:
10413 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10414 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010415 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010416 usage = 0;
10417 }
10418 }
10419 else
10420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010421 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10422 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010423 }
10424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010425 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010426 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010427 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010428 ret = -1;
10429 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010430#else
10431 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010432#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010434#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10435 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010437 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10438 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010439 }
10440 else
10441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010442 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10443 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010444 }
10445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010446 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010447 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010448 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010449 ret = -1;
10450 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010451#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010452
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010453 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010455#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010456
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010457/*
10458 * Convert version numbers to/from wire format
10459 * and, for DTLS, to/from TLS equivalent.
10460 *
10461 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010462 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010463 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10464 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010466void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010467 unsigned char ver[2] )
10468{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010469#if defined(MBEDTLS_SSL_PROTO_DTLS)
10470 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010472 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010473 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10474
10475 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10476 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10477 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010478 else
10479#else
10480 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010481#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010482 {
10483 ver[0] = (unsigned char) major;
10484 ver[1] = (unsigned char) minor;
10485 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010486}
10487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010488void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010489 const unsigned char ver[2] )
10490{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010491#if defined(MBEDTLS_SSL_PROTO_DTLS)
10492 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010493 {
10494 *major = 255 - ver[0] + 2;
10495 *minor = 255 - ver[1] + 1;
10496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010497 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010498 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10499 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010500 else
10501#else
10502 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010503#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010504 {
10505 *major = ver[0];
10506 *minor = ver[1];
10507 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010508}
10509
Simon Butcher99000142016-10-13 17:21:01 +010010510int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10511{
10512#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10513 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10514 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10515
10516 switch( md )
10517 {
10518#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10519#if defined(MBEDTLS_MD5_C)
10520 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010521 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010522#endif
10523#if defined(MBEDTLS_SHA1_C)
10524 case MBEDTLS_SSL_HASH_SHA1:
10525 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10526 break;
10527#endif
10528#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10529#if defined(MBEDTLS_SHA512_C)
10530 case MBEDTLS_SSL_HASH_SHA384:
10531 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10532 break;
10533#endif
10534#if defined(MBEDTLS_SHA256_C)
10535 case MBEDTLS_SSL_HASH_SHA256:
10536 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10537 break;
10538#endif
10539 default:
10540 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10541 }
10542
10543 return 0;
10544#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10545 (void) ssl;
10546 (void) md;
10547
10548 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10549#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10550}
10551
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010552#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10553 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10554int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10555 unsigned char *output,
10556 unsigned char *data, size_t data_len )
10557{
10558 int ret = 0;
10559 mbedtls_md5_context mbedtls_md5;
10560 mbedtls_sha1_context mbedtls_sha1;
10561
10562 mbedtls_md5_init( &mbedtls_md5 );
10563 mbedtls_sha1_init( &mbedtls_sha1 );
10564
10565 /*
10566 * digitally-signed struct {
10567 * opaque md5_hash[16];
10568 * opaque sha_hash[20];
10569 * };
10570 *
10571 * md5_hash
10572 * MD5(ClientHello.random + ServerHello.random
10573 * + ServerParams);
10574 * sha_hash
10575 * SHA(ClientHello.random + ServerHello.random
10576 * + ServerParams);
10577 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010578 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010579 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010581 goto exit;
10582 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010583 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010584 ssl->handshake->randbytes, 64 ) ) != 0 )
10585 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010586 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010587 goto exit;
10588 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010589 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010590 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010591 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010592 goto exit;
10593 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010594 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010595 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010596 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010597 goto exit;
10598 }
10599
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010600 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010601 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010602 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010603 goto exit;
10604 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010605 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010606 ssl->handshake->randbytes, 64 ) ) != 0 )
10607 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010608 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010609 goto exit;
10610 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010611 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010612 data_len ) ) != 0 )
10613 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010614 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010615 goto exit;
10616 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010617 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010618 output + 16 ) ) != 0 )
10619 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010620 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010621 goto exit;
10622 }
10623
10624exit:
10625 mbedtls_md5_free( &mbedtls_md5 );
10626 mbedtls_sha1_free( &mbedtls_sha1 );
10627
10628 if( ret != 0 )
10629 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10630 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10631
10632 return( ret );
10633
10634}
10635#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10636 MBEDTLS_SSL_PROTO_TLS1_1 */
10637
10638#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10639 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010640
10641#if defined(MBEDTLS_USE_PSA_CRYPTO)
10642int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10643 unsigned char *hash, size_t *hashlen,
10644 unsigned char *data, size_t data_len,
10645 mbedtls_md_type_t md_alg )
10646{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010647 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010648 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010649 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10650
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010651 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010652
10653 if( ( status = psa_hash_setup( &hash_operation,
10654 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010655 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010656 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010657 goto exit;
10658 }
10659
Andrzej Kurek814feff2019-01-14 04:35:19 -050010660 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10661 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010662 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010663 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010664 goto exit;
10665 }
10666
Andrzej Kurek814feff2019-01-14 04:35:19 -050010667 if( ( status = psa_hash_update( &hash_operation,
10668 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010669 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010670 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010671 goto exit;
10672 }
10673
Andrzej Kurek814feff2019-01-14 04:35:19 -050010674 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10675 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010676 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010677 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010678 goto exit;
10679 }
10680
10681exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010682 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010683 {
10684 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10685 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010686 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010687 {
10688 case PSA_ERROR_NOT_SUPPORTED:
10689 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010690 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010691 case PSA_ERROR_BUFFER_TOO_SMALL:
10692 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10693 case PSA_ERROR_INSUFFICIENT_MEMORY:
10694 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10695 default:
10696 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10697 }
10698 }
10699 return( 0 );
10700}
10701
10702#else
10703
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010704int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010705 unsigned char *hash, size_t *hashlen,
10706 unsigned char *data, size_t data_len,
10707 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010708{
10709 int ret = 0;
10710 mbedtls_md_context_t ctx;
10711 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010712 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010713
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010714 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010715
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010716 mbedtls_md_init( &ctx );
10717
10718 /*
10719 * digitally-signed struct {
10720 * opaque client_random[32];
10721 * opaque server_random[32];
10722 * ServerDHParams params;
10723 * };
10724 */
10725 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10726 {
10727 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10728 goto exit;
10729 }
10730 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10731 {
10732 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10733 goto exit;
10734 }
10735 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10736 {
10737 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10738 goto exit;
10739 }
10740 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10741 {
10742 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10743 goto exit;
10744 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010745 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010746 {
10747 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10748 goto exit;
10749 }
10750
10751exit:
10752 mbedtls_md_free( &ctx );
10753
10754 if( ret != 0 )
10755 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10756 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10757
10758 return( ret );
10759}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010760#endif /* MBEDTLS_USE_PSA_CRYPTO */
10761
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010762#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10763 MBEDTLS_SSL_PROTO_TLS1_2 */
10764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010765#endif /* MBEDTLS_SSL_TLS_C */