blob: e1415a8935895ce2d150591996bfa19ed81c4c4e [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;
Ron Eldor3b350852019-05-07 18:31:49 +0300428 unsigned char *tmp;
429 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 const mbedtls_md_info_t *md_info;
432 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100433 int ret;
434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000436
Ron Eldor3b350852019-05-07 18:31:49 +0300437 tmp_len = 20 + strlen( label ) + rlen;
438 tmp = mbedtls_calloc( 1, tmp_len );
439 if( tmp == NULL )
440 {
441 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
442 goto exit;
443 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000444
445 hs = ( slen + 1 ) / 2;
446 S1 = secret;
447 S2 = secret + slen - hs;
448
449 nb = strlen( label );
450 memcpy( tmp + 20, label, nb );
451 memcpy( tmp + 20 + nb, random, rlen );
452 nb += rlen;
453
454 /*
455 * First compute P_md5(secret,label+random)[0..dlen]
456 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300458 {
459 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
460 goto exit;
461 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300464 {
465 goto exit;
466 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
469 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
470 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000471
472 for( i = 0; i < dlen; i += 16 )
473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_md_hmac_reset ( &md_ctx );
475 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
476 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_md_hmac_reset ( &md_ctx );
479 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
480 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481
482 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
483
484 for( j = 0; j < k; j++ )
485 dstbuf[i + j] = h_i[j];
486 }
487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100489
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 /*
491 * XOR out with P_sha1(secret,label+random)[0..dlen]
492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300494 {
495 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
496 goto exit;
497 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300500 {
501 goto exit;
502 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
505 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
506 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
508 for( i = 0; i < dlen; i += 20 )
509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_md_hmac_reset ( &md_ctx );
511 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
512 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_md_hmac_reset ( &md_ctx );
515 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
516 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
518 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
519
520 for( j = 0; j < k; j++ )
521 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
522 }
523
Ron Eldor3b350852019-05-07 18:31:49 +0300524exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100526
Ron Eldor3b350852019-05-07 18:31:49 +0300527 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500528 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000529
Ron Eldor3b350852019-05-07 18:31:49 +0300530 mbedtls_free( tmp );
531 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000532}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500536#if defined(MBEDTLS_USE_PSA_CRYPTO)
537static int tls_prf_generic( mbedtls_md_type_t md_type,
538 const unsigned char *secret, size_t slen,
539 const char *label,
540 const unsigned char *random, size_t rlen,
541 unsigned char *dstbuf, size_t dlen )
542{
543 psa_status_t status;
544 psa_algorithm_t alg;
545 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500546 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500547 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
548
Andrzej Kurek2f760752019-01-28 08:08:15 -0500549 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500550 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500551
Andrzej Kurekc929a822019-01-14 03:51:11 -0500552 if( md_type == MBEDTLS_MD_SHA384 )
553 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
554 else
555 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
556
Andrzej Kurek2f760752019-01-28 08:08:15 -0500557 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500558 psa_key_policy_set_usage( &policy,
559 PSA_KEY_USAGE_DERIVE,
560 alg );
561 status = psa_set_key_policy( master_slot, &policy );
562 if( status != PSA_SUCCESS )
563 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
564
565 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
566 if( status != PSA_SUCCESS )
567 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
568
569 status = psa_key_derivation( &generator,
570 master_slot, alg,
571 random, rlen,
572 (unsigned char const *) label,
573 (size_t) strlen( label ),
574 dlen );
575 if( status != PSA_SUCCESS )
576 {
577 psa_generator_abort( &generator );
578 psa_destroy_key( master_slot );
579 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
580 }
581
582 status = psa_generator_read( &generator, dstbuf, dlen );
583 if( status != PSA_SUCCESS )
584 {
585 psa_generator_abort( &generator );
586 psa_destroy_key( master_slot );
587 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
588 }
589
590 status = psa_generator_abort( &generator );
591 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500592 {
593 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500594 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500595 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500596
597 status = psa_destroy_key( master_slot );
598 if( status != PSA_SUCCESS )
599 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
600
Andrzej Kurek33171262019-01-15 03:25:18 -0500601 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500602}
603
604#else /* MBEDTLS_USE_PSA_CRYPTO */
605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100607 const unsigned char *secret, size_t slen,
608 const char *label,
609 const unsigned char *random, size_t rlen,
610 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000611{
612 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100613 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300614 unsigned char *tmp;
615 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
617 const mbedtls_md_info_t *md_info;
618 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100619 int ret;
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
624 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100627
Ron Eldor3b350852019-05-07 18:31:49 +0300628 tmp_len = md_len + strlen( label ) + rlen;
629 tmp = mbedtls_calloc( 1, tmp_len );
630 if( tmp == NULL )
631 {
632 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
633 goto exit;
634 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000635
636 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100637 memcpy( tmp + md_len, label, nb );
638 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000639 nb += rlen;
640
641 /*
642 * Compute P_<hash>(secret, label + random)[0..dlen]
643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300645 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
648 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
649 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100650
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100651 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_md_hmac_reset ( &md_ctx );
654 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
655 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_md_hmac_reset ( &md_ctx );
658 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
659 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000660
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100661 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000662
663 for( j = 0; j < k; j++ )
664 dstbuf[i + j] = h_i[j];
665 }
666
Ron Eldor3b350852019-05-07 18:31:49 +0300667exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100669
Ron Eldor3b350852019-05-07 18:31:49 +0300670 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500671 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000672
Ron Eldor3b350852019-05-07 18:31:49 +0300673 mbedtls_free( tmp );
674
675 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000676}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500677#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100679static int tls_prf_sha256( const unsigned char *secret, size_t slen,
680 const char *label,
681 const unsigned char *random, size_t rlen,
682 unsigned char *dstbuf, size_t dlen )
683{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100685 label, random, rlen, dstbuf, dlen ) );
686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200690static int tls_prf_sha384( const unsigned char *secret, size_t slen,
691 const char *label,
692 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000693 unsigned char *dstbuf, size_t dlen )
694{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100696 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698#endif /* MBEDTLS_SHA512_C */
699#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
704 defined(MBEDTLS_SSL_PROTO_TLS1_1)
705static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200706#endif
Paul Bakker380da532012-04-18 16:10:25 +0000707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708#if defined(MBEDTLS_SSL_PROTO_SSL3)
709static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
710static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200711#endif
712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
714static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
715static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200716#endif
717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
719#if defined(MBEDTLS_SHA256_C)
720static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
721static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
722static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200723#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725#if defined(MBEDTLS_SHA512_C)
726static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
727static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
728static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100729#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000731
Hanno Becker7d0a5692018-10-23 15:26:22 +0100732#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
733 defined(MBEDTLS_USE_PSA_CRYPTO)
734static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
735{
736 if( ssl->conf->f_psk != NULL )
737 {
738 /* If we've used a callback to select the PSK,
739 * the static configuration is irrelevant. */
740 if( ssl->handshake->psk_opaque != 0 )
741 return( 1 );
742
743 return( 0 );
744 }
745
746 if( ssl->conf->psk_opaque != 0 )
747 return( 1 );
748
749 return( 0 );
750}
751#endif /* MBEDTLS_USE_PSA_CRYPTO &&
752 MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000755{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200756 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000757#if defined(MBEDTLS_USE_PSA_CRYPTO)
758 int psa_fallthrough;
759#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000760 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000761 unsigned char keyblk[256];
762 unsigned char *key1;
763 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100764 unsigned char *mac_enc;
765 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000766 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200767 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000768 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000769 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 const mbedtls_cipher_info_t *cipher_info;
771 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100772
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000773 /* cf. RFC 5246, Section 8.1:
774 * "The master secret is always exactly 48 bytes in length." */
775 size_t const master_secret_len = 48;
776
Hanno Becker35b23c72018-10-23 12:10:41 +0100777#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
778 unsigned char session_hash[48];
779#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 mbedtls_ssl_session *session = ssl->session_negotiate;
782 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
783 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000786
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000787#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
788 transform->encrypt_then_mac = session->encrypt_then_mac;
789#endif
790 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000791
792 ciphersuite_info = handshake->ciphersuite_info;
793 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100794 if( cipher_info == NULL )
795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000797 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100799 }
800
Hanno Beckere694c3e2017-12-27 21:34:08 +0000801 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100802 if( md_info == NULL )
803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000805 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100807 }
808
Paul Bakker5121ce52009-01-03 21:22:43 +0000809 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000810 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812#if defined(MBEDTLS_SSL_PROTO_SSL3)
813 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000814 {
Paul Bakker48916f92012-09-16 19:57:18 +0000815 handshake->tls_prf = ssl3_prf;
816 handshake->calc_verify = ssl_calc_verify_ssl;
817 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000818 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200819 else
820#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
822 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000823 {
Paul Bakker48916f92012-09-16 19:57:18 +0000824 handshake->tls_prf = tls1_prf;
825 handshake->calc_verify = ssl_calc_verify_tls;
826 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000827 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200828 else
829#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
831#if defined(MBEDTLS_SHA512_C)
832 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +0000833 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000834 {
Paul Bakker48916f92012-09-16 19:57:18 +0000835 handshake->tls_prf = tls_prf_sha384;
836 handshake->calc_verify = ssl_calc_verify_tls_sha384;
837 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000838 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000839 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200840#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#if defined(MBEDTLS_SHA256_C)
842 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000843 {
Paul Bakker48916f92012-09-16 19:57:18 +0000844 handshake->tls_prf = tls_prf_sha256;
845 handshake->calc_verify = ssl_calc_verify_tls_sha256;
846 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000847 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200848 else
849#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
853 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200854 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000855
856 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 * SSLv3:
858 * master =
859 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
860 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
861 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200862 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200863 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000864 * master = PRF( premaster, "master secret", randbytes )[0..47]
865 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100866 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000867 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100868 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
869 }
870 else
871 {
872 /* The label for the KDF used for key expansion.
873 * This is either "master secret" or "extended master secret"
874 * depending on whether the Extended Master Secret extension
875 * is used. */
876 char const *lbl = "master secret";
877
878 /* The salt for the KDF used for key expansion.
879 * - If the Extended Master Secret extension is not used,
880 * this is ClientHello.Random + ServerHello.Random
881 * (see Sect. 8.1 in RFC 5246).
882 * - If the Extended Master Secret extension is used,
883 * this is the transcript of the handshake so far.
884 * (see Sect. 4 in RFC 7627). */
885 unsigned char const *salt = handshake->randbytes;
886 size_t salt_len = 64;
887
888#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200892
Hanno Becker35b23c72018-10-23 12:10:41 +0100893 lbl = "extended master secret";
894 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200895 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
897 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +0000900 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
901 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100902 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000903 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200904 else
Hanno Becker35b23c72018-10-23 12:10:41 +0100905#endif /* MBEDTLS_SHA512_C */
906 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200907 }
908 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100910 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200911
Hanno Becker35b23c72018-10-23 12:10:41 +0100912 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200913 }
Hanno Becker35b23c72018-10-23 12:10:41 +0100914#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
915
Hanno Becker7d0a5692018-10-23 15:26:22 +0100916#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
917 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
918 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
919 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
920 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100921 {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100922 /* Perform PSK-to-MS expansion in a single step. */
923 psa_status_t status;
924 psa_algorithm_t alg;
925 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500926 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100927
928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
929
930 psk = ssl->conf->psk_opaque;
931 if( ssl->handshake->psk_opaque != 0 )
932 psk = ssl->handshake->psk_opaque;
933
Hanno Becker22bf1452019-04-05 11:21:08 +0100934 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +0100935 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
936 else
937 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
938
939 status = psa_key_derivation( &generator, psk, alg,
940 salt, salt_len,
941 (unsigned char const *) lbl,
942 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000943 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100944 if( status != PSA_SUCCESS )
945 {
946 psa_generator_abort( &generator );
947 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
948 }
949
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000950 status = psa_generator_read( &generator, session->master,
951 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100952 if( status != PSA_SUCCESS )
953 {
954 psa_generator_abort( &generator );
955 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
956 }
957
958 status = psa_generator_abort( &generator );
959 if( status != PSA_SUCCESS )
960 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100961 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100962 else
963#endif
964 {
965 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
966 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000967 session->master,
968 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100969 if( ret != 0 )
970 {
971 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
972 return( ret );
973 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200974
Hanno Becker7d0a5692018-10-23 15:26:22 +0100975 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
976 handshake->premaster,
977 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +0100978
Hanno Becker7d0a5692018-10-23 15:26:22 +0100979 mbedtls_platform_zeroize( handshake->premaster,
980 sizeof(handshake->premaster) );
981 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000982 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000983
984 /*
985 * Swap the client and server random values.
986 */
Paul Bakker48916f92012-09-16 19:57:18 +0000987 memcpy( tmp, handshake->randbytes, 64 );
988 memcpy( handshake->randbytes, tmp + 32, 32 );
989 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500990 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000991
992 /*
993 * SSLv3:
994 * key block =
995 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
996 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
997 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
998 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
999 * ...
1000 *
1001 * TLSv1:
1002 * key block = PRF( master, "key expansion", randbytes )
1003 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001004 ret = handshake->tls_prf( session->master, 48, "key expansion",
1005 handshake->randbytes, 64, keyblk, 256 );
1006 if( ret != 0 )
1007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001009 return( ret );
1010 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1013 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1014 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1015 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1016 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 /*
1019 * Determine the appropriate key, IV and MAC length.
1020 */
Paul Bakker68884e32013-01-07 18:20:04 +01001021
Hanno Becker88aaf652017-12-27 08:17:40 +00001022 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001023
Hanno Becker8031d062018-01-03 15:32:31 +00001024#if defined(MBEDTLS_GCM_C) || \
1025 defined(MBEDTLS_CCM_C) || \
1026 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001028 cipher_info->mode == MBEDTLS_MODE_CCM ||
1029 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001031 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001032
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001033 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001034 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001035 transform->taglen =
1036 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001037
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001038 /* All modes haves 96-bit IVs;
1039 * GCM and CCM has 4 implicit and 8 explicit bytes
1040 * ChachaPoly has all 12 bytes implicit
1041 */
Paul Bakker68884e32013-01-07 18:20:04 +01001042 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001043 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1044 transform->fixed_ivlen = 12;
1045 else
1046 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001047
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001048 /* Minimum length of encrypted record */
1049 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001050 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001051 }
1052 else
Hanno Becker8031d062018-01-03 15:32:31 +00001053#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1054#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1055 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1056 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001057 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001058 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1060 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001063 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001064 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001065
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001066 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001067 mac_key_len = mbedtls_md_get_size( md_info );
1068 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001071 /*
1072 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1073 * (rfc 6066 page 13 or rfc 2104 section 4),
1074 * so we only need to adjust the length here.
1075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001079
1080#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1081 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001082 * HMAC implementation which also truncates the key
1083 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001084 mac_key_len = transform->maclen;
1085#endif
1086 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001088
1089 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001090 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001091
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001092 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001094 transform->minlen = transform->maclen;
1095 else
Paul Bakker68884e32013-01-07 18:20:04 +01001096 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001097 /*
1098 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001099 * 1. if EtM is in use: one block plus MAC
1100 * otherwise: * first multiple of blocklen greater than maclen
1101 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1104 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001105 {
1106 transform->minlen = transform->maclen
1107 + cipher_info->block_size;
1108 }
1109 else
1110#endif
1111 {
1112 transform->minlen = transform->maclen
1113 + cipher_info->block_size
1114 - transform->maclen % cipher_info->block_size;
1115 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1118 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1119 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001120 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001121 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001122#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1124 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1125 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001126 {
1127 transform->minlen += transform->ivlen;
1128 }
1129 else
1130#endif
1131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001133 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1134 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001135 }
Paul Bakker68884e32013-01-07 18:20:04 +01001136 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001137 }
Hanno Becker8031d062018-01-03 15:32:31 +00001138 else
1139#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1140 {
1141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1142 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1143 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001144
Hanno Becker88aaf652017-12-27 08:17:40 +00001145 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1146 (unsigned) keylen,
1147 (unsigned) transform->minlen,
1148 (unsigned) transform->ivlen,
1149 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001150
1151 /*
1152 * Finally setup the cipher contexts, IVs and MAC secrets.
1153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001155 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001156 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001157 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001158 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001159
Paul Bakker68884e32013-01-07 18:20:04 +01001160 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001161 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001162
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001163 /*
1164 * This is not used in TLS v1.1.
1165 */
Paul Bakker48916f92012-09-16 19:57:18 +00001166 iv_copy_len = ( transform->fixed_ivlen ) ?
1167 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001168 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1169 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001170 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001171 }
1172 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173#endif /* MBEDTLS_SSL_CLI_C */
1174#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001175 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001176 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001177 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001178 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001179
Hanno Becker81c7b182017-11-09 18:39:33 +00001180 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001181 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001182
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001183 /*
1184 * This is not used in TLS v1.1.
1185 */
Paul Bakker48916f92012-09-16 19:57:18 +00001186 iv_copy_len = ( transform->fixed_ivlen ) ?
1187 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001188 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1189 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001190 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001191 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001192 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001196 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1197 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001198 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001199
Hanno Beckerd56ed242018-01-03 15:32:51 +00001200#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201#if defined(MBEDTLS_SSL_PROTO_SSL3)
1202 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001203 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001204 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001207 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1208 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001209 }
1210
Hanno Becker81c7b182017-11-09 18:39:33 +00001211 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1212 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001213 }
1214 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1216#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1217 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1218 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001219 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001220 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1221 For AEAD-based ciphersuites, there is nothing to do here. */
1222 if( mac_key_len != 0 )
1223 {
1224 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1225 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1226 }
Paul Bakker68884e32013-01-07 18:20:04 +01001227 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001228 else
1229#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001232 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1233 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001234 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001235#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1238 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001239 {
1240 int ret = 0;
1241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001243
Hanno Becker88aaf652017-12-27 08:17:40 +00001244 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001245 transform->iv_enc, transform->iv_dec,
1246 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001247 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001248 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001251 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1252 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001253 }
1254 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001255#else
1256 ((void) mac_dec);
1257 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001259
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001260#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1261 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001262 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001263 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1264 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001265 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001266 iv_copy_len );
1267 }
1268#endif
1269
Hanno Beckerf704bef2018-11-16 15:21:18 +00001270#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001271
1272 /* Only use PSA-based ciphers for TLS-1.2.
1273 * That's relevant at least for TLS-1.0, where
1274 * we assume that mbedtls_cipher_crypt() updates
1275 * the structure field for the IV, which the PSA-based
1276 * implementation currently doesn't. */
1277#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1278 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001279 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001280 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001281 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001282 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1283 {
1284 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001285 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001286 }
1287
1288 if( ret == 0 )
1289 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001290 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001291 psa_fallthrough = 0;
1292 }
1293 else
1294 {
1295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1296 psa_fallthrough = 1;
1297 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001298 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001299 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001300 psa_fallthrough = 1;
1301#else
1302 psa_fallthrough = 1;
1303#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001304
Hanno Beckercb1cc802018-11-17 22:27:38 +00001305 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001306#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001307 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001308 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001310 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001311 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001312 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001313
Hanno Beckerf704bef2018-11-16 15:21:18 +00001314#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001315 /* Only use PSA-based ciphers for TLS-1.2.
1316 * That's relevant at least for TLS-1.0, where
1317 * we assume that mbedtls_cipher_crypt() updates
1318 * the structure field for the IV, which the PSA-based
1319 * implementation currently doesn't. */
1320#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1321 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001322 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001323 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001324 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001325 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1326 {
1327 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001328 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001329 }
1330
1331 if( ret == 0 )
1332 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001333 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001334 psa_fallthrough = 0;
1335 }
1336 else
1337 {
1338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1339 psa_fallthrough = 1;
1340 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001341 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001342 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001343 psa_fallthrough = 1;
1344#else
1345 psa_fallthrough = 1;
1346#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001347
Hanno Beckercb1cc802018-11-17 22:27:38 +00001348 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001349#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001350 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001351 cipher_info ) ) != 0 )
1352 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001353 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001354 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001355 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001358 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001362 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001363 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001366 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001370 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001371 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373#if defined(MBEDTLS_CIPHER_MODE_CBC)
1374 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1377 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001380 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001381 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1384 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001387 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001388 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001389 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001391
Paul Bakker5121ce52009-01-03 21:22:43 +00001392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001394 // Initialize compression
1395 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001397 {
Paul Bakker16770332013-10-11 09:59:44 +02001398 if( ssl->compress_buf == NULL )
1399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001401 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001402 if( ssl->compress_buf == NULL )
1403 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001405 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001406 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1407 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001408 }
1409 }
1410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001412
Paul Bakker48916f92012-09-16 19:57:18 +00001413 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1414 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001415
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001416 if( deflateInit( &transform->ctx_deflate,
1417 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001418 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001421 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1422 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001423 }
1424 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001428end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001429 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1430 mbedtls_platform_zeroize( handshake->randbytes,
1431 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001432 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001433}
1434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#if defined(MBEDTLS_SSL_PROTO_SSL3)
1436void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001437{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001438 mbedtls_md5_context md5;
1439 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 unsigned char pad_1[48];
1441 unsigned char pad_2[48];
1442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001444
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001445 mbedtls_md5_init( &md5 );
1446 mbedtls_sha1_init( &sha1 );
1447
1448 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1449 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001450
Paul Bakker380da532012-04-18 16:10:25 +00001451 memset( pad_1, 0x36, 48 );
1452 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001453
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001454 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1455 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1456 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001457
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001458 mbedtls_md5_starts_ret( &md5 );
1459 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1460 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1461 mbedtls_md5_update_ret( &md5, hash, 16 );
1462 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001463
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001464 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1465 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1466 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001467
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001468 mbedtls_sha1_starts_ret( &sha1 );
1469 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1470 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1471 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1472 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001476
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001477 mbedtls_md5_free( &md5 );
1478 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001479
Paul Bakker380da532012-04-18 16:10:25 +00001480 return;
1481}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1485void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001486{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001487 mbedtls_md5_context md5;
1488 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001491
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001492 mbedtls_md5_init( &md5 );
1493 mbedtls_sha1_init( &sha1 );
1494
1495 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1496 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001497
Andrzej Kurekeb342242019-01-29 09:14:33 -05001498 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001499 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001503
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001504 mbedtls_md5_free( &md5 );
1505 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001506
Paul Bakker380da532012-04-18 16:10:25 +00001507 return;
1508}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1512#if defined(MBEDTLS_SHA256_C)
1513void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001514{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001515#if defined(MBEDTLS_USE_PSA_CRYPTO)
1516 size_t hash_size;
1517 psa_status_t status;
1518 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1519
1520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1521 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1522 if( status != PSA_SUCCESS )
1523 {
1524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1525 return;
1526 }
1527
1528 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1529 if( status != PSA_SUCCESS )
1530 {
1531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1532 return;
1533 }
1534 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1536#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001537 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001538
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001539 mbedtls_sha256_init( &sha256 );
1540
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001542
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001543 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001544 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001548
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001549 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001550#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001551 return;
1552}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555#if defined(MBEDTLS_SHA512_C)
1556void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001557{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001558#if defined(MBEDTLS_USE_PSA_CRYPTO)
1559 size_t hash_size;
1560 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001561 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001562
1563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001564 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001565 if( status != PSA_SUCCESS )
1566 {
1567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1568 return;
1569 }
1570
Andrzej Kurek972fba52019-01-30 03:29:12 -05001571 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001572 if( status != PSA_SUCCESS )
1573 {
1574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1575 return;
1576 }
1577 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1579#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001580 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001581
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001582 mbedtls_sha512_init( &sha512 );
1583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001585
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001586 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001587 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001591
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001592 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001593#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001594 return;
1595}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596#endif /* MBEDTLS_SHA512_C */
1597#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1600int 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 +02001601{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001602 unsigned char *p = ssl->handshake->premaster;
1603 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001604 const unsigned char *psk = ssl->conf->psk;
1605 size_t psk_len = ssl->conf->psk_len;
1606
1607 /* If the psk callback was called, use its result */
1608 if( ssl->handshake->psk != NULL )
1609 {
1610 psk = ssl->handshake->psk;
1611 psk_len = ssl->handshake->psk_len;
1612 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001613
1614 /*
1615 * PMS = struct {
1616 * opaque other_secret<0..2^16-1>;
1617 * opaque psk<0..2^16-1>;
1618 * };
1619 * with "other_secret" depending on the particular key exchange
1620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1622 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001623 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001624 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001626
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001627 *(p++) = (unsigned char)( psk_len >> 8 );
1628 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001629
1630 if( end < p || (size_t)( end - p ) < psk_len )
1631 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1632
1633 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001634 p += psk_len;
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_PSK_ENABLED */
1638#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1639 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001640 {
1641 /*
1642 * other_secret already set by the ClientKeyExchange message,
1643 * and is 48 bytes long
1644 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001645 if( end - p < 2 )
1646 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1647
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001648 *p++ = 0;
1649 *p++ = 48;
1650 p += 48;
1651 }
1652 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1654#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1655 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001656 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001657 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001658 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001659
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001660 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001662 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001663 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001666 return( ret );
1667 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001668 *(p++) = (unsigned char)( len >> 8 );
1669 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001670 p += len;
1671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001673 }
1674 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1676#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1677 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001678 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001679 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001680 size_t zlen;
1681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001683 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001684 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001687 return( ret );
1688 }
1689
1690 *(p++) = (unsigned char)( zlen >> 8 );
1691 *(p++) = (unsigned char)( zlen );
1692 p += zlen;
1693
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001694 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1695 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001696 }
1697 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1701 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001702 }
1703
1704 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001705 if( end - p < 2 )
1706 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001707
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001708 *(p++) = (unsigned char)( psk_len >> 8 );
1709 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001710
1711 if( end < p || (size_t)( end - p ) < psk_len )
1712 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1713
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001714 memcpy( p, psk, psk_len );
1715 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001716
1717 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1718
1719 return( 0 );
1720}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001724/*
1725 * SSLv3.0 MAC functions
1726 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001727#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001728static void ssl_mac( mbedtls_md_context_t *md_ctx,
1729 const unsigned char *secret,
1730 const unsigned char *buf, size_t len,
1731 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001732 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001733{
1734 unsigned char header[11];
1735 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001736 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1738 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001739
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001740 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001742 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001743 else
Paul Bakker68884e32013-01-07 18:20:04 +01001744 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001745
1746 memcpy( header, ctr, 8 );
1747 header[ 8] = (unsigned char) type;
1748 header[ 9] = (unsigned char)( len >> 8 );
1749 header[10] = (unsigned char)( len );
1750
Paul Bakker68884e32013-01-07 18:20:04 +01001751 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 mbedtls_md_starts( md_ctx );
1753 mbedtls_md_update( md_ctx, secret, md_size );
1754 mbedtls_md_update( md_ctx, padding, padlen );
1755 mbedtls_md_update( md_ctx, header, 11 );
1756 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001757 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001758
Paul Bakker68884e32013-01-07 18:20:04 +01001759 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 mbedtls_md_starts( md_ctx );
1761 mbedtls_md_update( md_ctx, secret, md_size );
1762 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001763 mbedtls_md_update( md_ctx, out, md_size );
1764 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001765}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001767
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001768/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001769 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001770#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001771 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1772 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1773 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1774/* This function makes sure every byte in the memory region is accessed
1775 * (in ascending addresses order) */
1776static void ssl_read_memory( unsigned char *p, size_t len )
1777{
1778 unsigned char acc = 0;
1779 volatile unsigned char force;
1780
1781 for( ; len != 0; p++, len-- )
1782 acc ^= *p;
1783
1784 force = acc;
1785 (void) force;
1786}
1787#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1788
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001789/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001790 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001791 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001792
1793static void ssl_extract_add_data_from_record( unsigned char* add_data,
1794 mbedtls_record *rec )
1795{
1796 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1797 add_data[8] = rec->type;
1798 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
1799 add_data[11] = ( rec->data_len >> 8 ) & 0xFF;
1800 add_data[12] = rec->data_len & 0xFF;
1801}
1802
Hanno Beckera18d1322018-01-03 14:27:32 +00001803int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1804 mbedtls_ssl_transform *transform,
1805 mbedtls_record *rec,
1806 int (*f_rng)(void *, unsigned char *, size_t),
1807 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001808{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001810 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001811 unsigned char * data;
1812 unsigned char add_data[13];
1813 size_t post_avail;
1814
1815 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00001816#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001817 ((void) ssl);
1818#endif
1819
1820 /* The PRNG is used for dynamic IV generation that's used
1821 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1822#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1823 ( defined(MBEDTLS_AES_C) || \
1824 defined(MBEDTLS_ARIA_C) || \
1825 defined(MBEDTLS_CAMELLIA_C) ) && \
1826 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1827 ((void) f_rng);
1828 ((void) p_rng);
1829#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001832
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001833 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001834 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1836 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1837 }
1838 if( rec == NULL ||
1839 rec->buf == NULL ||
1840 rec->buf_len < rec->data_offset ||
1841 rec->buf_len - rec->data_offset < rec->data_len )
1842 {
1843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001845 }
1846
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001847 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1848 data = rec->buf + rec->data_offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001850 data, rec->data_len );
1851
1852 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1853
1854 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1855 {
1856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1857 (unsigned) rec->data_len,
1858 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1859 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1860 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001861
Paul Bakker5121ce52009-01-03 21:22:43 +00001862 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001863 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001864 */
Hanno Becker52344c22018-01-03 15:24:20 +00001865#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 if( mode == MBEDTLS_MODE_STREAM ||
1867 ( mode == MBEDTLS_MODE_CBC
1868#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001869 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001870#endif
1871 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001872 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001873 if( post_avail < transform->maclen )
1874 {
1875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1876 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1877 }
1878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001880 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001881 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001882 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001883 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1884 data, rec->data_len, rec->ctr, rec->type, mac );
1885 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001886 }
1887 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001888#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1890 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001891 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001892 {
Hanno Becker992b6872017-11-09 18:57:39 +00001893 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1894
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001895 ssl_extract_add_data_from_record( add_data, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001896
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001897 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
1898 sizeof( add_data ) );
1899 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1900 data, rec->data_len );
1901 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1902 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1903
1904 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001905 }
1906 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001907#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1910 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001911 }
1912
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001913 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1914 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001915
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001916 rec->data_len += transform->maclen;
1917 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001918 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001919 }
Hanno Becker52344c22018-01-03 15:24:20 +00001920#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001921
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001922 /*
1923 * Encrypt
1924 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1926 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001928 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001929 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001931 "including %d bytes of padding",
1932 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001933
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001934 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1935 transform->iv_enc, transform->ivlen,
1936 data, rec->data_len,
1937 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001940 return( ret );
1941 }
1942
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001943 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1946 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001947 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001948 }
Paul Bakker68884e32013-01-07 18:20:04 +01001949 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001951
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001952#if defined(MBEDTLS_GCM_C) || \
1953 defined(MBEDTLS_CCM_C) || \
1954 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001956 mode == MBEDTLS_MODE_CCM ||
1957 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001958 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001959 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001960 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001961 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001962
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001963 /* Check that there's space for both the authentication tag
1964 * and the explicit IV before and after the record content. */
1965 if( post_avail < transform->taglen ||
1966 rec->data_offset < explicit_iv_len )
1967 {
1968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1969 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1970 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001971
Paul Bakker68884e32013-01-07 18:20:04 +01001972 /*
1973 * Generate IV
1974 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001975 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1976 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001977 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001978 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001979 memcpy( iv + transform->fixed_ivlen, rec->ctr,
1980 explicit_iv_len );
1981 /* Prefix record content with explicit IV. */
1982 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001983 }
1984 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1985 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001986 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001987 unsigned char i;
1988
1989 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1990
1991 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001992 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001993 }
1994 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001995 {
1996 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1998 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001999 }
2000
Hanno Becker1f10d762019-04-26 13:34:37 +01002001 ssl_extract_add_data_from_record( add_data, rec );
2002
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002003 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2004 iv, transform->ivlen );
2005 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002006 data - explicit_iv_len, explicit_iv_len );
2007 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2008 add_data, 13 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002010 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002011 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002012
Paul Bakker68884e32013-01-07 18:20:04 +01002013 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002014 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002015 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002016
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002017 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002018 iv, transform->ivlen,
2019 add_data, 13, /* add data */
2020 data, rec->data_len, /* source */
2021 data, &rec->data_len, /* destination */
2022 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002025 return( ret );
2026 }
2027
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002028 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2029 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002030
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002031 rec->data_len += transform->taglen + explicit_iv_len;
2032 rec->data_offset -= explicit_iv_len;
2033 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002034 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002035 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002036 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2038#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002039 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002042 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002043 size_t padlen, i;
2044 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002045
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002046 /* Currently we're always using minimal padding
2047 * (up to 255 bytes would be allowed). */
2048 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2049 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002050 padlen = 0;
2051
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002052 /* Check there's enough space in the buffer for the padding. */
2053 if( post_avail < padlen + 1 )
2054 {
2055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2056 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2057 }
2058
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002060 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002061
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002062 rec->data_len += padlen + 1;
2063 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002066 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002067 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2068 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002069 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002070 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002071 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002072 if( f_rng == NULL )
2073 {
2074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2075 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2076 }
2077
2078 if( rec->data_offset < transform->ivlen )
2079 {
2080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2081 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2082 }
2083
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002084 /*
2085 * Generate IV
2086 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002087 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002088 if( ret != 0 )
2089 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002090
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002091 memcpy( data - transform->ivlen, transform->iv_enc,
2092 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002093
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002094 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002098 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002099 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002100 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002101
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002102 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2103 transform->iv_enc,
2104 transform->ivlen,
2105 data, rec->data_len,
2106 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002109 return( ret );
2110 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002111
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002112 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2115 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002116 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002119 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002120 {
2121 /*
2122 * Save IV in SSL3 and TLS1
2123 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002124 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2125 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002127 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002128#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002129 {
2130 data -= transform->ivlen;
2131 rec->data_offset -= transform->ivlen;
2132 rec->data_len += transform->ivlen;
2133 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002136 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002137 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002138 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2139
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002140 /*
2141 * MAC(MAC_write_key, seq_num +
2142 * TLSCipherText.type +
2143 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002144 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002145 * IV + // except for TLS 1.0
2146 * ENC(content + padding + padding_length));
2147 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002148
2149 if( post_avail < transform->maclen)
2150 {
2151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2152 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2153 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002154
Hanno Becker1f10d762019-04-26 13:34:37 +01002155 ssl_extract_add_data_from_record( add_data, rec );
2156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002158 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2159 sizeof( add_data ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002160
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002161 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2162 sizeof( add_data ) );
2163 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2164 data, rec->data_len );
2165 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2166 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002167
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002168 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002169
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002170 rec->data_len += transform->maclen;
2171 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002172 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002173 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002175 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002176 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002178 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2181 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002182 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002183
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002184 /* Make extra sure authentication was performed, exactly once */
2185 if( auth_done != 1 )
2186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2188 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002189 }
2190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002192
2193 return( 0 );
2194}
2195
Hanno Beckera18d1322018-01-03 14:27:32 +00002196int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2197 mbedtls_ssl_transform *transform,
2198 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002199{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002200 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002202 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002203#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002204 size_t padlen = 0, correct = 1;
2205#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002206 unsigned char* data;
2207 unsigned char add_data[13];
2208
Hanno Beckera18d1322018-01-03 14:27:32 +00002209#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002210 ((void) ssl);
2211#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002214 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002215 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2217 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2218 }
2219 if( rec == NULL ||
2220 rec->buf == NULL ||
2221 rec->buf_len < rec->data_offset ||
2222 rec->buf_len - rec->data_offset < rec->data_len )
2223 {
2224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002226 }
2227
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002228 data = rec->buf + rec->data_offset;
2229 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2232 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002233 {
2234 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002235 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2236 transform->iv_dec,
2237 transform->ivlen,
2238 data, rec->data_len,
2239 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002242 return( ret );
2243 }
2244
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002245 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2248 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002249 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 }
Paul Bakker68884e32013-01-07 18:20:04 +01002251 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002253#if defined(MBEDTLS_GCM_C) || \
2254 defined(MBEDTLS_CCM_C) || \
2255 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002257 mode == MBEDTLS_MODE_CCM ||
2258 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002259 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002260 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002261 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002262
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002263 /*
2264 * Compute and update sizes
2265 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002266 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002269 "+ taglen (%d)", rec->data_len,
2270 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002272 }
Paul Bakker68884e32013-01-07 18:20:04 +01002273
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002274 /*
2275 * Prepare IV
2276 */
2277 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2278 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002279 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002280 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002281 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002282
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002283 }
2284 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2285 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002286 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002287 unsigned char i;
2288
2289 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2290
2291 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002292 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002293 }
2294 else
2295 {
2296 /* Reminder if we ever add an AEAD mode with a different size */
2297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2298 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2299 }
2300
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002301 data += explicit_iv_len;
2302 rec->data_offset += explicit_iv_len;
2303 rec->data_len -= explicit_iv_len + transform->taglen;
2304
2305 ssl_extract_add_data_from_record( add_data, rec );
2306 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2307 add_data, 13 );
2308
2309 memcpy( transform->iv_dec + transform->fixed_ivlen,
2310 data - explicit_iv_len, explicit_iv_len );
2311
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002312 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002313 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002314 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002315
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002316
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002317 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002318 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002319 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002320 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2321 iv, transform->ivlen,
2322 add_data, 13,
2323 data, rec->data_len,
2324 data, &olen,
2325 data + rec->data_len,
2326 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2331 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002332
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002333 return( ret );
2334 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002335 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002336
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002337 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2340 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002341 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002342 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002343 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2345#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002346 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002348 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002349 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002350
Paul Bakker5121ce52009-01-03 21:22:43 +00002351 /*
Paul Bakker45829992013-01-03 14:52:21 +01002352 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002355 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2356 {
2357 /* The ciphertext is prefixed with the CBC IV. */
2358 minlen += transform->ivlen;
2359 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002360#endif
Paul Bakker45829992013-01-03 14:52:21 +01002361
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002362 /* Size considerations:
2363 *
2364 * - The CBC cipher text must not be empty and hence
2365 * at least of size transform->ivlen.
2366 *
2367 * Together with the potential IV-prefix, this explains
2368 * the first of the two checks below.
2369 *
2370 * - The record must contain a MAC, either in plain or
2371 * encrypted, depending on whether Encrypt-then-MAC
2372 * is used or not.
2373 * - If it is, the message contains the IV-prefix,
2374 * the CBC ciphertext, and the MAC.
2375 * - If it is not, the padded plaintext, and hence
2376 * the CBC ciphertext, has at least length maclen + 1
2377 * because there is at least the padding length byte.
2378 *
2379 * As the CBC ciphertext is not empty, both cases give the
2380 * lower bound minlen + maclen + 1 on the record size, which
2381 * we test for in the second check below.
2382 */
2383 if( rec->data_len < minlen + transform->ivlen ||
2384 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002387 "+ 1 ) ( + expl IV )", rec->data_len,
2388 transform->ivlen,
2389 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002391 }
2392
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002393 /*
2394 * Authenticate before decrypt if enabled
2395 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002397 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002398 {
Hanno Becker992b6872017-11-09 18:57:39 +00002399 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002402
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002403 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2404 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002405
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002406 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002407
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002408 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, 13 );
2409 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2410 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2411 data, rec->data_len );
2412 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2413 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002414
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002415 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2416 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002417 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002418 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002419
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002420 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2421 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002425 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002426 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002427 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002429
2430 /*
2431 * Check length sanity
2432 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002433 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002436 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002438 }
2439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002441 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002442 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002443 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002444 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002445 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002446 /* This is safe because data_len >= minlen + maclen + 1 initially,
2447 * and at this point we have at most subtracted maclen (note that
2448 * minlen == transform->ivlen here). */
2449 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002450
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002451 data += transform->ivlen;
2452 rec->data_offset += transform->ivlen;
2453 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002454 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002456
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002457 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2458 transform->iv_dec, transform->ivlen,
2459 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002462 return( ret );
2463 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002464
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002465 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2468 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002469 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002472 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002473 {
2474 /*
2475 * Save IV in SSL3 and TLS1
2476 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002477 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2478 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002479 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002480#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002481
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002482 /* Safe since data_len >= minlen + maclen + 1, so after having
2483 * subtracted at most minlen and maclen up to this point,
2484 * data_len > 0. */
2485 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002486
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002487 if( auth_done == 1 )
2488 {
2489 correct *= ( rec->data_len >= padlen + 1 );
2490 padlen *= ( rec->data_len >= padlen + 1 );
2491 }
2492 else
Paul Bakker45829992013-01-03 14:52:21 +01002493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002495 if( rec->data_len < transform->maclen + padlen + 1 )
2496 {
2497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2498 rec->data_len,
2499 transform->maclen,
2500 padlen + 1 ) );
2501 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002502#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002503
2504 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2505 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002506 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002508 padlen++;
2509
2510 /* Regardless of the validity of the padding,
2511 * we have data_len >= padlen here. */
2512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002514 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002515 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002516 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518#if defined(MBEDTLS_SSL_DEBUG_ALL)
2519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002520 "should be no more than %d",
2521 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002522#endif
Paul Bakker45829992013-01-03 14:52:21 +01002523 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002524 }
2525 }
2526 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2528#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2529 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002530 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002532 /* The padding check involves a series of up to 256
2533 * consecutive memory reads at the end of the record
2534 * plaintext buffer. In order to hide the length and
2535 * validity of the padding, always perform exactly
2536 * `min(256,plaintext_len)` reads (but take into account
2537 * only the last `padlen` bytes for the padding check). */
2538 size_t pad_count = 0;
2539 size_t real_count = 0;
2540 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002541
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002542 /* Index of first padding byte; it has been ensured above
2543 * that the subtraction is safe. */
2544 size_t const padding_idx = rec->data_len - padlen;
2545 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2546 size_t const start_idx = rec->data_len - num_checks;
2547 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002548
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002549 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002550 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002551 real_count |= ( idx >= padding_idx );
2552 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002553 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002554 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002557 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002559#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002560 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002561 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002562 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2564 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2567 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002568 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002569
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002570 /* If the padding was found to be invalid, padlen == 0
2571 * and the subtraction is safe. If the padding was found valid,
2572 * padlen hasn't been changed and the previous assertion
2573 * data_len >= padlen still holds. */
2574 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002576 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002578 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2581 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002583
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002584#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002586 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002587#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002588
2589 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002590 * Authenticate if not done yet.
2591 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 */
Hanno Becker52344c22018-01-03 15:24:20 +00002593#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002594 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002595 {
Hanno Becker992b6872017-11-09 18:57:39 +00002596 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002597
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002598 /* If the initial value of padlen was such that
2599 * data_len < maclen + padlen + 1, then padlen
2600 * got reset to 1, and the initial check
2601 * data_len >= minlen + maclen + 1
2602 * guarantees that at this point we still
2603 * have at least data_len >= maclen.
2604 *
2605 * If the initial value of padlen was such that
2606 * data_len >= maclen + padlen + 1, then we have
2607 * subtracted either padlen + 1 (if the padding was correct)
2608 * or 0 (if the padding was incorrect) since then,
2609 * hence data_len >= maclen in any case.
2610 */
2611 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002612
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002613 ssl_extract_add_data_from_record( add_data, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002616 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002617 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002618 ssl_mac( &transform->md_ctx_dec,
2619 transform->mac_dec,
2620 data, rec->data_len,
2621 rec->ctr, rec->type,
2622 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002623 }
2624 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2626#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2627 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002628 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002629 {
2630 /*
2631 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002632 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002633 *
2634 * Known timing attacks:
2635 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2636 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002637 * To compensate for different timings for the MAC calculation
2638 * depending on how much padding was removed (which is determined
2639 * by padlen), process extra_run more blocks through the hash
2640 * function.
2641 *
2642 * The formula in the paper is
2643 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2644 * where L1 is the size of the header plus the decrypted message
2645 * plus CBC padding and L2 is the size of the header plus the
2646 * decrypted message. This is for an underlying hash function
2647 * with 64-byte blocks.
2648 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2649 * correctly. We round down instead of up, so -56 is the correct
2650 * value for our calculations instead of -55.
2651 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002652 * Repeat the formula rather than defining a block_size variable.
2653 * This avoids requiring division by a variable at runtime
2654 * (which would be marginally less efficient and would require
2655 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002656 */
2657 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002658 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002659
2660 /*
2661 * The next two sizes are the minimum and maximum values of
2662 * in_msglen over all padlen values.
2663 *
2664 * They're independent of padlen, since we previously did
2665 * in_msglen -= padlen.
2666 *
2667 * Note that max_len + maclen is never more than the buffer
2668 * length, as we previously did in_msglen -= maclen too.
2669 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002670 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002671 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2672
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002673 memset( tmp, 0, sizeof( tmp ) );
2674
2675 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002676 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002677#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2678 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002679 case MBEDTLS_MD_MD5:
2680 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002681 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002682 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002683 extra_run = ( 13 + rec->data_len + padlen + 8 ) / 64 -
2684 ( 13 + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002685 break;
2686#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002687#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002688 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002689 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002690 extra_run = ( 13 + rec->data_len + padlen + 16 ) / 128 -
2691 ( 13 + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002692 break;
2693#endif
2694 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002696 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2697 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002698
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002699 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002700
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002701 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2702 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2703 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002704 /* Make sure we access everything even when padlen > 0. This
2705 * makes the synchronisation requirements for just-in-time
2706 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002707 ssl_read_memory( data + rec->data_len, padlen );
2708 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002709
2710 /* Call mbedtls_md_process at least once due to cache attacks
2711 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002712 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002713 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002714
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002715 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002716
2717 /* Make sure we access all the memory that could contain the MAC,
2718 * before we check it in the next code block. This makes the
2719 * synchronisation requirements for just-in-time Prime+Probe
2720 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002721 ssl_read_memory( data + min_len,
2722 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002723 }
2724 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2726 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2729 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002730 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002731
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002732#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002733 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2734 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002735#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002736
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002737 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2738 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740#if defined(MBEDTLS_SSL_DEBUG_ALL)
2741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002742#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002743 correct = 0;
2744 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002745 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002746 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002747
2748 /*
2749 * Finally check the correct flag
2750 */
2751 if( correct == 0 )
2752 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00002753#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002754
2755 /* Make extra sure authentication was performed, exactly once */
2756 if( auth_done != 1 )
2757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2759 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002760 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002763
2764 return( 0 );
2765}
2766
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002767#undef MAC_NONE
2768#undef MAC_PLAINTEXT
2769#undef MAC_CIPHERTEXT
2770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002772/*
2773 * Compression/decompression functions
2774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002776{
2777 int ret;
2778 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002779 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002780 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002781 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002784
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002785 if( len_pre == 0 )
2786 return( 0 );
2787
Paul Bakker2770fbd2012-07-03 13:30:23 +00002788 memcpy( msg_pre, ssl->out_msg, len_pre );
2789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002791 ssl->out_msglen ) );
2792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002794 ssl->out_msg, ssl->out_msglen );
2795
Paul Bakker48916f92012-09-16 19:57:18 +00002796 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2797 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2798 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002799 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002800
Paul Bakker48916f92012-09-16 19:57:18 +00002801 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002802 if( ret != Z_OK )
2803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2805 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002806 }
2807
Angus Grattond8213d02016-05-25 20:56:48 +10002808 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002809 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002812 ssl->out_msglen ) );
2813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002815 ssl->out_msg, ssl->out_msglen );
2816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002818
2819 return( 0 );
2820}
2821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002823{
2824 int ret;
2825 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002826 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002827 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002828 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002831
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002832 if( len_pre == 0 )
2833 return( 0 );
2834
Paul Bakker2770fbd2012-07-03 13:30:23 +00002835 memcpy( msg_pre, ssl->in_msg, len_pre );
2836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002838 ssl->in_msglen ) );
2839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002841 ssl->in_msg, ssl->in_msglen );
2842
Paul Bakker48916f92012-09-16 19:57:18 +00002843 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2844 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2845 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002846 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002847 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002848
Paul Bakker48916f92012-09-16 19:57:18 +00002849 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002850 if( ret != Z_OK )
2851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2853 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002854 }
2855
Angus Grattond8213d02016-05-25 20:56:48 +10002856 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002857 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002859 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002860 ssl->in_msglen ) );
2861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002863 ssl->in_msg, ssl->in_msglen );
2864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002866
2867 return( 0 );
2868}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2872static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874#if defined(MBEDTLS_SSL_PROTO_DTLS)
2875static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002876{
2877 /* If renegotiation is not enforced, retransmit until we would reach max
2878 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002879 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002880 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002881 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002882 unsigned char doublings = 1;
2883
2884 while( ratio != 0 )
2885 {
2886 ++doublings;
2887 ratio >>= 1;
2888 }
2889
2890 if( ++ssl->renego_records_seen > doublings )
2891 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002893 return( 0 );
2894 }
2895 }
2896
2897 return( ssl_write_hello_request( ssl ) );
2898}
2899#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002901
Paul Bakker5121ce52009-01-03 21:22:43 +00002902/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002903 * Fill the input message buffer by appending data to it.
2904 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002905 *
2906 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2907 * available (from this read and/or a previous one). Otherwise, an error code
2908 * is returned (possibly EOF or WANT_READ).
2909 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002910 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2911 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2912 * since we always read a whole datagram at once.
2913 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002914 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002915 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002916 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002918{
Paul Bakker23986e52011-04-24 08:57:21 +00002919 int ret;
2920 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002923
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002924 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002927 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002929 }
2930
Angus Grattond8213d02016-05-25 20:56:48 +10002931 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2934 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002935 }
2936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002938 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002939 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002940 uint32_t timeout;
2941
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002942 /* Just to be sure */
2943 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2944 {
2945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2946 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2947 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2948 }
2949
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002950 /*
2951 * The point is, we need to always read a full datagram at once, so we
2952 * sometimes read more then requested, and handle the additional data.
2953 * It could be the rest of the current record (while fetching the
2954 * header) and/or some other records in the same datagram.
2955 */
2956
2957 /*
2958 * Move to the next record in the already read datagram if applicable
2959 */
2960 if( ssl->next_record_offset != 0 )
2961 {
2962 if( ssl->in_left < ssl->next_record_offset )
2963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2965 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002966 }
2967
2968 ssl->in_left -= ssl->next_record_offset;
2969
2970 if( ssl->in_left != 0 )
2971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002973 ssl->next_record_offset ) );
2974 memmove( ssl->in_hdr,
2975 ssl->in_hdr + ssl->next_record_offset,
2976 ssl->in_left );
2977 }
2978
2979 ssl->next_record_offset = 0;
2980 }
2981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002983 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002984
2985 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002986 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002987 */
2988 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002991 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002992 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002993
2994 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002995 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002996 * are not at the beginning of a new record, the caller did something
2997 * wrong.
2998 */
2999 if( ssl->in_left != 0 )
3000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3002 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003003 }
3004
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003005 /*
3006 * Don't even try to read if time's out already.
3007 * This avoids by-passing the timer when repeatedly receiving messages
3008 * that will end up being dropped.
3009 */
3010 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003011 {
3012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003013 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003014 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003015 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003016 {
Angus Grattond8213d02016-05-25 20:56:48 +10003017 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003020 timeout = ssl->handshake->retransmit_timeout;
3021 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003022 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003025
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003026 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003027 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3028 timeout );
3029 else
3030 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003033
3034 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003036 }
3037
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003038 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003041 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003044 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003045 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003048 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003049 }
3050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003054 return( ret );
3055 }
3056
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003057 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003058 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003060 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003062 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003063 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003066 return( ret );
3067 }
3068
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003069 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003070 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003072 }
3073
Paul Bakker5121ce52009-01-03 21:22:43 +00003074 if( ret < 0 )
3075 return( ret );
3076
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003077 ssl->in_left = ret;
3078 }
3079 else
3080#endif
3081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003083 ssl->in_left, nb_want ) );
3084
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003085 while( ssl->in_left < nb_want )
3086 {
3087 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003088
3089 if( ssl_check_timer( ssl ) != 0 )
3090 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3091 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003092 {
3093 if( ssl->f_recv_timeout != NULL )
3094 {
3095 ret = ssl->f_recv_timeout( ssl->p_bio,
3096 ssl->in_hdr + ssl->in_left, len,
3097 ssl->conf->read_timeout );
3098 }
3099 else
3100 {
3101 ret = ssl->f_recv( ssl->p_bio,
3102 ssl->in_hdr + ssl->in_left, len );
3103 }
3104 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003107 ssl->in_left, nb_want ) );
3108 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003109
3110 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003112
3113 if( ret < 0 )
3114 return( ret );
3115
mohammad160352aecb92018-03-28 23:41:40 -07003116 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003117 {
Darryl Green11999bb2018-03-13 15:22:58 +00003118 MBEDTLS_SSL_DEBUG_MSG( 1,
3119 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003120 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003121 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3122 }
3123
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003124 ssl->in_left += ret;
3125 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003126 }
3127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003129
3130 return( 0 );
3131}
3132
3133/*
3134 * Flush any data not yet written
3135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003137{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003138 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003139 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003142
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003143 if( ssl->f_send == NULL )
3144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003146 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003148 }
3149
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003150 /* Avoid incrementing counter if data is flushed */
3151 if( ssl->out_left == 0 )
3152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003154 return( 0 );
3155 }
3156
Paul Bakker5121ce52009-01-03 21:22:43 +00003157 while( ssl->out_left > 0 )
3158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3160 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003161
Hanno Becker2b1e3542018-08-06 11:19:13 +01003162 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003163 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003166
3167 if( ret <= 0 )
3168 return( ret );
3169
mohammad160352aecb92018-03-28 23:41:40 -07003170 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003171 {
Darryl Green11999bb2018-03-13 15:22:58 +00003172 MBEDTLS_SSL_DEBUG_MSG( 1,
3173 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003174 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003175 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3176 }
3177
Paul Bakker5121ce52009-01-03 21:22:43 +00003178 ssl->out_left -= ret;
3179 }
3180
Hanno Becker2b1e3542018-08-06 11:19:13 +01003181#if defined(MBEDTLS_SSL_PROTO_DTLS)
3182 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003183 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003184 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003185 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003186 else
3187#endif
3188 {
3189 ssl->out_hdr = ssl->out_buf + 8;
3190 }
3191 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003194
3195 return( 0 );
3196}
3197
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003198/*
3199 * Functions to handle the DTLS retransmission state machine
3200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003202/*
3203 * Append current handshake message to current outgoing flight
3204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003206{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3209 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3210 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003211
3212 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003213 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003214 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003217 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003218 }
3219
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003220 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003221 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003224 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003225 }
3226
3227 /* Copy current handshake message with headers */
3228 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3229 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003230 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003231 msg->next = NULL;
3232
3233 /* Append to the current flight */
3234 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003235 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003236 else
3237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003239 while( cur->next != NULL )
3240 cur = cur->next;
3241 cur->next = msg;
3242 }
3243
Hanno Becker3b235902018-08-06 09:54:53 +01003244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003245 return( 0 );
3246}
3247
3248/*
3249 * Free the current flight of handshake messages
3250 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003252{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253 mbedtls_ssl_flight_item *cur = flight;
3254 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003255
3256 while( cur != NULL )
3257 {
3258 next = cur->next;
3259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 mbedtls_free( cur->p );
3261 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003262
3263 cur = next;
3264 }
3265}
3266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003267#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3268static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003269#endif
3270
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003271/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003272 * Swap transform_out and out_ctr with the alternative ones
3273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003274static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003275{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003277 unsigned char tmp_out_ctr[8];
3278
3279 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003282 return;
3283 }
3284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003285 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003286
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003287 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003288 tmp_transform = ssl->transform_out;
3289 ssl->transform_out = ssl->handshake->alt_transform_out;
3290 ssl->handshake->alt_transform_out = tmp_transform;
3291
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003292 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003293 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3294 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003295 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003296
3297 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003298 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3301 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3306 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003307 }
3308 }
3309#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003310}
3311
3312/*
3313 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003314 */
3315int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3316{
3317 int ret = 0;
3318
3319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3320
3321 ret = mbedtls_ssl_flight_transmit( ssl );
3322
3323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3324
3325 return( ret );
3326}
3327
3328/*
3329 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003330 *
3331 * Need to remember the current message in case flush_output returns
3332 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003333 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003334 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003335int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003336{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003337 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003341 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003343
3344 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003345 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003346 ssl_swap_epochs( ssl );
3347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003349 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003350
3351 while( ssl->handshake->cur_msg != NULL )
3352 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003353 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003354 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003355
Hanno Beckere1dcb032018-08-17 16:47:58 +01003356 int const is_finished =
3357 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3358 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3359
Hanno Becker04da1892018-08-14 13:22:10 +01003360 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3361 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3362
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003363 /* Swap epochs before sending Finished: we can't do it after
3364 * sending ChangeCipherSpec, in case write returns WANT_READ.
3365 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003366 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003367 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003369 ssl_swap_epochs( ssl );
3370 }
3371
Hanno Becker67bc7c32018-08-06 11:33:50 +01003372 ret = ssl_get_remaining_payload_in_datagram( ssl );
3373 if( ret < 0 )
3374 return( ret );
3375 max_frag_len = (size_t) ret;
3376
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003377 /* CCS is copied as is, while HS messages may need fragmentation */
3378 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3379 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003380 if( max_frag_len == 0 )
3381 {
3382 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3383 return( ret );
3384
3385 continue;
3386 }
3387
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003388 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003389 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003390 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003391
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003392 /* Update position inside current message */
3393 ssl->handshake->cur_msg_p += cur->len;
3394 }
3395 else
3396 {
3397 const unsigned char * const p = ssl->handshake->cur_msg_p;
3398 const size_t hs_len = cur->len - 12;
3399 const size_t frag_off = p - ( cur->p + 12 );
3400 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003401 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003402
Hanno Beckere1dcb032018-08-17 16:47:58 +01003403 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003404 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003405 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003406 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003407
Hanno Becker67bc7c32018-08-06 11:33:50 +01003408 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3409 return( ret );
3410
3411 continue;
3412 }
3413 max_hs_frag_len = max_frag_len - 12;
3414
3415 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3416 max_hs_frag_len : rem_len;
3417
3418 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003419 {
3420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003421 (unsigned) cur_hs_frag_len,
3422 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003423 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003424
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003425 /* Messages are stored with handshake headers as if not fragmented,
3426 * copy beginning of headers then fill fragmentation fields.
3427 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3428 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003429
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003430 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3431 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3432 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3433
Hanno Becker67bc7c32018-08-06 11:33:50 +01003434 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3435 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3436 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003437
3438 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3439
Hanno Becker3f7b9732018-08-28 09:53:25 +01003440 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003441 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3442 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003443 ssl->out_msgtype = cur->type;
3444
3445 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003446 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003447 }
3448
3449 /* If done with the current message move to the next one if any */
3450 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3451 {
3452 if( cur->next != NULL )
3453 {
3454 ssl->handshake->cur_msg = cur->next;
3455 ssl->handshake->cur_msg_p = cur->next->p + 12;
3456 }
3457 else
3458 {
3459 ssl->handshake->cur_msg = NULL;
3460 ssl->handshake->cur_msg_p = NULL;
3461 }
3462 }
3463
3464 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003465 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003468 return( ret );
3469 }
3470 }
3471
Hanno Becker67bc7c32018-08-06 11:33:50 +01003472 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3473 return( ret );
3474
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003475 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3477 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003478 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003481 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3482 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003483
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003485
3486 return( 0 );
3487}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003488
3489/*
3490 * To be called when the last message of an incoming flight is received.
3491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003493{
3494 /* We won't need to resend that one any more */
3495 ssl_flight_free( ssl->handshake->flight );
3496 ssl->handshake->flight = NULL;
3497 ssl->handshake->cur_msg = NULL;
3498
3499 /* The next incoming flight will start with this msg_seq */
3500 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3501
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003502 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003503 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003504
Hanno Becker0271f962018-08-16 13:23:47 +01003505 /* Clear future message buffering structure. */
3506 ssl_buffering_free( ssl );
3507
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003508 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003509 ssl_set_timer( ssl, 0 );
3510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3512 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003515 }
3516 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003517 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003518}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003519
3520/*
3521 * To be called when the last message of an outgoing flight is send.
3522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003524{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003525 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003526 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3529 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003532 }
3533 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003535}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003537
Paul Bakker5121ce52009-01-03 21:22:43 +00003538/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003539 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003540 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003541
3542/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003543 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003544 *
3545 * - fill in handshake headers
3546 * - update handshake checksum
3547 * - DTLS: save message for resending
3548 * - then pass to the record layer
3549 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003550 * DTLS: except for HelloRequest, messages are only queued, and will only be
3551 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003552 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003553 * Inputs:
3554 * - ssl->out_msglen: 4 + actual handshake message len
3555 * (4 is the size of handshake headers for TLS)
3556 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3557 * - ssl->out_msg + 4: the handshake message body
3558 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003559 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003560 * - ssl->out_msglen: the length of the record contents
3561 * (including handshake headers but excluding record headers)
3562 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003563 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003564int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003565{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003566 int ret;
3567 const size_t hs_len = ssl->out_msglen - 4;
3568 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003569
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3571
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003572 /*
3573 * Sanity checks
3574 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003575 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003576 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3577 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003578 /* In SSLv3, the client might send a NoCertificate alert. */
3579#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3580 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3581 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3582 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3583#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3584 {
3585 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3586 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3587 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003588 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003589
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003590 /* Whenever we send anything different from a
3591 * HelloRequest we should be in a handshake - double check. */
3592 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3593 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003594 ssl->handshake == NULL )
3595 {
3596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3597 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3598 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003601 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003602 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003604 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3606 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003607 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003608#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003609
Hanno Beckerb50a2532018-08-06 11:52:54 +01003610 /* Double-check that we did not exceed the bounds
3611 * of the outgoing record buffer.
3612 * This should never fail as the various message
3613 * writing functions must obey the bounds of the
3614 * outgoing record buffer, but better be safe.
3615 *
3616 * Note: We deliberately do not check for the MTU or MFL here.
3617 */
3618 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3619 {
3620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3621 "size %u, maximum %u",
3622 (unsigned) ssl->out_msglen,
3623 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3624 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3625 }
3626
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003627 /*
3628 * Fill handshake headers
3629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003631 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003632 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3633 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3634 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003635
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003636 /*
3637 * DTLS has additional fields in the Handshake layer,
3638 * between the length field and the actual payload:
3639 * uint16 message_seq;
3640 * uint24 fragment_offset;
3641 * uint24 fragment_length;
3642 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003644 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003645 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003646 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003647 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003648 {
3649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3650 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003651 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003652 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3654 }
3655
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003656 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003657 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003658
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003659 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003660 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003661 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003662 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3663 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3664 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003665 }
3666 else
3667 {
3668 ssl->out_msg[4] = 0;
3669 ssl->out_msg[5] = 0;
3670 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003671
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003672 /* Handshake hashes are computed without fragmentation,
3673 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003674 memset( ssl->out_msg + 6, 0x00, 3 );
3675 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003676 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003678
Hanno Becker0207e532018-08-28 10:28:28 +01003679 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003680 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3681 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003682 }
3683
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003684 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003685#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003686 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003687 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3688 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003689 {
3690 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003692 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003693 return( ret );
3694 }
3695 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003696 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003697#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003698 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003699 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003700 {
3701 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3702 return( ret );
3703 }
3704 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003705
3706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3707
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003708 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003709}
3710
3711/*
3712 * Record layer functions
3713 */
3714
3715/*
3716 * Write current record.
3717 *
3718 * Uses:
3719 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3720 * - ssl->out_msglen: length of the record content (excl headers)
3721 * - ssl->out_msg: record content
3722 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003723int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003724{
3725 int ret, done = 0;
3726 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003727 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003728
3729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003732 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003734 {
3735 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003738 return( ret );
3739 }
3740
3741 len = ssl->out_msglen;
3742 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003745#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3746 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750 ret = mbedtls_ssl_hw_record_write( ssl );
3751 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003753 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3754 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003755 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003756
3757 if( ret == 0 )
3758 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003759 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003761 if( !done )
3762 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003763 unsigned i;
3764 size_t protected_record_size;
3765
Paul Bakker05ef8352012-05-08 09:17:57 +00003766 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003767 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003768 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003769
Hanno Becker19859472018-08-06 09:40:20 +01003770 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003771 ssl->out_len[0] = (unsigned char)( len >> 8 );
3772 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003773
Paul Bakker48916f92012-09-16 19:57:18 +00003774 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003775 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003776 mbedtls_record rec;
3777
3778 rec.buf = ssl->out_iv;
3779 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3780 ( ssl->out_iv - ssl->out_buf );
3781 rec.data_len = ssl->out_msglen;
3782 rec.data_offset = ssl->out_msg - rec.buf;
3783
3784 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3785 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3786 ssl->conf->transport, rec.ver );
3787 rec.type = ssl->out_msgtype;
3788
Hanno Beckera18d1322018-01-03 14:27:32 +00003789 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003790 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003793 return( ret );
3794 }
3795
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003796 if( rec.data_offset != 0 )
3797 {
3798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3799 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3800 }
3801
Hanno Becker78f839d2019-03-14 12:56:23 +00003802 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003803 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3804 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003805 }
3806
Hanno Becker2b1e3542018-08-06 11:19:13 +01003807 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3808
3809#if defined(MBEDTLS_SSL_PROTO_DTLS)
3810 /* In case of DTLS, double-check that we don't exceed
3811 * the remaining space in the datagram. */
3812 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3813 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003814 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003815 if( ret < 0 )
3816 return( ret );
3817
3818 if( protected_record_size > (size_t) ret )
3819 {
3820 /* Should never happen */
3821 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3822 }
3823 }
3824#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003827 "version = [%d:%d], msglen = %d",
3828 ssl->out_hdr[0], ssl->out_hdr[1],
3829 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003832 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003833
3834 ssl->out_left += protected_record_size;
3835 ssl->out_hdr += protected_record_size;
3836 ssl_update_out_pointers( ssl, ssl->transform_out );
3837
Hanno Becker04484622018-08-06 09:49:38 +01003838 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3839 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3840 break;
3841
3842 /* The loop goes to its end iff the counter is wrapping */
3843 if( i == ssl_ep_len( ssl ) )
3844 {
3845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3846 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3847 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003848 }
3849
Hanno Becker67bc7c32018-08-06 11:33:50 +01003850#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003851 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3852 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003853 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003854 size_t remaining;
3855 ret = ssl_get_remaining_payload_in_datagram( ssl );
3856 if( ret < 0 )
3857 {
3858 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3859 ret );
3860 return( ret );
3861 }
3862
3863 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003864 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003865 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003866 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003867 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003868 else
3869 {
Hanno Becker513815a2018-08-20 11:56:09 +01003870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003871 }
3872 }
3873#endif /* MBEDTLS_SSL_PROTO_DTLS */
3874
3875 if( ( flush == SSL_FORCE_FLUSH ) &&
3876 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003879 return( ret );
3880 }
3881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003883
3884 return( 0 );
3885}
3886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003888
3889static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3890{
3891 if( ssl->in_msglen < ssl->in_hslen ||
3892 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3893 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3894 {
3895 return( 1 );
3896 }
3897 return( 0 );
3898}
Hanno Becker44650b72018-08-16 12:51:11 +01003899
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003900static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003901{
3902 return( ( ssl->in_msg[9] << 16 ) |
3903 ( ssl->in_msg[10] << 8 ) |
3904 ssl->in_msg[11] );
3905}
3906
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003907static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003908{
3909 return( ( ssl->in_msg[6] << 16 ) |
3910 ( ssl->in_msg[7] << 8 ) |
3911 ssl->in_msg[8] );
3912}
3913
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003914static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003915{
3916 uint32_t msg_len, frag_off, frag_len;
3917
3918 msg_len = ssl_get_hs_total_len( ssl );
3919 frag_off = ssl_get_hs_frag_off( ssl );
3920 frag_len = ssl_get_hs_frag_len( ssl );
3921
3922 if( frag_off > msg_len )
3923 return( -1 );
3924
3925 if( frag_len > msg_len - frag_off )
3926 return( -1 );
3927
3928 if( frag_len + 12 > ssl->in_msglen )
3929 return( -1 );
3930
3931 return( 0 );
3932}
3933
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003934/*
3935 * Mark bits in bitmask (used for DTLS HS reassembly)
3936 */
3937static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3938{
3939 unsigned int start_bits, end_bits;
3940
3941 start_bits = 8 - ( offset % 8 );
3942 if( start_bits != 8 )
3943 {
3944 size_t first_byte_idx = offset / 8;
3945
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003946 /* Special case */
3947 if( len <= start_bits )
3948 {
3949 for( ; len != 0; len-- )
3950 mask[first_byte_idx] |= 1 << ( start_bits - len );
3951
3952 /* Avoid potential issues with offset or len becoming invalid */
3953 return;
3954 }
3955
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003956 offset += start_bits; /* Now offset % 8 == 0 */
3957 len -= start_bits;
3958
3959 for( ; start_bits != 0; start_bits-- )
3960 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3961 }
3962
3963 end_bits = len % 8;
3964 if( end_bits != 0 )
3965 {
3966 size_t last_byte_idx = ( offset + len ) / 8;
3967
3968 len -= end_bits; /* Now len % 8 == 0 */
3969
3970 for( ; end_bits != 0; end_bits-- )
3971 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3972 }
3973
3974 memset( mask + offset / 8, 0xFF, len / 8 );
3975}
3976
3977/*
3978 * Check that bitmask is full
3979 */
3980static int ssl_bitmask_check( unsigned char *mask, size_t len )
3981{
3982 size_t i;
3983
3984 for( i = 0; i < len / 8; i++ )
3985 if( mask[i] != 0xFF )
3986 return( -1 );
3987
3988 for( i = 0; i < len % 8; i++ )
3989 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3990 return( -1 );
3991
3992 return( 0 );
3993}
3994
Hanno Becker56e205e2018-08-16 09:06:12 +01003995/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003996static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003997 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003998{
Hanno Becker56e205e2018-08-16 09:06:12 +01003999 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004000
Hanno Becker56e205e2018-08-16 09:06:12 +01004001 alloc_len = 12; /* Handshake header */
4002 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004003
Hanno Beckerd07df862018-08-16 09:14:58 +01004004 if( add_bitmap )
4005 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004006
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004007 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004008}
Hanno Becker56e205e2018-08-16 09:06:12 +01004009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004010#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004011
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004012static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004013{
4014 return( ( ssl->in_msg[1] << 16 ) |
4015 ( ssl->in_msg[2] << 8 ) |
4016 ssl->in_msg[3] );
4017}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004018
Simon Butcher99000142016-10-13 17:21:01 +01004019int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004020{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004021 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004024 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004025 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004026 }
4027
Hanno Becker12555c62018-08-16 12:47:53 +01004028 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004030 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004031 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004032 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004034#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004035 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004036 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004037 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004038 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004039
Hanno Becker44650b72018-08-16 12:51:11 +01004040 if( ssl_check_hs_header( ssl ) != 0 )
4041 {
4042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4043 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4044 }
4045
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004046 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004047 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4048 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4049 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4050 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004051 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004052 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4053 {
4054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4055 recv_msg_seq,
4056 ssl->handshake->in_msg_seq ) );
4057 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4058 }
4059
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004060 /* Retransmit only on last message from previous flight, to avoid
4061 * too many retransmissions.
4062 * Besides, No sane server ever retransmits HelloVerifyRequest */
4063 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004067 "message_seq = %d, start_of_flight = %d",
4068 recv_msg_seq,
4069 ssl->handshake->in_flight_start_seq ) );
4070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004073 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004074 return( ret );
4075 }
4076 }
4077 else
4078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004080 "message_seq = %d, expected = %d",
4081 recv_msg_seq,
4082 ssl->handshake->in_msg_seq ) );
4083 }
4084
Hanno Becker90333da2017-10-10 11:27:13 +01004085 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004086 }
4087 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004088
Hanno Becker6d97ef52018-08-16 13:09:04 +01004089 /* Message reassembly is handled alongside buffering of future
4090 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004091 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004092 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004093 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004096 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004097 }
4098 }
4099 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004100#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004101 /* With TLS we don't handle fragmentation (for now) */
4102 if( ssl->in_msglen < ssl->in_hslen )
4103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4105 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004106 }
4107
Simon Butcher99000142016-10-13 17:21:01 +01004108 return( 0 );
4109}
4110
4111void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4112{
Hanno Becker0271f962018-08-16 13:23:47 +01004113 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004114
Hanno Becker0271f962018-08-16 13:23:47 +01004115 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004116 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004117 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004118 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004119
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004120 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004121#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004122 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004123 ssl->handshake != NULL )
4124 {
Hanno Becker0271f962018-08-16 13:23:47 +01004125 unsigned offset;
4126 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004127
Hanno Becker0271f962018-08-16 13:23:47 +01004128 /* Increment handshake sequence number */
4129 hs->in_msg_seq++;
4130
4131 /*
4132 * Clear up handshake buffering and reassembly structure.
4133 */
4134
4135 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004136 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004137
4138 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004139 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4140 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004141 offset++, hs_buf++ )
4142 {
4143 *hs_buf = *(hs_buf + 1);
4144 }
4145
4146 /* Create a fresh last entry */
4147 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004148 }
4149#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004150}
4151
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004152/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004153 * DTLS anti-replay: RFC 6347 4.1.2.6
4154 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004155 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4156 * Bit n is set iff record number in_window_top - n has been seen.
4157 *
4158 * Usually, in_window_top is the last record number seen and the lsb of
4159 * in_window is set. The only exception is the initial state (record number 0
4160 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004162#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4163static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004164{
4165 ssl->in_window_top = 0;
4166 ssl->in_window = 0;
4167}
4168
4169static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4170{
4171 return( ( (uint64_t) buf[0] << 40 ) |
4172 ( (uint64_t) buf[1] << 32 ) |
4173 ( (uint64_t) buf[2] << 24 ) |
4174 ( (uint64_t) buf[3] << 16 ) |
4175 ( (uint64_t) buf[4] << 8 ) |
4176 ( (uint64_t) buf[5] ) );
4177}
4178
4179/*
4180 * Return 0 if sequence number is acceptable, -1 otherwise
4181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004182int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004183{
4184 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4185 uint64_t bit;
4186
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004187 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004188 return( 0 );
4189
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004190 if( rec_seqnum > ssl->in_window_top )
4191 return( 0 );
4192
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004193 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004194
4195 if( bit >= 64 )
4196 return( -1 );
4197
4198 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4199 return( -1 );
4200
4201 return( 0 );
4202}
4203
4204/*
4205 * Update replay window on new validated record
4206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004208{
4209 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4210
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004211 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004212 return;
4213
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004214 if( rec_seqnum > ssl->in_window_top )
4215 {
4216 /* Update window_top and the contents of the window */
4217 uint64_t shift = rec_seqnum - ssl->in_window_top;
4218
4219 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004220 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004221 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004222 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004223 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004224 ssl->in_window |= 1;
4225 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004226
4227 ssl->in_window_top = rec_seqnum;
4228 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004229 else
4230 {
4231 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004232 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004233
4234 if( bit < 64 ) /* Always true, but be extra sure */
4235 ssl->in_window |= (uint64_t) 1 << bit;
4236 }
4237}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004238#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004239
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004240#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004241/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004242static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4243
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004244/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004245 * Without any SSL context, check if a datagram looks like a ClientHello with
4246 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004247 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004248 *
4249 * - if cookie is valid, return 0
4250 * - if ClientHello looks superficially valid but cookie is not,
4251 * fill obuf and set olen, then
4252 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4253 * - otherwise return a specific error code
4254 */
4255static int ssl_check_dtls_clihlo_cookie(
4256 mbedtls_ssl_cookie_write_t *f_cookie_write,
4257 mbedtls_ssl_cookie_check_t *f_cookie_check,
4258 void *p_cookie,
4259 const unsigned char *cli_id, size_t cli_id_len,
4260 const unsigned char *in, size_t in_len,
4261 unsigned char *obuf, size_t buf_len, size_t *olen )
4262{
4263 size_t sid_len, cookie_len;
4264 unsigned char *p;
4265
4266 if( f_cookie_write == NULL || f_cookie_check == NULL )
4267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4268
4269 /*
4270 * Structure of ClientHello with record and handshake headers,
4271 * and expected values. We don't need to check a lot, more checks will be
4272 * done when actually parsing the ClientHello - skipping those checks
4273 * avoids code duplication and does not make cookie forging any easier.
4274 *
4275 * 0-0 ContentType type; copied, must be handshake
4276 * 1-2 ProtocolVersion version; copied
4277 * 3-4 uint16 epoch; copied, must be 0
4278 * 5-10 uint48 sequence_number; copied
4279 * 11-12 uint16 length; (ignored)
4280 *
4281 * 13-13 HandshakeType msg_type; (ignored)
4282 * 14-16 uint24 length; (ignored)
4283 * 17-18 uint16 message_seq; copied
4284 * 19-21 uint24 fragment_offset; copied, must be 0
4285 * 22-24 uint24 fragment_length; (ignored)
4286 *
4287 * 25-26 ProtocolVersion client_version; (ignored)
4288 * 27-58 Random random; (ignored)
4289 * 59-xx SessionID session_id; 1 byte len + sid_len content
4290 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4291 * ...
4292 *
4293 * Minimum length is 61 bytes.
4294 */
4295 if( in_len < 61 ||
4296 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4297 in[3] != 0 || in[4] != 0 ||
4298 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4299 {
4300 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4301 }
4302
4303 sid_len = in[59];
4304 if( sid_len > in_len - 61 )
4305 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4306
4307 cookie_len = in[60 + sid_len];
4308 if( cookie_len > in_len - 60 )
4309 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4310
4311 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4312 cli_id, cli_id_len ) == 0 )
4313 {
4314 /* Valid cookie */
4315 return( 0 );
4316 }
4317
4318 /*
4319 * If we get here, we've got an invalid cookie, let's prepare HVR.
4320 *
4321 * 0-0 ContentType type; copied
4322 * 1-2 ProtocolVersion version; copied
4323 * 3-4 uint16 epoch; copied
4324 * 5-10 uint48 sequence_number; copied
4325 * 11-12 uint16 length; olen - 13
4326 *
4327 * 13-13 HandshakeType msg_type; hello_verify_request
4328 * 14-16 uint24 length; olen - 25
4329 * 17-18 uint16 message_seq; copied
4330 * 19-21 uint24 fragment_offset; copied
4331 * 22-24 uint24 fragment_length; olen - 25
4332 *
4333 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4334 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4335 *
4336 * Minimum length is 28.
4337 */
4338 if( buf_len < 28 )
4339 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4340
4341 /* Copy most fields and adapt others */
4342 memcpy( obuf, in, 25 );
4343 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4344 obuf[25] = 0xfe;
4345 obuf[26] = 0xff;
4346
4347 /* Generate and write actual cookie */
4348 p = obuf + 28;
4349 if( f_cookie_write( p_cookie,
4350 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4351 {
4352 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4353 }
4354
4355 *olen = p - obuf;
4356
4357 /* Go back and fill length fields */
4358 obuf[27] = (unsigned char)( *olen - 28 );
4359
4360 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4361 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4362 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4363
4364 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4365 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4366
4367 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4368}
4369
4370/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004371 * Handle possible client reconnect with the same UDP quadruplet
4372 * (RFC 6347 Section 4.2.8).
4373 *
4374 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4375 * that looks like a ClientHello.
4376 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004377 * - if the input looks like a ClientHello without cookies,
4378 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004379 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004380 * - if the input looks like a ClientHello with a valid cookie,
4381 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004382 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004383 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004384 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004385 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004386 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4387 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004388 */
4389static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4390{
4391 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004392 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004393
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004394 ret = ssl_check_dtls_clihlo_cookie(
4395 ssl->conf->f_cookie_write,
4396 ssl->conf->f_cookie_check,
4397 ssl->conf->p_cookie,
4398 ssl->cli_id, ssl->cli_id_len,
4399 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004400 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004401
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004402 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4403
4404 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004405 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004406 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004407 * If the error is permanent we'll catch it later,
4408 * if it's not, then hopefully it'll work next time. */
4409 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4410
4411 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004412 }
4413
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004414 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004415 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004416 /* Got a valid cookie, partially reset context */
4417 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4418 {
4419 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4420 return( ret );
4421 }
4422
4423 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004424 }
4425
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004426 return( ret );
4427}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004428#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004429
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004430/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004431 * ContentType type;
4432 * ProtocolVersion version;
4433 * uint16 epoch; // DTLS only
4434 * uint48 sequence_number; // DTLS only
4435 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004436 *
4437 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004438 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004439 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4440 *
4441 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004442 * 1. proceed with the record if this function returns 0
4443 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4444 * 3. return CLIENT_RECONNECT if this function return that value
4445 * 4. drop the whole datagram if this function returns anything else.
4446 * Point 2 is needed when the peer is resending, and we have already received
4447 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004449static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004450{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004451 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004453 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 +02004454
Paul Bakker5121ce52009-01-03 21:22:43 +00004455 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004456 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004457 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004460 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004461 ssl->in_msgtype,
4462 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004463
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004464 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004465 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4466 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4467 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4468 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004471
4472#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004473 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4474 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004475 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4476#endif /* MBEDTLS_SSL_PROTO_DTLS */
4477 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4478 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004480 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004481 }
4482
4483 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004484 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4487 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004488 }
4489
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004490 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4493 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004494 }
4495
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004496 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004497 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004498 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4501 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004502 }
4503
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004504 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004505 * DTLS-related tests.
4506 * Check epoch before checking length constraint because
4507 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4508 * message gets duplicated before the corresponding Finished message,
4509 * the second ChangeCipherSpec should be discarded because it belongs
4510 * to an old epoch, but not because its length is shorter than
4511 * the minimum record length for packets using the new record transform.
4512 * Note that these two kinds of failures are handled differently,
4513 * as an unexpected record is silently skipped but an invalid
4514 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004515 */
4516#if defined(MBEDTLS_SSL_PROTO_DTLS)
4517 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4518 {
4519 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4520
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004521 /* Check epoch (and sequence number) with DTLS */
4522 if( rec_epoch != ssl->in_epoch )
4523 {
4524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4525 "expected %d, received %d",
4526 ssl->in_epoch, rec_epoch ) );
4527
4528#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4529 /*
4530 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4531 * access the first byte of record content (handshake type), as we
4532 * have an active transform (possibly iv_len != 0), so use the
4533 * fact that the record header len is 13 instead.
4534 */
4535 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4536 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4537 rec_epoch == 0 &&
4538 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4539 ssl->in_left > 13 &&
4540 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4541 {
4542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4543 "from the same port" ) );
4544 return( ssl_handle_possible_reconnect( ssl ) );
4545 }
4546 else
4547#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004548 {
4549 /* Consider buffering the record. */
4550 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4551 {
4552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4553 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4554 }
4555
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004556 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004557 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004558 }
4559
4560#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4561 /* Replay detection only works for the current epoch */
4562 if( rec_epoch == ssl->in_epoch &&
4563 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4564 {
4565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4566 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4567 }
4568#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004569
Hanno Becker52c6dc62017-05-26 16:07:36 +01004570 /* Drop unexpected ApplicationData records,
4571 * except at the beginning of renegotiations */
4572 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4573 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4574#if defined(MBEDTLS_SSL_RENEGOTIATION)
4575 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4576 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4577#endif
4578 )
4579 {
4580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4581 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4582 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004583 }
4584#endif /* MBEDTLS_SSL_PROTO_DTLS */
4585
Hanno Becker52c6dc62017-05-26 16:07:36 +01004586
4587 /* Check length against bounds of the current transform and version */
4588 if( ssl->transform_in == NULL )
4589 {
4590 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004591 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004592 {
4593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4594 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4595 }
4596 }
4597 else
4598 {
4599 if( ssl->in_msglen < ssl->transform_in->minlen )
4600 {
4601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4602 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4603 }
4604
4605#if defined(MBEDTLS_SSL_PROTO_SSL3)
4606 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004607 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004608 {
4609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4610 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4611 }
4612#endif
4613#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4614 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4615 /*
4616 * TLS encrypted messages can have up to 256 bytes of padding
4617 */
4618 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4619 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004620 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004621 {
4622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4623 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4624 }
4625#endif
4626 }
4627
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004628 return( 0 );
4629}
Paul Bakker5121ce52009-01-03 21:22:43 +00004630
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004631/*
4632 * If applicable, decrypt (and decompress) record content
4633 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004634static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004635{
4636 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004638 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4639 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004641#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4642 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004646 ret = mbedtls_ssl_hw_record_read( ssl );
4647 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4650 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004651 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004652
4653 if( ret == 0 )
4654 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004655 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004656#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004657 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004658 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004659 mbedtls_record rec;
4660
4661 rec.buf = ssl->in_iv;
4662 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4663 - ( ssl->in_iv - ssl->in_buf );
4664 rec.data_len = ssl->in_msglen;
4665 rec.data_offset = 0;
4666
4667 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4668 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4669 ssl->conf->transport, rec.ver );
4670 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00004671 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4672 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004674 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004675 return( ret );
4676 }
4677
Hanno Becker29800d22018-08-07 14:30:18 +01004678 if( ssl->in_iv + rec.data_offset != ssl->in_msg )
4679 {
4680 /* Should never happen */
4681 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4682 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004683
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004684 ssl->in_msglen = rec.data_len;
4685 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4686 ssl->in_len[1] = (unsigned char)( rec.data_len );
4687
Hanno Becker1c0c37f2018-08-07 14:29:29 +01004688 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4689 ssl->in_msg, ssl->in_msglen );
4690
Angus Grattond8213d02016-05-25 20:56:48 +10004691 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4694 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004695 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004696 else if( ssl->in_msglen == 0 )
4697 {
4698#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4699 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4700 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4701 {
4702 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4704 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4705 }
4706#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4707
4708 ssl->nb_zero++;
4709
4710 /*
4711 * Three or more empty messages may be a DoS attack
4712 * (excessive CPU consumption).
4713 */
4714 if( ssl->nb_zero > 3 )
4715 {
4716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
4717 "messages, possible DoS attack" ) );
4718 /* Q: Is that the right error code? */
4719 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4720 }
4721 }
4722 else
4723 ssl->nb_zero = 0;
4724
4725#if defined(MBEDTLS_SSL_PROTO_DTLS)
4726 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4727 {
4728 ; /* in_ctr read from peer, not maintained internally */
4729 }
4730 else
4731#endif
4732 {
4733 unsigned i;
4734 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4735 if( ++ssl->in_ctr[i - 1] != 0 )
4736 break;
4737
4738 /* The loop goes to its end iff the counter is wrapping */
4739 if( i == ssl_ep_len( ssl ) )
4740 {
4741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4742 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4743 }
4744 }
4745
Paul Bakker5121ce52009-01-03 21:22:43 +00004746 }
4747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004748#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004749 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004750 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004751 {
4752 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004754 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004755 return( ret );
4756 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004757 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004758#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004760#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004761 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004763 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004764 }
4765#endif
4766
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004767 return( 0 );
4768}
4769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004770static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004771
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004772/*
4773 * Read a record.
4774 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004775 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4776 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4777 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004778 */
Hanno Becker1097b342018-08-15 14:09:41 +01004779
4780/* Helper functions for mbedtls_ssl_read_record(). */
4781static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004782static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4783static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004784
Hanno Becker327c93b2018-08-15 13:56:18 +01004785int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004786 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004787{
4788 int ret;
4789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004791
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004792 if( ssl->keep_current_message == 0 )
4793 {
4794 do {
Simon Butcher99000142016-10-13 17:21:01 +01004795
Hanno Becker26994592018-08-15 14:14:59 +01004796 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004797 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004798 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004799
Hanno Beckere74d5562018-08-15 14:26:08 +01004800 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004801 {
Hanno Becker40f50842018-08-15 14:48:01 +01004802#if defined(MBEDTLS_SSL_PROTO_DTLS)
4803 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004804
Hanno Becker40f50842018-08-15 14:48:01 +01004805 /* We only check for buffered messages if the
4806 * current datagram is fully consumed. */
4807 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004808 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004809 {
Hanno Becker40f50842018-08-15 14:48:01 +01004810 if( ssl_load_buffered_message( ssl ) == 0 )
4811 have_buffered = 1;
4812 }
4813
4814 if( have_buffered == 0 )
4815#endif /* MBEDTLS_SSL_PROTO_DTLS */
4816 {
4817 ret = ssl_get_next_record( ssl );
4818 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4819 continue;
4820
4821 if( ret != 0 )
4822 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004823 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004824 return( ret );
4825 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004826 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004827 }
4828
4829 ret = mbedtls_ssl_handle_message_type( ssl );
4830
Hanno Becker40f50842018-08-15 14:48:01 +01004831#if defined(MBEDTLS_SSL_PROTO_DTLS)
4832 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4833 {
4834 /* Buffer future message */
4835 ret = ssl_buffer_message( ssl );
4836 if( ret != 0 )
4837 return( ret );
4838
4839 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4840 }
4841#endif /* MBEDTLS_SSL_PROTO_DTLS */
4842
Hanno Becker90333da2017-10-10 11:27:13 +01004843 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4844 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004845
4846 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004847 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004848 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004849 return( ret );
4850 }
4851
Hanno Becker327c93b2018-08-15 13:56:18 +01004852 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004853 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004854 {
4855 mbedtls_ssl_update_handshake_status( ssl );
4856 }
Simon Butcher99000142016-10-13 17:21:01 +01004857 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004858 else
Simon Butcher99000142016-10-13 17:21:01 +01004859 {
Hanno Becker02f59072018-08-15 14:00:24 +01004860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004861 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004862 }
4863
4864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4865
4866 return( 0 );
4867}
4868
Hanno Becker40f50842018-08-15 14:48:01 +01004869#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004870static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004871{
Hanno Becker40f50842018-08-15 14:48:01 +01004872 if( ssl->in_left > ssl->next_record_offset )
4873 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004874
Hanno Becker40f50842018-08-15 14:48:01 +01004875 return( 0 );
4876}
4877
4878static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4879{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004880 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004881 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004882 int ret = 0;
4883
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004884 if( hs == NULL )
4885 return( -1 );
4886
Hanno Beckere00ae372018-08-20 09:39:42 +01004887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4888
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004889 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4890 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4891 {
4892 /* Check if we have seen a ChangeCipherSpec before.
4893 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004894 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004895 {
4896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4897 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004898 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004899 }
4900
Hanno Becker39b8bc92018-08-28 17:17:13 +01004901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004902 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4903 ssl->in_msglen = 1;
4904 ssl->in_msg[0] = 1;
4905
4906 /* As long as they are equal, the exact value doesn't matter. */
4907 ssl->in_left = 0;
4908 ssl->next_record_offset = 0;
4909
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004910 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004911 goto exit;
4912 }
Hanno Becker37f95322018-08-16 13:55:32 +01004913
Hanno Beckerb8f50142018-08-28 10:01:34 +01004914#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004915 /* Debug only */
4916 {
4917 unsigned offset;
4918 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4919 {
4920 hs_buf = &hs->buffering.hs[offset];
4921 if( hs_buf->is_valid == 1 )
4922 {
4923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4924 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004925 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004926 }
4927 }
4928 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004929#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004930
4931 /* Check if we have buffered and/or fully reassembled the
4932 * next handshake message. */
4933 hs_buf = &hs->buffering.hs[0];
4934 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4935 {
4936 /* Synthesize a record containing the buffered HS message. */
4937 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4938 ( hs_buf->data[2] << 8 ) |
4939 hs_buf->data[3];
4940
4941 /* Double-check that we haven't accidentally buffered
4942 * a message that doesn't fit into the input buffer. */
4943 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4944 {
4945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4946 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4947 }
4948
4949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4950 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4951 hs_buf->data, msg_len + 12 );
4952
4953 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4954 ssl->in_hslen = msg_len + 12;
4955 ssl->in_msglen = msg_len + 12;
4956 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4957
4958 ret = 0;
4959 goto exit;
4960 }
4961 else
4962 {
4963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4964 hs->in_msg_seq ) );
4965 }
4966
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004967 ret = -1;
4968
4969exit:
4970
4971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4972 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004973}
4974
Hanno Beckera02b0b42018-08-21 17:20:27 +01004975static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4976 size_t desired )
4977{
4978 int offset;
4979 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4981 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004982
Hanno Becker01315ea2018-08-21 17:22:17 +01004983 /* Get rid of future records epoch first, if such exist. */
4984 ssl_free_buffered_record( ssl );
4985
4986 /* Check if we have enough space available now. */
4987 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4988 hs->buffering.total_bytes_buffered ) )
4989 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004991 return( 0 );
4992 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004993
Hanno Becker4f432ad2018-08-28 10:02:32 +01004994 /* We don't have enough space to buffer the next expected handshake
4995 * message. Remove buffers used for future messages to gain space,
4996 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004997 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4998 offset >= 0; offset-- )
4999 {
5000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5001 offset ) );
5002
Hanno Beckerb309b922018-08-23 13:18:05 +01005003 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005004
5005 /* Check if we have enough space available now. */
5006 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5007 hs->buffering.total_bytes_buffered ) )
5008 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005010 return( 0 );
5011 }
5012 }
5013
5014 return( -1 );
5015}
5016
Hanno Becker40f50842018-08-15 14:48:01 +01005017static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5018{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005019 int ret = 0;
5020 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5021
5022 if( hs == NULL )
5023 return( 0 );
5024
5025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5026
5027 switch( ssl->in_msgtype )
5028 {
5029 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005031
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005032 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005033 break;
5034
5035 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005036 {
5037 unsigned recv_msg_seq_offset;
5038 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5039 mbedtls_ssl_hs_buffer *hs_buf;
5040 size_t msg_len = ssl->in_hslen - 12;
5041
5042 /* We should never receive an old handshake
5043 * message - double-check nonetheless. */
5044 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5045 {
5046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5047 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5048 }
5049
5050 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5051 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5052 {
5053 /* Silently ignore -- message too far in the future */
5054 MBEDTLS_SSL_DEBUG_MSG( 2,
5055 ( "Ignore future HS message with sequence number %u, "
5056 "buffering window %u - %u",
5057 recv_msg_seq, ssl->handshake->in_msg_seq,
5058 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5059
5060 goto exit;
5061 }
5062
5063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5064 recv_msg_seq, recv_msg_seq_offset ) );
5065
5066 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5067
5068 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005069 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005070 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005071 size_t reassembly_buf_sz;
5072
Hanno Becker37f95322018-08-16 13:55:32 +01005073 hs_buf->is_fragmented =
5074 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5075
5076 /* We copy the message back into the input buffer
5077 * after reassembly, so check that it's not too large.
5078 * This is an implementation-specific limitation
5079 * and not one from the standard, hence it is not
5080 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005081 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005082 {
5083 /* Ignore message */
5084 goto exit;
5085 }
5086
Hanno Beckere0b150f2018-08-21 15:51:03 +01005087 /* Check if we have enough space to buffer the message. */
5088 if( hs->buffering.total_bytes_buffered >
5089 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5090 {
5091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5092 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5093 }
5094
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005095 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5096 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005097
5098 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5099 hs->buffering.total_bytes_buffered ) )
5100 {
5101 if( recv_msg_seq_offset > 0 )
5102 {
5103 /* If we can't buffer a future message because
5104 * of space limitations -- ignore. */
5105 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",
5106 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5107 (unsigned) hs->buffering.total_bytes_buffered ) );
5108 goto exit;
5109 }
Hanno Beckere1801392018-08-21 16:51:05 +01005110 else
5111 {
5112 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",
5113 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5114 (unsigned) hs->buffering.total_bytes_buffered ) );
5115 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005116
Hanno Beckera02b0b42018-08-21 17:20:27 +01005117 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005118 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005119 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",
5120 (unsigned) msg_len,
5121 (unsigned) reassembly_buf_sz,
5122 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005123 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005124 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5125 goto exit;
5126 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005127 }
5128
5129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5130 msg_len ) );
5131
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005132 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5133 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005134 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005135 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005136 goto exit;
5137 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005138 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005139
5140 /* Prepare final header: copy msg_type, length and message_seq,
5141 * then add standardised fragment_offset and fragment_length */
5142 memcpy( hs_buf->data, ssl->in_msg, 6 );
5143 memset( hs_buf->data + 6, 0, 3 );
5144 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5145
5146 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005147
5148 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005149 }
5150 else
5151 {
5152 /* Make sure msg_type and length are consistent */
5153 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5154 {
5155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5156 /* Ignore */
5157 goto exit;
5158 }
5159 }
5160
Hanno Becker4422bbb2018-08-20 09:40:19 +01005161 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005162 {
5163 size_t frag_len, frag_off;
5164 unsigned char * const msg = hs_buf->data + 12;
5165
5166 /*
5167 * Check and copy current fragment
5168 */
5169
5170 /* Validation of header fields already done in
5171 * mbedtls_ssl_prepare_handshake_record(). */
5172 frag_off = ssl_get_hs_frag_off( ssl );
5173 frag_len = ssl_get_hs_frag_len( ssl );
5174
5175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5176 frag_off, frag_len ) );
5177 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5178
5179 if( hs_buf->is_fragmented )
5180 {
5181 unsigned char * const bitmask = msg + msg_len;
5182 ssl_bitmask_set( bitmask, frag_off, frag_len );
5183 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5184 msg_len ) == 0 );
5185 }
5186 else
5187 {
5188 hs_buf->is_complete = 1;
5189 }
5190
5191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5192 hs_buf->is_complete ? "" : "not yet " ) );
5193 }
5194
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005195 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005196 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005197
5198 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005199 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005200 break;
5201 }
5202
5203exit:
5204
5205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5206 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005207}
5208#endif /* MBEDTLS_SSL_PROTO_DTLS */
5209
Hanno Becker1097b342018-08-15 14:09:41 +01005210static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005211{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005212 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005213 * Consume last content-layer message and potentially
5214 * update in_msglen which keeps track of the contents'
5215 * consumption state.
5216 *
5217 * (1) Handshake messages:
5218 * Remove last handshake message, move content
5219 * and adapt in_msglen.
5220 *
5221 * (2) Alert messages:
5222 * Consume whole record content, in_msglen = 0.
5223 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005224 * (3) Change cipher spec:
5225 * Consume whole record content, in_msglen = 0.
5226 *
5227 * (4) Application data:
5228 * Don't do anything - the record layer provides
5229 * the application data as a stream transport
5230 * and consumes through mbedtls_ssl_read only.
5231 *
5232 */
5233
5234 /* Case (1): Handshake messages */
5235 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005236 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005237 /* Hard assertion to be sure that no application data
5238 * is in flight, as corrupting ssl->in_msglen during
5239 * ssl->in_offt != NULL is fatal. */
5240 if( ssl->in_offt != NULL )
5241 {
5242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5243 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5244 }
5245
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005246 /*
5247 * Get next Handshake message in the current record
5248 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005249
Hanno Becker4a810fb2017-05-24 16:27:30 +01005250 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005251 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005252 * current handshake content: If DTLS handshake
5253 * fragmentation is used, that's the fragment
5254 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005255 * size here is faulty and should be changed at
5256 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005257 * (2) While it doesn't seem to cause problems, one
5258 * has to be very careful not to assume that in_hslen
5259 * is always <= in_msglen in a sensible communication.
5260 * Again, it's wrong for DTLS handshake fragmentation.
5261 * The following check is therefore mandatory, and
5262 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005263 * Additionally, ssl->in_hslen might be arbitrarily out of
5264 * bounds after handling a DTLS message with an unexpected
5265 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005266 */
5267 if( ssl->in_hslen < ssl->in_msglen )
5268 {
5269 ssl->in_msglen -= ssl->in_hslen;
5270 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5271 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005272
Hanno Becker4a810fb2017-05-24 16:27:30 +01005273 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5274 ssl->in_msg, ssl->in_msglen );
5275 }
5276 else
5277 {
5278 ssl->in_msglen = 0;
5279 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005280
Hanno Becker4a810fb2017-05-24 16:27:30 +01005281 ssl->in_hslen = 0;
5282 }
5283 /* Case (4): Application data */
5284 else if( ssl->in_offt != NULL )
5285 {
5286 return( 0 );
5287 }
5288 /* Everything else (CCS & Alerts) */
5289 else
5290 {
5291 ssl->in_msglen = 0;
5292 }
5293
Hanno Becker1097b342018-08-15 14:09:41 +01005294 return( 0 );
5295}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005296
Hanno Beckere74d5562018-08-15 14:26:08 +01005297static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5298{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005299 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005300 return( 1 );
5301
5302 return( 0 );
5303}
5304
Hanno Becker5f066e72018-08-16 14:56:31 +01005305#if defined(MBEDTLS_SSL_PROTO_DTLS)
5306
5307static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5308{
5309 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5310 if( hs == NULL )
5311 return;
5312
Hanno Becker01315ea2018-08-21 17:22:17 +01005313 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005314 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005315 hs->buffering.total_bytes_buffered -=
5316 hs->buffering.future_record.len;
5317
5318 mbedtls_free( hs->buffering.future_record.data );
5319 hs->buffering.future_record.data = NULL;
5320 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005321}
5322
5323static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5324{
5325 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5326 unsigned char * rec;
5327 size_t rec_len;
5328 unsigned rec_epoch;
5329
5330 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5331 return( 0 );
5332
5333 if( hs == NULL )
5334 return( 0 );
5335
Hanno Becker5f066e72018-08-16 14:56:31 +01005336 rec = hs->buffering.future_record.data;
5337 rec_len = hs->buffering.future_record.len;
5338 rec_epoch = hs->buffering.future_record.epoch;
5339
5340 if( rec == NULL )
5341 return( 0 );
5342
Hanno Becker4cb782d2018-08-20 11:19:05 +01005343 /* Only consider loading future records if the
5344 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005345 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005346 return( 0 );
5347
Hanno Becker5f066e72018-08-16 14:56:31 +01005348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5349
5350 if( rec_epoch != ssl->in_epoch )
5351 {
5352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5353 goto exit;
5354 }
5355
5356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5357
5358 /* Double-check that the record is not too large */
5359 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5360 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5361 {
5362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5363 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5364 }
5365
5366 memcpy( ssl->in_hdr, rec, rec_len );
5367 ssl->in_left = rec_len;
5368 ssl->next_record_offset = 0;
5369
5370 ssl_free_buffered_record( ssl );
5371
5372exit:
5373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5374 return( 0 );
5375}
5376
5377static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5378{
5379 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5380 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005381 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005382
5383 /* Don't buffer future records outside handshakes. */
5384 if( hs == NULL )
5385 return( 0 );
5386
5387 /* Only buffer handshake records (we are only interested
5388 * in Finished messages). */
5389 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5390 return( 0 );
5391
5392 /* Don't buffer more than one future epoch record. */
5393 if( hs->buffering.future_record.data != NULL )
5394 return( 0 );
5395
Hanno Becker01315ea2018-08-21 17:22:17 +01005396 /* Don't buffer record if there's not enough buffering space remaining. */
5397 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5398 hs->buffering.total_bytes_buffered ) )
5399 {
5400 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",
5401 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5402 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005403 return( 0 );
5404 }
5405
Hanno Becker5f066e72018-08-16 14:56:31 +01005406 /* Buffer record */
5407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5408 ssl->in_epoch + 1 ) );
5409 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5410 rec_hdr_len + ssl->in_msglen );
5411
5412 /* ssl_parse_record_header() only considers records
5413 * of the next epoch as candidates for buffering. */
5414 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005415 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005416
5417 hs->buffering.future_record.data =
5418 mbedtls_calloc( 1, hs->buffering.future_record.len );
5419 if( hs->buffering.future_record.data == NULL )
5420 {
5421 /* If we run out of RAM trying to buffer a
5422 * record from the next epoch, just ignore. */
5423 return( 0 );
5424 }
5425
Hanno Becker01315ea2018-08-21 17:22:17 +01005426 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005427
Hanno Becker01315ea2018-08-21 17:22:17 +01005428 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005429 return( 0 );
5430}
5431
5432#endif /* MBEDTLS_SSL_PROTO_DTLS */
5433
Hanno Beckere74d5562018-08-15 14:26:08 +01005434static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005435{
5436 int ret;
5437
Hanno Becker5f066e72018-08-16 14:56:31 +01005438#if defined(MBEDTLS_SSL_PROTO_DTLS)
5439 /* We might have buffered a future record; if so,
5440 * and if the epoch matches now, load it.
5441 * On success, this call will set ssl->in_left to
5442 * the length of the buffered record, so that
5443 * the calls to ssl_fetch_input() below will
5444 * essentially be no-ops. */
5445 ret = ssl_load_buffered_record( ssl );
5446 if( ret != 0 )
5447 return( ret );
5448#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005450 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005453 return( ret );
5454 }
5455
5456 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005459 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5460 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005461 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005462 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5463 {
5464 ret = ssl_buffer_future_record( ssl );
5465 if( ret != 0 )
5466 return( ret );
5467
5468 /* Fall through to handling of unexpected records */
5469 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5470 }
5471
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005472 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5473 {
5474 /* Skip unexpected record (but not whole datagram) */
5475 ssl->next_record_offset = ssl->in_msglen
5476 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005477
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5479 "(header)" ) );
5480 }
5481 else
5482 {
5483 /* Skip invalid record and the rest of the datagram */
5484 ssl->next_record_offset = 0;
5485 ssl->in_left = 0;
5486
5487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5488 "(header)" ) );
5489 }
5490
5491 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005492 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005493 }
5494#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005495 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005496 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005497
5498 /*
5499 * Read and optionally decrypt the message contents
5500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005501 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5502 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005504 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005505 return( ret );
5506 }
5507
5508 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005510 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005512 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005513 if( ssl->next_record_offset < ssl->in_left )
5514 {
5515 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5516 }
5517 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005518 else
5519#endif
5520 ssl->in_left = 0;
5521
5522 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005524#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005525 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005526 {
5527 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005528 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5529 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005530 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005531 /* Except when waiting for Finished as a bad mac here
5532 * probably means something went wrong in the handshake
5533 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5534 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5535 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5536 {
5537#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5538 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5539 {
5540 mbedtls_ssl_send_alert_message( ssl,
5541 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5542 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5543 }
5544#endif
5545 return( ret );
5546 }
5547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005548#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005549 if( ssl->conf->badmac_limit != 0 &&
5550 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5553 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005554 }
5555#endif
5556
Hanno Becker4a810fb2017-05-24 16:27:30 +01005557 /* As above, invalid records cause
5558 * dismissal of the whole datagram. */
5559
5560 ssl->next_record_offset = 0;
5561 ssl->in_left = 0;
5562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005564 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005565 }
5566
5567 return( ret );
5568 }
5569 else
5570#endif
5571 {
5572 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005573#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5574 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005576 mbedtls_ssl_send_alert_message( ssl,
5577 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5578 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005579 }
5580#endif
5581 return( ret );
5582 }
5583 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005584
Simon Butcher99000142016-10-13 17:21:01 +01005585 return( 0 );
5586}
5587
5588int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5589{
5590 int ret;
5591
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005592 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005593 * Handle particular types of records
5594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005596 {
Simon Butcher99000142016-10-13 17:21:01 +01005597 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5598 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005599 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005600 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005601 }
5602
Hanno Beckere678eaa2018-08-21 14:57:46 +01005603 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005604 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005605 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005606 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5608 ssl->in_msglen ) );
5609 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005610 }
5611
Hanno Beckere678eaa2018-08-21 14:57:46 +01005612 if( ssl->in_msg[0] != 1 )
5613 {
5614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5615 ssl->in_msg[0] ) );
5616 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5617 }
5618
5619#if defined(MBEDTLS_SSL_PROTO_DTLS)
5620 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5621 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5622 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5623 {
5624 if( ssl->handshake == NULL )
5625 {
5626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5627 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5628 }
5629
5630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5631 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5632 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005633#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005634 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005636 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005637 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005638 if( ssl->in_msglen != 2 )
5639 {
5640 /* Note: Standard allows for more than one 2 byte alert
5641 to be packed in a single message, but Mbed TLS doesn't
5642 currently support this. */
5643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5644 ssl->in_msglen ) );
5645 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5646 }
5647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005649 ssl->in_msg[0], ssl->in_msg[1] ) );
5650
5651 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005652 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005653 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005654 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005657 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005659 }
5660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5662 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5665 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005666 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005667
5668#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5669 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5670 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5671 {
Hanno Becker90333da2017-10-10 11:27:13 +01005672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005673 /* Will be handled when trying to parse ServerHello */
5674 return( 0 );
5675 }
5676#endif
5677
5678#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5679 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5680 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5681 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5682 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5683 {
5684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5685 /* Will be handled in mbedtls_ssl_parse_certificate() */
5686 return( 0 );
5687 }
5688#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5689
5690 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005691 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005692 }
5693
Hanno Beckerc76c6192017-06-06 10:03:17 +01005694#if defined(MBEDTLS_SSL_PROTO_DTLS)
5695 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5696 ssl->handshake != NULL &&
5697 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5698 {
5699 ssl_handshake_wrapup_free_hs_transform( ssl );
5700 }
5701#endif
5702
Paul Bakker5121ce52009-01-03 21:22:43 +00005703 return( 0 );
5704}
5705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005707{
5708 int ret;
5709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005710 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5711 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5712 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005713 {
5714 return( ret );
5715 }
5716
5717 return( 0 );
5718}
5719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005720int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005721 unsigned char level,
5722 unsigned char message )
5723{
5724 int ret;
5725
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005726 if( ssl == NULL || ssl->conf == NULL )
5727 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005730 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005733 ssl->out_msglen = 2;
5734 ssl->out_msg[0] = level;
5735 ssl->out_msg[1] = message;
5736
Hanno Becker67bc7c32018-08-06 11:33:50 +01005737 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005739 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005740 return( ret );
5741 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005743
5744 return( 0 );
5745}
5746
Hanno Beckerb9d44792019-02-08 07:19:04 +00005747#if defined(MBEDTLS_X509_CRT_PARSE_C)
5748static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5749{
5750#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5751 if( session->peer_cert != NULL )
5752 {
5753 mbedtls_x509_crt_free( session->peer_cert );
5754 mbedtls_free( session->peer_cert );
5755 session->peer_cert = NULL;
5756 }
5757#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5758 if( session->peer_cert_digest != NULL )
5759 {
5760 /* Zeroization is not necessary. */
5761 mbedtls_free( session->peer_cert_digest );
5762 session->peer_cert_digest = NULL;
5763 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5764 session->peer_cert_digest_len = 0;
5765 }
5766#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5767}
5768#endif /* MBEDTLS_X509_CRT_PARSE_C */
5769
Paul Bakker5121ce52009-01-03 21:22:43 +00005770/*
5771 * Handshake functions
5772 */
Hanno Becker21489932019-02-05 13:20:55 +00005773#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005774/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005775int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005776{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005777 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5778 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005781
Hanno Becker7177a882019-02-05 13:36:46 +00005782 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005785 ssl->state++;
5786 return( 0 );
5787 }
5788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5790 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005791}
5792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005793int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005794{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005795 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5796 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005799
Hanno Becker7177a882019-02-05 13:36:46 +00005800 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005803 ssl->state++;
5804 return( 0 );
5805 }
5806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5808 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005809}
Gilles Peskinef9828522017-05-03 12:28:43 +02005810
Hanno Becker21489932019-02-05 13:20:55 +00005811#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005812/* Some certificate support -> implement write and parse */
5813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005814int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005815{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005816 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005817 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00005819 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5820 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005823
Hanno Becker7177a882019-02-05 13:36:46 +00005824 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005827 ssl->state++;
5828 return( 0 );
5829 }
5830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005832 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005833 {
5834 if( ssl->client_auth == 0 )
5835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005837 ssl->state++;
5838 return( 0 );
5839 }
5840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005841#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005842 /*
5843 * If using SSLv3 and got no cert, send an Alert message
5844 * (otherwise an empty Certificate message will be sent).
5845 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005846 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5847 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005848 {
5849 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005850 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5851 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5852 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005855 goto write_msg;
5856 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005857#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005858 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005859#endif /* MBEDTLS_SSL_CLI_C */
5860#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005861 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5866 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005867 }
5868 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005869#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005871 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005872
5873 /*
5874 * 0 . 0 handshake type
5875 * 1 . 3 handshake length
5876 * 4 . 6 length of all certs
5877 * 7 . 9 length of cert. 1
5878 * 10 . n-1 peer certificate
5879 * n . n+2 length of cert. 2
5880 * n+3 . ... upper level cert, etc.
5881 */
5882 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005883 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005884
Paul Bakker29087132010-03-21 21:03:34 +00005885 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005886 {
5887 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005888 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005891 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005892 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005893 }
5894
5895 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5896 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5897 ssl->out_msg[i + 2] = (unsigned char)( n );
5898
5899 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5900 i += n; crt = crt->next;
5901 }
5902
5903 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5904 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5905 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5906
5907 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005908 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5909 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005910
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005911#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005912write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005913#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005914
5915 ssl->state++;
5916
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005917 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005918 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005920 return( ret );
5921 }
5922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005924
Paul Bakkered27a042013-04-18 22:46:23 +02005925 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005926}
5927
Hanno Becker84879e32019-01-31 07:44:03 +00005928#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00005929
5930#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005931static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5932 unsigned char *crt_buf,
5933 size_t crt_buf_len )
5934{
5935 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5936
5937 if( peer_crt == NULL )
5938 return( -1 );
5939
5940 if( peer_crt->raw.len != crt_buf_len )
5941 return( -1 );
5942
Hanno Becker46f34d02019-02-08 14:00:04 +00005943 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005944}
Hanno Becker177475a2019-02-05 17:02:46 +00005945#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5946static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5947 unsigned char *crt_buf,
5948 size_t crt_buf_len )
5949{
5950 int ret;
5951 unsigned char const * const peer_cert_digest =
5952 ssl->session->peer_cert_digest;
5953 mbedtls_md_type_t const peer_cert_digest_type =
5954 ssl->session->peer_cert_digest_type;
5955 mbedtls_md_info_t const * const digest_info =
5956 mbedtls_md_info_from_type( peer_cert_digest_type );
5957 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
5958 size_t digest_len;
5959
5960 if( peer_cert_digest == NULL || digest_info == NULL )
5961 return( -1 );
5962
5963 digest_len = mbedtls_md_get_size( digest_info );
5964 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
5965 return( -1 );
5966
5967 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
5968 if( ret != 0 )
5969 return( -1 );
5970
5971 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
5972}
5973#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00005974#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005975
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005976/*
5977 * Once the certificate message is read, parse it into a cert chain and
5978 * perform basic checks, but leave actual verification to the caller
5979 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005980static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5981 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00005982{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005983 int ret;
5984#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5985 int crt_cnt=0;
5986#endif
Paul Bakker23986e52011-04-24 08:57:21 +00005987 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005988 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005990 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005993 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5994 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005996 }
5997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005998 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5999 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006002 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6003 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006004 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006005 }
6006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006007 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006008
Paul Bakker5121ce52009-01-03 21:22:43 +00006009 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006010 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006011 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006012 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006013
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006014 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006018 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6019 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006021 }
6022
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006023 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6024 i += 3;
6025
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006026 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006027 while( i < ssl->in_hslen )
6028 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006029 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006030 if ( i + 3 > ssl->in_hslen ) {
6031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006032 mbedtls_ssl_send_alert_message( ssl,
6033 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6034 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006035 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6036 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006037 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6038 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006039 if( ssl->in_msg[i] != 0 )
6040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006042 mbedtls_ssl_send_alert_message( ssl,
6043 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6044 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006045 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006046 }
6047
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006048 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006049 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6050 | (unsigned int) ssl->in_msg[i + 2];
6051 i += 3;
6052
6053 if( n < 128 || i + n > ssl->in_hslen )
6054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006056 mbedtls_ssl_send_alert_message( ssl,
6057 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6058 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006060 }
6061
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006062 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006063#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6064 if( crt_cnt++ == 0 &&
6065 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6066 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006067 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006068 /* During client-side renegotiation, check that the server's
6069 * end-CRTs hasn't changed compared to the initial handshake,
6070 * mitigating the triple handshake attack. On success, reuse
6071 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006072 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6073 if( ssl_check_peer_crt_unchanged( ssl,
6074 &ssl->in_msg[i],
6075 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006076 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6078 mbedtls_ssl_send_alert_message( ssl,
6079 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6080 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6081 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006082 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006083
6084 /* Now we can safely free the original chain. */
6085 ssl_clear_peer_cert( ssl->session );
6086 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006087#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6088
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006089 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006090#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006091 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006092#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006093 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006094 * it in-place from the input buffer instead of making a copy. */
6095 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6096#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006097 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006098 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006099 case 0: /*ok*/
6100 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6101 /* Ignore certificate with an unknown algorithm: maybe a
6102 prior certificate was already trusted. */
6103 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006104
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006105 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6106 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6107 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006108
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006109 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6110 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6111 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006112
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006113 default:
6114 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6115 crt_parse_der_failed:
6116 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6117 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6118 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006119 }
6120
6121 i += n;
6122 }
6123
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006124 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006125 return( 0 );
6126}
6127
Hanno Becker4a55f632019-02-05 12:49:06 +00006128#if defined(MBEDTLS_SSL_SRV_C)
6129static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6130{
6131 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6132 return( -1 );
6133
6134#if defined(MBEDTLS_SSL_PROTO_SSL3)
6135 /*
6136 * Check if the client sent an empty certificate
6137 */
6138 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6139 {
6140 if( ssl->in_msglen == 2 &&
6141 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6142 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6143 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6144 {
6145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6146 return( 0 );
6147 }
6148
6149 return( -1 );
6150 }
6151#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6152
6153#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6154 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6155 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6156 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6157 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6158 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6159 {
6160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6161 return( 0 );
6162 }
6163
6164 return( -1 );
6165#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6166 MBEDTLS_SSL_PROTO_TLS1_2 */
6167}
6168#endif /* MBEDTLS_SSL_SRV_C */
6169
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006170/* Check if a certificate message is expected.
6171 * Return either
6172 * - SSL_CERTIFICATE_EXPECTED, or
6173 * - SSL_CERTIFICATE_SKIP
6174 * indicating whether a Certificate message is expected or not.
6175 */
6176#define SSL_CERTIFICATE_EXPECTED 0
6177#define SSL_CERTIFICATE_SKIP 1
6178static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6179 int authmode )
6180{
6181 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006182 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006183
6184 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6185 return( SSL_CERTIFICATE_SKIP );
6186
6187#if defined(MBEDTLS_SSL_SRV_C)
6188 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6189 {
6190 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6191 return( SSL_CERTIFICATE_SKIP );
6192
6193 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6194 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006195 ssl->session_negotiate->verify_result =
6196 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6197 return( SSL_CERTIFICATE_SKIP );
6198 }
6199 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006200#else
6201 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006202#endif /* MBEDTLS_SSL_SRV_C */
6203
6204 return( SSL_CERTIFICATE_EXPECTED );
6205}
6206
Hanno Becker68636192019-02-05 14:36:34 +00006207static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6208 int authmode,
6209 mbedtls_x509_crt *chain,
6210 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006211{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006212 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006213 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006214 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006215 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006216
Hanno Becker8927c832019-04-03 12:52:50 +01006217 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6218 void *p_vrfy;
6219
Hanno Becker68636192019-02-05 14:36:34 +00006220 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6221 return( 0 );
6222
Hanno Becker8927c832019-04-03 12:52:50 +01006223 if( ssl->f_vrfy != NULL )
6224 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006225 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006226 f_vrfy = ssl->f_vrfy;
6227 p_vrfy = ssl->p_vrfy;
6228 }
6229 else
6230 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006231 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006232 f_vrfy = ssl->conf->f_vrfy;
6233 p_vrfy = ssl->conf->p_vrfy;
6234 }
6235
Hanno Becker68636192019-02-05 14:36:34 +00006236 /*
6237 * Main check: verify certificate
6238 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006239#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6240 if( ssl->conf->f_ca_cb != NULL )
6241 {
6242 ((void) rs_ctx);
6243 have_ca_chain = 1;
6244
6245 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006246 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006247 chain,
6248 ssl->conf->f_ca_cb,
6249 ssl->conf->p_ca_cb,
6250 ssl->conf->cert_profile,
6251 ssl->hostname,
6252 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006253 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006254 }
6255 else
6256#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6257 {
6258 mbedtls_x509_crt *ca_chain;
6259 mbedtls_x509_crl *ca_crl;
6260
6261#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6262 if( ssl->handshake->sni_ca_chain != NULL )
6263 {
6264 ca_chain = ssl->handshake->sni_ca_chain;
6265 ca_crl = ssl->handshake->sni_ca_crl;
6266 }
6267 else
6268#endif
6269 {
6270 ca_chain = ssl->conf->ca_chain;
6271 ca_crl = ssl->conf->ca_crl;
6272 }
6273
6274 if( ca_chain != NULL )
6275 have_ca_chain = 1;
6276
6277 ret = mbedtls_x509_crt_verify_restartable(
6278 chain,
6279 ca_chain, ca_crl,
6280 ssl->conf->cert_profile,
6281 ssl->hostname,
6282 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006283 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006284 }
Hanno Becker68636192019-02-05 14:36:34 +00006285
6286 if( ret != 0 )
6287 {
6288 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6289 }
6290
6291#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6292 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6293 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6294#endif
6295
6296 /*
6297 * Secondary checks: always done, but change 'ret' only if it was 0
6298 */
6299
6300#if defined(MBEDTLS_ECP_C)
6301 {
6302 const mbedtls_pk_context *pk = &chain->pk;
6303
6304 /* If certificate uses an EC key, make sure the curve is OK */
6305 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6306 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6307 {
6308 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6309
6310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6311 if( ret == 0 )
6312 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6313 }
6314 }
6315#endif /* MBEDTLS_ECP_C */
6316
6317 if( mbedtls_ssl_check_cert_usage( chain,
6318 ciphersuite_info,
6319 ! ssl->conf->endpoint,
6320 &ssl->session_negotiate->verify_result ) != 0 )
6321 {
6322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6323 if( ret == 0 )
6324 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6325 }
6326
6327 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6328 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6329 * with details encoded in the verification flags. All other kinds
6330 * of error codes, including those from the user provided f_vrfy
6331 * functions, are treated as fatal and lead to a failure of
6332 * ssl_parse_certificate even if verification was optional. */
6333 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6334 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6335 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6336 {
6337 ret = 0;
6338 }
6339
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006340 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006341 {
6342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6343 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6344 }
6345
6346 if( ret != 0 )
6347 {
6348 uint8_t alert;
6349
6350 /* The certificate may have been rejected for several reasons.
6351 Pick one and send the corresponding alert. Which alert to send
6352 may be a subject of debate in some cases. */
6353 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6354 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6355 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6356 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6357 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6358 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6359 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6360 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6361 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6362 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6363 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6364 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6365 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6366 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6367 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6368 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6369 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6370 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6371 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6372 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6373 else
6374 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6375 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6376 alert );
6377 }
6378
6379#if defined(MBEDTLS_DEBUG_C)
6380 if( ssl->session_negotiate->verify_result != 0 )
6381 {
6382 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6383 ssl->session_negotiate->verify_result ) );
6384 }
6385 else
6386 {
6387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6388 }
6389#endif /* MBEDTLS_DEBUG_C */
6390
6391 return( ret );
6392}
6393
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006394#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6395static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6396 unsigned char *start, size_t len )
6397{
6398 int ret;
6399 /* Remember digest of the peer's end-CRT. */
6400 ssl->session_negotiate->peer_cert_digest =
6401 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6402 if( ssl->session_negotiate->peer_cert_digest == NULL )
6403 {
6404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6405 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6406 mbedtls_ssl_send_alert_message( ssl,
6407 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6408 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6409
6410 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6411 }
6412
6413 ret = mbedtls_md( mbedtls_md_info_from_type(
6414 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6415 start, len,
6416 ssl->session_negotiate->peer_cert_digest );
6417
6418 ssl->session_negotiate->peer_cert_digest_type =
6419 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6420 ssl->session_negotiate->peer_cert_digest_len =
6421 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6422
6423 return( ret );
6424}
6425
6426static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6427 unsigned char *start, size_t len )
6428{
6429 unsigned char *end = start + len;
6430 int ret;
6431
6432 /* Make a copy of the peer's raw public key. */
6433 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6434 ret = mbedtls_pk_parse_subpubkey( &start, end,
6435 &ssl->handshake->peer_pubkey );
6436 if( ret != 0 )
6437 {
6438 /* We should have parsed the public key before. */
6439 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6440 }
6441
6442 return( 0 );
6443}
6444#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6445
Hanno Becker68636192019-02-05 14:36:34 +00006446int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6447{
6448 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006449 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006450#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6451 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6452 ? ssl->handshake->sni_authmode
6453 : ssl->conf->authmode;
6454#else
6455 const int authmode = ssl->conf->authmode;
6456#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006457 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006458 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006459
6460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6461
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006462 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6463 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006464 {
6465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006466 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006467 }
6468
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006469#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6470 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006471 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006472 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006473 chain = ssl->handshake->ecrs_peer_cert;
6474 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006475 goto crt_verify;
6476 }
6477#endif
6478
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006479 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006480 {
6481 /* mbedtls_ssl_read_record may have sent an alert already. We
6482 let it decide whether to alert. */
6483 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006484 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006485 }
6486
Hanno Becker4a55f632019-02-05 12:49:06 +00006487#if defined(MBEDTLS_SSL_SRV_C)
6488 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6489 {
6490 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006491
6492 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006493 ret = 0;
6494 else
6495 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006496
Hanno Becker6bdfab22019-02-05 13:11:17 +00006497 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006498 }
6499#endif /* MBEDTLS_SSL_SRV_C */
6500
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006501 /* Clear existing peer CRT structure in case we tried to
6502 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006503 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006504
6505 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6506 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006507 {
6508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6509 sizeof( mbedtls_x509_crt ) ) );
6510 mbedtls_ssl_send_alert_message( ssl,
6511 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6512 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006513
Hanno Becker3dad3112019-02-05 17:19:52 +00006514 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6515 goto exit;
6516 }
6517 mbedtls_x509_crt_init( chain );
6518
6519 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006520 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006521 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006522
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006523#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6524 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006525 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006526
6527crt_verify:
6528 if( ssl->handshake->ecrs_enabled)
6529 rs_ctx = &ssl->handshake->ecrs_ctx;
6530#endif
6531
Hanno Becker68636192019-02-05 14:36:34 +00006532 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006533 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006534 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006535 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006536
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006537#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006538 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006539 unsigned char *crt_start, *pk_start;
6540 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006541
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006542 /* We parse the CRT chain without copying, so
6543 * these pointers point into the input buffer,
6544 * and are hence still valid after freeing the
6545 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006546
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006547 crt_start = chain->raw.p;
6548 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006549
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006550 pk_start = chain->pk_raw.p;
6551 pk_len = chain->pk_raw.len;
6552
6553 /* Free the CRT structures before computing
6554 * digest and copying the peer's public key. */
6555 mbedtls_x509_crt_free( chain );
6556 mbedtls_free( chain );
6557 chain = NULL;
6558
6559 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006560 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006561 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006562
6563 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6564 if( ret != 0 )
6565 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006566 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006567#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6568 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006569 ssl->session_negotiate->peer_cert = chain;
6570 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006571#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006574
Hanno Becker6bdfab22019-02-05 13:11:17 +00006575exit:
6576
Hanno Becker3dad3112019-02-05 17:19:52 +00006577 if( ret == 0 )
6578 ssl->state++;
6579
6580#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6581 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6582 {
6583 ssl->handshake->ecrs_peer_cert = chain;
6584 chain = NULL;
6585 }
6586#endif
6587
6588 if( chain != NULL )
6589 {
6590 mbedtls_x509_crt_free( chain );
6591 mbedtls_free( chain );
6592 }
6593
Paul Bakker5121ce52009-01-03 21:22:43 +00006594 return( ret );
6595}
Hanno Becker21489932019-02-05 13:20:55 +00006596#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006598int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006599{
6600 int ret;
6601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006604 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006605 ssl->out_msglen = 1;
6606 ssl->out_msg[0] = 1;
6607
Paul Bakker5121ce52009-01-03 21:22:43 +00006608 ssl->state++;
6609
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006610 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006611 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006612 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006613 return( ret );
6614 }
6615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006617
6618 return( 0 );
6619}
6620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006622{
6623 int ret;
6624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006626
Hanno Becker327c93b2018-08-15 13:56:18 +01006627 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006629 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006630 return( ret );
6631 }
6632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006633 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006636 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6637 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006639 }
6640
Hanno Beckere678eaa2018-08-21 14:57:46 +01006641 /* CCS records are only accepted if they have length 1 and content '1',
6642 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006643
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006644 /*
6645 * Switch to our negotiated transform and session parameters for inbound
6646 * data.
6647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006649 ssl->transform_in = ssl->transform_negotiate;
6650 ssl->session_in = ssl->session_negotiate;
6651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006653 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006656 ssl_dtls_replay_reset( ssl );
6657#endif
6658
6659 /* Increment epoch */
6660 if( ++ssl->in_epoch == 0 )
6661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006663 /* This is highly unlikely to happen for legitimate reasons, so
6664 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006665 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006666 }
6667 }
6668 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006669#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006670 memset( ssl->in_ctr, 0, 8 );
6671
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006672 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6675 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006677 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006679 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006680 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6681 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006683 }
6684 }
6685#endif
6686
Paul Bakker5121ce52009-01-03 21:22:43 +00006687 ssl->state++;
6688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006690
6691 return( 0 );
6692}
6693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6695 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006696{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006697 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6700 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6701 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006702 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006703 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006704#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6706#if defined(MBEDTLS_SHA512_C)
6707 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006708 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6709 else
6710#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711#if defined(MBEDTLS_SHA256_C)
6712 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006713 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006714 else
6715#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006716#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006719 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006720 }
Paul Bakker380da532012-04-18 16:10:25 +00006721}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006723void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006724{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6726 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006727 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6728 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006729#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6731#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006732#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006733 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006734 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6735#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006736 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006737#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006738#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006740#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006741 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006742 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006743#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006744 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006745#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006746#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006747#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006748}
6749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006750static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006751 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006752{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6754 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006755 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6756 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006757#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006758#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6759#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006760#if defined(MBEDTLS_USE_PSA_CRYPTO)
6761 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6762#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006763 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006764#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006765#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006767#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006768 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006769#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006770 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006771#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006772#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006773#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006774}
6775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6777 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6778static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006779 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006780{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006781 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6782 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006783}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006784#endif
Paul Bakker380da532012-04-18 16:10:25 +00006785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6787#if defined(MBEDTLS_SHA256_C)
6788static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006789 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006790{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006791#if defined(MBEDTLS_USE_PSA_CRYPTO)
6792 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6793#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006794 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006795#endif
Paul Bakker380da532012-04-18 16:10:25 +00006796}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006797#endif
Paul Bakker380da532012-04-18 16:10:25 +00006798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799#if defined(MBEDTLS_SHA512_C)
6800static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006801 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006802{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006803#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006804 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006805#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006806 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006807#endif
Paul Bakker380da532012-04-18 16:10:25 +00006808}
Paul Bakker769075d2012-11-24 11:26:46 +01006809#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006813static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006815{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006816 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006817 mbedtls_md5_context md5;
6818 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006819
Paul Bakker5121ce52009-01-03 21:22:43 +00006820 unsigned char padbuf[48];
6821 unsigned char md5sum[16];
6822 unsigned char sha1sum[20];
6823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006824 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006825 if( !session )
6826 session = ssl->session;
6827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006829
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006830 mbedtls_md5_init( &md5 );
6831 mbedtls_sha1_init( &sha1 );
6832
6833 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6834 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006835
6836 /*
6837 * SSLv3:
6838 * hash =
6839 * MD5( master + pad2 +
6840 * MD5( handshake + sender + master + pad1 ) )
6841 * + SHA1( master + pad2 +
6842 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006843 */
6844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006845#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006846 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6847 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006848#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006851 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6852 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006853#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006856 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006857
Paul Bakker1ef83d62012-04-11 12:09:53 +00006858 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006859
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006860 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6861 mbedtls_md5_update_ret( &md5, session->master, 48 );
6862 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6863 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006864
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006865 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6866 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6867 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6868 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006869
Paul Bakker1ef83d62012-04-11 12:09:53 +00006870 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006871
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006872 mbedtls_md5_starts_ret( &md5 );
6873 mbedtls_md5_update_ret( &md5, session->master, 48 );
6874 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6875 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6876 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006877
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006878 mbedtls_sha1_starts_ret( &sha1 );
6879 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6880 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6881 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6882 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006884 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006885
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006886 mbedtls_md5_free( &md5 );
6887 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006888
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006889 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6890 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6891 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006894}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006895#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006897#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006898static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006899 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006900{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006901 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006902 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006903 mbedtls_md5_context md5;
6904 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006905 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006908 if( !session )
6909 session = ssl->session;
6910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006912
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006913 mbedtls_md5_init( &md5 );
6914 mbedtls_sha1_init( &sha1 );
6915
6916 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6917 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006918
Paul Bakker1ef83d62012-04-11 12:09:53 +00006919 /*
6920 * TLSv1:
6921 * hash = PRF( master, finished_label,
6922 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6923 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006926 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6927 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006928#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006931 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6932 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006933#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006935 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006936 ? "client finished"
6937 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006938
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006939 mbedtls_md5_finish_ret( &md5, padbuf );
6940 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006941
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006942 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006943 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006946
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006947 mbedtls_md5_free( &md5 );
6948 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006949
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006950 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006953}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006954#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6957#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006958static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006960{
6961 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006962 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006963 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006964#if defined(MBEDTLS_USE_PSA_CRYPTO)
6965 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006966 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006967 psa_status_t status;
6968#else
6969 mbedtls_sha256_context sha256;
6970#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006973 if( !session )
6974 session = ssl->session;
6975
Andrzej Kurekeb342242019-01-29 09:14:33 -05006976 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6977 ? "client finished"
6978 : "server finished";
6979
6980#if defined(MBEDTLS_USE_PSA_CRYPTO)
6981 sha256_psa = psa_hash_operation_init();
6982
6983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6984
6985 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6986 if( status != PSA_SUCCESS )
6987 {
6988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6989 return;
6990 }
6991
6992 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6993 if( status != PSA_SUCCESS )
6994 {
6995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6996 return;
6997 }
6998 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6999#else
7000
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007001 mbedtls_sha256_init( &sha256 );
7002
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007004
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007005 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007006
7007 /*
7008 * TLSv1.2:
7009 * hash = PRF( master, finished_label,
7010 * Hash( handshake ) )[0.11]
7011 */
7012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013#if !defined(MBEDTLS_SHA256_ALT)
7014 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007015 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007016#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007017
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007018 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007019 mbedtls_sha256_free( &sha256 );
7020#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007021
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007022 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007023 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007025 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007026
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007027 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007030}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007033#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007034static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007036{
7037 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007038 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007039 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007040#if defined(MBEDTLS_USE_PSA_CRYPTO)
7041 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007042 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007043 psa_status_t status;
7044#else
7045 mbedtls_sha512_context sha512;
7046#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007048 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007049 if( !session )
7050 session = ssl->session;
7051
Andrzej Kurekeb342242019-01-29 09:14:33 -05007052 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7053 ? "client finished"
7054 : "server finished";
7055
7056#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007057 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007058
7059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7060
Andrzej Kurek972fba52019-01-30 03:29:12 -05007061 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007062 if( status != PSA_SUCCESS )
7063 {
7064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7065 return;
7066 }
7067
Andrzej Kurek972fba52019-01-30 03:29:12 -05007068 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007069 if( status != PSA_SUCCESS )
7070 {
7071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7072 return;
7073 }
7074 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7075#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007076 mbedtls_sha512_init( &sha512 );
7077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007079
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007080 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007081
7082 /*
7083 * TLSv1.2:
7084 * hash = PRF( master, finished_label,
7085 * Hash( handshake ) )[0.11]
7086 */
7087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007089 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7090 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007091#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007092
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007093 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007094 mbedtls_sha512_free( &sha512 );
7095#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007096
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007097 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007098 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007101
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007102 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007105}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106#endif /* MBEDTLS_SHA512_C */
7107#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007110{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007112
7113 /*
7114 * Free our handshake params
7115 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007116 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007118 ssl->handshake = NULL;
7119
7120 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007121 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007122 */
7123 if( ssl->transform )
7124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125 mbedtls_ssl_transform_free( ssl->transform );
7126 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007127 }
7128 ssl->transform = ssl->transform_negotiate;
7129 ssl->transform_negotiate = NULL;
7130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007132}
7133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007135{
7136 int resume = ssl->handshake->resume;
7137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140#if defined(MBEDTLS_SSL_RENEGOTIATION)
7141 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007143 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007144 ssl->renego_records_seen = 0;
7145 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007146#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007147
7148 /*
7149 * Free the previous session and switch in the current one
7150 */
Paul Bakker0a597072012-09-25 21:55:46 +00007151 if( ssl->session )
7152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007154 /* RFC 7366 3.1: keep the EtM state */
7155 ssl->session_negotiate->encrypt_then_mac =
7156 ssl->session->encrypt_then_mac;
7157#endif
7158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159 mbedtls_ssl_session_free( ssl->session );
7160 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007161 }
7162 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007163 ssl->session_negotiate = NULL;
7164
Paul Bakker0a597072012-09-25 21:55:46 +00007165 /*
7166 * Add cache entry
7167 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007168 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007169 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007170 resume == 0 )
7171 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007172 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007174 }
Paul Bakker0a597072012-09-25 21:55:46 +00007175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007177 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007178 ssl->handshake->flight != NULL )
7179 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007180 /* Cancel handshake timer */
7181 ssl_set_timer( ssl, 0 );
7182
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007183 /* Keep last flight around in case we need to resend it:
7184 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007186 }
7187 else
7188#endif
7189 ssl_handshake_wrapup_free_hs_transform( ssl );
7190
Paul Bakker48916f92012-09-16 19:57:18 +00007191 ssl->state++;
7192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007194}
7195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007197{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007198 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007201
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007202 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007203
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007204 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007205
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007206 /*
7207 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7208 * may define some other value. Currently (early 2016), no defined
7209 * ciphersuite does this (and this is unlikely to change as activity has
7210 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007215 ssl->verify_data_len = hash_len;
7216 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007217#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007218
Paul Bakker5121ce52009-01-03 21:22:43 +00007219 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7221 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007222
7223 /*
7224 * In case of session resuming, invert the client and server
7225 * ChangeCipherSpec messages order.
7226 */
Paul Bakker0a597072012-09-25 21:55:46 +00007227 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007229#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007230 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007231 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007232#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007233#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007234 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007236#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007237 }
7238 else
7239 ssl->state++;
7240
Paul Bakker48916f92012-09-16 19:57:18 +00007241 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007242 * Switch to our negotiated transform and session parameters for outbound
7243 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007245 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007248 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007249 {
7250 unsigned char i;
7251
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007252 /* Remember current epoch settings for resending */
7253 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007254 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007255
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007256 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007257 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007258
7259 /* Increment epoch */
7260 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007261 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007262 break;
7263
7264 /* The loop goes to its end iff the counter is wrapping */
7265 if( i == 0 )
7266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7268 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007269 }
7270 }
7271 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007273 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007274
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007275 ssl->transform_out = ssl->transform_negotiate;
7276 ssl->session_out = ssl->session_negotiate;
7277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7279 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007281 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7284 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007285 }
7286 }
7287#endif
7288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007290 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007292#endif
7293
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007294 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007295 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007296 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007297 return( ret );
7298 }
7299
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007300#if defined(MBEDTLS_SSL_PROTO_DTLS)
7301 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7302 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7303 {
7304 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7305 return( ret );
7306 }
7307#endif
7308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007310
7311 return( 0 );
7312}
7313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007315#define SSL_MAX_HASH_LEN 36
7316#else
7317#define SSL_MAX_HASH_LEN 12
7318#endif
7319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007321{
Paul Bakker23986e52011-04-24 08:57:21 +00007322 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007323 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007324 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007327
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007328 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007329
Hanno Becker327c93b2018-08-15 13:56:18 +01007330 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007332 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007333 return( ret );
7334 }
7335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007339 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7340 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007341 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007342 }
7343
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007344 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007345#if defined(MBEDTLS_SSL_PROTO_SSL3)
7346 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007347 hash_len = 36;
7348 else
7349#endif
7350 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007352 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7353 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007356 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7357 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007358 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007359 }
7360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007361 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007362 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007365 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7366 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007368 }
7369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007371 ssl->verify_data_len = hash_len;
7372 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007373#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007374
Paul Bakker0a597072012-09-25 21:55:46 +00007375 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007378 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007380#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007382 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007384#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007385 }
7386 else
7387 ssl->state++;
7388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007390 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007391 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007392#endif
7393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007395
7396 return( 0 );
7397}
7398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007400{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7404 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7405 mbedtls_md5_init( &handshake->fin_md5 );
7406 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007407 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7408 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007409#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007410#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7411#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007412#if defined(MBEDTLS_USE_PSA_CRYPTO)
7413 handshake->fin_sha256_psa = psa_hash_operation_init();
7414 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7415#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007417 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007418#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007419#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007421#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007422 handshake->fin_sha384_psa = psa_hash_operation_init();
7423 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007424#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007425 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007426 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007427#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007428#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007429#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007430
7431 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007432
7433#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7434 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7435 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7436#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007438#if defined(MBEDTLS_DHM_C)
7439 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007440#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441#if defined(MBEDTLS_ECDH_C)
7442 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007443#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007444#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007445 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007446#if defined(MBEDTLS_SSL_CLI_C)
7447 handshake->ecjpake_cache = NULL;
7448 handshake->ecjpake_cache_len = 0;
7449#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007450#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007451
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007452#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007453 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007454#endif
7455
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007456#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7457 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7458#endif
Hanno Becker75173122019-02-06 16:18:31 +00007459
7460#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7461 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7462 mbedtls_pk_init( &handshake->peer_pubkey );
7463#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007464}
7465
Hanno Beckera18d1322018-01-03 14:27:32 +00007466void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007467{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7471 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007472
Hanno Beckerd56ed242018-01-03 15:32:51 +00007473#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474 mbedtls_md_init( &transform->md_ctx_enc );
7475 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007476#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007477}
7478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007479void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007480{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007481 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007482}
7483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007485{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007486 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007487 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007489 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007490 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007491 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007492 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007493
7494 /*
7495 * Either the pointers are now NULL or cleared properly and can be freed.
7496 * Now allocate missing structures.
7497 */
7498 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007499 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007500 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007501 }
Paul Bakker48916f92012-09-16 19:57:18 +00007502
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007503 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007504 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007505 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007506 }
Paul Bakker48916f92012-09-16 19:57:18 +00007507
Paul Bakker82788fb2014-10-20 13:59:19 +02007508 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007509 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007510 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007511 }
Paul Bakker48916f92012-09-16 19:57:18 +00007512
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007513 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007514 if( ssl->handshake == NULL ||
7515 ssl->transform_negotiate == NULL ||
7516 ssl->session_negotiate == NULL )
7517 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520 mbedtls_free( ssl->handshake );
7521 mbedtls_free( ssl->transform_negotiate );
7522 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007523
7524 ssl->handshake = NULL;
7525 ssl->transform_negotiate = NULL;
7526 ssl->session_negotiate = NULL;
7527
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007528 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007529 }
7530
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007531 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007533 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007534 ssl_handshake_params_init( ssl->handshake );
7535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007536#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007537 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7538 {
7539 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007540
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007541 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7542 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7543 else
7544 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007545
7546 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007547 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007548#endif
7549
Paul Bakker48916f92012-09-16 19:57:18 +00007550 return( 0 );
7551}
7552
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007553#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007554/* Dummy cookie callbacks for defaults */
7555static int ssl_cookie_write_dummy( void *ctx,
7556 unsigned char **p, unsigned char *end,
7557 const unsigned char *cli_id, size_t cli_id_len )
7558{
7559 ((void) ctx);
7560 ((void) p);
7561 ((void) end);
7562 ((void) cli_id);
7563 ((void) cli_id_len);
7564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007566}
7567
7568static int ssl_cookie_check_dummy( void *ctx,
7569 const unsigned char *cookie, size_t cookie_len,
7570 const unsigned char *cli_id, size_t cli_id_len )
7571{
7572 ((void) ctx);
7573 ((void) cookie);
7574 ((void) cookie_len);
7575 ((void) cli_id);
7576 ((void) cli_id_len);
7577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007579}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007580#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007581
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007582/* Once ssl->out_hdr as the address of the beginning of the
7583 * next outgoing record is set, deduce the other pointers.
7584 *
7585 * Note: For TLS, we save the implicit record sequence number
7586 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7587 * and the caller has to make sure there's space for this.
7588 */
7589
7590static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7591 mbedtls_ssl_transform *transform )
7592{
7593#if defined(MBEDTLS_SSL_PROTO_DTLS)
7594 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7595 {
7596 ssl->out_ctr = ssl->out_hdr + 3;
7597 ssl->out_len = ssl->out_hdr + 11;
7598 ssl->out_iv = ssl->out_hdr + 13;
7599 }
7600 else
7601#endif
7602 {
7603 ssl->out_ctr = ssl->out_hdr - 8;
7604 ssl->out_len = ssl->out_hdr + 3;
7605 ssl->out_iv = ssl->out_hdr + 5;
7606 }
7607
7608 /* Adjust out_msg to make space for explicit IV, if used. */
7609 if( transform != NULL &&
7610 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7611 {
7612 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7613 }
7614 else
7615 ssl->out_msg = ssl->out_iv;
7616}
7617
7618/* Once ssl->in_hdr as the address of the beginning of the
7619 * next incoming record is set, deduce the other pointers.
7620 *
7621 * Note: For TLS, we save the implicit record sequence number
7622 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7623 * and the caller has to make sure there's space for this.
7624 */
7625
7626static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7627 mbedtls_ssl_transform *transform )
7628{
7629#if defined(MBEDTLS_SSL_PROTO_DTLS)
7630 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7631 {
7632 ssl->in_ctr = ssl->in_hdr + 3;
7633 ssl->in_len = ssl->in_hdr + 11;
7634 ssl->in_iv = ssl->in_hdr + 13;
7635 }
7636 else
7637#endif
7638 {
7639 ssl->in_ctr = ssl->in_hdr - 8;
7640 ssl->in_len = ssl->in_hdr + 3;
7641 ssl->in_iv = ssl->in_hdr + 5;
7642 }
7643
7644 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7645 if( transform != NULL &&
7646 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7647 {
7648 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7649 }
7650 else
7651 ssl->in_msg = ssl->in_iv;
7652}
7653
Paul Bakker5121ce52009-01-03 21:22:43 +00007654/*
7655 * Initialize an SSL context
7656 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007657void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7658{
7659 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7660}
7661
7662/*
7663 * Setup an SSL context
7664 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007665
7666static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7667{
7668 /* Set the incoming and outgoing record pointers. */
7669#if defined(MBEDTLS_SSL_PROTO_DTLS)
7670 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7671 {
7672 ssl->out_hdr = ssl->out_buf;
7673 ssl->in_hdr = ssl->in_buf;
7674 }
7675 else
7676#endif /* MBEDTLS_SSL_PROTO_DTLS */
7677 {
7678 ssl->out_hdr = ssl->out_buf + 8;
7679 ssl->in_hdr = ssl->in_buf + 8;
7680 }
7681
7682 /* Derive other internal pointers. */
7683 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7684 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7685}
7686
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007687int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007688 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007689{
Paul Bakker48916f92012-09-16 19:57:18 +00007690 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007691
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007692 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007693
7694 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007695 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007696 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007697
7698 /* Set to NULL in case of an error condition */
7699 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007700
Angus Grattond8213d02016-05-25 20:56:48 +10007701 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7702 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007703 {
Angus Grattond8213d02016-05-25 20:56:48 +10007704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007705 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007706 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007707 }
7708
7709 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7710 if( ssl->out_buf == NULL )
7711 {
7712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007713 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007714 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007715 }
7716
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007717 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007718
Paul Bakker48916f92012-09-16 19:57:18 +00007719 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007720 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007721
7722 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007723
7724error:
7725 mbedtls_free( ssl->in_buf );
7726 mbedtls_free( ssl->out_buf );
7727
7728 ssl->conf = NULL;
7729
7730 ssl->in_buf = NULL;
7731 ssl->out_buf = NULL;
7732
7733 ssl->in_hdr = NULL;
7734 ssl->in_ctr = NULL;
7735 ssl->in_len = NULL;
7736 ssl->in_iv = NULL;
7737 ssl->in_msg = NULL;
7738
7739 ssl->out_hdr = NULL;
7740 ssl->out_ctr = NULL;
7741 ssl->out_len = NULL;
7742 ssl->out_iv = NULL;
7743 ssl->out_msg = NULL;
7744
k-stachowiak9f7798e2018-07-31 16:52:32 +02007745 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007746}
7747
7748/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007749 * Reset an initialized and used SSL context for re-use while retaining
7750 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007751 *
7752 * If partial is non-zero, keep data in the input buffer and client ID.
7753 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007754 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007755static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007756{
Paul Bakker48916f92012-09-16 19:57:18 +00007757 int ret;
7758
Hanno Becker7e772132018-08-10 12:38:21 +01007759#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7760 !defined(MBEDTLS_SSL_SRV_C)
7761 ((void) partial);
7762#endif
7763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007765
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007766 /* Cancel any possibly running timer */
7767 ssl_set_timer( ssl, 0 );
7768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769#if defined(MBEDTLS_SSL_RENEGOTIATION)
7770 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007771 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007772
7773 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7775 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007776#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007778
Paul Bakker7eb013f2011-10-06 12:37:39 +00007779 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007780 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007781
7782 ssl->in_msgtype = 0;
7783 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007785 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007786 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007787#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007789 ssl_dtls_replay_reset( ssl );
7790#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007791
7792 ssl->in_hslen = 0;
7793 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007794
7795 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007796
7797 ssl->out_msgtype = 0;
7798 ssl->out_msglen = 0;
7799 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007800#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7801 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007802 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007803#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007804
Hanno Becker19859472018-08-06 09:40:20 +01007805 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7806
Paul Bakker48916f92012-09-16 19:57:18 +00007807 ssl->transform_in = NULL;
7808 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007809
Hanno Becker78640902018-08-13 16:35:15 +01007810 ssl->session_in = NULL;
7811 ssl->session_out = NULL;
7812
Angus Grattond8213d02016-05-25 20:56:48 +10007813 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007814
7815#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007816 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007817#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7818 {
7819 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007820 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007821 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007823#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7824 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7827 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7830 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007831 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007832 }
7833#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007834
Paul Bakker48916f92012-09-16 19:57:18 +00007835 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007837 mbedtls_ssl_transform_free( ssl->transform );
7838 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007839 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007840 }
Paul Bakker48916f92012-09-16 19:57:18 +00007841
Paul Bakkerc0463502013-02-14 11:19:38 +01007842 if( ssl->session )
7843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844 mbedtls_ssl_session_free( ssl->session );
7845 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007846 ssl->session = NULL;
7847 }
7848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007850 ssl->alpn_chosen = NULL;
7851#endif
7852
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007853#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007854#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007855 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007856#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007857 {
7858 mbedtls_free( ssl->cli_id );
7859 ssl->cli_id = NULL;
7860 ssl->cli_id_len = 0;
7861 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007862#endif
7863
Paul Bakker48916f92012-09-16 19:57:18 +00007864 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7865 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007866
7867 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007868}
7869
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007870/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007871 * Reset an initialized and used SSL context for re-use while retaining
7872 * all application-set variables, function pointers and data.
7873 */
7874int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7875{
7876 return( ssl_session_reset_int( ssl, 0 ) );
7877}
7878
7879/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007880 * SSL set accessors
7881 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007882void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007883{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007884 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007885}
7886
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007887void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007888{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007889 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007890}
7891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007893void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007894{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007895 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007896}
7897#endif
7898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007900void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007901{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007902 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007903}
7904#endif
7905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007907
Hanno Becker1841b0a2018-08-24 11:13:57 +01007908void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7909 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007910{
7911 ssl->disable_datagram_packing = !allow_packing;
7912}
7913
7914void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7915 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007916{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007917 conf->hs_timeout_min = min;
7918 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007919}
7920#endif
7921
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007922void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007923{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007924 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007925}
7926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007928void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007929 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007930 void *p_vrfy )
7931{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007932 conf->f_vrfy = f_vrfy;
7933 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007934}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007936
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007937void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007938 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007939 void *p_rng )
7940{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007941 conf->f_rng = f_rng;
7942 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007943}
7944
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007945void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007946 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007947 void *p_dbg )
7948{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007949 conf->f_dbg = f_dbg;
7950 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007951}
7952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007953void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007954 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007955 mbedtls_ssl_send_t *f_send,
7956 mbedtls_ssl_recv_t *f_recv,
7957 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007958{
7959 ssl->p_bio = p_bio;
7960 ssl->f_send = f_send;
7961 ssl->f_recv = f_recv;
7962 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007963}
7964
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007965#if defined(MBEDTLS_SSL_PROTO_DTLS)
7966void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7967{
7968 ssl->mtu = mtu;
7969}
7970#endif
7971
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007972void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007973{
7974 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007975}
7976
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007977void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7978 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007979 mbedtls_ssl_set_timer_t *f_set_timer,
7980 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007981{
7982 ssl->p_timer = p_timer;
7983 ssl->f_set_timer = f_set_timer;
7984 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007985
7986 /* Make sure we start with no timer running */
7987 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007988}
7989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007990#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007991void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007992 void *p_cache,
7993 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7994 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007995{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007996 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007997 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007998 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007999}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008000#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008002#if defined(MBEDTLS_SSL_CLI_C)
8003int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008004{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008005 int ret;
8006
8007 if( ssl == NULL ||
8008 session == NULL ||
8009 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008010 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008013 }
8014
Hanno Becker52055ae2019-02-06 14:30:46 +00008015 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8016 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008017 return( ret );
8018
Paul Bakker0a597072012-09-25 21:55:46 +00008019 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008020
8021 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008022}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008023#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008024
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008025void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008026 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008027{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008028 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8029 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8030 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8031 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008032}
8033
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008034void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008035 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008036 int major, int minor )
8037{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008038 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008039 return;
8040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008041 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008042 return;
8043
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008044 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008045}
8046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008047#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008048void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008049 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008050{
8051 conf->cert_profile = profile;
8052}
8053
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008054/* Append a new keycert entry to a (possibly empty) list */
8055static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8056 mbedtls_x509_crt *cert,
8057 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008058{
niisato8ee24222018-06-25 19:05:48 +09008059 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008060
niisato8ee24222018-06-25 19:05:48 +09008061 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8062 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008063 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008064
niisato8ee24222018-06-25 19:05:48 +09008065 new_cert->cert = cert;
8066 new_cert->key = key;
8067 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008068
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008069 /* Update head is the list was null, else add to the end */
8070 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008071 {
niisato8ee24222018-06-25 19:05:48 +09008072 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008073 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008074 else
8075 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008076 mbedtls_ssl_key_cert *cur = *head;
8077 while( cur->next != NULL )
8078 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008079 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008080 }
8081
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008082 return( 0 );
8083}
8084
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008085int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008086 mbedtls_x509_crt *own_cert,
8087 mbedtls_pk_context *pk_key )
8088{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008089 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008090}
8091
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008092void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008093 mbedtls_x509_crt *ca_chain,
8094 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008095{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008096 conf->ca_chain = ca_chain;
8097 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008098
8099#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8100 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8101 * cannot be used together. */
8102 conf->f_ca_cb = NULL;
8103 conf->p_ca_cb = NULL;
8104#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008105}
Hanno Becker5adaad92019-03-27 16:54:37 +00008106
8107#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8108void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008109 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008110 void *p_ca_cb )
8111{
8112 conf->f_ca_cb = f_ca_cb;
8113 conf->p_ca_cb = p_ca_cb;
8114
8115 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8116 * cannot be used together. */
8117 conf->ca_chain = NULL;
8118 conf->ca_crl = NULL;
8119}
8120#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008121#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008122
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008123#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8124int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8125 mbedtls_x509_crt *own_cert,
8126 mbedtls_pk_context *pk_key )
8127{
8128 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8129 own_cert, pk_key ) );
8130}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008131
8132void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8133 mbedtls_x509_crt *ca_chain,
8134 mbedtls_x509_crl *ca_crl )
8135{
8136 ssl->handshake->sni_ca_chain = ca_chain;
8137 ssl->handshake->sni_ca_crl = ca_crl;
8138}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008139
8140void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8141 int authmode )
8142{
8143 ssl->handshake->sni_authmode = authmode;
8144}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008145#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8146
Hanno Becker8927c832019-04-03 12:52:50 +01008147#if defined(MBEDTLS_X509_CRT_PARSE_C)
8148void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8149 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8150 void *p_vrfy )
8151{
8152 ssl->f_vrfy = f_vrfy;
8153 ssl->p_vrfy = p_vrfy;
8154}
8155#endif
8156
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008157#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008158/*
8159 * Set EC J-PAKE password for current handshake
8160 */
8161int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8162 const unsigned char *pw,
8163 size_t pw_len )
8164{
8165 mbedtls_ecjpake_role role;
8166
Janos Follath8eb64132016-06-03 15:40:57 +01008167 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008168 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8169
8170 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8171 role = MBEDTLS_ECJPAKE_SERVER;
8172 else
8173 role = MBEDTLS_ECJPAKE_CLIENT;
8174
8175 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8176 role,
8177 MBEDTLS_MD_SHA256,
8178 MBEDTLS_ECP_DP_SECP256R1,
8179 pw, pw_len ) );
8180}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008181#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008184
8185static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8186{
8187 /* Remove reference to existing PSK, if any. */
8188#if defined(MBEDTLS_USE_PSA_CRYPTO)
8189 if( conf->psk_opaque != 0 )
8190 {
8191 /* The maintenance of the PSK key slot is the
8192 * user's responsibility. */
8193 conf->psk_opaque = 0;
8194 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008195 /* This and the following branch should never
8196 * be taken simultaenously as we maintain the
8197 * invariant that raw and opaque PSKs are never
8198 * configured simultaneously. As a safeguard,
8199 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008200#endif /* MBEDTLS_USE_PSA_CRYPTO */
8201 if( conf->psk != NULL )
8202 {
8203 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8204
8205 mbedtls_free( conf->psk );
8206 conf->psk = NULL;
8207 conf->psk_len = 0;
8208 }
8209
8210 /* Remove reference to PSK identity, if any. */
8211 if( conf->psk_identity != NULL )
8212 {
8213 mbedtls_free( conf->psk_identity );
8214 conf->psk_identity = NULL;
8215 conf->psk_identity_len = 0;
8216 }
8217}
8218
Hanno Becker7390c712018-11-15 13:33:04 +00008219/* This function assumes that PSK identity in the SSL config is unset.
8220 * It checks that the provided identity is well-formed and attempts
8221 * to make a copy of it in the SSL config.
8222 * On failure, the PSK identity in the config remains unset. */
8223static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8224 unsigned char const *psk_identity,
8225 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008226{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008227 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008228 if( psk_identity == NULL ||
8229 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008230 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008231 {
8232 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8233 }
8234
Hanno Becker7390c712018-11-15 13:33:04 +00008235 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8236 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008237 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008238
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008239 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008240 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008241
8242 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008243}
8244
Hanno Becker7390c712018-11-15 13:33:04 +00008245int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8246 const unsigned char *psk, size_t psk_len,
8247 const unsigned char *psk_identity, size_t psk_identity_len )
8248{
8249 int ret;
8250 /* Remove opaque/raw PSK + PSK Identity */
8251 ssl_conf_remove_psk( conf );
8252
8253 /* Check and set raw PSK */
8254 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8255 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8256 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8257 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8258 conf->psk_len = psk_len;
8259 memcpy( conf->psk, psk, conf->psk_len );
8260
8261 /* Check and set PSK Identity */
8262 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8263 if( ret != 0 )
8264 ssl_conf_remove_psk( conf );
8265
8266 return( ret );
8267}
8268
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008269static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8270{
8271#if defined(MBEDTLS_USE_PSA_CRYPTO)
8272 if( ssl->handshake->psk_opaque != 0 )
8273 {
8274 ssl->handshake->psk_opaque = 0;
8275 }
8276 else
8277#endif /* MBEDTLS_USE_PSA_CRYPTO */
8278 if( ssl->handshake->psk != NULL )
8279 {
8280 mbedtls_platform_zeroize( ssl->handshake->psk,
8281 ssl->handshake->psk_len );
8282 mbedtls_free( ssl->handshake->psk );
8283 ssl->handshake->psk_len = 0;
8284 }
8285}
8286
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008287int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8288 const unsigned char *psk, size_t psk_len )
8289{
8290 if( psk == NULL || ssl->handshake == NULL )
8291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8292
8293 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8294 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8295
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008296 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008297
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008298 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008299 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008300
8301 ssl->handshake->psk_len = psk_len;
8302 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8303
8304 return( 0 );
8305}
8306
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008307#if defined(MBEDTLS_USE_PSA_CRYPTO)
8308int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008309 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008310 const unsigned char *psk_identity,
8311 size_t psk_identity_len )
8312{
Hanno Becker7390c712018-11-15 13:33:04 +00008313 int ret;
8314 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008315 ssl_conf_remove_psk( conf );
8316
Hanno Becker7390c712018-11-15 13:33:04 +00008317 /* Check and set opaque PSK */
8318 if( psk_slot == 0 )
8319 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008320 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008321
8322 /* Check and set PSK Identity */
8323 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8324 psk_identity_len );
8325 if( ret != 0 )
8326 ssl_conf_remove_psk( conf );
8327
8328 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008329}
8330
8331int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008332 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008333{
8334 if( psk_slot == 0 || ssl->handshake == NULL )
8335 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8336
8337 ssl_remove_psk( ssl );
8338 ssl->handshake->psk_opaque = psk_slot;
8339 return( 0 );
8340}
8341#endif /* MBEDTLS_USE_PSA_CRYPTO */
8342
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008343void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008344 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008345 size_t),
8346 void *p_psk )
8347{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008348 conf->f_psk = f_psk;
8349 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008350}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008352
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008353#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008354
8355#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008356int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008357{
8358 int ret;
8359
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008360 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8361 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8362 {
8363 mbedtls_mpi_free( &conf->dhm_P );
8364 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008365 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008366 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008367
8368 return( 0 );
8369}
Hanno Becker470a8c42017-10-04 15:28:46 +01008370#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008371
Hanno Beckera90658f2017-10-04 15:29:08 +01008372int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8373 const unsigned char *dhm_P, size_t P_len,
8374 const unsigned char *dhm_G, size_t G_len )
8375{
8376 int ret;
8377
8378 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8379 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8380 {
8381 mbedtls_mpi_free( &conf->dhm_P );
8382 mbedtls_mpi_free( &conf->dhm_G );
8383 return( ret );
8384 }
8385
8386 return( 0 );
8387}
Paul Bakker5121ce52009-01-03 21:22:43 +00008388
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008389int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008390{
8391 int ret;
8392
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008393 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8394 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8395 {
8396 mbedtls_mpi_free( &conf->dhm_P );
8397 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008398 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008399 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008400
8401 return( 0 );
8402}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008403#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008404
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008405#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8406/*
8407 * Set the minimum length for Diffie-Hellman parameters
8408 */
8409void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8410 unsigned int bitlen )
8411{
8412 conf->dhm_min_bitlen = bitlen;
8413}
8414#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8415
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008416#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008417/*
8418 * Set allowed/preferred hashes for handshake signatures
8419 */
8420void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8421 const int *hashes )
8422{
8423 conf->sig_hashes = hashes;
8424}
Hanno Becker947194e2017-04-07 13:25:49 +01008425#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008426
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008427#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008428/*
8429 * Set the allowed elliptic curves
8430 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008431void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008432 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008433{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008434 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008435}
Hanno Becker947194e2017-04-07 13:25:49 +01008436#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008437
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008438#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008439int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008440{
Hanno Becker947194e2017-04-07 13:25:49 +01008441 /* Initialize to suppress unnecessary compiler warning */
8442 size_t hostname_len = 0;
8443
8444 /* Check if new hostname is valid before
8445 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008446 if( hostname != NULL )
8447 {
8448 hostname_len = strlen( hostname );
8449
8450 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8451 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8452 }
8453
8454 /* Now it's clear that we will overwrite the old hostname,
8455 * so we can free it safely */
8456
8457 if( ssl->hostname != NULL )
8458 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008459 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008460 mbedtls_free( ssl->hostname );
8461 }
8462
8463 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008464
Paul Bakker5121ce52009-01-03 21:22:43 +00008465 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008466 {
8467 ssl->hostname = NULL;
8468 }
8469 else
8470 {
8471 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008472 if( ssl->hostname == NULL )
8473 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008474
Hanno Becker947194e2017-04-07 13:25:49 +01008475 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008476
Hanno Becker947194e2017-04-07 13:25:49 +01008477 ssl->hostname[hostname_len] = '\0';
8478 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008479
8480 return( 0 );
8481}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008482#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008483
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008484#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008485void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008486 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008487 const unsigned char *, size_t),
8488 void *p_sni )
8489{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008490 conf->f_sni = f_sni;
8491 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008492}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008493#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008495#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008496int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008497{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008498 size_t cur_len, tot_len;
8499 const char **p;
8500
8501 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008502 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8503 * MUST NOT be truncated."
8504 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008505 */
8506 tot_len = 0;
8507 for( p = protos; *p != NULL; p++ )
8508 {
8509 cur_len = strlen( *p );
8510 tot_len += cur_len;
8511
8512 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008513 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008514 }
8515
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008516 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008517
8518 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008519}
8520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008521const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008522{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008523 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008524}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008525#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008526
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008527void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008528{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008529 conf->max_major_ver = major;
8530 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008531}
8532
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008533void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008534{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008535 conf->min_major_ver = major;
8536 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008537}
8538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008539#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008540void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008541{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008542 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008543}
8544#endif
8545
Janos Follath088ce432017-04-10 12:42:31 +01008546#if defined(MBEDTLS_SSL_SRV_C)
8547void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8548 char cert_req_ca_list )
8549{
8550 conf->cert_req_ca_list = cert_req_ca_list;
8551}
8552#endif
8553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008554#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008555void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008556{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008557 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008558}
8559#endif
8560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008561#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008562void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008563{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008564 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008565}
8566#endif
8567
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008568#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008569void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008570{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008571 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008572}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008573#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008575#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008576int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008577{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008579 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008581 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008582 }
8583
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008584 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008585
8586 return( 0 );
8587}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008588#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008590#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008591void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008592{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008593 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008594}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008597#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008598void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008599{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008600 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008601}
8602#endif
8603
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008604void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008605{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008606 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008607}
8608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008610void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008611{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008612 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008613}
8614
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008615void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008616{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008617 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008618}
8619
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008620void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008621 const unsigned char period[8] )
8622{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008623 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008624}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008625#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008627#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008628#if defined(MBEDTLS_SSL_CLI_C)
8629void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008630{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008631 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008632}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008633#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008634
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008635#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008636void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8637 mbedtls_ssl_ticket_write_t *f_ticket_write,
8638 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8639 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008640{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008641 conf->f_ticket_write = f_ticket_write;
8642 conf->f_ticket_parse = f_ticket_parse;
8643 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008644}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008645#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008646#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008647
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008648#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8649void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8650 mbedtls_ssl_export_keys_t *f_export_keys,
8651 void *p_export_keys )
8652{
8653 conf->f_export_keys = f_export_keys;
8654 conf->p_export_keys = p_export_keys;
8655}
8656#endif
8657
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008658#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008659void mbedtls_ssl_conf_async_private_cb(
8660 mbedtls_ssl_config *conf,
8661 mbedtls_ssl_async_sign_t *f_async_sign,
8662 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8663 mbedtls_ssl_async_resume_t *f_async_resume,
8664 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008665 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008666{
8667 conf->f_async_sign_start = f_async_sign;
8668 conf->f_async_decrypt_start = f_async_decrypt;
8669 conf->f_async_resume = f_async_resume;
8670 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008671 conf->p_async_config_data = async_config_data;
8672}
8673
Gilles Peskine8f97af72018-04-26 11:46:10 +02008674void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8675{
8676 return( conf->p_async_config_data );
8677}
8678
Gilles Peskine1febfef2018-04-30 11:54:39 +02008679void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008680{
8681 if( ssl->handshake == NULL )
8682 return( NULL );
8683 else
8684 return( ssl->handshake->user_async_ctx );
8685}
8686
Gilles Peskine1febfef2018-04-30 11:54:39 +02008687void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008688 void *ctx )
8689{
8690 if( ssl->handshake != NULL )
8691 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008692}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008693#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008694
Paul Bakker5121ce52009-01-03 21:22:43 +00008695/*
8696 * SSL get accessors
8697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008698size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008699{
8700 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8701}
8702
Hanno Becker8b170a02017-10-10 11:51:19 +01008703int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8704{
8705 /*
8706 * Case A: We're currently holding back
8707 * a message for further processing.
8708 */
8709
8710 if( ssl->keep_current_message == 1 )
8711 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008712 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008713 return( 1 );
8714 }
8715
8716 /*
8717 * Case B: Further records are pending in the current datagram.
8718 */
8719
8720#if defined(MBEDTLS_SSL_PROTO_DTLS)
8721 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8722 ssl->in_left > ssl->next_record_offset )
8723 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008724 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008725 return( 1 );
8726 }
8727#endif /* MBEDTLS_SSL_PROTO_DTLS */
8728
8729 /*
8730 * Case C: A handshake message is being processed.
8731 */
8732
Hanno Becker8b170a02017-10-10 11:51:19 +01008733 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8734 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008735 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008736 return( 1 );
8737 }
8738
8739 /*
8740 * Case D: An application data message is being processed
8741 */
8742 if( ssl->in_offt != NULL )
8743 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008744 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008745 return( 1 );
8746 }
8747
8748 /*
8749 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008750 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008751 * we implement support for multiple alerts in single records.
8752 */
8753
8754 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8755 return( 0 );
8756}
8757
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008758uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008759{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008760 if( ssl->session != NULL )
8761 return( ssl->session->verify_result );
8762
8763 if( ssl->session_negotiate != NULL )
8764 return( ssl->session_negotiate->verify_result );
8765
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008766 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008767}
8768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008770{
Paul Bakker926c8e42013-03-06 10:23:34 +01008771 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008772 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008774 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008775}
8776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008777const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008778{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008779#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008780 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008781 {
8782 switch( ssl->minor_ver )
8783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008784 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008785 return( "DTLSv1.0" );
8786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008787 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008788 return( "DTLSv1.2" );
8789
8790 default:
8791 return( "unknown (DTLS)" );
8792 }
8793 }
8794#endif
8795
Paul Bakker43ca69c2011-01-15 17:35:19 +00008796 switch( ssl->minor_ver )
8797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008798 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008799 return( "SSLv3.0" );
8800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008801 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008802 return( "TLSv1.0" );
8803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008804 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008805 return( "TLSv1.1" );
8806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008807 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008808 return( "TLSv1.2" );
8809
Paul Bakker43ca69c2011-01-15 17:35:19 +00008810 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008811 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008812 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008813}
8814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008815int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008816{
Hanno Becker3136ede2018-08-17 15:28:19 +01008817 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008818 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008819 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008820
Hanno Becker78640902018-08-13 16:35:15 +01008821 if( transform == NULL )
8822 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008824#if defined(MBEDTLS_ZLIB_SUPPORT)
8825 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8826 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008827#endif
8828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008829 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008831 case MBEDTLS_MODE_GCM:
8832 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008833 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008834 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008835 transform_expansion = transform->minlen;
8836 break;
8837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008838 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008839
8840 block_size = mbedtls_cipher_get_block_size(
8841 &transform->cipher_ctx_enc );
8842
Hanno Becker3136ede2018-08-17 15:28:19 +01008843 /* Expansion due to the addition of the MAC. */
8844 transform_expansion += transform->maclen;
8845
8846 /* Expansion due to the addition of CBC padding;
8847 * Theoretically up to 256 bytes, but we never use
8848 * more than the block size of the underlying cipher. */
8849 transform_expansion += block_size;
8850
8851 /* For TLS 1.1 or higher, an explicit IV is added
8852 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008853#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8854 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008855 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008856#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008857
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008858 break;
8859
8860 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008862 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008863 }
8864
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008865 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008866}
8867
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008868#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8869size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8870{
8871 size_t max_len;
8872
8873 /*
8874 * Assume mfl_code is correct since it was checked when set
8875 */
Angus Grattond8213d02016-05-25 20:56:48 +10008876 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008877
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008878 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008879 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008880 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008881 {
Angus Grattond8213d02016-05-25 20:56:48 +10008882 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008883 }
8884
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008885 /* During a handshake, use the value being negotiated */
8886 if( ssl->session_negotiate != NULL &&
8887 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8888 {
8889 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8890 }
8891
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008892 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008893}
8894#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8895
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008896#if defined(MBEDTLS_SSL_PROTO_DTLS)
8897static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8898{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008899 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8900 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8901 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8902 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8903 return ( 0 );
8904
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008905 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8906 return( ssl->mtu );
8907
8908 if( ssl->mtu == 0 )
8909 return( ssl->handshake->mtu );
8910
8911 return( ssl->mtu < ssl->handshake->mtu ?
8912 ssl->mtu : ssl->handshake->mtu );
8913}
8914#endif /* MBEDTLS_SSL_PROTO_DTLS */
8915
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008916int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8917{
8918 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8919
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008920#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8921 !defined(MBEDTLS_SSL_PROTO_DTLS)
8922 (void) ssl;
8923#endif
8924
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008925#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8926 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8927
8928 if( max_len > mfl )
8929 max_len = mfl;
8930#endif
8931
8932#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008933 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008934 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008935 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008936 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8937 const size_t overhead = (size_t) ret;
8938
8939 if( ret < 0 )
8940 return( ret );
8941
8942 if( mtu <= overhead )
8943 {
8944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8945 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8946 }
8947
8948 if( max_len > mtu - overhead )
8949 max_len = mtu - overhead;
8950 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008951#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008952
Hanno Becker0defedb2018-08-10 12:35:02 +01008953#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8954 !defined(MBEDTLS_SSL_PROTO_DTLS)
8955 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008956#endif
8957
8958 return( (int) max_len );
8959}
8960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008961#if defined(MBEDTLS_X509_CRT_PARSE_C)
8962const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008963{
8964 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008965 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008966
Hanno Beckere6824572019-02-07 13:18:46 +00008967#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008968 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00008969#else
8970 return( NULL );
8971#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008972}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008973#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008975#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00008976int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8977 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008978{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008979 if( ssl == NULL ||
8980 dst == NULL ||
8981 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008982 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008984 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008985 }
8986
Hanno Becker52055ae2019-02-06 14:30:46 +00008987 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008988}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008989#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008990
Paul Bakker5121ce52009-01-03 21:22:43 +00008991/*
Paul Bakker1961b702013-01-25 14:49:24 +01008992 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008993 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008994int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008995{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008996 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008997
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008998 if( ssl == NULL || ssl->conf == NULL )
8999 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009001#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009002 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009003 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009004#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009005#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009006 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009007 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009008#endif
9009
Paul Bakker1961b702013-01-25 14:49:24 +01009010 return( ret );
9011}
9012
9013/*
9014 * Perform the SSL handshake
9015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009016int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009017{
9018 int ret = 0;
9019
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009020 if( ssl == NULL || ssl->conf == NULL )
9021 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009025 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009027 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009028
9029 if( ret != 0 )
9030 break;
9031 }
9032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009034
9035 return( ret );
9036}
9037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009038#if defined(MBEDTLS_SSL_RENEGOTIATION)
9039#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009040/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009041 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009042 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009043static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009044{
9045 int ret;
9046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009048
9049 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009050 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9051 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009052
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009053 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009054 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009055 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009056 return( ret );
9057 }
9058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009060
9061 return( 0 );
9062}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009063#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009064
9065/*
9066 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009067 * - any side: calling mbedtls_ssl_renegotiate(),
9068 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9069 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009070 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009071 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009072 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009073 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009074static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009075{
9076 int ret;
9077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009079
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009080 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9081 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009082
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009083 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9084 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009085#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009086 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009087 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009088 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009089 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009090 ssl->handshake->out_msg_seq = 1;
9091 else
9092 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009093 }
9094#endif
9095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009096 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9097 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009099 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009101 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009102 return( ret );
9103 }
9104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009106
9107 return( 0 );
9108}
9109
9110/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009111 * Renegotiate current connection on client,
9112 * or request renegotiation on server
9113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009114int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009115{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009116 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009117
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009118 if( ssl == NULL || ssl->conf == NULL )
9119 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009121#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009122 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009123 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009125 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9126 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009128 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009129
9130 /* Did we already try/start sending HelloRequest? */
9131 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009132 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009133
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009134 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009135 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009136#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009138#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009139 /*
9140 * On client, either start the renegotiation process or,
9141 * if already in progress, continue the handshake
9142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009143 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009145 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9146 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009147
9148 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009150 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009151 return( ret );
9152 }
9153 }
9154 else
9155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009156 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009158 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009159 return( ret );
9160 }
9161 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009162#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009163
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009164 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009165}
9166
9167/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009168 * Check record counters and renegotiate if they're above the limit.
9169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009170static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009171{
Andres AG2196c7f2016-12-15 17:01:16 +00009172 size_t ep_len = ssl_ep_len( ssl );
9173 int in_ctr_cmp;
9174 int out_ctr_cmp;
9175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009176 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9177 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009178 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009179 {
9180 return( 0 );
9181 }
9182
Andres AG2196c7f2016-12-15 17:01:16 +00009183 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9184 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009185 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009186 ssl->conf->renego_period + ep_len, 8 - ep_len );
9187
9188 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009189 {
9190 return( 0 );
9191 }
9192
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009194 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009195}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009196#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009197
9198/*
9199 * Receive application data decrypted from the SSL layer
9200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009201int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009202{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009203 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009204 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009205
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009206 if( ssl == NULL || ssl->conf == NULL )
9207 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009211#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009212 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009214 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009215 return( ret );
9216
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009217 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009218 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009219 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009220 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009221 return( ret );
9222 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009223 }
9224#endif
9225
Hanno Becker4a810fb2017-05-24 16:27:30 +01009226 /*
9227 * Check if renegotiation is necessary and/or handshake is
9228 * in process. If yes, perform/continue, and fall through
9229 * if an unexpected packet is received while the client
9230 * is waiting for the ServerHello.
9231 *
9232 * (There is no equivalent to the last condition on
9233 * the server-side as it is not treated as within
9234 * a handshake while waiting for the ClientHello
9235 * after a renegotiation request.)
9236 */
9237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009238#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009239 ret = ssl_check_ctr_renegotiate( ssl );
9240 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9241 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009243 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009244 return( ret );
9245 }
9246#endif
9247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009248 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009250 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009251 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9252 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009254 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009255 return( ret );
9256 }
9257 }
9258
Hanno Beckere41158b2017-10-23 13:30:32 +01009259 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009260 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009261 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009262 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009263 if( ssl->f_get_timer != NULL &&
9264 ssl->f_get_timer( ssl->p_timer ) == -1 )
9265 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009266 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009267 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009268
Hanno Becker327c93b2018-08-15 13:56:18 +01009269 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009270 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009271 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9272 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009273
Hanno Becker4a810fb2017-05-24 16:27:30 +01009274 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9275 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009276 }
9277
9278 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009279 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009280 {
9281 /*
9282 * OpenSSL sends empty messages to randomize the IV
9283 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009284 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009286 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009287 return( 0 );
9288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009289 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009290 return( ret );
9291 }
9292 }
9293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009294 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009297
Hanno Becker4a810fb2017-05-24 16:27:30 +01009298 /*
9299 * - For client-side, expect SERVER_HELLO_REQUEST.
9300 * - For server-side, expect CLIENT_HELLO.
9301 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9302 */
9303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009305 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009306 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009307 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009310
9311 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009312#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009313 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009314 {
9315 continue;
9316 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009317#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009318 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009319 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009320#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009321
Hanno Becker4a810fb2017-05-24 16:27:30 +01009322#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009323 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009324 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009327
9328 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009329#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009330 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009331 {
9332 continue;
9333 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009334#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009335 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009336 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009337#endif /* MBEDTLS_SSL_SRV_C */
9338
Hanno Becker21df7f92017-10-17 11:03:26 +01009339#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009340 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009341 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9342 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9343 ssl->conf->allow_legacy_renegotiation ==
9344 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9345 {
9346 /*
9347 * Accept renegotiation request
9348 */
Paul Bakker48916f92012-09-16 19:57:18 +00009349
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009350 /* DTLS clients need to know renego is server-initiated */
9351#if defined(MBEDTLS_SSL_PROTO_DTLS)
9352 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9353 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9354 {
9355 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9356 }
9357#endif
9358 ret = ssl_start_renegotiation( ssl );
9359 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9360 ret != 0 )
9361 {
9362 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9363 return( ret );
9364 }
9365 }
9366 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009367#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009368 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009369 /*
9370 * Refuse renegotiation
9371 */
9372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009373 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009375#if defined(MBEDTLS_SSL_PROTO_SSL3)
9376 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009377 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009378 /* SSLv3 does not have a "no_renegotiation" warning, so
9379 we send a fatal alert and abort the connection. */
9380 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9381 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9382 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009383 }
9384 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009385#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9386#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9387 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9388 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9391 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9392 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009393 {
9394 return( ret );
9395 }
Paul Bakker48916f92012-09-16 19:57:18 +00009396 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009397 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009398#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9399 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9402 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009403 }
Paul Bakker48916f92012-09-16 19:57:18 +00009404 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009405
Hanno Becker90333da2017-10-10 11:27:13 +01009406 /* At this point, we don't know whether the renegotiation has been
9407 * completed or not. The cases to consider are the following:
9408 * 1) The renegotiation is complete. In this case, no new record
9409 * has been read yet.
9410 * 2) The renegotiation is incomplete because the client received
9411 * an application data record while awaiting the ServerHello.
9412 * 3) The renegotiation is incomplete because the client received
9413 * a non-handshake, non-application data message while awaiting
9414 * the ServerHello.
9415 * In each of these case, looping will be the proper action:
9416 * - For 1), the next iteration will read a new record and check
9417 * if it's application data.
9418 * - For 2), the loop condition isn't satisfied as application data
9419 * is present, hence continue is the same as break
9420 * - For 3), the loop condition is satisfied and read_record
9421 * will re-deliver the message that was held back by the client
9422 * when expecting the ServerHello.
9423 */
9424 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009425 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009426#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009427 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009428 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009429 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009430 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009431 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009434 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009435 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009436 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009437 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009438 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009439#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009441 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9442 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009445 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009446 }
9447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009448 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9451 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009452 }
9453
9454 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009455
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009456 /* We're going to return something now, cancel timer,
9457 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009458 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009459 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009460
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009461#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009462 /* If we requested renego but received AppData, resend HelloRequest.
9463 * Do it now, after setting in_offt, to avoid taking this branch
9464 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009465#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009466 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009467 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009468 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009469 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009471 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009472 return( ret );
9473 }
9474 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009475#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009476#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009477 }
9478
9479 n = ( len < ssl->in_msglen )
9480 ? len : ssl->in_msglen;
9481
9482 memcpy( buf, ssl->in_offt, n );
9483 ssl->in_msglen -= n;
9484
9485 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009486 {
9487 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009488 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009489 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009490 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009491 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009492 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009493 /* more data available */
9494 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009495 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009498
Paul Bakker23986e52011-04-24 08:57:21 +00009499 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009500}
9501
9502/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009503 * Send application data to be encrypted by the SSL layer, taking care of max
9504 * fragment length and buffer size.
9505 *
9506 * According to RFC 5246 Section 6.2.1:
9507 *
9508 * Zero-length fragments of Application data MAY be sent as they are
9509 * potentially useful as a traffic analysis countermeasure.
9510 *
9511 * Therefore, it is possible that the input message length is 0 and the
9512 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009513 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009514static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009515 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009516{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009517 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9518 const size_t max_len = (size_t) ret;
9519
9520 if( ret < 0 )
9521 {
9522 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9523 return( ret );
9524 }
9525
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009526 if( len > max_len )
9527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009528#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009529 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009532 "maximum fragment length: %d > %d",
9533 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009535 }
9536 else
9537#endif
9538 len = max_len;
9539 }
Paul Bakker887bd502011-06-08 13:10:54 +00009540
Paul Bakker5121ce52009-01-03 21:22:43 +00009541 if( ssl->out_left != 0 )
9542 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009543 /*
9544 * The user has previously tried to send the data and
9545 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9546 * written. In this case, we expect the high-level write function
9547 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009552 return( ret );
9553 }
9554 }
Paul Bakker887bd502011-06-08 13:10:54 +00009555 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009556 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009557 /*
9558 * The user is trying to send a message the first time, so we need to
9559 * copy the data into the internal buffers and setup the data structure
9560 * to keep track of partial writes
9561 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009562 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009564 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009565
Hanno Becker67bc7c32018-08-06 11:33:50 +01009566 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009568 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009569 return( ret );
9570 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009571 }
9572
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009573 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009574}
9575
9576/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009577 * Write application data, doing 1/n-1 splitting if necessary.
9578 *
9579 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009580 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009581 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009582 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009583#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009584static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009585 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009586{
9587 int ret;
9588
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009589 if( ssl->conf->cbc_record_splitting ==
9590 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009591 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009592 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9593 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9594 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009595 {
9596 return( ssl_write_real( ssl, buf, len ) );
9597 }
9598
9599 if( ssl->split_done == 0 )
9600 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009601 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009602 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009603 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009604 }
9605
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009606 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9607 return( ret );
9608 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009609
9610 return( ret + 1 );
9611}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009612#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009613
9614/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009615 * Write application data (public-facing wrapper)
9616 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009617int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009618{
9619 int ret;
9620
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009622
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009623 if( ssl == NULL || ssl->conf == NULL )
9624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9625
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009626#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009627 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9628 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009629 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009630 return( ret );
9631 }
9632#endif
9633
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009634 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009635 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009636 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009637 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009638 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009639 return( ret );
9640 }
9641 }
9642
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009643#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009644 ret = ssl_write_split( ssl, buf, len );
9645#else
9646 ret = ssl_write_real( ssl, buf, len );
9647#endif
9648
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009650
9651 return( ret );
9652}
9653
9654/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009655 * Notify the peer that the connection is being closed
9656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009657int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009658{
9659 int ret;
9660
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009661 if( ssl == NULL || ssl->conf == NULL )
9662 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009665
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009666 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009667 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009671 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9672 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9673 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009676 return( ret );
9677 }
9678 }
9679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009681
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009682 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009683}
9684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009686{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009687 if( transform == NULL )
9688 return;
9689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009690#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009691 deflateEnd( &transform->ctx_deflate );
9692 inflateEnd( &transform->ctx_inflate );
9693#endif
9694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009695 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9696 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009697
Hanno Beckerd56ed242018-01-03 15:32:51 +00009698#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009699 mbedtls_md_free( &transform->md_ctx_enc );
9700 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00009701#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009702
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009703 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009704}
9705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009706#if defined(MBEDTLS_X509_CRT_PARSE_C)
9707static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009708{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009709 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009710
9711 while( cur != NULL )
9712 {
9713 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009714 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009715 cur = next;
9716 }
9717}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009718#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009719
Hanno Becker0271f962018-08-16 13:23:47 +01009720#if defined(MBEDTLS_SSL_PROTO_DTLS)
9721
9722static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9723{
9724 unsigned offset;
9725 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9726
9727 if( hs == NULL )
9728 return;
9729
Hanno Becker283f5ef2018-08-24 09:34:47 +01009730 ssl_free_buffered_record( ssl );
9731
Hanno Becker0271f962018-08-16 13:23:47 +01009732 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009733 ssl_buffering_free_slot( ssl, offset );
9734}
9735
9736static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9737 uint8_t slot )
9738{
9739 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9740 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009741
9742 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9743 return;
9744
Hanno Beckere605b192018-08-21 15:59:07 +01009745 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009746 {
Hanno Beckere605b192018-08-21 15:59:07 +01009747 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009748 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009749 mbedtls_free( hs_buf->data );
9750 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009751 }
9752}
9753
9754#endif /* MBEDTLS_SSL_PROTO_DTLS */
9755
Gilles Peskine9b562d52018-04-25 20:32:43 +02009756void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009757{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009758 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9759
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009760 if( handshake == NULL )
9761 return;
9762
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009763#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9764 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9765 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009766 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009767 handshake->async_in_progress = 0;
9768 }
9769#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9770
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009771#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9772 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9773 mbedtls_md5_free( &handshake->fin_md5 );
9774 mbedtls_sha1_free( &handshake->fin_sha1 );
9775#endif
9776#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9777#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009778#if defined(MBEDTLS_USE_PSA_CRYPTO)
9779 psa_hash_abort( &handshake->fin_sha256_psa );
9780#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009781 mbedtls_sha256_free( &handshake->fin_sha256 );
9782#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009783#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009784#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009785#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009786 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009787#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009788 mbedtls_sha512_free( &handshake->fin_sha512 );
9789#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009790#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009791#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009793#if defined(MBEDTLS_DHM_C)
9794 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009795#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009796#if defined(MBEDTLS_ECDH_C)
9797 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009798#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009799#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009800 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009801#if defined(MBEDTLS_SSL_CLI_C)
9802 mbedtls_free( handshake->ecjpake_cache );
9803 handshake->ecjpake_cache = NULL;
9804 handshake->ecjpake_cache_len = 0;
9805#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009806#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009807
Janos Follath4ae5c292016-02-10 11:27:43 +00009808#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9809 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009810 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009811 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009812#endif
9813
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009814#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9815 if( handshake->psk != NULL )
9816 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009817 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009818 mbedtls_free( handshake->psk );
9819 }
9820#endif
9821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9823 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009824 /*
9825 * Free only the linked list wrapper, not the keys themselves
9826 * since the belong to the SNI callback
9827 */
9828 if( handshake->sni_key_cert != NULL )
9829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009831
9832 while( cur != NULL )
9833 {
9834 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009835 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009836 cur = next;
9837 }
9838 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009840
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009841#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009842 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00009843 if( handshake->ecrs_peer_cert != NULL )
9844 {
9845 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
9846 mbedtls_free( handshake->ecrs_peer_cert );
9847 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009848#endif
9849
Hanno Becker75173122019-02-06 16:18:31 +00009850#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9851 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9852 mbedtls_pk_free( &handshake->peer_pubkey );
9853#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855#if defined(MBEDTLS_SSL_PROTO_DTLS)
9856 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009857 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009858 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009859#endif
9860
Hanno Becker4a63ed42019-01-08 11:39:35 +00009861#if defined(MBEDTLS_ECDH_C) && \
9862 defined(MBEDTLS_USE_PSA_CRYPTO)
9863 psa_destroy_key( handshake->ecdh_psa_privkey );
9864#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9865
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009866 mbedtls_platform_zeroize( handshake,
9867 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009868}
9869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009870void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009871{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009872 if( session == NULL )
9873 return;
9874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009875#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009876 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009877#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009878
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009879#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009880 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009881#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009882
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009883 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009884}
9885
Paul Bakker5121ce52009-01-03 21:22:43 +00009886/*
9887 * Free an SSL context
9888 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009889void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009890{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009891 if( ssl == NULL )
9892 return;
9893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009895
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009896 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009897 {
Angus Grattond8213d02016-05-25 20:56:48 +10009898 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009899 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009900 }
9901
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009902 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009903 {
Angus Grattond8213d02016-05-25 20:56:48 +10009904 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009905 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009906 }
9907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009908#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009909 if( ssl->compress_buf != NULL )
9910 {
Angus Grattond8213d02016-05-25 20:56:48 +10009911 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009912 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009913 }
9914#endif
9915
Paul Bakker48916f92012-09-16 19:57:18 +00009916 if( ssl->transform )
9917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009918 mbedtls_ssl_transform_free( ssl->transform );
9919 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009920 }
9921
9922 if( ssl->handshake )
9923 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009924 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009925 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9926 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009928 mbedtls_free( ssl->handshake );
9929 mbedtls_free( ssl->transform_negotiate );
9930 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009931 }
9932
Paul Bakkerc0463502013-02-14 11:19:38 +01009933 if( ssl->session )
9934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009935 mbedtls_ssl_session_free( ssl->session );
9936 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009937 }
9938
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009939#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009940 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009941 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009942 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009943 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009944 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009945#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009947#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9948 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9951 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009952 }
9953#endif
9954
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009955#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009956 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009957#endif
9958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009960
Paul Bakker86f04f42013-02-14 11:20:09 +01009961 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009962 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009963}
9964
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009965/*
9966 * Initialze mbedtls_ssl_config
9967 */
9968void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9969{
9970 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9971}
9972
Simon Butcherc97b6972015-12-27 23:48:17 +00009973#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009974static int ssl_preset_default_hashes[] = {
9975#if defined(MBEDTLS_SHA512_C)
9976 MBEDTLS_MD_SHA512,
9977 MBEDTLS_MD_SHA384,
9978#endif
9979#if defined(MBEDTLS_SHA256_C)
9980 MBEDTLS_MD_SHA256,
9981 MBEDTLS_MD_SHA224,
9982#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009983#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009984 MBEDTLS_MD_SHA1,
9985#endif
9986 MBEDTLS_MD_NONE
9987};
Simon Butcherc97b6972015-12-27 23:48:17 +00009988#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009989
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009990static int ssl_preset_suiteb_ciphersuites[] = {
9991 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9992 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9993 0
9994};
9995
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009996#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009997static int ssl_preset_suiteb_hashes[] = {
9998 MBEDTLS_MD_SHA256,
9999 MBEDTLS_MD_SHA384,
10000 MBEDTLS_MD_NONE
10001};
10002#endif
10003
10004#if defined(MBEDTLS_ECP_C)
10005static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10006 MBEDTLS_ECP_DP_SECP256R1,
10007 MBEDTLS_ECP_DP_SECP384R1,
10008 MBEDTLS_ECP_DP_NONE
10009};
10010#endif
10011
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010012/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010013 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010014 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010015int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010016 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010017{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010018#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010019 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010020#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010021
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010022 /* Use the functions here so that they are covered in tests,
10023 * but otherwise access member directly for efficiency */
10024 mbedtls_ssl_conf_endpoint( conf, endpoint );
10025 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010026
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010027 /*
10028 * Things that are common to all presets
10029 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010030#if defined(MBEDTLS_SSL_CLI_C)
10031 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10032 {
10033 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10034#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10035 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10036#endif
10037 }
10038#endif
10039
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010040#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010041 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010042#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010043
10044#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10045 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10046#endif
10047
10048#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10049 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10050#endif
10051
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010052#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10053 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10054#endif
10055
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010056#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010057 conf->f_cookie_write = ssl_cookie_write_dummy;
10058 conf->f_cookie_check = ssl_cookie_check_dummy;
10059#endif
10060
10061#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10062 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10063#endif
10064
Janos Follath088ce432017-04-10 12:42:31 +010010065#if defined(MBEDTLS_SSL_SRV_C)
10066 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10067#endif
10068
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010069#if defined(MBEDTLS_SSL_PROTO_DTLS)
10070 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10071 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10072#endif
10073
10074#if defined(MBEDTLS_SSL_RENEGOTIATION)
10075 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010076 memset( conf->renego_period, 0x00, 2 );
10077 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010078#endif
10079
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010080#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10081 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10082 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010083 const unsigned char dhm_p[] =
10084 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10085 const unsigned char dhm_g[] =
10086 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10087
Hanno Beckera90658f2017-10-04 15:29:08 +010010088 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10089 dhm_p, sizeof( dhm_p ),
10090 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010091 {
10092 return( ret );
10093 }
10094 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010095#endif
10096
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010097 /*
10098 * Preset-specific defaults
10099 */
10100 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010101 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010102 /*
10103 * NSA Suite B
10104 */
10105 case MBEDTLS_SSL_PRESET_SUITEB:
10106 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10107 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10108 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10109 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10110
10111 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10112 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10113 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10114 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10115 ssl_preset_suiteb_ciphersuites;
10116
10117#if defined(MBEDTLS_X509_CRT_PARSE_C)
10118 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010119#endif
10120
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010121#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010122 conf->sig_hashes = ssl_preset_suiteb_hashes;
10123#endif
10124
10125#if defined(MBEDTLS_ECP_C)
10126 conf->curve_list = ssl_preset_suiteb_curves;
10127#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010128 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010129
10130 /*
10131 * Default
10132 */
10133 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010134 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10135 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10136 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10137 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10138 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10139 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10140 MBEDTLS_SSL_MIN_MINOR_VERSION :
10141 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010142 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10143 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10144
10145#if defined(MBEDTLS_SSL_PROTO_DTLS)
10146 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10147 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10148#endif
10149
10150 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10151 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10152 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10153 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10154 mbedtls_ssl_list_ciphersuites();
10155
10156#if defined(MBEDTLS_X509_CRT_PARSE_C)
10157 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10158#endif
10159
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010160#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010161 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010162#endif
10163
10164#if defined(MBEDTLS_ECP_C)
10165 conf->curve_list = mbedtls_ecp_grp_id_list();
10166#endif
10167
10168#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10169 conf->dhm_min_bitlen = 1024;
10170#endif
10171 }
10172
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010173 return( 0 );
10174}
10175
10176/*
10177 * Free mbedtls_ssl_config
10178 */
10179void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10180{
10181#if defined(MBEDTLS_DHM_C)
10182 mbedtls_mpi_free( &conf->dhm_P );
10183 mbedtls_mpi_free( &conf->dhm_G );
10184#endif
10185
10186#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10187 if( conf->psk != NULL )
10188 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010189 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010190 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010191 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010192 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010193 }
10194
10195 if( conf->psk_identity != NULL )
10196 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010197 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010198 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010199 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010200 conf->psk_identity_len = 0;
10201 }
10202#endif
10203
10204#if defined(MBEDTLS_X509_CRT_PARSE_C)
10205 ssl_key_cert_free( conf->key_cert );
10206#endif
10207
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010208 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010209}
10210
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010211#if defined(MBEDTLS_PK_C) && \
10212 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010213/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010214 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010217{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010218#if defined(MBEDTLS_RSA_C)
10219 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10220 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010221#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010222#if defined(MBEDTLS_ECDSA_C)
10223 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10224 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010225#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010226 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010227}
10228
Hanno Becker7e5437a2017-04-28 17:15:26 +010010229unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10230{
10231 switch( type ) {
10232 case MBEDTLS_PK_RSA:
10233 return( MBEDTLS_SSL_SIG_RSA );
10234 case MBEDTLS_PK_ECDSA:
10235 case MBEDTLS_PK_ECKEY:
10236 return( MBEDTLS_SSL_SIG_ECDSA );
10237 default:
10238 return( MBEDTLS_SSL_SIG_ANON );
10239 }
10240}
10241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010242mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010243{
10244 switch( sig )
10245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010246#if defined(MBEDTLS_RSA_C)
10247 case MBEDTLS_SSL_SIG_RSA:
10248 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010249#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010250#if defined(MBEDTLS_ECDSA_C)
10251 case MBEDTLS_SSL_SIG_ECDSA:
10252 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010253#endif
10254 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010255 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010256 }
10257}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010258#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010259
Hanno Becker7e5437a2017-04-28 17:15:26 +010010260#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10261 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10262
10263/* Find an entry in a signature-hash set matching a given hash algorithm. */
10264mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10265 mbedtls_pk_type_t sig_alg )
10266{
10267 switch( sig_alg )
10268 {
10269 case MBEDTLS_PK_RSA:
10270 return( set->rsa );
10271 case MBEDTLS_PK_ECDSA:
10272 return( set->ecdsa );
10273 default:
10274 return( MBEDTLS_MD_NONE );
10275 }
10276}
10277
10278/* Add a signature-hash-pair to a signature-hash set */
10279void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10280 mbedtls_pk_type_t sig_alg,
10281 mbedtls_md_type_t md_alg )
10282{
10283 switch( sig_alg )
10284 {
10285 case MBEDTLS_PK_RSA:
10286 if( set->rsa == MBEDTLS_MD_NONE )
10287 set->rsa = md_alg;
10288 break;
10289
10290 case MBEDTLS_PK_ECDSA:
10291 if( set->ecdsa == MBEDTLS_MD_NONE )
10292 set->ecdsa = md_alg;
10293 break;
10294
10295 default:
10296 break;
10297 }
10298}
10299
10300/* Allow exactly one hash algorithm for each signature. */
10301void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10302 mbedtls_md_type_t md_alg )
10303{
10304 set->rsa = md_alg;
10305 set->ecdsa = md_alg;
10306}
10307
10308#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10309 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10310
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010311/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010312 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010314mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010315{
10316 switch( hash )
10317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010318#if defined(MBEDTLS_MD5_C)
10319 case MBEDTLS_SSL_HASH_MD5:
10320 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010321#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010322#if defined(MBEDTLS_SHA1_C)
10323 case MBEDTLS_SSL_HASH_SHA1:
10324 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010325#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010326#if defined(MBEDTLS_SHA256_C)
10327 case MBEDTLS_SSL_HASH_SHA224:
10328 return( MBEDTLS_MD_SHA224 );
10329 case MBEDTLS_SSL_HASH_SHA256:
10330 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010331#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010332#if defined(MBEDTLS_SHA512_C)
10333 case MBEDTLS_SSL_HASH_SHA384:
10334 return( MBEDTLS_MD_SHA384 );
10335 case MBEDTLS_SSL_HASH_SHA512:
10336 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010337#endif
10338 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010340 }
10341}
10342
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010343/*
10344 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10345 */
10346unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10347{
10348 switch( md )
10349 {
10350#if defined(MBEDTLS_MD5_C)
10351 case MBEDTLS_MD_MD5:
10352 return( MBEDTLS_SSL_HASH_MD5 );
10353#endif
10354#if defined(MBEDTLS_SHA1_C)
10355 case MBEDTLS_MD_SHA1:
10356 return( MBEDTLS_SSL_HASH_SHA1 );
10357#endif
10358#if defined(MBEDTLS_SHA256_C)
10359 case MBEDTLS_MD_SHA224:
10360 return( MBEDTLS_SSL_HASH_SHA224 );
10361 case MBEDTLS_MD_SHA256:
10362 return( MBEDTLS_SSL_HASH_SHA256 );
10363#endif
10364#if defined(MBEDTLS_SHA512_C)
10365 case MBEDTLS_MD_SHA384:
10366 return( MBEDTLS_SSL_HASH_SHA384 );
10367 case MBEDTLS_MD_SHA512:
10368 return( MBEDTLS_SSL_HASH_SHA512 );
10369#endif
10370 default:
10371 return( MBEDTLS_SSL_HASH_NONE );
10372 }
10373}
10374
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010375#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010376/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010377 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010378 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010379 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010380int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010381{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010382 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010383
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010384 if( ssl->conf->curve_list == NULL )
10385 return( -1 );
10386
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010387 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010388 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010389 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010390
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010391 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010392}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010393#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010394
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010395#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010396/*
10397 * Check if a hash proposed by the peer is in our list.
10398 * Return 0 if we're willing to use it, -1 otherwise.
10399 */
10400int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10401 mbedtls_md_type_t md )
10402{
10403 const int *cur;
10404
10405 if( ssl->conf->sig_hashes == NULL )
10406 return( -1 );
10407
10408 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10409 if( *cur == (int) md )
10410 return( 0 );
10411
10412 return( -1 );
10413}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010414#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010416#if defined(MBEDTLS_X509_CRT_PARSE_C)
10417int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10418 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010419 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010420 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010421{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010422 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010423#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010424 int usage = 0;
10425#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010426#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010427 const char *ext_oid;
10428 size_t ext_len;
10429#endif
10430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010431#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10432 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010433 ((void) cert);
10434 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010435 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010436#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010438#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10439 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010440 {
10441 /* Server part of the key exchange */
10442 switch( ciphersuite->key_exchange )
10443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010444 case MBEDTLS_KEY_EXCHANGE_RSA:
10445 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010446 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010447 break;
10448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010449 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10450 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10451 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10452 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010453 break;
10454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010455 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10456 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010457 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010458 break;
10459
10460 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010461 case MBEDTLS_KEY_EXCHANGE_NONE:
10462 case MBEDTLS_KEY_EXCHANGE_PSK:
10463 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10464 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010465 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010466 usage = 0;
10467 }
10468 }
10469 else
10470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010471 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10472 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010473 }
10474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010475 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010476 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010477 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010478 ret = -1;
10479 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010480#else
10481 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010482#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010484#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10485 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010487 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10488 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010489 }
10490 else
10491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010492 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10493 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010494 }
10495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010496 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010497 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010498 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010499 ret = -1;
10500 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010501#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010502
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010503 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010504}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010505#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010506
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010507/*
10508 * Convert version numbers to/from wire format
10509 * and, for DTLS, to/from TLS equivalent.
10510 *
10511 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010512 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010513 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10514 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010516void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010517 unsigned char ver[2] )
10518{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010519#if defined(MBEDTLS_SSL_PROTO_DTLS)
10520 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010522 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010523 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10524
10525 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10526 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10527 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010528 else
10529#else
10530 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010531#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010532 {
10533 ver[0] = (unsigned char) major;
10534 ver[1] = (unsigned char) minor;
10535 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010536}
10537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010538void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010539 const unsigned char ver[2] )
10540{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010541#if defined(MBEDTLS_SSL_PROTO_DTLS)
10542 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010543 {
10544 *major = 255 - ver[0] + 2;
10545 *minor = 255 - ver[1] + 1;
10546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010547 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010548 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10549 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010550 else
10551#else
10552 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010553#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010554 {
10555 *major = ver[0];
10556 *minor = ver[1];
10557 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010558}
10559
Simon Butcher99000142016-10-13 17:21:01 +010010560int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10561{
10562#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10563 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10564 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10565
10566 switch( md )
10567 {
10568#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10569#if defined(MBEDTLS_MD5_C)
10570 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010571 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010572#endif
10573#if defined(MBEDTLS_SHA1_C)
10574 case MBEDTLS_SSL_HASH_SHA1:
10575 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10576 break;
10577#endif
10578#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10579#if defined(MBEDTLS_SHA512_C)
10580 case MBEDTLS_SSL_HASH_SHA384:
10581 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10582 break;
10583#endif
10584#if defined(MBEDTLS_SHA256_C)
10585 case MBEDTLS_SSL_HASH_SHA256:
10586 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10587 break;
10588#endif
10589 default:
10590 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10591 }
10592
10593 return 0;
10594#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10595 (void) ssl;
10596 (void) md;
10597
10598 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10599#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10600}
10601
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010602#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10603 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10604int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10605 unsigned char *output,
10606 unsigned char *data, size_t data_len )
10607{
10608 int ret = 0;
10609 mbedtls_md5_context mbedtls_md5;
10610 mbedtls_sha1_context mbedtls_sha1;
10611
10612 mbedtls_md5_init( &mbedtls_md5 );
10613 mbedtls_sha1_init( &mbedtls_sha1 );
10614
10615 /*
10616 * digitally-signed struct {
10617 * opaque md5_hash[16];
10618 * opaque sha_hash[20];
10619 * };
10620 *
10621 * md5_hash
10622 * MD5(ClientHello.random + ServerHello.random
10623 * + ServerParams);
10624 * sha_hash
10625 * SHA(ClientHello.random + ServerHello.random
10626 * + ServerParams);
10627 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010628 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010629 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010630 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010631 goto exit;
10632 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010633 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010634 ssl->handshake->randbytes, 64 ) ) != 0 )
10635 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010636 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010637 goto exit;
10638 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010639 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010640 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010641 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010642 goto exit;
10643 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010644 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010645 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010646 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010647 goto exit;
10648 }
10649
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010650 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010651 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010652 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010653 goto exit;
10654 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010655 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010656 ssl->handshake->randbytes, 64 ) ) != 0 )
10657 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010658 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010659 goto exit;
10660 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010661 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010662 data_len ) ) != 0 )
10663 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010664 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010665 goto exit;
10666 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010667 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010668 output + 16 ) ) != 0 )
10669 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010671 goto exit;
10672 }
10673
10674exit:
10675 mbedtls_md5_free( &mbedtls_md5 );
10676 mbedtls_sha1_free( &mbedtls_sha1 );
10677
10678 if( ret != 0 )
10679 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10680 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10681
10682 return( ret );
10683
10684}
10685#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10686 MBEDTLS_SSL_PROTO_TLS1_1 */
10687
10688#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10689 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010690
10691#if defined(MBEDTLS_USE_PSA_CRYPTO)
10692int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10693 unsigned char *hash, size_t *hashlen,
10694 unsigned char *data, size_t data_len,
10695 mbedtls_md_type_t md_alg )
10696{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010697 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010698 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010699 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10700
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010701 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010702
10703 if( ( status = psa_hash_setup( &hash_operation,
10704 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010705 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010706 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010707 goto exit;
10708 }
10709
Andrzej Kurek814feff2019-01-14 04:35:19 -050010710 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10711 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010712 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010713 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010714 goto exit;
10715 }
10716
Andrzej Kurek814feff2019-01-14 04:35:19 -050010717 if( ( status = psa_hash_update( &hash_operation,
10718 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010719 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010720 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010721 goto exit;
10722 }
10723
Andrzej Kurek814feff2019-01-14 04:35:19 -050010724 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10725 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010726 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010727 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010728 goto exit;
10729 }
10730
10731exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010732 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010733 {
10734 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10735 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010736 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010737 {
10738 case PSA_ERROR_NOT_SUPPORTED:
10739 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010740 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010741 case PSA_ERROR_BUFFER_TOO_SMALL:
10742 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10743 case PSA_ERROR_INSUFFICIENT_MEMORY:
10744 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10745 default:
10746 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10747 }
10748 }
10749 return( 0 );
10750}
10751
10752#else
10753
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010754int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010755 unsigned char *hash, size_t *hashlen,
10756 unsigned char *data, size_t data_len,
10757 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010758{
10759 int ret = 0;
10760 mbedtls_md_context_t ctx;
10761 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010762 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010763
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010764 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010765
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010766 mbedtls_md_init( &ctx );
10767
10768 /*
10769 * digitally-signed struct {
10770 * opaque client_random[32];
10771 * opaque server_random[32];
10772 * ServerDHParams params;
10773 * };
10774 */
10775 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10776 {
10777 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10778 goto exit;
10779 }
10780 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10781 {
10782 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10783 goto exit;
10784 }
10785 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10786 {
10787 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10788 goto exit;
10789 }
10790 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10791 {
10792 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10793 goto exit;
10794 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010795 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010796 {
10797 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10798 goto exit;
10799 }
10800
10801exit:
10802 mbedtls_md_free( &ctx );
10803
10804 if( ret != 0 )
10805 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10806 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10807
10808 return( ret );
10809}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010810#endif /* MBEDTLS_USE_PSA_CRYPTO */
10811
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010812#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10813 MBEDTLS_SSL_PROTO_TLS1_2 */
10814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010815#endif /* MBEDTLS_SSL_TLS_C */