blob: 01428f03aa90705a59e2c688a9b4d7a4abdcb29a [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
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200282#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_ssl_session_free( dst );
286 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200289 if( src->peer_cert != NULL )
290 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200291 int ret;
292
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200293 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200294 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200295 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200300 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303 dst->peer_cert = NULL;
304 return( ret );
305 }
306 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200308
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200309#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200310 if( src->ticket != NULL )
311 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200312 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200313 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200314 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200315
316 memcpy( dst->ticket, src->ticket, src->ticket_len );
317 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200318#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200319
320 return( 0 );
321}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200322#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
325int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200326 const unsigned char *key_enc, const unsigned char *key_dec,
327 size_t keylen,
328 const unsigned char *iv_enc, const unsigned char *iv_dec,
329 size_t ivlen,
330 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200331 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
333int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
334int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
335int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
336int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
337#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000338
Paul Bakker5121ce52009-01-03 21:22:43 +0000339/*
340 * Key material generation
341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200343static int ssl3_prf( const unsigned char *secret, size_t slen,
344 const char *label,
345 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000346 unsigned char *dstbuf, size_t dlen )
347{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100348 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000349 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200350 mbedtls_md5_context md5;
351 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000352 unsigned char padding[16];
353 unsigned char sha1sum[20];
354 ((void)label);
355
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200356 mbedtls_md5_init( &md5 );
357 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200358
Paul Bakker5f70b252012-09-13 14:23:06 +0000359 /*
360 * SSLv3:
361 * block =
362 * MD5( secret + SHA1( 'A' + secret + random ) ) +
363 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
364 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
365 * ...
366 */
367 for( i = 0; i < dlen / 16; i++ )
368 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200369 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000370
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100371 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100372 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100373 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100374 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100375 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100376 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100377 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100378 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100379 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100380 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000381
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100382 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100383 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100384 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100385 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100386 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100387 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100388 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100389 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000390 }
391
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100392exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200393 mbedtls_md5_free( &md5 );
394 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000395
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500396 mbedtls_platform_zeroize( padding, sizeof( padding ) );
397 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000398
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100399 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000400}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200404static int tls1_prf( const unsigned char *secret, size_t slen,
405 const char *label,
406 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000407 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000408{
Paul Bakker23986e52011-04-24 08:57:21 +0000409 size_t nb, hs;
410 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200411 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 unsigned char tmp[128];
413 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 const mbedtls_md_info_t *md_info;
415 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100416 int ret;
417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
420 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
423 hs = ( slen + 1 ) / 2;
424 S1 = secret;
425 S2 = secret + slen - hs;
426
427 nb = strlen( label );
428 memcpy( tmp + 20, label, nb );
429 memcpy( tmp + 20 + nb, random, rlen );
430 nb += rlen;
431
432 /*
433 * First compute P_md5(secret,label+random)[0..dlen]
434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
436 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100439 return( ret );
440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
442 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
443 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000444
445 for( i = 0; i < dlen; i += 16 )
446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_md_hmac_reset ( &md_ctx );
448 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
449 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_md_hmac_reset ( &md_ctx );
452 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
453 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000454
455 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
456
457 for( j = 0; j < k; j++ )
458 dstbuf[i + j] = h_i[j];
459 }
460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100462
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 /*
464 * XOR out with P_sha1(secret,label+random)[0..dlen]
465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
467 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100470 return( ret );
471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
473 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
474 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
476 for( i = 0; i < dlen; i += 20 )
477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_md_hmac_reset ( &md_ctx );
479 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
480 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_md_hmac_reset ( &md_ctx );
483 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
484 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
487
488 for( j = 0; j < k; j++ )
489 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
490 }
491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100493
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500494 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
495 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000496
497 return( 0 );
498}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500502#if defined(MBEDTLS_USE_PSA_CRYPTO)
503static int tls_prf_generic( mbedtls_md_type_t md_type,
504 const unsigned char *secret, size_t slen,
505 const char *label,
506 const unsigned char *random, size_t rlen,
507 unsigned char *dstbuf, size_t dlen )
508{
509 psa_status_t status;
510 psa_algorithm_t alg;
511 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500512 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500513 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
514
Andrzej Kurek2f760752019-01-28 08:08:15 -0500515 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500516 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500517
Andrzej Kurekc929a822019-01-14 03:51:11 -0500518 if( md_type == MBEDTLS_MD_SHA384 )
519 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
520 else
521 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
522
Andrzej Kurek2f760752019-01-28 08:08:15 -0500523 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500524 psa_key_policy_set_usage( &policy,
525 PSA_KEY_USAGE_DERIVE,
526 alg );
527 status = psa_set_key_policy( master_slot, &policy );
528 if( status != PSA_SUCCESS )
529 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
530
531 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
532 if( status != PSA_SUCCESS )
533 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
534
535 status = psa_key_derivation( &generator,
536 master_slot, alg,
537 random, rlen,
538 (unsigned char const *) label,
539 (size_t) strlen( label ),
540 dlen );
541 if( status != PSA_SUCCESS )
542 {
543 psa_generator_abort( &generator );
544 psa_destroy_key( master_slot );
545 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
546 }
547
548 status = psa_generator_read( &generator, dstbuf, dlen );
549 if( status != PSA_SUCCESS )
550 {
551 psa_generator_abort( &generator );
552 psa_destroy_key( master_slot );
553 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
554 }
555
556 status = psa_generator_abort( &generator );
557 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500558 {
559 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500560 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500561 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500562
563 status = psa_destroy_key( master_slot );
564 if( status != PSA_SUCCESS )
565 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
566
Andrzej Kurek33171262019-01-15 03:25:18 -0500567 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500568}
569
570#else /* MBEDTLS_USE_PSA_CRYPTO */
571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100573 const unsigned char *secret, size_t slen,
574 const char *label,
575 const unsigned char *random, size_t rlen,
576 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000577{
578 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100579 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000580 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
582 const mbedtls_md_info_t *md_info;
583 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100584 int ret;
585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
589 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100592
593 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000595
596 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100597 memcpy( tmp + md_len, label, nb );
598 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000599 nb += rlen;
600
601 /*
602 * Compute P_<hash>(secret, label + random)[0..dlen]
603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100605 return( ret );
606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
608 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
609 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100610
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100611 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 mbedtls_md_hmac_reset ( &md_ctx );
614 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
615 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_md_hmac_reset ( &md_ctx );
618 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
619 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000620
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100621 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000622
623 for( j = 0; j < k; j++ )
624 dstbuf[i + j] = h_i[j];
625 }
626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100628
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500629 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
630 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000631
632 return( 0 );
633}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500634#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100636static int tls_prf_sha256( const unsigned char *secret, size_t slen,
637 const char *label,
638 const unsigned char *random, size_t rlen,
639 unsigned char *dstbuf, size_t dlen )
640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100642 label, random, rlen, dstbuf, dlen ) );
643}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200647static int tls_prf_sha384( const unsigned char *secret, size_t slen,
648 const char *label,
649 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000650 unsigned char *dstbuf, size_t dlen )
651{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100653 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000654}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#endif /* MBEDTLS_SHA512_C */
656#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
661 defined(MBEDTLS_SSL_PROTO_TLS1_1)
662static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200663#endif
Paul Bakker380da532012-04-18 16:10:25 +0000664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665#if defined(MBEDTLS_SSL_PROTO_SSL3)
666static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
667static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200668#endif
669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
671static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
672static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200673#endif
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
676#if defined(MBEDTLS_SHA256_C)
677static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
678static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
679static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200680#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682#if defined(MBEDTLS_SHA512_C)
683static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
684static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
685static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100686#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000688
Hanno Becker7d0a5692018-10-23 15:26:22 +0100689#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
690 defined(MBEDTLS_USE_PSA_CRYPTO)
691static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
692{
693 if( ssl->conf->f_psk != NULL )
694 {
695 /* If we've used a callback to select the PSK,
696 * the static configuration is irrelevant. */
697 if( ssl->handshake->psk_opaque != 0 )
698 return( 1 );
699
700 return( 0 );
701 }
702
703 if( ssl->conf->psk_opaque != 0 )
704 return( 1 );
705
706 return( 0 );
707}
708#endif /* MBEDTLS_USE_PSA_CRYPTO &&
709 MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000712{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200713 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000714#if defined(MBEDTLS_USE_PSA_CRYPTO)
715 int psa_fallthrough;
716#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000717 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000718 unsigned char keyblk[256];
719 unsigned char *key1;
720 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100721 unsigned char *mac_enc;
722 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000723 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200724 size_t iv_copy_len;
Hanno Beckerf704bef2018-11-16 15:21:18 +0000725 size_t taglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 const mbedtls_cipher_info_t *cipher_info;
727 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100728
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000729 /* cf. RFC 5246, Section 8.1:
730 * "The master secret is always exactly 48 bytes in length." */
731 size_t const master_secret_len = 48;
732
Hanno Becker35b23c72018-10-23 12:10:41 +0100733#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
734 unsigned char session_hash[48];
735#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_ssl_session *session = ssl->session_negotiate;
738 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
739 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100744 if( cipher_info == NULL )
745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100747 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100749 }
750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100752 if( md_info == NULL )
753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100755 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100757 }
758
Paul Bakker5121ce52009-01-03 21:22:43 +0000759 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000760 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000761 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#if defined(MBEDTLS_SSL_PROTO_SSL3)
763 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000764 {
Paul Bakker48916f92012-09-16 19:57:18 +0000765 handshake->tls_prf = ssl3_prf;
766 handshake->calc_verify = ssl_calc_verify_ssl;
767 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000768 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200769 else
770#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
772 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000773 {
Paul Bakker48916f92012-09-16 19:57:18 +0000774 handshake->tls_prf = tls1_prf;
775 handshake->calc_verify = ssl_calc_verify_tls;
776 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000777 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200778 else
779#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
781#if defined(MBEDTLS_SHA512_C)
782 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
783 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000784 {
Paul Bakker48916f92012-09-16 19:57:18 +0000785 handshake->tls_prf = tls_prf_sha384;
786 handshake->calc_verify = ssl_calc_verify_tls_sha384;
787 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000788 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000789 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200790#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#if defined(MBEDTLS_SHA256_C)
792 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000793 {
Paul Bakker48916f92012-09-16 19:57:18 +0000794 handshake->tls_prf = tls_prf_sha256;
795 handshake->calc_verify = ssl_calc_verify_tls_sha256;
796 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000797 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200798 else
799#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
803 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200804 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000805
806 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000807 * SSLv3:
808 * master =
809 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
810 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
811 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200812 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200813 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000814 * master = PRF( premaster, "master secret", randbytes )[0..47]
815 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100816 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000817 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100818 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
819 }
820 else
821 {
822 /* The label for the KDF used for key expansion.
823 * This is either "master secret" or "extended master secret"
824 * depending on whether the Extended Master Secret extension
825 * is used. */
826 char const *lbl = "master secret";
827
828 /* The salt for the KDF used for key expansion.
829 * - If the Extended Master Secret extension is not used,
830 * this is ClientHello.Random + ServerHello.Random
831 * (see Sect. 8.1 in RFC 5246).
832 * - If the Extended Master Secret extension is used,
833 * this is the transcript of the handshake so far.
834 * (see Sect. 4 in RFC 7627). */
835 unsigned char const *salt = handshake->randbytes;
836 size_t salt_len = 64;
837
838#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
839 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
840 ssl->transform_negotiate->ciphersuite_info;
841 mbedtls_md_type_t const md_type = ciphersuite_info->mac;
842#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Paul Bakker5121ce52009-01-03 21:22:43 +0000843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
845 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200848
Hanno Becker35b23c72018-10-23 12:10:41 +0100849 lbl = "extended master secret";
850 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200851 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
853 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#if defined(MBEDTLS_SHA512_C)
Hanno Becker35b23c72018-10-23 12:10:41 +0100856 if( md_type == MBEDTLS_MD_SHA384 )
857 salt_len = 48;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200858 else
Hanno Becker35b23c72018-10-23 12:10:41 +0100859#endif /* MBEDTLS_SHA512_C */
860 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200861 }
862 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100864 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200865
Hanno Becker35b23c72018-10-23 12:10:41 +0100866 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200867 }
Hanno Becker35b23c72018-10-23 12:10:41 +0100868#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
869
Hanno Becker7d0a5692018-10-23 15:26:22 +0100870#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
871 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
872 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
873 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
874 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100875 {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100876 /* Perform PSK-to-MS expansion in a single step. */
877 psa_status_t status;
878 psa_algorithm_t alg;
879 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500880 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100881
882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
883
884 psk = ssl->conf->psk_opaque;
885 if( ssl->handshake->psk_opaque != 0 )
886 psk = ssl->handshake->psk_opaque;
887
888 if( md_type == MBEDTLS_MD_SHA384 )
889 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
890 else
891 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
892
893 status = psa_key_derivation( &generator, psk, alg,
894 salt, salt_len,
895 (unsigned char const *) lbl,
896 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000897 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100898 if( status != PSA_SUCCESS )
899 {
900 psa_generator_abort( &generator );
901 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
902 }
903
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000904 status = psa_generator_read( &generator, session->master,
905 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100906 if( status != PSA_SUCCESS )
907 {
908 psa_generator_abort( &generator );
909 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
910 }
911
912 status = psa_generator_abort( &generator );
913 if( status != PSA_SUCCESS )
914 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100915 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100916 else
917#endif
918 {
919 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
920 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000921 session->master,
922 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100923 if( ret != 0 )
924 {
925 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
926 return( ret );
927 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200928
Hanno Becker7d0a5692018-10-23 15:26:22 +0100929 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
930 handshake->premaster,
931 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +0100932
Hanno Becker7d0a5692018-10-23 15:26:22 +0100933 mbedtls_platform_zeroize( handshake->premaster,
934 sizeof(handshake->premaster) );
935 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000936 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000937
938 /*
939 * Swap the client and server random values.
940 */
Paul Bakker48916f92012-09-16 19:57:18 +0000941 memcpy( tmp, handshake->randbytes, 64 );
942 memcpy( handshake->randbytes, tmp + 32, 32 );
943 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500944 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000945
946 /*
947 * SSLv3:
948 * key block =
949 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
950 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
951 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
952 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
953 * ...
954 *
955 * TLSv1:
956 * key block = PRF( master, "key expansion", randbytes )
957 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100958 ret = handshake->tls_prf( session->master, 48, "key expansion",
959 handshake->randbytes, 64, keyblk, 256 );
960 if( ret != 0 )
961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100963 return( ret );
964 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
967 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
968 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
969 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
970 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000971
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500972 mbedtls_platform_zeroize( handshake->randbytes,
973 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000974
975 /*
976 * Determine the appropriate key, IV and MAC length.
977 */
Paul Bakker68884e32013-01-07 18:20:04 +0100978
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200979 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200982 cipher_info->mode == MBEDTLS_MODE_CCM ||
983 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000984 {
Hanno Beckerf704bef2018-11-16 15:21:18 +0000985 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200986
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200987 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000988 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200989
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200990 /* All modes haves 96-bit IVs;
991 * GCM and CCM has 4 implicit and 8 explicit bytes
992 * ChachaPoly has all 12 bytes implicit
993 */
Paul Bakker68884e32013-01-07 18:20:04 +0100994 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200995 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
996 transform->fixed_ivlen = 12;
997 else
998 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200999
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001000 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
1001 taglen = transform->ciphersuite_info->flags &
1002 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
1003
1004
1005 /* Minimum length of encrypted record */
1006 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
1007 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001008 }
1009 else
1010 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001011 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1013 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001016 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001017 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001019 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001020 mac_key_len = mbedtls_md_get_size( md_info );
1021 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001024 /*
1025 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1026 * (rfc 6066 page 13 or rfc 2104 section 4),
1027 * so we only need to adjust the length here.
1028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001032
1033#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1034 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001035 * HMAC implementation which also truncates the key
1036 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001037 mac_key_len = transform->maclen;
1038#endif
1039 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001041
1042 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001043 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001044
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001045 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001047 transform->minlen = transform->maclen;
1048 else
Paul Bakker68884e32013-01-07 18:20:04 +01001049 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001050 /*
1051 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001052 * 1. if EtM is in use: one block plus MAC
1053 * otherwise: * first multiple of blocklen greater than maclen
1054 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1057 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001058 {
1059 transform->minlen = transform->maclen
1060 + cipher_info->block_size;
1061 }
1062 else
1063#endif
1064 {
1065 transform->minlen = transform->maclen
1066 + cipher_info->block_size
1067 - transform->maclen % cipher_info->block_size;
1068 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1071 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1072 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001073 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001074 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001075#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1077 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1078 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001079 {
1080 transform->minlen += transform->ivlen;
1081 }
1082 else
1083#endif
1084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1086 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001087 }
Paul Bakker68884e32013-01-07 18:20:04 +01001088 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001089 }
1090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001092 transform->keylen, transform->minlen, transform->ivlen,
1093 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001094
1095 /*
1096 * Finally setup the cipher contexts, IVs and MAC secrets.
1097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001099 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001101 key1 = keyblk + mac_key_len * 2;
1102 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001103
Paul Bakker68884e32013-01-07 18:20:04 +01001104 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001105 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001106
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001107 /*
1108 * This is not used in TLS v1.1.
1109 */
Paul Bakker48916f92012-09-16 19:57:18 +00001110 iv_copy_len = ( transform->fixed_ivlen ) ?
1111 transform->fixed_ivlen : transform->ivlen;
1112 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
1113 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001114 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001115 }
1116 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117#endif /* MBEDTLS_SSL_CLI_C */
1118#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001119 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001120 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001121 key1 = keyblk + mac_key_len * 2 + transform->keylen;
1122 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001123
Hanno Becker81c7b182017-11-09 18:39:33 +00001124 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001125 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001126
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001127 /*
1128 * This is not used in TLS v1.1.
1129 */
Paul Bakker48916f92012-09-16 19:57:18 +00001130 iv_copy_len = ( transform->fixed_ivlen ) ?
1131 transform->fixed_ivlen : transform->ivlen;
1132 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
1133 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001134 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001136 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1140 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001141 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143#if defined(MBEDTLS_SSL_PROTO_SSL3)
1144 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001145 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001146 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1149 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001150 }
1151
Hanno Becker81c7b182017-11-09 18:39:33 +00001152 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1153 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001154 }
1155 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1157#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1158 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1159 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001160 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001161 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1162 For AEAD-based ciphersuites, there is nothing to do here. */
1163 if( mac_key_len != 0 )
1164 {
1165 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1166 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1167 }
Paul Bakker68884e32013-01-07 18:20:04 +01001168 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001169 else
1170#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1173 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001174 }
Paul Bakker68884e32013-01-07 18:20:04 +01001175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1177 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001178 {
1179 int ret = 0;
1180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001184 transform->iv_enc, transform->iv_dec,
1185 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001186 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001187 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1190 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001191 }
1192 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001194
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001195#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1196 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001197 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001198 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1199 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001200 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001201 iv_copy_len );
1202 }
1203#endif
1204
Hanno Beckerf704bef2018-11-16 15:21:18 +00001205#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001206
1207 /* Only use PSA-based ciphers for TLS-1.2.
1208 * That's relevant at least for TLS-1.0, where
1209 * we assume that mbedtls_cipher_crypt() updates
1210 * the structure field for the IV, which the PSA-based
1211 * implementation currently doesn't. */
1212#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1213 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001214 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001215 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
1216 cipher_info, taglen );
1217 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1218 {
1219 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1220 return( ret );
1221 }
1222
1223 if( ret == 0 )
1224 {
1225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based encryption cipher context" ) );
1226 psa_fallthrough = 0;
1227 }
1228 else
1229 {
1230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1231 psa_fallthrough = 1;
1232 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001233 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001234 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001235 psa_fallthrough = 1;
1236#else
1237 psa_fallthrough = 1;
1238#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001239
Hanno Beckercb1cc802018-11-17 22:27:38 +00001240 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001241#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001242 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001243 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001245 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001246 return( ret );
1247 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001248
Hanno Beckerf704bef2018-11-16 15:21:18 +00001249#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001250 /* Only use PSA-based ciphers for TLS-1.2.
1251 * That's relevant at least for TLS-1.0, where
1252 * we assume that mbedtls_cipher_crypt() updates
1253 * the structure field for the IV, which the PSA-based
1254 * implementation currently doesn't. */
1255#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1256 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001257 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001258 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
1259 cipher_info, taglen );
1260 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1261 {
1262 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1263 return( ret );
1264 }
1265
1266 if( ret == 0 )
1267 {
1268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based decryption cipher context" ) );
1269 psa_fallthrough = 0;
1270 }
1271 else
1272 {
1273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1274 psa_fallthrough = 1;
1275 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001276 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001277 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001278 psa_fallthrough = 1;
1279#else
1280 psa_fallthrough = 1;
1281#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001282
Hanno Beckercb1cc802018-11-17 22:27:38 +00001283 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001284#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001285 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001286 cipher_info ) ) != 0 )
1287 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001288 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001289 return( ret );
1290 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001293 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001297 return( ret );
1298 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001301 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001305 return( ret );
1306 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308#if defined(MBEDTLS_CIPHER_MODE_CBC)
1309 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1312 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001315 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001316 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1319 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001322 return( ret );
1323 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001324 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001327 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001330 // Initialize compression
1331 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001333 {
Paul Bakker16770332013-10-11 09:59:44 +02001334 if( ssl->compress_buf == NULL )
1335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001337 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001338 if( ssl->compress_buf == NULL )
1339 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001341 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001342 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001343 }
1344 }
1345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001347
Paul Bakker48916f92012-09-16 19:57:18 +00001348 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1349 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001350
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001351 if( deflateInit( &transform->ctx_deflate,
1352 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001353 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1356 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001357 }
1358 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001362
1363 return( 0 );
1364}
1365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#if defined(MBEDTLS_SSL_PROTO_SSL3)
1367void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001368{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001369 mbedtls_md5_context md5;
1370 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 unsigned char pad_1[48];
1372 unsigned char pad_2[48];
1373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001375
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001376 mbedtls_md5_init( &md5 );
1377 mbedtls_sha1_init( &sha1 );
1378
1379 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1380 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
Paul Bakker380da532012-04-18 16:10:25 +00001382 memset( pad_1, 0x36, 48 );
1383 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001384
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001385 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1386 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1387 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001388
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001389 mbedtls_md5_starts_ret( &md5 );
1390 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1391 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1392 mbedtls_md5_update_ret( &md5, hash, 16 );
1393 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001394
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001395 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1396 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1397 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001398
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001399 mbedtls_sha1_starts_ret( &sha1 );
1400 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1401 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1402 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1403 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001407
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001408 mbedtls_md5_free( &md5 );
1409 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001410
Paul Bakker380da532012-04-18 16:10:25 +00001411 return;
1412}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1416void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001417{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001418 mbedtls_md5_context md5;
1419 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001422
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001423 mbedtls_md5_init( &md5 );
1424 mbedtls_sha1_init( &sha1 );
1425
1426 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1427 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001428
Andrzej Kurekeb342242019-01-29 09:14:33 -05001429 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001430 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001434
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001435 mbedtls_md5_free( &md5 );
1436 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001437
Paul Bakker380da532012-04-18 16:10:25 +00001438 return;
1439}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1443#if defined(MBEDTLS_SHA256_C)
1444void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001445{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001446#if defined(MBEDTLS_USE_PSA_CRYPTO)
1447 size_t hash_size;
1448 psa_status_t status;
1449 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1450
1451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1452 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1453 if( status != PSA_SUCCESS )
1454 {
1455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1456 return;
1457 }
1458
1459 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1460 if( status != PSA_SUCCESS )
1461 {
1462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1463 return;
1464 }
1465 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1467#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001468 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001469
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001470 mbedtls_sha256_init( &sha256 );
1471
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001473
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001474 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001475 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001479
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001480 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001481#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001482 return;
1483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#if defined(MBEDTLS_SHA512_C)
1487void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001488{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001489#if defined(MBEDTLS_USE_PSA_CRYPTO)
1490 size_t hash_size;
1491 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001492 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001493
1494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001495 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001496 if( status != PSA_SUCCESS )
1497 {
1498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1499 return;
1500 }
1501
Andrzej Kurek972fba52019-01-30 03:29:12 -05001502 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001503 if( status != PSA_SUCCESS )
1504 {
1505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1506 return;
1507 }
1508 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1510#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001511 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001512
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001513 mbedtls_sha512_init( &sha512 );
1514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001516
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001517 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001518 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001522
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001523 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001524#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001525 return;
1526}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#endif /* MBEDTLS_SHA512_C */
1528#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1531int 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 +02001532{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001533 unsigned char *p = ssl->handshake->premaster;
1534 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001535 const unsigned char *psk = ssl->conf->psk;
1536 size_t psk_len = ssl->conf->psk_len;
1537
1538 /* If the psk callback was called, use its result */
1539 if( ssl->handshake->psk != NULL )
1540 {
1541 psk = ssl->handshake->psk;
1542 psk_len = ssl->handshake->psk_len;
1543 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001544
1545 /*
1546 * PMS = struct {
1547 * opaque other_secret<0..2^16-1>;
1548 * opaque psk<0..2^16-1>;
1549 * };
1550 * with "other_secret" depending on the particular key exchange
1551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1553 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001554 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001555 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001557
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001558 *(p++) = (unsigned char)( psk_len >> 8 );
1559 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001560
1561 if( end < p || (size_t)( end - p ) < psk_len )
1562 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1563
1564 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001565 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001566 }
1567 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1569#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1570 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001571 {
1572 /*
1573 * other_secret already set by the ClientKeyExchange message,
1574 * and is 48 bytes long
1575 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001576 if( end - p < 2 )
1577 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1578
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001579 *p++ = 0;
1580 *p++ = 48;
1581 p += 48;
1582 }
1583 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1585#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1586 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001587 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001588 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001589 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001590
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001591 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001593 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001594 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001597 return( ret );
1598 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001599 *(p++) = (unsigned char)( len >> 8 );
1600 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001601 p += len;
1602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001604 }
1605 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1607#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1608 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001609 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001610 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001611 size_t zlen;
1612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001614 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001615 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001618 return( ret );
1619 }
1620
1621 *(p++) = (unsigned char)( zlen >> 8 );
1622 *(p++) = (unsigned char)( zlen );
1623 p += zlen;
1624
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001625 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1626 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001627 }
1628 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1632 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001633 }
1634
1635 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001636 if( end - p < 2 )
1637 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001638
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001639 *(p++) = (unsigned char)( psk_len >> 8 );
1640 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001641
1642 if( end < p || (size_t)( end - p ) < psk_len )
1643 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1644
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001645 memcpy( p, psk, psk_len );
1646 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001647
1648 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1649
1650 return( 0 );
1651}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001655/*
1656 * SSLv3.0 MAC functions
1657 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001658#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001659static void ssl_mac( mbedtls_md_context_t *md_ctx,
1660 const unsigned char *secret,
1661 const unsigned char *buf, size_t len,
1662 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001663 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001664{
1665 unsigned char header[11];
1666 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001667 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1669 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001670
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001671 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001673 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001674 else
Paul Bakker68884e32013-01-07 18:20:04 +01001675 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
1677 memcpy( header, ctr, 8 );
1678 header[ 8] = (unsigned char) type;
1679 header[ 9] = (unsigned char)( len >> 8 );
1680 header[10] = (unsigned char)( len );
1681
Paul Bakker68884e32013-01-07 18:20:04 +01001682 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 mbedtls_md_starts( md_ctx );
1684 mbedtls_md_update( md_ctx, secret, md_size );
1685 mbedtls_md_update( md_ctx, padding, padlen );
1686 mbedtls_md_update( md_ctx, header, 11 );
1687 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001688 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001689
Paul Bakker68884e32013-01-07 18:20:04 +01001690 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691 mbedtls_md_starts( md_ctx );
1692 mbedtls_md_update( md_ctx, secret, md_size );
1693 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001694 mbedtls_md_update( md_ctx, out, md_size );
1695 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001696}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1700 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001701 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001702#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001703#endif
1704
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001705/* The function below is only used in the Lucky 13 counter-measure in
1706 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1707#if defined(SSL_SOME_MODES_USE_MAC) && \
1708 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1709 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1710 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1711/* This function makes sure every byte in the memory region is accessed
1712 * (in ascending addresses order) */
1713static void ssl_read_memory( unsigned char *p, size_t len )
1714{
1715 unsigned char acc = 0;
1716 volatile unsigned char force;
1717
1718 for( ; len != 0; p++, len-- )
1719 acc ^= *p;
1720
1721 force = acc;
1722 (void) force;
1723}
1724#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1725
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001726/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001727 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001730{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001732 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001735
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001736 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1739 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001740 }
1741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001745 ssl->out_msg, ssl->out_msglen );
1746
Paul Bakker5121ce52009-01-03 21:22:43 +00001747 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001748 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001749 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001750#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 if( mode == MBEDTLS_MODE_STREAM ||
1752 ( mode == MBEDTLS_MODE_CBC
1753#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1754 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001755#endif
1756 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758#if defined(MBEDTLS_SSL_PROTO_SSL3)
1759 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001760 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001761 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001762
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001763 ssl_mac( &ssl->transform_out->md_ctx_enc,
1764 ssl->transform_out->mac_enc,
1765 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001766 ssl->out_ctr, ssl->out_msgtype,
1767 mac );
1768
1769 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001770 }
1771 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001772#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1774 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1775 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001776 {
Hanno Becker992b6872017-11-09 18:57:39 +00001777 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1780 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1781 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1782 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001783 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001784 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001786
1787 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001788 }
1789 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001790#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1793 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001794 }
1795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001797 ssl->out_msg + ssl->out_msglen,
1798 ssl->transform_out->maclen );
1799
1800 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001801 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001802 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001803#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001804
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001805 /*
1806 * Encrypt
1807 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1809 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001810 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001811 int ret;
1812 size_t olen = 0;
1813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001815 "including %d bytes of padding",
1816 ssl->out_msglen, 0 ) );
1817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001819 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001820 ssl->transform_out->ivlen,
1821 ssl->out_msg, ssl->out_msglen,
1822 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001825 return( ret );
1826 }
1827
1828 if( ssl->out_msglen != olen )
1829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1831 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001832 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001833 }
Paul Bakker68884e32013-01-07 18:20:04 +01001834 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001836#if defined(MBEDTLS_GCM_C) || \
1837 defined(MBEDTLS_CCM_C) || \
1838 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001840 mode == MBEDTLS_MODE_CCM ||
1841 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001842 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001843 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001844 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001845 unsigned char *enc_msg;
1846 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001847 unsigned char iv[12];
1848 mbedtls_ssl_transform *transform = ssl->transform_out;
1849 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001851 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001852
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001853 /*
1854 * Prepare additional authenticated data
1855 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001856 memcpy( add_data, ssl->out_ctr, 8 );
1857 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001859 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001860 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1861 add_data[12] = ssl->out_msglen & 0xFF;
1862
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001863 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001864
Paul Bakker68884e32013-01-07 18:20:04 +01001865 /*
1866 * Generate IV
1867 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001868 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1869 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001870 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001871 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1872 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1873 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1874
1875 }
1876 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1877 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001878 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001879 unsigned char i;
1880
1881 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1882
1883 for( i = 0; i < 8; i++ )
1884 iv[i+4] ^= ssl->out_ctr[i];
1885 }
1886 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001887 {
1888 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1890 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001891 }
1892
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001893 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1894 iv, transform->ivlen );
1895 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1896 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001897
Paul Bakker68884e32013-01-07 18:20:04 +01001898 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001899 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001900 */
1901 enc_msg = ssl->out_msg;
1902 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001903 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001906 "including 0 bytes of padding",
1907 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001908
Paul Bakker68884e32013-01-07 18:20:04 +01001909 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001910 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001911 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001912 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1913 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001914 add_data, 13,
1915 enc_msg, enc_msglen,
1916 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001917 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001920 return( ret );
1921 }
1922
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001923 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1926 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001927 }
1928
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001929 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001930 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001933 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001934 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1936#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001937 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001939 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001940 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001941 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001942 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001943
Paul Bakker48916f92012-09-16 19:57:18 +00001944 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1945 ssl->transform_out->ivlen;
1946 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 padlen = 0;
1948
1949 for( i = 0; i <= padlen; i++ )
1950 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1951
1952 ssl->out_msglen += padlen + 1;
1953
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001954 enc_msglen = ssl->out_msglen;
1955 enc_msg = ssl->out_msg;
1956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001958 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001959 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1960 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001961 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001963 {
1964 /*
1965 * Generate IV
1966 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001967 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001968 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001969 if( ret != 0 )
1970 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001971
Paul Bakker92be97b2013-01-02 17:30:03 +01001972 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001973 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001974
1975 /*
1976 * Fix pointer positions and message length with added IV
1977 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001978 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001979 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001980 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001981 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001985 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001986 ssl->out_msglen, ssl->transform_out->ivlen,
1987 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001990 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001991 ssl->transform_out->ivlen,
1992 enc_msg, enc_msglen,
1993 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001996 return( ret );
1997 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001998
Paul Bakkercca5b812013-08-31 17:40:26 +02001999 if( enc_msglen != olen )
2000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2002 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002003 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2006 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002007 {
2008 /*
2009 * Save IV in SSL3 and TLS1
2010 */
2011 memcpy( ssl->transform_out->iv_enc,
2012 ssl->transform_out->cipher_ctx_enc.iv,
2013 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002015#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002018 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002019 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002020 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2021
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002022 /*
2023 * MAC(MAC_write_key, seq_num +
2024 * TLSCipherText.type +
2025 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002026 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002027 * IV + // except for TLS 1.0
2028 * ENC(content + padding + padding_length));
2029 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002030 unsigned char pseudo_hdr[13];
2031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002033
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002034 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
2035 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002036 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
2037 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
2038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
2042 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002043 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00002044 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002046
Hanno Becker3d8c9072018-01-05 16:24:22 +00002047 memcpy( ssl->out_iv + ssl->out_msglen, mac,
2048 ssl->transform_out->maclen );
2049
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002050 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002051 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002052 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002055 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002057 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2060 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002061 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002062
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002063 /* Make extra sure authentication was performed, exactly once */
2064 if( auth_done != 1 )
2065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2067 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002068 }
2069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002071
2072 return( 0 );
2073}
2074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002078 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002079#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002080 size_t padlen = 0, correct = 1;
2081#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002084
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002085 if( ssl->session_in == NULL || ssl->transform_in == NULL )
2086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2088 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002089 }
2090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002092
Paul Bakker48916f92012-09-16 19:57:18 +00002093 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00002096 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002098 }
2099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2101 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002102 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002103 int ret;
2104 size_t olen = 0;
2105
Paul Bakker68884e32013-01-07 18:20:04 +01002106 padlen = 0;
2107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002109 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002110 ssl->transform_in->ivlen,
2111 ssl->in_msg, ssl->in_msglen,
2112 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002115 return( ret );
2116 }
2117
2118 if( ssl->in_msglen != olen )
2119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2121 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002122 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 }
Paul Bakker68884e32013-01-07 18:20:04 +01002124 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002126#if defined(MBEDTLS_GCM_C) || \
2127 defined(MBEDTLS_CCM_C) || \
2128 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002130 mode == MBEDTLS_MODE_CCM ||
2131 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002132 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002133 int ret;
2134 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002135 unsigned char *dec_msg;
2136 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002137 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002138 unsigned char iv[12];
2139 mbedtls_ssl_transform *transform = ssl->transform_in;
2140 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002142 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002143
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002144 /*
2145 * Compute and update sizes
2146 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002147 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002150 "+ taglen (%d)", ssl->in_msglen,
2151 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002153 }
2154 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2155
Paul Bakker68884e32013-01-07 18:20:04 +01002156 dec_msg = ssl->in_msg;
2157 dec_msg_result = ssl->in_msg;
2158 ssl->in_msglen = dec_msglen;
2159
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002160 /*
2161 * Prepare additional authenticated data
2162 */
Paul Bakker68884e32013-01-07 18:20:04 +01002163 memcpy( add_data, ssl->in_ctr, 8 );
2164 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002166 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002167 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2168 add_data[12] = ssl->in_msglen & 0xFF;
2169
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002170 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002171
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002172 /*
2173 * Prepare IV
2174 */
2175 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2176 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002177 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002178 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2179 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002180
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002181 }
2182 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2183 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002184 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002185 unsigned char i;
2186
2187 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2188
2189 for( i = 0; i < 8; i++ )
2190 iv[i+4] ^= ssl->in_ctr[i];
2191 }
2192 else
2193 {
2194 /* Reminder if we ever add an AEAD mode with a different size */
2195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2196 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2197 }
2198
2199 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002201
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002202 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002203 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002206 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002207 add_data, 13,
2208 dec_msg, dec_msglen,
2209 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002210 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2215 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002216
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002217 return( ret );
2218 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002219 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002220
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002221 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2224 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002225 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002226 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002227 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2229#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002230 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 {
Paul Bakker45829992013-01-03 14:52:21 +01002233 /*
2234 * Decrypt and check the padding
2235 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002236 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002237 unsigned char *dec_msg;
2238 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002239 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002240 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002241 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002242
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 /*
Paul Bakker45829992013-01-03 14:52:21 +01002244 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2247 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002248 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002249#endif
Paul Bakker45829992013-01-03 14:52:21 +01002250
2251 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2252 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002255 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2256 ssl->transform_in->ivlen,
2257 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002259 }
2260
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002261 dec_msglen = ssl->in_msglen;
2262 dec_msg = ssl->in_msg;
2263 dec_msg_result = ssl->in_msg;
2264
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002265 /*
2266 * Authenticate before decrypt if enabled
2267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2269 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002270 {
Hanno Becker992b6872017-11-09 18:57:39 +00002271 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002272 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002275
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002276 dec_msglen -= ssl->transform_in->maclen;
2277 ssl->in_msglen -= ssl->transform_in->maclen;
2278
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002279 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2280 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2281 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2282 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2287 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002288 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002289 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002293 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002294 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002295 ssl->transform_in->maclen );
2296
Hanno Becker992b6872017-11-09 18:57:39 +00002297 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2298 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002303 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002304 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002305 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002307
2308 /*
2309 * Check length sanity
2310 */
2311 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002314 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002316 }
2317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002319 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002320 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002323 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002324 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002325 dec_msglen -= ssl->transform_in->ivlen;
2326 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002327
Paul Bakker48916f92012-09-16 19:57:18 +00002328 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002329 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002330 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002334 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002335 ssl->transform_in->ivlen,
2336 dec_msg, dec_msglen,
2337 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002340 return( ret );
2341 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002342
Paul Bakkercca5b812013-08-31 17:40:26 +02002343 if( dec_msglen != olen )
2344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2346 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002347 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2350 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002351 {
2352 /*
2353 * Save IV in SSL3 and TLS1
2354 */
2355 memcpy( ssl->transform_in->iv_dec,
2356 ssl->transform_in->cipher_ctx_dec.iv,
2357 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002358 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002359#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002360
2361 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002362
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002363 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002364 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366#if defined(MBEDTLS_SSL_DEBUG_ALL)
2367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002368 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002369#endif
Paul Bakker45829992013-01-03 14:52:21 +01002370 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002371 correct = 0;
2372 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374#if defined(MBEDTLS_SSL_PROTO_SSL3)
2375 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 {
Paul Bakker48916f92012-09-16 19:57:18 +00002377 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379#if defined(MBEDTLS_SSL_DEBUG_ALL)
2380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002381 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002382 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002383#endif
Paul Bakker45829992013-01-03 14:52:21 +01002384 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 }
2386 }
2387 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2389#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2390 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2391 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 {
2393 /*
Paul Bakker45829992013-01-03 14:52:21 +01002394 * TLSv1+: always check the padding up to the first failure
2395 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002397 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002398 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002399 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002400
Paul Bakker956c9e02013-12-19 14:42:28 +01002401 /*
2402 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002403 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002404 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002405 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002406 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002407 *
2408 * In both cases we reset padding_idx to a safe value (0) to
2409 * prevent out-of-buffer reads.
2410 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002411 correct &= ( padlen <= ssl->in_msglen );
2412 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002413 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002414
2415 padding_idx *= correct;
2416
Angus Grattonb512bc12018-06-19 15:57:50 +10002417 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002418 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002419 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002420 pad_count += real_count *
2421 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2422 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002423
2424 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002427 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002429#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002430 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002432 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2434 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2437 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002438 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002439
2440 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002441 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002442 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002444 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2447 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002448 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002449
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002450#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002452 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002453#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002454
2455 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002456 * Authenticate if not done yet.
2457 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002459#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002460 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002461 {
Hanno Becker992b6872017-11-09 18:57:39 +00002462 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002463
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002464 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002465
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002466 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2467 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469#if defined(MBEDTLS_SSL_PROTO_SSL3)
2470 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002471 {
2472 ssl_mac( &ssl->transform_in->md_ctx_dec,
2473 ssl->transform_in->mac_dec,
2474 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002475 ssl->in_ctr, ssl->in_msgtype,
2476 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002477 }
2478 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2480#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2481 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2482 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002483 {
2484 /*
2485 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002486 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002487 *
2488 * Known timing attacks:
2489 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2490 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002491 * To compensate for different timings for the MAC calculation
2492 * depending on how much padding was removed (which is determined
2493 * by padlen), process extra_run more blocks through the hash
2494 * function.
2495 *
2496 * The formula in the paper is
2497 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2498 * where L1 is the size of the header plus the decrypted message
2499 * plus CBC padding and L2 is the size of the header plus the
2500 * decrypted message. This is for an underlying hash function
2501 * with 64-byte blocks.
2502 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2503 * correctly. We round down instead of up, so -56 is the correct
2504 * value for our calculations instead of -55.
2505 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002506 * Repeat the formula rather than defining a block_size variable.
2507 * This avoids requiring division by a variable at runtime
2508 * (which would be marginally less efficient and would require
2509 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002510 */
2511 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002512
2513 /*
2514 * The next two sizes are the minimum and maximum values of
2515 * in_msglen over all padlen values.
2516 *
2517 * They're independent of padlen, since we previously did
2518 * in_msglen -= padlen.
2519 *
2520 * Note that max_len + maclen is never more than the buffer
2521 * length, as we previously did in_msglen -= maclen too.
2522 */
2523 const size_t max_len = ssl->in_msglen + padlen;
2524 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2525
Gilles Peskine20b44082018-05-29 14:06:49 +02002526 switch( ssl->transform_in->ciphersuite_info->mac )
2527 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002528#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2529 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002530 case MBEDTLS_MD_MD5:
2531 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002532 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002533 /* 8 bytes of message size, 64-byte compression blocks */
2534 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2535 ( 13 + ssl->in_msglen + 8 ) / 64;
2536 break;
2537#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002538#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002539 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002540 /* 16 bytes of message size, 128-byte compression blocks */
2541 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2542 ( 13 + ssl->in_msglen + 16 ) / 128;
2543 break;
2544#endif
2545 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002547 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2548 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002549
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002550 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2553 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2554 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2555 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002556 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002557 /* Make sure we access everything even when padlen > 0. This
2558 * makes the synchronisation requirements for just-in-time
2559 * Prime+Probe attacks much tighter and hopefully impractical. */
2560 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002561 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002562
2563 /* Call mbedtls_md_process at least once due to cache attacks
2564 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002565 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002569
2570 /* Make sure we access all the memory that could contain the MAC,
2571 * before we check it in the next code block. This makes the
2572 * synchronisation requirements for just-in-time Prime+Probe
2573 * attacks much tighter and hopefully impractical. */
2574 ssl_read_memory( ssl->in_msg + min_len,
2575 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002576 }
2577 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2579 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2582 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002583 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002584
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002585#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002586 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2587 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2588 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002589#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002590
Hanno Becker992b6872017-11-09 18:57:39 +00002591 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2592 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594#if defined(MBEDTLS_SSL_DEBUG_ALL)
2595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002596#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002597 correct = 0;
2598 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002599 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002600 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002601
2602 /*
2603 * Finally check the correct flag
2604 */
2605 if( correct == 0 )
2606 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002607#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002608
2609 /* Make extra sure authentication was performed, exactly once */
2610 if( auth_done != 1 )
2611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2613 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002615
2616 if( ssl->in_msglen == 0 )
2617 {
Angus Gratton34817922018-06-19 15:58:22 +10002618#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2619 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2620 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2621 {
2622 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2624 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2625 }
2626#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2627
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 ssl->nb_zero++;
2629
2630 /*
2631 * Three or more empty messages may be a DoS attack
2632 * (excessive CPU consumption).
2633 */
2634 if( ssl->nb_zero > 3 )
2635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002637 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002639 }
2640 }
2641 else
2642 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002645 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002646 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002647 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002648 }
2649 else
2650#endif
2651 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002652 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002653 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2654 if( ++ssl->in_ctr[i - 1] != 0 )
2655 break;
2656
2657 /* The loop goes to its end iff the counter is wrapping */
2658 if( i == ssl_ep_len( ssl ) )
2659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2661 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002662 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002663 }
2664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002666
2667 return( 0 );
2668}
2669
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002670#undef MAC_NONE
2671#undef MAC_PLAINTEXT
2672#undef MAC_CIPHERTEXT
2673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002675/*
2676 * Compression/decompression functions
2677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002679{
2680 int ret;
2681 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002682 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002683 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002684 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002687
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002688 if( len_pre == 0 )
2689 return( 0 );
2690
Paul Bakker2770fbd2012-07-03 13:30:23 +00002691 memcpy( msg_pre, ssl->out_msg, len_pre );
2692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002694 ssl->out_msglen ) );
2695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002697 ssl->out_msg, ssl->out_msglen );
2698
Paul Bakker48916f92012-09-16 19:57:18 +00002699 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2700 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2701 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002702 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002703
Paul Bakker48916f92012-09-16 19:57:18 +00002704 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002705 if( ret != Z_OK )
2706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2708 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002709 }
2710
Angus Grattond8213d02016-05-25 20:56:48 +10002711 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002712 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002715 ssl->out_msglen ) );
2716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002718 ssl->out_msg, ssl->out_msglen );
2719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002721
2722 return( 0 );
2723}
2724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002726{
2727 int ret;
2728 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002729 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002730 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002731 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002734
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002735 if( len_pre == 0 )
2736 return( 0 );
2737
Paul Bakker2770fbd2012-07-03 13:30:23 +00002738 memcpy( msg_pre, ssl->in_msg, len_pre );
2739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002741 ssl->in_msglen ) );
2742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002744 ssl->in_msg, ssl->in_msglen );
2745
Paul Bakker48916f92012-09-16 19:57:18 +00002746 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2747 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2748 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002749 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002750 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002751
Paul Bakker48916f92012-09-16 19:57:18 +00002752 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002753 if( ret != Z_OK )
2754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2756 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002757 }
2758
Angus Grattond8213d02016-05-25 20:56:48 +10002759 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002760 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002763 ssl->in_msglen ) );
2764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002766 ssl->in_msg, ssl->in_msglen );
2767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002769
2770 return( 0 );
2771}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2775static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777#if defined(MBEDTLS_SSL_PROTO_DTLS)
2778static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002779{
2780 /* If renegotiation is not enforced, retransmit until we would reach max
2781 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002782 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002783 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002784 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002785 unsigned char doublings = 1;
2786
2787 while( ratio != 0 )
2788 {
2789 ++doublings;
2790 ratio >>= 1;
2791 }
2792
2793 if( ++ssl->renego_records_seen > doublings )
2794 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002796 return( 0 );
2797 }
2798 }
2799
2800 return( ssl_write_hello_request( ssl ) );
2801}
2802#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002804
Paul Bakker5121ce52009-01-03 21:22:43 +00002805/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002806 * Fill the input message buffer by appending data to it.
2807 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002808 *
2809 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2810 * available (from this read and/or a previous one). Otherwise, an error code
2811 * is returned (possibly EOF or WANT_READ).
2812 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002813 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2814 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2815 * since we always read a whole datagram at once.
2816 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002817 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002818 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002821{
Paul Bakker23986e52011-04-24 08:57:21 +00002822 int ret;
2823 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002826
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002827 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002830 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002832 }
2833
Angus Grattond8213d02016-05-25 20:56:48 +10002834 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2837 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002838 }
2839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002841 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002842 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002843 uint32_t timeout;
2844
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002845 /* Just to be sure */
2846 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2847 {
2848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2849 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2850 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2851 }
2852
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002853 /*
2854 * The point is, we need to always read a full datagram at once, so we
2855 * sometimes read more then requested, and handle the additional data.
2856 * It could be the rest of the current record (while fetching the
2857 * header) and/or some other records in the same datagram.
2858 */
2859
2860 /*
2861 * Move to the next record in the already read datagram if applicable
2862 */
2863 if( ssl->next_record_offset != 0 )
2864 {
2865 if( ssl->in_left < ssl->next_record_offset )
2866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2868 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002869 }
2870
2871 ssl->in_left -= ssl->next_record_offset;
2872
2873 if( ssl->in_left != 0 )
2874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002876 ssl->next_record_offset ) );
2877 memmove( ssl->in_hdr,
2878 ssl->in_hdr + ssl->next_record_offset,
2879 ssl->in_left );
2880 }
2881
2882 ssl->next_record_offset = 0;
2883 }
2884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002886 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002887
2888 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002889 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002890 */
2891 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002894 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002895 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002896
2897 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002898 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002899 * are not at the beginning of a new record, the caller did something
2900 * wrong.
2901 */
2902 if( ssl->in_left != 0 )
2903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2905 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002906 }
2907
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002908 /*
2909 * Don't even try to read if time's out already.
2910 * This avoids by-passing the timer when repeatedly receiving messages
2911 * that will end up being dropped.
2912 */
2913 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002914 {
2915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002916 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002917 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002918 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002919 {
Angus Grattond8213d02016-05-25 20:56:48 +10002920 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002923 timeout = ssl->handshake->retransmit_timeout;
2924 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002925 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002928
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002929 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002930 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2931 timeout );
2932 else
2933 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002936
2937 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002939 }
2940
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002941 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002944 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002947 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002948 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002951 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002952 }
2953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002957 return( ret );
2958 }
2959
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002960 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002961 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002963 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002965 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002966 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002969 return( ret );
2970 }
2971
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002972 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002973 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002975 }
2976
Paul Bakker5121ce52009-01-03 21:22:43 +00002977 if( ret < 0 )
2978 return( ret );
2979
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002980 ssl->in_left = ret;
2981 }
2982 else
2983#endif
2984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002986 ssl->in_left, nb_want ) );
2987
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002988 while( ssl->in_left < nb_want )
2989 {
2990 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002991
2992 if( ssl_check_timer( ssl ) != 0 )
2993 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2994 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002995 {
2996 if( ssl->f_recv_timeout != NULL )
2997 {
2998 ret = ssl->f_recv_timeout( ssl->p_bio,
2999 ssl->in_hdr + ssl->in_left, len,
3000 ssl->conf->read_timeout );
3001 }
3002 else
3003 {
3004 ret = ssl->f_recv( ssl->p_bio,
3005 ssl->in_hdr + ssl->in_left, len );
3006 }
3007 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003010 ssl->in_left, nb_want ) );
3011 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003012
3013 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003015
3016 if( ret < 0 )
3017 return( ret );
3018
mohammad160352aecb92018-03-28 23:41:40 -07003019 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003020 {
Darryl Green11999bb2018-03-13 15:22:58 +00003021 MBEDTLS_SSL_DEBUG_MSG( 1,
3022 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003023 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003024 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3025 }
3026
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003027 ssl->in_left += ret;
3028 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003029 }
3030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003032
3033 return( 0 );
3034}
3035
3036/*
3037 * Flush any data not yet written
3038 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003040{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003041 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003042 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003045
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003046 if( ssl->f_send == NULL )
3047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003049 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003051 }
3052
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003053 /* Avoid incrementing counter if data is flushed */
3054 if( ssl->out_left == 0 )
3055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003057 return( 0 );
3058 }
3059
Paul Bakker5121ce52009-01-03 21:22:43 +00003060 while( ssl->out_left > 0 )
3061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3063 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003064
Hanno Becker2b1e3542018-08-06 11:19:13 +01003065 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003066 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003069
3070 if( ret <= 0 )
3071 return( ret );
3072
mohammad160352aecb92018-03-28 23:41:40 -07003073 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003074 {
Darryl Green11999bb2018-03-13 15:22:58 +00003075 MBEDTLS_SSL_DEBUG_MSG( 1,
3076 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003077 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003078 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3079 }
3080
Paul Bakker5121ce52009-01-03 21:22:43 +00003081 ssl->out_left -= ret;
3082 }
3083
Hanno Becker2b1e3542018-08-06 11:19:13 +01003084#if defined(MBEDTLS_SSL_PROTO_DTLS)
3085 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003086 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003087 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003088 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003089 else
3090#endif
3091 {
3092 ssl->out_hdr = ssl->out_buf + 8;
3093 }
3094 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003097
3098 return( 0 );
3099}
3100
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003101/*
3102 * Functions to handle the DTLS retransmission state machine
3103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003105/*
3106 * Append current handshake message to current outgoing flight
3107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003108static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003109{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3112 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3113 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003114
3115 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003116 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003117 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003120 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003121 }
3122
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003123 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003124 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003127 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003128 }
3129
3130 /* Copy current handshake message with headers */
3131 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3132 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003133 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003134 msg->next = NULL;
3135
3136 /* Append to the current flight */
3137 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003138 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003139 else
3140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003142 while( cur->next != NULL )
3143 cur = cur->next;
3144 cur->next = msg;
3145 }
3146
Hanno Becker3b235902018-08-06 09:54:53 +01003147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003148 return( 0 );
3149}
3150
3151/*
3152 * Free the current flight of handshake messages
3153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003155{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 mbedtls_ssl_flight_item *cur = flight;
3157 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003158
3159 while( cur != NULL )
3160 {
3161 next = cur->next;
3162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163 mbedtls_free( cur->p );
3164 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003165
3166 cur = next;
3167 }
3168}
3169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3171static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003172#endif
3173
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003174/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003175 * Swap transform_out and out_ctr with the alternative ones
3176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003178{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003180 unsigned char tmp_out_ctr[8];
3181
3182 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003185 return;
3186 }
3187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003189
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003190 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003191 tmp_transform = ssl->transform_out;
3192 ssl->transform_out = ssl->handshake->alt_transform_out;
3193 ssl->handshake->alt_transform_out = tmp_transform;
3194
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003195 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003196 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3197 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003198 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003199
3200 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003201 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3204 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3209 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003210 }
3211 }
3212#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003213}
3214
3215/*
3216 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003217 */
3218int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3219{
3220 int ret = 0;
3221
3222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3223
3224 ret = mbedtls_ssl_flight_transmit( ssl );
3225
3226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3227
3228 return( ret );
3229}
3230
3231/*
3232 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003233 *
3234 * Need to remember the current message in case flush_output returns
3235 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003236 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003237 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003238int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003239{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003240 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003244 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003246
3247 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003248 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003249 ssl_swap_epochs( ssl );
3250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003252 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003253
3254 while( ssl->handshake->cur_msg != NULL )
3255 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003256 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003257 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003258
Hanno Beckere1dcb032018-08-17 16:47:58 +01003259 int const is_finished =
3260 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3261 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3262
Hanno Becker04da1892018-08-14 13:22:10 +01003263 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3264 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3265
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003266 /* Swap epochs before sending Finished: we can't do it after
3267 * sending ChangeCipherSpec, in case write returns WANT_READ.
3268 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003269 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003270 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003272 ssl_swap_epochs( ssl );
3273 }
3274
Hanno Becker67bc7c32018-08-06 11:33:50 +01003275 ret = ssl_get_remaining_payload_in_datagram( ssl );
3276 if( ret < 0 )
3277 return( ret );
3278 max_frag_len = (size_t) ret;
3279
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003280 /* CCS is copied as is, while HS messages may need fragmentation */
3281 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3282 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003283 if( max_frag_len == 0 )
3284 {
3285 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3286 return( ret );
3287
3288 continue;
3289 }
3290
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003291 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003292 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003293 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003294
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003295 /* Update position inside current message */
3296 ssl->handshake->cur_msg_p += cur->len;
3297 }
3298 else
3299 {
3300 const unsigned char * const p = ssl->handshake->cur_msg_p;
3301 const size_t hs_len = cur->len - 12;
3302 const size_t frag_off = p - ( cur->p + 12 );
3303 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003304 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003305
Hanno Beckere1dcb032018-08-17 16:47:58 +01003306 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003307 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003308 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003309 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003310
Hanno Becker67bc7c32018-08-06 11:33:50 +01003311 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3312 return( ret );
3313
3314 continue;
3315 }
3316 max_hs_frag_len = max_frag_len - 12;
3317
3318 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3319 max_hs_frag_len : rem_len;
3320
3321 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003322 {
3323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003324 (unsigned) cur_hs_frag_len,
3325 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003326 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003327
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003328 /* Messages are stored with handshake headers as if not fragmented,
3329 * copy beginning of headers then fill fragmentation fields.
3330 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3331 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003332
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003333 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3334 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3335 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3336
Hanno Becker67bc7c32018-08-06 11:33:50 +01003337 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3338 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3339 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003340
3341 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3342
Hanno Becker3f7b9732018-08-28 09:53:25 +01003343 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003344 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3345 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003346 ssl->out_msgtype = cur->type;
3347
3348 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003349 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003350 }
3351
3352 /* If done with the current message move to the next one if any */
3353 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3354 {
3355 if( cur->next != NULL )
3356 {
3357 ssl->handshake->cur_msg = cur->next;
3358 ssl->handshake->cur_msg_p = cur->next->p + 12;
3359 }
3360 else
3361 {
3362 ssl->handshake->cur_msg = NULL;
3363 ssl->handshake->cur_msg_p = NULL;
3364 }
3365 }
3366
3367 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003368 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003371 return( ret );
3372 }
3373 }
3374
Hanno Becker67bc7c32018-08-06 11:33:50 +01003375 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3376 return( ret );
3377
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003378 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3380 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003381 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003384 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3385 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003386
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003388
3389 return( 0 );
3390}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003391
3392/*
3393 * To be called when the last message of an incoming flight is received.
3394 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003396{
3397 /* We won't need to resend that one any more */
3398 ssl_flight_free( ssl->handshake->flight );
3399 ssl->handshake->flight = NULL;
3400 ssl->handshake->cur_msg = NULL;
3401
3402 /* The next incoming flight will start with this msg_seq */
3403 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3404
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003405 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003406 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003407
Hanno Becker0271f962018-08-16 13:23:47 +01003408 /* Clear future message buffering structure. */
3409 ssl_buffering_free( ssl );
3410
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003411 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003412 ssl_set_timer( ssl, 0 );
3413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3415 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003418 }
3419 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003421}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003422
3423/*
3424 * To be called when the last message of an outgoing flight is send.
3425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003427{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003428 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003429 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3432 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003435 }
3436 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003438}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003440
Paul Bakker5121ce52009-01-03 21:22:43 +00003441/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003442 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003443 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003444
3445/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003446 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003447 *
3448 * - fill in handshake headers
3449 * - update handshake checksum
3450 * - DTLS: save message for resending
3451 * - then pass to the record layer
3452 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003453 * DTLS: except for HelloRequest, messages are only queued, and will only be
3454 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003455 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003456 * Inputs:
3457 * - ssl->out_msglen: 4 + actual handshake message len
3458 * (4 is the size of handshake headers for TLS)
3459 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3460 * - ssl->out_msg + 4: the handshake message body
3461 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003462 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003463 * - ssl->out_msglen: the length of the record contents
3464 * (including handshake headers but excluding record headers)
3465 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003466 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003467int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003468{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003469 int ret;
3470 const size_t hs_len = ssl->out_msglen - 4;
3471 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003472
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3474
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003475 /*
3476 * Sanity checks
3477 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003478 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003479 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3480 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003481 /* In SSLv3, the client might send a NoCertificate alert. */
3482#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3483 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3484 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3485 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3486#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3487 {
3488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3490 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003491 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003492
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003493 /* Whenever we send anything different from a
3494 * HelloRequest we should be in a handshake - double check. */
3495 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3496 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003497 ssl->handshake == NULL )
3498 {
3499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3500 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3501 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003504 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003505 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003507 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3509 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003510 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003511#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003512
Hanno Beckerb50a2532018-08-06 11:52:54 +01003513 /* Double-check that we did not exceed the bounds
3514 * of the outgoing record buffer.
3515 * This should never fail as the various message
3516 * writing functions must obey the bounds of the
3517 * outgoing record buffer, but better be safe.
3518 *
3519 * Note: We deliberately do not check for the MTU or MFL here.
3520 */
3521 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3522 {
3523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3524 "size %u, maximum %u",
3525 (unsigned) ssl->out_msglen,
3526 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3527 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3528 }
3529
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003530 /*
3531 * Fill handshake headers
3532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003534 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003535 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3536 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3537 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003538
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003539 /*
3540 * DTLS has additional fields in the Handshake layer,
3541 * between the length field and the actual payload:
3542 * uint16 message_seq;
3543 * uint24 fragment_offset;
3544 * uint24 fragment_length;
3545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003547 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003548 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003549 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003550 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003551 {
3552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3553 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003554 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003555 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003556 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3557 }
3558
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003559 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003560 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003561
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003562 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003563 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003564 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003565 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3566 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3567 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003568 }
3569 else
3570 {
3571 ssl->out_msg[4] = 0;
3572 ssl->out_msg[5] = 0;
3573 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003574
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003575 /* Handshake hashes are computed without fragmentation,
3576 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003577 memset( ssl->out_msg + 6, 0x00, 3 );
3578 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003579 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003581
Hanno Becker0207e532018-08-28 10:28:28 +01003582 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003583 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3584 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003585 }
3586
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003587 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003589 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003590 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3591 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003592 {
3593 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003596 return( ret );
3597 }
3598 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003599 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003600#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003601 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003602 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003603 {
3604 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3605 return( ret );
3606 }
3607 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003608
3609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3610
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003611 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003612}
3613
3614/*
3615 * Record layer functions
3616 */
3617
3618/*
3619 * Write current record.
3620 *
3621 * Uses:
3622 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3623 * - ssl->out_msglen: length of the record content (excl headers)
3624 * - ssl->out_msg: record content
3625 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003626int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003627{
3628 int ret, done = 0;
3629 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003630 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003631
3632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003634#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003635 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003637 {
3638 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003641 return( ret );
3642 }
3643
3644 len = ssl->out_msglen;
3645 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3649 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653 ret = mbedtls_ssl_hw_record_write( ssl );
3654 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3657 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003658 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003659
3660 if( ret == 0 )
3661 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003662 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003664 if( !done )
3665 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003666 unsigned i;
3667 size_t protected_record_size;
3668
Paul Bakker05ef8352012-05-08 09:17:57 +00003669 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003671 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003672
Hanno Becker19859472018-08-06 09:40:20 +01003673 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003674 ssl->out_len[0] = (unsigned char)( len >> 8 );
3675 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003676
Paul Bakker48916f92012-09-16 19:57:18 +00003677 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003678 {
3679 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003681 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003682 return( ret );
3683 }
3684
3685 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003686 ssl->out_len[0] = (unsigned char)( len >> 8 );
3687 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003688 }
3689
Hanno Becker2b1e3542018-08-06 11:19:13 +01003690 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3691
3692#if defined(MBEDTLS_SSL_PROTO_DTLS)
3693 /* In case of DTLS, double-check that we don't exceed
3694 * the remaining space in the datagram. */
3695 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3696 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003697 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003698 if( ret < 0 )
3699 return( ret );
3700
3701 if( protected_record_size > (size_t) ret )
3702 {
3703 /* Should never happen */
3704 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3705 }
3706 }
3707#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003710 "version = [%d:%d], msglen = %d",
3711 ssl->out_hdr[0], ssl->out_hdr[1],
3712 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003715 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003716
3717 ssl->out_left += protected_record_size;
3718 ssl->out_hdr += protected_record_size;
3719 ssl_update_out_pointers( ssl, ssl->transform_out );
3720
Hanno Becker04484622018-08-06 09:49:38 +01003721 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3722 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3723 break;
3724
3725 /* The loop goes to its end iff the counter is wrapping */
3726 if( i == ssl_ep_len( ssl ) )
3727 {
3728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3729 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3730 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003731 }
3732
Hanno Becker67bc7c32018-08-06 11:33:50 +01003733#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003734 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3735 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003736 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003737 size_t remaining;
3738 ret = ssl_get_remaining_payload_in_datagram( ssl );
3739 if( ret < 0 )
3740 {
3741 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3742 ret );
3743 return( ret );
3744 }
3745
3746 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003747 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003748 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003749 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003750 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003751 else
3752 {
Hanno Becker513815a2018-08-20 11:56:09 +01003753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003754 }
3755 }
3756#endif /* MBEDTLS_SSL_PROTO_DTLS */
3757
3758 if( ( flush == SSL_FORCE_FLUSH ) &&
3759 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003762 return( ret );
3763 }
3764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003766
3767 return( 0 );
3768}
3769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003771
3772static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3773{
3774 if( ssl->in_msglen < ssl->in_hslen ||
3775 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3776 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3777 {
3778 return( 1 );
3779 }
3780 return( 0 );
3781}
Hanno Becker44650b72018-08-16 12:51:11 +01003782
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003783static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003784{
3785 return( ( ssl->in_msg[9] << 16 ) |
3786 ( ssl->in_msg[10] << 8 ) |
3787 ssl->in_msg[11] );
3788}
3789
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003790static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003791{
3792 return( ( ssl->in_msg[6] << 16 ) |
3793 ( ssl->in_msg[7] << 8 ) |
3794 ssl->in_msg[8] );
3795}
3796
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003797static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003798{
3799 uint32_t msg_len, frag_off, frag_len;
3800
3801 msg_len = ssl_get_hs_total_len( ssl );
3802 frag_off = ssl_get_hs_frag_off( ssl );
3803 frag_len = ssl_get_hs_frag_len( ssl );
3804
3805 if( frag_off > msg_len )
3806 return( -1 );
3807
3808 if( frag_len > msg_len - frag_off )
3809 return( -1 );
3810
3811 if( frag_len + 12 > ssl->in_msglen )
3812 return( -1 );
3813
3814 return( 0 );
3815}
3816
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003817/*
3818 * Mark bits in bitmask (used for DTLS HS reassembly)
3819 */
3820static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3821{
3822 unsigned int start_bits, end_bits;
3823
3824 start_bits = 8 - ( offset % 8 );
3825 if( start_bits != 8 )
3826 {
3827 size_t first_byte_idx = offset / 8;
3828
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003829 /* Special case */
3830 if( len <= start_bits )
3831 {
3832 for( ; len != 0; len-- )
3833 mask[first_byte_idx] |= 1 << ( start_bits - len );
3834
3835 /* Avoid potential issues with offset or len becoming invalid */
3836 return;
3837 }
3838
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003839 offset += start_bits; /* Now offset % 8 == 0 */
3840 len -= start_bits;
3841
3842 for( ; start_bits != 0; start_bits-- )
3843 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3844 }
3845
3846 end_bits = len % 8;
3847 if( end_bits != 0 )
3848 {
3849 size_t last_byte_idx = ( offset + len ) / 8;
3850
3851 len -= end_bits; /* Now len % 8 == 0 */
3852
3853 for( ; end_bits != 0; end_bits-- )
3854 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3855 }
3856
3857 memset( mask + offset / 8, 0xFF, len / 8 );
3858}
3859
3860/*
3861 * Check that bitmask is full
3862 */
3863static int ssl_bitmask_check( unsigned char *mask, size_t len )
3864{
3865 size_t i;
3866
3867 for( i = 0; i < len / 8; i++ )
3868 if( mask[i] != 0xFF )
3869 return( -1 );
3870
3871 for( i = 0; i < len % 8; i++ )
3872 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3873 return( -1 );
3874
3875 return( 0 );
3876}
3877
Hanno Becker56e205e2018-08-16 09:06:12 +01003878/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003879static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003880 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003881{
Hanno Becker56e205e2018-08-16 09:06:12 +01003882 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003883
Hanno Becker56e205e2018-08-16 09:06:12 +01003884 alloc_len = 12; /* Handshake header */
3885 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003886
Hanno Beckerd07df862018-08-16 09:14:58 +01003887 if( add_bitmap )
3888 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003889
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003890 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003891}
Hanno Becker56e205e2018-08-16 09:06:12 +01003892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003894
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003895static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003896{
3897 return( ( ssl->in_msg[1] << 16 ) |
3898 ( ssl->in_msg[2] << 8 ) |
3899 ssl->in_msg[3] );
3900}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003901
Simon Butcher99000142016-10-13 17:21:01 +01003902int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003903{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003907 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003909 }
3910
Hanno Becker12555c62018-08-16 12:47:53 +01003911 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003913 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003914 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003915 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003917#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003918 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003919 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003920 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003921 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003922
Hanno Becker44650b72018-08-16 12:51:11 +01003923 if( ssl_check_hs_header( ssl ) != 0 )
3924 {
3925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3926 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3927 }
3928
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003929 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003930 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3931 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3932 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3933 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003934 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003935 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3936 {
3937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3938 recv_msg_seq,
3939 ssl->handshake->in_msg_seq ) );
3940 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3941 }
3942
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003943 /* Retransmit only on last message from previous flight, to avoid
3944 * too many retransmissions.
3945 * Besides, No sane server ever retransmits HelloVerifyRequest */
3946 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003947 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003950 "message_seq = %d, start_of_flight = %d",
3951 recv_msg_seq,
3952 ssl->handshake->in_flight_start_seq ) );
3953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003957 return( ret );
3958 }
3959 }
3960 else
3961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003963 "message_seq = %d, expected = %d",
3964 recv_msg_seq,
3965 ssl->handshake->in_msg_seq ) );
3966 }
3967
Hanno Becker90333da2017-10-10 11:27:13 +01003968 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003969 }
3970 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003971
Hanno Becker6d97ef52018-08-16 13:09:04 +01003972 /* Message reassembly is handled alongside buffering of future
3973 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003974 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003975 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003976 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003979 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003980 }
3981 }
3982 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003984 /* With TLS we don't handle fragmentation (for now) */
3985 if( ssl->in_msglen < ssl->in_hslen )
3986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3988 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003989 }
3990
Simon Butcher99000142016-10-13 17:21:01 +01003991 return( 0 );
3992}
3993
3994void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3995{
Hanno Becker0271f962018-08-16 13:23:47 +01003996 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003997
Hanno Becker0271f962018-08-16 13:23:47 +01003998 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003999 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004000 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004001 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004002
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004003 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004004#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004005 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004006 ssl->handshake != NULL )
4007 {
Hanno Becker0271f962018-08-16 13:23:47 +01004008 unsigned offset;
4009 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004010
Hanno Becker0271f962018-08-16 13:23:47 +01004011 /* Increment handshake sequence number */
4012 hs->in_msg_seq++;
4013
4014 /*
4015 * Clear up handshake buffering and reassembly structure.
4016 */
4017
4018 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004019 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004020
4021 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004022 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4023 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004024 offset++, hs_buf++ )
4025 {
4026 *hs_buf = *(hs_buf + 1);
4027 }
4028
4029 /* Create a fresh last entry */
4030 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004031 }
4032#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004033}
4034
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004035/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004036 * DTLS anti-replay: RFC 6347 4.1.2.6
4037 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004038 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4039 * Bit n is set iff record number in_window_top - n has been seen.
4040 *
4041 * Usually, in_window_top is the last record number seen and the lsb of
4042 * in_window is set. The only exception is the initial state (record number 0
4043 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004044 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004045#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4046static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004047{
4048 ssl->in_window_top = 0;
4049 ssl->in_window = 0;
4050}
4051
4052static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4053{
4054 return( ( (uint64_t) buf[0] << 40 ) |
4055 ( (uint64_t) buf[1] << 32 ) |
4056 ( (uint64_t) buf[2] << 24 ) |
4057 ( (uint64_t) buf[3] << 16 ) |
4058 ( (uint64_t) buf[4] << 8 ) |
4059 ( (uint64_t) buf[5] ) );
4060}
4061
4062/*
4063 * Return 0 if sequence number is acceptable, -1 otherwise
4064 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004065int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004066{
4067 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4068 uint64_t bit;
4069
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004070 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004071 return( 0 );
4072
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004073 if( rec_seqnum > ssl->in_window_top )
4074 return( 0 );
4075
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004076 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004077
4078 if( bit >= 64 )
4079 return( -1 );
4080
4081 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4082 return( -1 );
4083
4084 return( 0 );
4085}
4086
4087/*
4088 * Update replay window on new validated record
4089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004090void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004091{
4092 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4093
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004094 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004095 return;
4096
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004097 if( rec_seqnum > ssl->in_window_top )
4098 {
4099 /* Update window_top and the contents of the window */
4100 uint64_t shift = rec_seqnum - ssl->in_window_top;
4101
4102 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004103 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004104 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004105 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004106 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004107 ssl->in_window |= 1;
4108 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004109
4110 ssl->in_window_top = rec_seqnum;
4111 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004112 else
4113 {
4114 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004115 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004116
4117 if( bit < 64 ) /* Always true, but be extra sure */
4118 ssl->in_window |= (uint64_t) 1 << bit;
4119 }
4120}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004121#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004122
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004123#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004124/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004125static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4126
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004127/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004128 * Without any SSL context, check if a datagram looks like a ClientHello with
4129 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004130 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004131 *
4132 * - if cookie is valid, return 0
4133 * - if ClientHello looks superficially valid but cookie is not,
4134 * fill obuf and set olen, then
4135 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4136 * - otherwise return a specific error code
4137 */
4138static int ssl_check_dtls_clihlo_cookie(
4139 mbedtls_ssl_cookie_write_t *f_cookie_write,
4140 mbedtls_ssl_cookie_check_t *f_cookie_check,
4141 void *p_cookie,
4142 const unsigned char *cli_id, size_t cli_id_len,
4143 const unsigned char *in, size_t in_len,
4144 unsigned char *obuf, size_t buf_len, size_t *olen )
4145{
4146 size_t sid_len, cookie_len;
4147 unsigned char *p;
4148
4149 if( f_cookie_write == NULL || f_cookie_check == NULL )
4150 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4151
4152 /*
4153 * Structure of ClientHello with record and handshake headers,
4154 * and expected values. We don't need to check a lot, more checks will be
4155 * done when actually parsing the ClientHello - skipping those checks
4156 * avoids code duplication and does not make cookie forging any easier.
4157 *
4158 * 0-0 ContentType type; copied, must be handshake
4159 * 1-2 ProtocolVersion version; copied
4160 * 3-4 uint16 epoch; copied, must be 0
4161 * 5-10 uint48 sequence_number; copied
4162 * 11-12 uint16 length; (ignored)
4163 *
4164 * 13-13 HandshakeType msg_type; (ignored)
4165 * 14-16 uint24 length; (ignored)
4166 * 17-18 uint16 message_seq; copied
4167 * 19-21 uint24 fragment_offset; copied, must be 0
4168 * 22-24 uint24 fragment_length; (ignored)
4169 *
4170 * 25-26 ProtocolVersion client_version; (ignored)
4171 * 27-58 Random random; (ignored)
4172 * 59-xx SessionID session_id; 1 byte len + sid_len content
4173 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4174 * ...
4175 *
4176 * Minimum length is 61 bytes.
4177 */
4178 if( in_len < 61 ||
4179 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4180 in[3] != 0 || in[4] != 0 ||
4181 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4182 {
4183 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4184 }
4185
4186 sid_len = in[59];
4187 if( sid_len > in_len - 61 )
4188 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4189
4190 cookie_len = in[60 + sid_len];
4191 if( cookie_len > in_len - 60 )
4192 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4193
4194 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4195 cli_id, cli_id_len ) == 0 )
4196 {
4197 /* Valid cookie */
4198 return( 0 );
4199 }
4200
4201 /*
4202 * If we get here, we've got an invalid cookie, let's prepare HVR.
4203 *
4204 * 0-0 ContentType type; copied
4205 * 1-2 ProtocolVersion version; copied
4206 * 3-4 uint16 epoch; copied
4207 * 5-10 uint48 sequence_number; copied
4208 * 11-12 uint16 length; olen - 13
4209 *
4210 * 13-13 HandshakeType msg_type; hello_verify_request
4211 * 14-16 uint24 length; olen - 25
4212 * 17-18 uint16 message_seq; copied
4213 * 19-21 uint24 fragment_offset; copied
4214 * 22-24 uint24 fragment_length; olen - 25
4215 *
4216 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4217 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4218 *
4219 * Minimum length is 28.
4220 */
4221 if( buf_len < 28 )
4222 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4223
4224 /* Copy most fields and adapt others */
4225 memcpy( obuf, in, 25 );
4226 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4227 obuf[25] = 0xfe;
4228 obuf[26] = 0xff;
4229
4230 /* Generate and write actual cookie */
4231 p = obuf + 28;
4232 if( f_cookie_write( p_cookie,
4233 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4234 {
4235 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4236 }
4237
4238 *olen = p - obuf;
4239
4240 /* Go back and fill length fields */
4241 obuf[27] = (unsigned char)( *olen - 28 );
4242
4243 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4244 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4245 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4246
4247 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4248 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4249
4250 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4251}
4252
4253/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004254 * Handle possible client reconnect with the same UDP quadruplet
4255 * (RFC 6347 Section 4.2.8).
4256 *
4257 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4258 * that looks like a ClientHello.
4259 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004260 * - if the input looks like a ClientHello without cookies,
4261 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004262 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004263 * - if the input looks like a ClientHello with a valid cookie,
4264 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004265 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004266 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004267 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004268 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004269 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4270 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004271 */
4272static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4273{
4274 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004275 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004276
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004277 ret = ssl_check_dtls_clihlo_cookie(
4278 ssl->conf->f_cookie_write,
4279 ssl->conf->f_cookie_check,
4280 ssl->conf->p_cookie,
4281 ssl->cli_id, ssl->cli_id_len,
4282 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004283 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004284
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004285 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4286
4287 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004288 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004289 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004290 * If the error is permanent we'll catch it later,
4291 * if it's not, then hopefully it'll work next time. */
4292 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4293
4294 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004295 }
4296
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004297 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004298 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004299 /* Got a valid cookie, partially reset context */
4300 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4301 {
4302 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4303 return( ret );
4304 }
4305
4306 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004307 }
4308
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004309 return( ret );
4310}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004311#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004312
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004313/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004314 * ContentType type;
4315 * ProtocolVersion version;
4316 * uint16 epoch; // DTLS only
4317 * uint48 sequence_number; // DTLS only
4318 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004319 *
4320 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004321 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004322 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4323 *
4324 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004325 * 1. proceed with the record if this function returns 0
4326 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4327 * 3. return CLIENT_RECONNECT if this function return that value
4328 * 4. drop the whole datagram if this function returns anything else.
4329 * Point 2 is needed when the peer is resending, and we have already received
4330 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004332static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004333{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004334 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004336 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 +02004337
Paul Bakker5121ce52009-01-03 21:22:43 +00004338 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004339 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004340 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004343 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004344 ssl->in_msgtype,
4345 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004346
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004347 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004348 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4349 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4350 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4351 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004354
4355#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004356 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4357 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004358 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4359#endif /* MBEDTLS_SSL_PROTO_DTLS */
4360 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4361 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004363 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004364 }
4365
4366 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004367 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4370 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004371 }
4372
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004373 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4376 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004377 }
4378
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004379 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004380 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004381 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4384 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004385 }
4386
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004387 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004388 * DTLS-related tests.
4389 * Check epoch before checking length constraint because
4390 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4391 * message gets duplicated before the corresponding Finished message,
4392 * the second ChangeCipherSpec should be discarded because it belongs
4393 * to an old epoch, but not because its length is shorter than
4394 * the minimum record length for packets using the new record transform.
4395 * Note that these two kinds of failures are handled differently,
4396 * as an unexpected record is silently skipped but an invalid
4397 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004398 */
4399#if defined(MBEDTLS_SSL_PROTO_DTLS)
4400 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4401 {
4402 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4403
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004404 /* Check epoch (and sequence number) with DTLS */
4405 if( rec_epoch != ssl->in_epoch )
4406 {
4407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4408 "expected %d, received %d",
4409 ssl->in_epoch, rec_epoch ) );
4410
4411#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4412 /*
4413 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4414 * access the first byte of record content (handshake type), as we
4415 * have an active transform (possibly iv_len != 0), so use the
4416 * fact that the record header len is 13 instead.
4417 */
4418 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4419 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4420 rec_epoch == 0 &&
4421 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4422 ssl->in_left > 13 &&
4423 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4424 {
4425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4426 "from the same port" ) );
4427 return( ssl_handle_possible_reconnect( ssl ) );
4428 }
4429 else
4430#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004431 {
4432 /* Consider buffering the record. */
4433 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4434 {
4435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4436 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4437 }
4438
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004439 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004440 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004441 }
4442
4443#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4444 /* Replay detection only works for the current epoch */
4445 if( rec_epoch == ssl->in_epoch &&
4446 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4447 {
4448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4449 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4450 }
4451#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004452
Hanno Becker52c6dc62017-05-26 16:07:36 +01004453 /* Drop unexpected ApplicationData records,
4454 * except at the beginning of renegotiations */
4455 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4456 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4457#if defined(MBEDTLS_SSL_RENEGOTIATION)
4458 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4459 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4460#endif
4461 )
4462 {
4463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4464 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4465 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004466 }
4467#endif /* MBEDTLS_SSL_PROTO_DTLS */
4468
Hanno Becker52c6dc62017-05-26 16:07:36 +01004469
4470 /* Check length against bounds of the current transform and version */
4471 if( ssl->transform_in == NULL )
4472 {
4473 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004474 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004475 {
4476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4477 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4478 }
4479 }
4480 else
4481 {
4482 if( ssl->in_msglen < ssl->transform_in->minlen )
4483 {
4484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4485 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4486 }
4487
4488#if defined(MBEDTLS_SSL_PROTO_SSL3)
4489 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004490 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004491 {
4492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4493 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4494 }
4495#endif
4496#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4497 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4498 /*
4499 * TLS encrypted messages can have up to 256 bytes of padding
4500 */
4501 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4502 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004503 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004504 {
4505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4506 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4507 }
4508#endif
4509 }
4510
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004511 return( 0 );
4512}
Paul Bakker5121ce52009-01-03 21:22:43 +00004513
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004514/*
4515 * If applicable, decrypt (and decompress) record content
4516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004517static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004518{
4519 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004521 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4522 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004524#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4525 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004529 ret = mbedtls_ssl_hw_record_read( ssl );
4530 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004532 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4533 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004534 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004535
4536 if( ret == 0 )
4537 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004538 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004540 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004541 {
4542 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004544 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004545 return( ret );
4546 }
4547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004548 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004549 ssl->in_msg, ssl->in_msglen );
4550
Angus Grattond8213d02016-05-25 20:56:48 +10004551 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4554 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004555 }
4556 }
4557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004558#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004559 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004560 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004561 {
4562 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004565 return( ret );
4566 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004567 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004571 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004574 }
4575#endif
4576
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004577 return( 0 );
4578}
4579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004580static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004581
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004582/*
4583 * Read a record.
4584 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004585 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4586 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4587 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004588 */
Hanno Becker1097b342018-08-15 14:09:41 +01004589
4590/* Helper functions for mbedtls_ssl_read_record(). */
4591static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004592static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4593static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004594
Hanno Becker327c93b2018-08-15 13:56:18 +01004595int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004596 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004597{
4598 int ret;
4599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004601
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004602 if( ssl->keep_current_message == 0 )
4603 {
4604 do {
Simon Butcher99000142016-10-13 17:21:01 +01004605
Hanno Becker26994592018-08-15 14:14:59 +01004606 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004607 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004608 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004609
Hanno Beckere74d5562018-08-15 14:26:08 +01004610 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004611 {
Hanno Becker40f50842018-08-15 14:48:01 +01004612#if defined(MBEDTLS_SSL_PROTO_DTLS)
4613 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004614
Hanno Becker40f50842018-08-15 14:48:01 +01004615 /* We only check for buffered messages if the
4616 * current datagram is fully consumed. */
4617 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004618 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004619 {
Hanno Becker40f50842018-08-15 14:48:01 +01004620 if( ssl_load_buffered_message( ssl ) == 0 )
4621 have_buffered = 1;
4622 }
4623
4624 if( have_buffered == 0 )
4625#endif /* MBEDTLS_SSL_PROTO_DTLS */
4626 {
4627 ret = ssl_get_next_record( ssl );
4628 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4629 continue;
4630
4631 if( ret != 0 )
4632 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004633 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004634 return( ret );
4635 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004636 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004637 }
4638
4639 ret = mbedtls_ssl_handle_message_type( ssl );
4640
Hanno Becker40f50842018-08-15 14:48:01 +01004641#if defined(MBEDTLS_SSL_PROTO_DTLS)
4642 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4643 {
4644 /* Buffer future message */
4645 ret = ssl_buffer_message( ssl );
4646 if( ret != 0 )
4647 return( ret );
4648
4649 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4650 }
4651#endif /* MBEDTLS_SSL_PROTO_DTLS */
4652
Hanno Becker90333da2017-10-10 11:27:13 +01004653 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4654 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004655
4656 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004657 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004658 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004659 return( ret );
4660 }
4661
Hanno Becker327c93b2018-08-15 13:56:18 +01004662 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004663 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004664 {
4665 mbedtls_ssl_update_handshake_status( ssl );
4666 }
Simon Butcher99000142016-10-13 17:21:01 +01004667 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004668 else
Simon Butcher99000142016-10-13 17:21:01 +01004669 {
Hanno Becker02f59072018-08-15 14:00:24 +01004670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004671 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004672 }
4673
4674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4675
4676 return( 0 );
4677}
4678
Hanno Becker40f50842018-08-15 14:48:01 +01004679#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004680static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004681{
Hanno Becker40f50842018-08-15 14:48:01 +01004682 if( ssl->in_left > ssl->next_record_offset )
4683 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004684
Hanno Becker40f50842018-08-15 14:48:01 +01004685 return( 0 );
4686}
4687
4688static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4689{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004690 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004691 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004692 int ret = 0;
4693
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004694 if( hs == NULL )
4695 return( -1 );
4696
Hanno Beckere00ae372018-08-20 09:39:42 +01004697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4698
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004699 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4700 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4701 {
4702 /* Check if we have seen a ChangeCipherSpec before.
4703 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004704 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004705 {
4706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4707 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004708 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004709 }
4710
Hanno Becker39b8bc92018-08-28 17:17:13 +01004711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004712 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4713 ssl->in_msglen = 1;
4714 ssl->in_msg[0] = 1;
4715
4716 /* As long as they are equal, the exact value doesn't matter. */
4717 ssl->in_left = 0;
4718 ssl->next_record_offset = 0;
4719
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004720 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004721 goto exit;
4722 }
Hanno Becker37f95322018-08-16 13:55:32 +01004723
Hanno Beckerb8f50142018-08-28 10:01:34 +01004724#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004725 /* Debug only */
4726 {
4727 unsigned offset;
4728 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4729 {
4730 hs_buf = &hs->buffering.hs[offset];
4731 if( hs_buf->is_valid == 1 )
4732 {
4733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4734 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004735 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004736 }
4737 }
4738 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004739#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004740
4741 /* Check if we have buffered and/or fully reassembled the
4742 * next handshake message. */
4743 hs_buf = &hs->buffering.hs[0];
4744 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4745 {
4746 /* Synthesize a record containing the buffered HS message. */
4747 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4748 ( hs_buf->data[2] << 8 ) |
4749 hs_buf->data[3];
4750
4751 /* Double-check that we haven't accidentally buffered
4752 * a message that doesn't fit into the input buffer. */
4753 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4754 {
4755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4756 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4757 }
4758
4759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4760 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4761 hs_buf->data, msg_len + 12 );
4762
4763 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4764 ssl->in_hslen = msg_len + 12;
4765 ssl->in_msglen = msg_len + 12;
4766 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4767
4768 ret = 0;
4769 goto exit;
4770 }
4771 else
4772 {
4773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4774 hs->in_msg_seq ) );
4775 }
4776
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004777 ret = -1;
4778
4779exit:
4780
4781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4782 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004783}
4784
Hanno Beckera02b0b42018-08-21 17:20:27 +01004785static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4786 size_t desired )
4787{
4788 int offset;
4789 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4791 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004792
Hanno Becker01315ea2018-08-21 17:22:17 +01004793 /* Get rid of future records epoch first, if such exist. */
4794 ssl_free_buffered_record( ssl );
4795
4796 /* Check if we have enough space available now. */
4797 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4798 hs->buffering.total_bytes_buffered ) )
4799 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004801 return( 0 );
4802 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004803
Hanno Becker4f432ad2018-08-28 10:02:32 +01004804 /* We don't have enough space to buffer the next expected handshake
4805 * message. Remove buffers used for future messages to gain space,
4806 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004807 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4808 offset >= 0; offset-- )
4809 {
4810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4811 offset ) );
4812
Hanno Beckerb309b922018-08-23 13:18:05 +01004813 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004814
4815 /* Check if we have enough space available now. */
4816 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4817 hs->buffering.total_bytes_buffered ) )
4818 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004820 return( 0 );
4821 }
4822 }
4823
4824 return( -1 );
4825}
4826
Hanno Becker40f50842018-08-15 14:48:01 +01004827static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4828{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004829 int ret = 0;
4830 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4831
4832 if( hs == NULL )
4833 return( 0 );
4834
4835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4836
4837 switch( ssl->in_msgtype )
4838 {
4839 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004841
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004842 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004843 break;
4844
4845 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004846 {
4847 unsigned recv_msg_seq_offset;
4848 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4849 mbedtls_ssl_hs_buffer *hs_buf;
4850 size_t msg_len = ssl->in_hslen - 12;
4851
4852 /* We should never receive an old handshake
4853 * message - double-check nonetheless. */
4854 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4855 {
4856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4857 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4858 }
4859
4860 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4861 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4862 {
4863 /* Silently ignore -- message too far in the future */
4864 MBEDTLS_SSL_DEBUG_MSG( 2,
4865 ( "Ignore future HS message with sequence number %u, "
4866 "buffering window %u - %u",
4867 recv_msg_seq, ssl->handshake->in_msg_seq,
4868 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4869
4870 goto exit;
4871 }
4872
4873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4874 recv_msg_seq, recv_msg_seq_offset ) );
4875
4876 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4877
4878 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004879 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004880 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004881 size_t reassembly_buf_sz;
4882
Hanno Becker37f95322018-08-16 13:55:32 +01004883 hs_buf->is_fragmented =
4884 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4885
4886 /* We copy the message back into the input buffer
4887 * after reassembly, so check that it's not too large.
4888 * This is an implementation-specific limitation
4889 * and not one from the standard, hence it is not
4890 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004891 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004892 {
4893 /* Ignore message */
4894 goto exit;
4895 }
4896
Hanno Beckere0b150f2018-08-21 15:51:03 +01004897 /* Check if we have enough space to buffer the message. */
4898 if( hs->buffering.total_bytes_buffered >
4899 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4900 {
4901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4902 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4903 }
4904
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004905 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4906 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004907
4908 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4909 hs->buffering.total_bytes_buffered ) )
4910 {
4911 if( recv_msg_seq_offset > 0 )
4912 {
4913 /* If we can't buffer a future message because
4914 * of space limitations -- ignore. */
4915 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",
4916 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4917 (unsigned) hs->buffering.total_bytes_buffered ) );
4918 goto exit;
4919 }
Hanno Beckere1801392018-08-21 16:51:05 +01004920 else
4921 {
4922 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",
4923 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4924 (unsigned) hs->buffering.total_bytes_buffered ) );
4925 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004926
Hanno Beckera02b0b42018-08-21 17:20:27 +01004927 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004928 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004929 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",
4930 (unsigned) msg_len,
4931 (unsigned) reassembly_buf_sz,
4932 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004933 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004934 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4935 goto exit;
4936 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004937 }
4938
4939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4940 msg_len ) );
4941
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004942 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4943 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004944 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004945 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004946 goto exit;
4947 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004948 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004949
4950 /* Prepare final header: copy msg_type, length and message_seq,
4951 * then add standardised fragment_offset and fragment_length */
4952 memcpy( hs_buf->data, ssl->in_msg, 6 );
4953 memset( hs_buf->data + 6, 0, 3 );
4954 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4955
4956 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004957
4958 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004959 }
4960 else
4961 {
4962 /* Make sure msg_type and length are consistent */
4963 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4964 {
4965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4966 /* Ignore */
4967 goto exit;
4968 }
4969 }
4970
Hanno Becker4422bbb2018-08-20 09:40:19 +01004971 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004972 {
4973 size_t frag_len, frag_off;
4974 unsigned char * const msg = hs_buf->data + 12;
4975
4976 /*
4977 * Check and copy current fragment
4978 */
4979
4980 /* Validation of header fields already done in
4981 * mbedtls_ssl_prepare_handshake_record(). */
4982 frag_off = ssl_get_hs_frag_off( ssl );
4983 frag_len = ssl_get_hs_frag_len( ssl );
4984
4985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4986 frag_off, frag_len ) );
4987 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4988
4989 if( hs_buf->is_fragmented )
4990 {
4991 unsigned char * const bitmask = msg + msg_len;
4992 ssl_bitmask_set( bitmask, frag_off, frag_len );
4993 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4994 msg_len ) == 0 );
4995 }
4996 else
4997 {
4998 hs_buf->is_complete = 1;
4999 }
5000
5001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5002 hs_buf->is_complete ? "" : "not yet " ) );
5003 }
5004
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005005 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005006 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005007
5008 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005009 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005010 break;
5011 }
5012
5013exit:
5014
5015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5016 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005017}
5018#endif /* MBEDTLS_SSL_PROTO_DTLS */
5019
Hanno Becker1097b342018-08-15 14:09:41 +01005020static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005021{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005022 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005023 * Consume last content-layer message and potentially
5024 * update in_msglen which keeps track of the contents'
5025 * consumption state.
5026 *
5027 * (1) Handshake messages:
5028 * Remove last handshake message, move content
5029 * and adapt in_msglen.
5030 *
5031 * (2) Alert messages:
5032 * Consume whole record content, in_msglen = 0.
5033 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005034 * (3) Change cipher spec:
5035 * Consume whole record content, in_msglen = 0.
5036 *
5037 * (4) Application data:
5038 * Don't do anything - the record layer provides
5039 * the application data as a stream transport
5040 * and consumes through mbedtls_ssl_read only.
5041 *
5042 */
5043
5044 /* Case (1): Handshake messages */
5045 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005046 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005047 /* Hard assertion to be sure that no application data
5048 * is in flight, as corrupting ssl->in_msglen during
5049 * ssl->in_offt != NULL is fatal. */
5050 if( ssl->in_offt != NULL )
5051 {
5052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5054 }
5055
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005056 /*
5057 * Get next Handshake message in the current record
5058 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005059
Hanno Becker4a810fb2017-05-24 16:27:30 +01005060 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005061 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005062 * current handshake content: If DTLS handshake
5063 * fragmentation is used, that's the fragment
5064 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005065 * size here is faulty and should be changed at
5066 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005067 * (2) While it doesn't seem to cause problems, one
5068 * has to be very careful not to assume that in_hslen
5069 * is always <= in_msglen in a sensible communication.
5070 * Again, it's wrong for DTLS handshake fragmentation.
5071 * The following check is therefore mandatory, and
5072 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005073 * Additionally, ssl->in_hslen might be arbitrarily out of
5074 * bounds after handling a DTLS message with an unexpected
5075 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005076 */
5077 if( ssl->in_hslen < ssl->in_msglen )
5078 {
5079 ssl->in_msglen -= ssl->in_hslen;
5080 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5081 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005082
Hanno Becker4a810fb2017-05-24 16:27:30 +01005083 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5084 ssl->in_msg, ssl->in_msglen );
5085 }
5086 else
5087 {
5088 ssl->in_msglen = 0;
5089 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005090
Hanno Becker4a810fb2017-05-24 16:27:30 +01005091 ssl->in_hslen = 0;
5092 }
5093 /* Case (4): Application data */
5094 else if( ssl->in_offt != NULL )
5095 {
5096 return( 0 );
5097 }
5098 /* Everything else (CCS & Alerts) */
5099 else
5100 {
5101 ssl->in_msglen = 0;
5102 }
5103
Hanno Becker1097b342018-08-15 14:09:41 +01005104 return( 0 );
5105}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005106
Hanno Beckere74d5562018-08-15 14:26:08 +01005107static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5108{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005109 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005110 return( 1 );
5111
5112 return( 0 );
5113}
5114
Hanno Becker5f066e72018-08-16 14:56:31 +01005115#if defined(MBEDTLS_SSL_PROTO_DTLS)
5116
5117static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5118{
5119 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5120 if( hs == NULL )
5121 return;
5122
Hanno Becker01315ea2018-08-21 17:22:17 +01005123 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005124 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005125 hs->buffering.total_bytes_buffered -=
5126 hs->buffering.future_record.len;
5127
5128 mbedtls_free( hs->buffering.future_record.data );
5129 hs->buffering.future_record.data = NULL;
5130 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005131}
5132
5133static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5134{
5135 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5136 unsigned char * rec;
5137 size_t rec_len;
5138 unsigned rec_epoch;
5139
5140 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5141 return( 0 );
5142
5143 if( hs == NULL )
5144 return( 0 );
5145
Hanno Becker5f066e72018-08-16 14:56:31 +01005146 rec = hs->buffering.future_record.data;
5147 rec_len = hs->buffering.future_record.len;
5148 rec_epoch = hs->buffering.future_record.epoch;
5149
5150 if( rec == NULL )
5151 return( 0 );
5152
Hanno Becker4cb782d2018-08-20 11:19:05 +01005153 /* Only consider loading future records if the
5154 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005155 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005156 return( 0 );
5157
Hanno Becker5f066e72018-08-16 14:56:31 +01005158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5159
5160 if( rec_epoch != ssl->in_epoch )
5161 {
5162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5163 goto exit;
5164 }
5165
5166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5167
5168 /* Double-check that the record is not too large */
5169 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5170 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5171 {
5172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5173 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5174 }
5175
5176 memcpy( ssl->in_hdr, rec, rec_len );
5177 ssl->in_left = rec_len;
5178 ssl->next_record_offset = 0;
5179
5180 ssl_free_buffered_record( ssl );
5181
5182exit:
5183 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5184 return( 0 );
5185}
5186
5187static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5188{
5189 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5190 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005191 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005192
5193 /* Don't buffer future records outside handshakes. */
5194 if( hs == NULL )
5195 return( 0 );
5196
5197 /* Only buffer handshake records (we are only interested
5198 * in Finished messages). */
5199 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5200 return( 0 );
5201
5202 /* Don't buffer more than one future epoch record. */
5203 if( hs->buffering.future_record.data != NULL )
5204 return( 0 );
5205
Hanno Becker01315ea2018-08-21 17:22:17 +01005206 /* Don't buffer record if there's not enough buffering space remaining. */
5207 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5208 hs->buffering.total_bytes_buffered ) )
5209 {
5210 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",
5211 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5212 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005213 return( 0 );
5214 }
5215
Hanno Becker5f066e72018-08-16 14:56:31 +01005216 /* Buffer record */
5217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5218 ssl->in_epoch + 1 ) );
5219 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5220 rec_hdr_len + ssl->in_msglen );
5221
5222 /* ssl_parse_record_header() only considers records
5223 * of the next epoch as candidates for buffering. */
5224 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005225 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005226
5227 hs->buffering.future_record.data =
5228 mbedtls_calloc( 1, hs->buffering.future_record.len );
5229 if( hs->buffering.future_record.data == NULL )
5230 {
5231 /* If we run out of RAM trying to buffer a
5232 * record from the next epoch, just ignore. */
5233 return( 0 );
5234 }
5235
Hanno Becker01315ea2018-08-21 17:22:17 +01005236 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005237
Hanno Becker01315ea2018-08-21 17:22:17 +01005238 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005239 return( 0 );
5240}
5241
5242#endif /* MBEDTLS_SSL_PROTO_DTLS */
5243
Hanno Beckere74d5562018-08-15 14:26:08 +01005244static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005245{
5246 int ret;
5247
Hanno Becker5f066e72018-08-16 14:56:31 +01005248#if defined(MBEDTLS_SSL_PROTO_DTLS)
5249 /* We might have buffered a future record; if so,
5250 * and if the epoch matches now, load it.
5251 * On success, this call will set ssl->in_left to
5252 * the length of the buffered record, so that
5253 * the calls to ssl_fetch_input() below will
5254 * essentially be no-ops. */
5255 ret = ssl_load_buffered_record( ssl );
5256 if( ret != 0 )
5257 return( ret );
5258#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005260 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005262 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005263 return( ret );
5264 }
5265
5266 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005268#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005269 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5270 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005271 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005272 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5273 {
5274 ret = ssl_buffer_future_record( ssl );
5275 if( ret != 0 )
5276 return( ret );
5277
5278 /* Fall through to handling of unexpected records */
5279 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5280 }
5281
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005282 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5283 {
5284 /* Skip unexpected record (but not whole datagram) */
5285 ssl->next_record_offset = ssl->in_msglen
5286 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005287
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5289 "(header)" ) );
5290 }
5291 else
5292 {
5293 /* Skip invalid record and the rest of the datagram */
5294 ssl->next_record_offset = 0;
5295 ssl->in_left = 0;
5296
5297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5298 "(header)" ) );
5299 }
5300
5301 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005302 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005303 }
5304#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005305 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005306 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005307
5308 /*
5309 * Read and optionally decrypt the message contents
5310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005311 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5312 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005314 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005315 return( ret );
5316 }
5317
5318 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005319#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005320 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005322 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005323 if( ssl->next_record_offset < ssl->in_left )
5324 {
5325 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5326 }
5327 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005328 else
5329#endif
5330 ssl->in_left = 0;
5331
5332 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005334#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005335 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005336 {
5337 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005338 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5339 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005340 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005341 /* Except when waiting for Finished as a bad mac here
5342 * probably means something went wrong in the handshake
5343 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5344 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5345 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5346 {
5347#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5348 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5349 {
5350 mbedtls_ssl_send_alert_message( ssl,
5351 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5352 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5353 }
5354#endif
5355 return( ret );
5356 }
5357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005358#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005359 if( ssl->conf->badmac_limit != 0 &&
5360 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5363 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005364 }
5365#endif
5366
Hanno Becker4a810fb2017-05-24 16:27:30 +01005367 /* As above, invalid records cause
5368 * dismissal of the whole datagram. */
5369
5370 ssl->next_record_offset = 0;
5371 ssl->in_left = 0;
5372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005374 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005375 }
5376
5377 return( ret );
5378 }
5379 else
5380#endif
5381 {
5382 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005383#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5384 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 mbedtls_ssl_send_alert_message( ssl,
5387 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5388 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005389 }
5390#endif
5391 return( ret );
5392 }
5393 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005394
Simon Butcher99000142016-10-13 17:21:01 +01005395 return( 0 );
5396}
5397
5398int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5399{
5400 int ret;
5401
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005402 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005403 * Handle particular types of records
5404 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005405 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005406 {
Simon Butcher99000142016-10-13 17:21:01 +01005407 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5408 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005409 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005410 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005411 }
5412
Hanno Beckere678eaa2018-08-21 14:57:46 +01005413 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005414 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005415 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005416 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5418 ssl->in_msglen ) );
5419 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005420 }
5421
Hanno Beckere678eaa2018-08-21 14:57:46 +01005422 if( ssl->in_msg[0] != 1 )
5423 {
5424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5425 ssl->in_msg[0] ) );
5426 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5427 }
5428
5429#if defined(MBEDTLS_SSL_PROTO_DTLS)
5430 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5431 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5432 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5433 {
5434 if( ssl->handshake == NULL )
5435 {
5436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5437 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5438 }
5439
5440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5441 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5442 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005443#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005444 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005446 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005447 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005448 if( ssl->in_msglen != 2 )
5449 {
5450 /* Note: Standard allows for more than one 2 byte alert
5451 to be packed in a single message, but Mbed TLS doesn't
5452 currently support this. */
5453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5454 ssl->in_msglen ) );
5455 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5456 }
5457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005459 ssl->in_msg[0], ssl->in_msg[1] ) );
5460
5461 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005462 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005467 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005469 }
5470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5472 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5475 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005476 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005477
5478#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5479 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5480 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5481 {
Hanno Becker90333da2017-10-10 11:27:13 +01005482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005483 /* Will be handled when trying to parse ServerHello */
5484 return( 0 );
5485 }
5486#endif
5487
5488#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5489 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5490 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5491 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5492 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5493 {
5494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5495 /* Will be handled in mbedtls_ssl_parse_certificate() */
5496 return( 0 );
5497 }
5498#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5499
5500 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005501 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005502 }
5503
Hanno Beckerc76c6192017-06-06 10:03:17 +01005504#if defined(MBEDTLS_SSL_PROTO_DTLS)
5505 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5506 ssl->handshake != NULL &&
5507 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5508 {
5509 ssl_handshake_wrapup_free_hs_transform( ssl );
5510 }
5511#endif
5512
Paul Bakker5121ce52009-01-03 21:22:43 +00005513 return( 0 );
5514}
5515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005516int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005517{
5518 int ret;
5519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005520 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5521 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5522 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005523 {
5524 return( ret );
5525 }
5526
5527 return( 0 );
5528}
5529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005530int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005531 unsigned char level,
5532 unsigned char message )
5533{
5534 int ret;
5535
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005536 if( ssl == NULL || ssl->conf == NULL )
5537 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005540 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005542 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005543 ssl->out_msglen = 2;
5544 ssl->out_msg[0] = level;
5545 ssl->out_msg[1] = message;
5546
Hanno Becker67bc7c32018-08-06 11:33:50 +01005547 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005549 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005550 return( ret );
5551 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005553
5554 return( 0 );
5555}
5556
Paul Bakker5121ce52009-01-03 21:22:43 +00005557/*
5558 * Handshake functions
5559 */
Hanno Becker21489932019-02-05 13:20:55 +00005560#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005561/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005562int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005563{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005564 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005567
Hanno Becker7177a882019-02-05 13:36:46 +00005568 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005571 ssl->state++;
5572 return( 0 );
5573 }
5574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5576 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005577}
5578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005580{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005581 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005584
Hanno Becker7177a882019-02-05 13:36:46 +00005585 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005588 ssl->state++;
5589 return( 0 );
5590 }
5591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5593 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005594}
Gilles Peskinef9828522017-05-03 12:28:43 +02005595
Hanno Becker21489932019-02-05 13:20:55 +00005596#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005597/* Some certificate support -> implement write and parse */
5598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005600{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005601 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005602 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005603 const mbedtls_x509_crt *crt;
5604 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005607
Hanno Becker7177a882019-02-05 13:36:46 +00005608 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005611 ssl->state++;
5612 return( 0 );
5613 }
5614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005616 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005617 {
5618 if( ssl->client_auth == 0 )
5619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005621 ssl->state++;
5622 return( 0 );
5623 }
5624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005626 /*
5627 * If using SSLv3 and got no cert, send an Alert message
5628 * (otherwise an empty Certificate message will be sent).
5629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005630 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5631 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005632 {
5633 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005634 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5635 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5636 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005639 goto write_msg;
5640 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005641#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005642 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005643#endif /* MBEDTLS_SSL_CLI_C */
5644#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005645 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005647 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5650 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005651 }
5652 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005653#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005656
5657 /*
5658 * 0 . 0 handshake type
5659 * 1 . 3 handshake length
5660 * 4 . 6 length of all certs
5661 * 7 . 9 length of cert. 1
5662 * 10 . n-1 peer certificate
5663 * n . n+2 length of cert. 2
5664 * n+3 . ... upper level cert, etc.
5665 */
5666 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005667 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005668
Paul Bakker29087132010-03-21 21:03:34 +00005669 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005670 {
5671 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005672 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005675 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005677 }
5678
5679 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5680 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5681 ssl->out_msg[i + 2] = (unsigned char)( n );
5682
5683 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5684 i += n; crt = crt->next;
5685 }
5686
5687 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5688 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5689 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5690
5691 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005692 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5693 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005694
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005695#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005696write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005697#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005698
5699 ssl->state++;
5700
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005701 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005702 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005703 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005704 return( ret );
5705 }
5706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005708
Paul Bakkered27a042013-04-18 22:46:23 +02005709 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005710}
5711
Hanno Becker84879e32019-01-31 07:44:03 +00005712#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005713static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5714 unsigned char *crt_buf,
5715 size_t crt_buf_len )
5716{
5717 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5718
5719 if( peer_crt == NULL )
5720 return( -1 );
5721
5722 if( peer_crt->raw.len != crt_buf_len )
5723 return( -1 );
5724
Hanno Becker46f34d02019-02-08 14:00:04 +00005725 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005726}
Hanno Becker84879e32019-01-31 07:44:03 +00005727#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005728
Hanno Becker1294a0b2019-02-05 12:38:15 +00005729static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5730{
5731 if( session->peer_cert != NULL )
5732 {
5733 mbedtls_x509_crt_free( session->peer_cert );
5734 mbedtls_free( session->peer_cert );
5735 session->peer_cert = NULL;
5736 }
5737}
5738
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005739/*
5740 * Once the certificate message is read, parse it into a cert chain and
5741 * perform basic checks, but leave actual verification to the caller
5742 */
5743static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005744{
Hanno Beckera028c5b2019-02-05 12:38:45 +00005745 int ret, crt_cnt=0;
Paul Bakker23986e52011-04-24 08:57:21 +00005746 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005747 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005752 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5753 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005754 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005755 }
5756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005757 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5758 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005761 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5762 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005764 }
5765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005766 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005767
Paul Bakker5121ce52009-01-03 21:22:43 +00005768 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005770 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005771 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005772
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005773 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005774 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005777 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5778 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005780 }
5781
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005782 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5783 i += 3;
5784
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005785 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005786 while( i < ssl->in_hslen )
5787 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005788 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02005789 if ( i + 3 > ssl->in_hslen ) {
5790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005791 mbedtls_ssl_send_alert_message( ssl,
5792 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5793 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02005794 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5795 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005796 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5797 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005798 if( ssl->in_msg[i] != 0 )
5799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005801 mbedtls_ssl_send_alert_message( ssl,
5802 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5803 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005805 }
5806
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005807 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005808 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5809 | (unsigned int) ssl->in_msg[i + 2];
5810 i += 3;
5811
5812 if( n < 128 || i + n > ssl->in_hslen )
5813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005815 mbedtls_ssl_send_alert_message( ssl,
5816 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5817 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005819 }
5820
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005821 /* Check if we're handling the first CRT in the chain. */
Hanno Beckera028c5b2019-02-05 12:38:45 +00005822 if( crt_cnt++ == 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005823 {
Hanno Becker46f34d02019-02-08 14:00:04 +00005824 /* During client-side renegotiation, check that the server's
5825 * end-CRTs hasn't changed compared to the initial handshake,
5826 * mitigating the triple handshake attack. On success, reuse
5827 * the original end-CRT instead of parsing it again. */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005828#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5829 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5830 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
5831 {
5832 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005833 if( ssl_check_peer_crt_unchanged( ssl,
5834 &ssl->in_msg[i],
5835 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005836 {
5837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005838 mbedtls_ssl_send_alert_message( ssl,
5839 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5840 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005841 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5842 }
5843
Hanno Becker60848e62019-02-05 15:06:15 +00005844 /* Now we can safely free the original chain. */
Hanno Becker1294a0b2019-02-05 12:38:15 +00005845 ssl_clear_peer_cert( ssl->session );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005846
Hanno Becker60848e62019-02-05 15:06:15 +00005847 /* Intentional fallthrough. */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005848 }
5849#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5850
5851 /* Outside of client-side renegotiation, create a fresh X.509 CRT
5852 * instance to parse the end-CRT into. */
5853
5854 ssl->session_negotiate->peer_cert =
5855 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
5856 if( ssl->session_negotiate->peer_cert == NULL )
5857 {
5858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
5859 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005860 mbedtls_ssl_send_alert_message( ssl,
5861 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5862 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005863 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
5864 }
5865
5866 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
5867
5868 /* Intentional fall through */
5869 }
5870
5871 /* Parse the next certificate in the chain. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005873 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005874 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005875 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005876 case 0: /*ok*/
5877 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5878 /* Ignore certificate with an unknown algorithm: maybe a
5879 prior certificate was already trusted. */
5880 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005881
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005882 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5883 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5884 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005885
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005886 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5887 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5888 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005889
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005890 default:
5891 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5892 crt_parse_der_failed:
5893 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
5894 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
5895 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005896 }
5897
5898 i += n;
5899 }
5900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005901 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005902 return( 0 );
5903}
5904
Hanno Becker4a55f632019-02-05 12:49:06 +00005905#if defined(MBEDTLS_SSL_SRV_C)
5906static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
5907{
5908 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5909 return( -1 );
5910
5911#if defined(MBEDTLS_SSL_PROTO_SSL3)
5912 /*
5913 * Check if the client sent an empty certificate
5914 */
5915 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
5916 {
5917 if( ssl->in_msglen == 2 &&
5918 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5919 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5920 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5921 {
5922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
5923 return( 0 );
5924 }
5925
5926 return( -1 );
5927 }
5928#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5929
5930#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5931 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5932 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5933 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5934 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5935 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
5936 {
5937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
5938 return( 0 );
5939 }
5940
5941 return( -1 );
5942#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5943 MBEDTLS_SSL_PROTO_TLS1_2 */
5944}
5945#endif /* MBEDTLS_SSL_SRV_C */
5946
Hanno Becker28f2fcd2019-02-07 10:11:07 +00005947/* Check if a certificate message is expected.
5948 * Return either
5949 * - SSL_CERTIFICATE_EXPECTED, or
5950 * - SSL_CERTIFICATE_SKIP
5951 * indicating whether a Certificate message is expected or not.
5952 */
5953#define SSL_CERTIFICATE_EXPECTED 0
5954#define SSL_CERTIFICATE_SKIP 1
5955static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
5956 int authmode )
5957{
5958 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5959 ssl->transform_negotiate->ciphersuite_info;
5960
5961 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5962 return( SSL_CERTIFICATE_SKIP );
5963
5964#if defined(MBEDTLS_SSL_SRV_C)
5965 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5966 {
5967 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5968 return( SSL_CERTIFICATE_SKIP );
5969
5970 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
5971 {
5972 /* NOTE: Is it intentional that we set verify_result
5973 * to SKIP_VERIFY on server-side only? */
5974 ssl->session_negotiate->verify_result =
5975 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5976 return( SSL_CERTIFICATE_SKIP );
5977 }
5978 }
5979#endif /* MBEDTLS_SSL_SRV_C */
5980
5981 return( SSL_CERTIFICATE_EXPECTED );
5982}
5983
Hanno Becker68636192019-02-05 14:36:34 +00005984static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
5985 int authmode,
5986 mbedtls_x509_crt *chain,
5987 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005988{
Hanno Becker6bdfab22019-02-05 13:11:17 +00005989 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00005990 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5991 ssl->transform_negotiate->ciphersuite_info;
Hanno Becker68636192019-02-05 14:36:34 +00005992 mbedtls_x509_crt *ca_chain;
5993 mbedtls_x509_crl *ca_crl;
5994
5995 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
5996 return( 0 );
5997
5998#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5999 if( ssl->handshake->sni_ca_chain != NULL )
6000 {
6001 ca_chain = ssl->handshake->sni_ca_chain;
6002 ca_crl = ssl->handshake->sni_ca_crl;
6003 }
6004 else
6005#endif
6006 {
6007 ca_chain = ssl->conf->ca_chain;
6008 ca_crl = ssl->conf->ca_crl;
6009 }
6010
6011 /*
6012 * Main check: verify certificate
6013 */
6014 ret = mbedtls_x509_crt_verify_restartable(
6015 chain,
6016 ca_chain, ca_crl,
6017 ssl->conf->cert_profile,
6018 ssl->hostname,
6019 &ssl->session_negotiate->verify_result,
6020 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6021
6022 if( ret != 0 )
6023 {
6024 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6025 }
6026
6027#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6028 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6029 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6030#endif
6031
6032 /*
6033 * Secondary checks: always done, but change 'ret' only if it was 0
6034 */
6035
6036#if defined(MBEDTLS_ECP_C)
6037 {
6038 const mbedtls_pk_context *pk = &chain->pk;
6039
6040 /* If certificate uses an EC key, make sure the curve is OK */
6041 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6042 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6043 {
6044 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6045
6046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6047 if( ret == 0 )
6048 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6049 }
6050 }
6051#endif /* MBEDTLS_ECP_C */
6052
6053 if( mbedtls_ssl_check_cert_usage( chain,
6054 ciphersuite_info,
6055 ! ssl->conf->endpoint,
6056 &ssl->session_negotiate->verify_result ) != 0 )
6057 {
6058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6059 if( ret == 0 )
6060 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6061 }
6062
6063 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6064 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6065 * with details encoded in the verification flags. All other kinds
6066 * of error codes, including those from the user provided f_vrfy
6067 * functions, are treated as fatal and lead to a failure of
6068 * ssl_parse_certificate even if verification was optional. */
6069 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6070 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6071 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6072 {
6073 ret = 0;
6074 }
6075
6076 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6077 {
6078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6079 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6080 }
6081
6082 if( ret != 0 )
6083 {
6084 uint8_t alert;
6085
6086 /* The certificate may have been rejected for several reasons.
6087 Pick one and send the corresponding alert. Which alert to send
6088 may be a subject of debate in some cases. */
6089 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6090 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6091 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6092 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6093 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6094 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6095 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6096 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6097 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6098 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6099 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6100 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6101 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6102 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6103 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6104 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6105 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6106 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6107 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6108 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6109 else
6110 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6111 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6112 alert );
6113 }
6114
6115#if defined(MBEDTLS_DEBUG_C)
6116 if( ssl->session_negotiate->verify_result != 0 )
6117 {
6118 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6119 ssl->session_negotiate->verify_result ) );
6120 }
6121 else
6122 {
6123 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6124 }
6125#endif /* MBEDTLS_DEBUG_C */
6126
6127 return( ret );
6128}
6129
6130int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6131{
6132 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006133 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006134#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6135 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6136 ? ssl->handshake->sni_authmode
6137 : ssl->conf->authmode;
6138#else
6139 const int authmode = ssl->conf->authmode;
6140#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006141 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006142
6143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6144
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006145 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6146 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006147 {
6148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006149 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006150 }
6151
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006152#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6153 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006154 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006155 {
6156 goto crt_verify;
6157 }
6158#endif
6159
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006160 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006161 {
6162 /* mbedtls_ssl_read_record may have sent an alert already. We
6163 let it decide whether to alert. */
6164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6165 return( ret );
6166 }
6167
Hanno Becker4a55f632019-02-05 12:49:06 +00006168#if defined(MBEDTLS_SSL_SRV_C)
6169 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6170 {
6171 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006172
6173 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006174 ret = 0;
6175 else
6176 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006177
Hanno Becker6bdfab22019-02-05 13:11:17 +00006178 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006179 }
6180#endif /* MBEDTLS_SSL_SRV_C */
6181
Hanno Becker7a955a02019-02-05 13:08:01 +00006182 /* In case we tried to reuse a session but it failed. */
6183 ssl_clear_peer_cert( ssl->session_negotiate );
6184
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006185 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
Hanno Beckerfcd9e712019-02-05 14:35:46 +00006186 return( ret );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006187
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006188#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6189 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006190 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006191
6192crt_verify:
6193 if( ssl->handshake->ecrs_enabled)
6194 rs_ctx = &ssl->handshake->ecrs_ctx;
6195#endif
6196
Hanno Becker68636192019-02-05 14:36:34 +00006197 ret = ssl_parse_certificate_verify( ssl, authmode,
6198 ssl->session_negotiate->peer_cert,
6199 rs_ctx );
6200 if( ret != 0 )
6201 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006204
Hanno Becker6bdfab22019-02-05 13:11:17 +00006205exit:
6206
6207 ssl->state++;
Paul Bakker5121ce52009-01-03 21:22:43 +00006208 return( ret );
6209}
Hanno Becker21489932019-02-05 13:20:55 +00006210#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006212int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006213{
6214 int ret;
6215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006219 ssl->out_msglen = 1;
6220 ssl->out_msg[0] = 1;
6221
Paul Bakker5121ce52009-01-03 21:22:43 +00006222 ssl->state++;
6223
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006224 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006225 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006226 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006227 return( ret );
6228 }
6229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006231
6232 return( 0 );
6233}
6234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006236{
6237 int ret;
6238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006240
Hanno Becker327c93b2018-08-15 13:56:18 +01006241 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006243 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006244 return( ret );
6245 }
6246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006247 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006250 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6251 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006252 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006253 }
6254
Hanno Beckere678eaa2018-08-21 14:57:46 +01006255 /* CCS records are only accepted if they have length 1 and content '1',
6256 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006257
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006258 /*
6259 * Switch to our negotiated transform and session parameters for inbound
6260 * data.
6261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006263 ssl->transform_in = ssl->transform_negotiate;
6264 ssl->session_in = ssl->session_negotiate;
6265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006266#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006267 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006269#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006270 ssl_dtls_replay_reset( ssl );
6271#endif
6272
6273 /* Increment epoch */
6274 if( ++ssl->in_epoch == 0 )
6275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006277 /* This is highly unlikely to happen for legitimate reasons, so
6278 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006280 }
6281 }
6282 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006283#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006284 memset( ssl->in_ctr, 0, 8 );
6285
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006286 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006288#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6289 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006294 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6295 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006297 }
6298 }
6299#endif
6300
Paul Bakker5121ce52009-01-03 21:22:43 +00006301 ssl->state++;
6302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006304
6305 return( 0 );
6306}
6307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006308void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6309 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006310{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006311 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006313#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6314 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6315 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006316 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006317 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006318#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6320#if defined(MBEDTLS_SHA512_C)
6321 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006322 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6323 else
6324#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006325#if defined(MBEDTLS_SHA256_C)
6326 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006327 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006328 else
6329#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006330#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006333 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006334 }
Paul Bakker380da532012-04-18 16:10:25 +00006335}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006338{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006339#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6340 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006341 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6342 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006343#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006344#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6345#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006346#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006347 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006348 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6349#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006350 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006351#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006352#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006353#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006354#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006355 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006356 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006357#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006358 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006359#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006360#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006361#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006362}
6363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006365 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006366{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006367#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6368 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006369 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6370 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006371#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6373#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006374#if defined(MBEDTLS_USE_PSA_CRYPTO)
6375 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6376#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006377 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006378#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006379#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006380#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006381#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006382 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006383#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006384 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006385#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006386#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006387#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006388}
6389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006390#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6391 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6392static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006393 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006394{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006395 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6396 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006397}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006398#endif
Paul Bakker380da532012-04-18 16:10:25 +00006399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6401#if defined(MBEDTLS_SHA256_C)
6402static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006403 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006404{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006405#if defined(MBEDTLS_USE_PSA_CRYPTO)
6406 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6407#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006408 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006409#endif
Paul Bakker380da532012-04-18 16:10:25 +00006410}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006411#endif
Paul Bakker380da532012-04-18 16:10:25 +00006412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006413#if defined(MBEDTLS_SHA512_C)
6414static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006415 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006416{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006417#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006418 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006419#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006420 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006421#endif
Paul Bakker380da532012-04-18 16:10:25 +00006422}
Paul Bakker769075d2012-11-24 11:26:46 +01006423#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006424#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006426#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006427static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006428 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006429{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006430 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006431 mbedtls_md5_context md5;
6432 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006433
Paul Bakker5121ce52009-01-03 21:22:43 +00006434 unsigned char padbuf[48];
6435 unsigned char md5sum[16];
6436 unsigned char sha1sum[20];
6437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006438 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006439 if( !session )
6440 session = ssl->session;
6441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006443
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006444 mbedtls_md5_init( &md5 );
6445 mbedtls_sha1_init( &sha1 );
6446
6447 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6448 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006449
6450 /*
6451 * SSLv3:
6452 * hash =
6453 * MD5( master + pad2 +
6454 * MD5( handshake + sender + master + pad1 ) )
6455 * + SHA1( master + pad2 +
6456 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006457 */
6458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006459#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006460 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6461 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006462#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006465 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6466 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006467#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006469 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006470 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006471
Paul Bakker1ef83d62012-04-11 12:09:53 +00006472 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006473
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006474 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6475 mbedtls_md5_update_ret( &md5, session->master, 48 );
6476 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6477 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006478
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006479 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6480 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6481 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6482 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006483
Paul Bakker1ef83d62012-04-11 12:09:53 +00006484 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006485
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006486 mbedtls_md5_starts_ret( &md5 );
6487 mbedtls_md5_update_ret( &md5, session->master, 48 );
6488 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6489 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6490 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006491
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006492 mbedtls_sha1_starts_ret( &sha1 );
6493 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6494 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6495 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6496 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006498 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006499
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006500 mbedtls_md5_free( &md5 );
6501 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006502
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006503 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6504 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6505 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006508}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006509#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006512static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006514{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006515 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006516 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006517 mbedtls_md5_context md5;
6518 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006519 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006521 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006522 if( !session )
6523 session = ssl->session;
6524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006526
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006527 mbedtls_md5_init( &md5 );
6528 mbedtls_sha1_init( &sha1 );
6529
6530 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6531 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006532
Paul Bakker1ef83d62012-04-11 12:09:53 +00006533 /*
6534 * TLSv1:
6535 * hash = PRF( master, finished_label,
6536 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6537 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006539#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006540 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6541 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006542#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006545 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6546 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006547#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006549 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006550 ? "client finished"
6551 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006552
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006553 mbedtls_md5_finish_ret( &md5, padbuf );
6554 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006555
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006556 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006557 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006559 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006560
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006561 mbedtls_md5_free( &md5 );
6562 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006563
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006564 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006567}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006568#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6571#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006572static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006574{
6575 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006576 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006577 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006578#if defined(MBEDTLS_USE_PSA_CRYPTO)
6579 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006580 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006581 psa_status_t status;
6582#else
6583 mbedtls_sha256_context sha256;
6584#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006587 if( !session )
6588 session = ssl->session;
6589
Andrzej Kurekeb342242019-01-29 09:14:33 -05006590 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6591 ? "client finished"
6592 : "server finished";
6593
6594#if defined(MBEDTLS_USE_PSA_CRYPTO)
6595 sha256_psa = psa_hash_operation_init();
6596
6597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6598
6599 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6600 if( status != PSA_SUCCESS )
6601 {
6602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6603 return;
6604 }
6605
6606 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6607 if( status != PSA_SUCCESS )
6608 {
6609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6610 return;
6611 }
6612 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6613#else
6614
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006615 mbedtls_sha256_init( &sha256 );
6616
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006618
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006619 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006620
6621 /*
6622 * TLSv1.2:
6623 * hash = PRF( master, finished_label,
6624 * Hash( handshake ) )[0.11]
6625 */
6626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006627#if !defined(MBEDTLS_SHA256_ALT)
6628 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006629 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006630#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006631
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006632 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006633 mbedtls_sha256_free( &sha256 );
6634#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006635
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006636 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006637 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006640
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006641 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006644}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006645#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006647#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006648static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006650{
6651 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006652 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006653 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006654#if defined(MBEDTLS_USE_PSA_CRYPTO)
6655 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006656 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006657 psa_status_t status;
6658#else
6659 mbedtls_sha512_context sha512;
6660#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006662 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006663 if( !session )
6664 session = ssl->session;
6665
Andrzej Kurekeb342242019-01-29 09:14:33 -05006666 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6667 ? "client finished"
6668 : "server finished";
6669
6670#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006671 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05006672
6673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6674
Andrzej Kurek972fba52019-01-30 03:29:12 -05006675 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006676 if( status != PSA_SUCCESS )
6677 {
6678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6679 return;
6680 }
6681
Andrzej Kurek972fba52019-01-30 03:29:12 -05006682 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006683 if( status != PSA_SUCCESS )
6684 {
6685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6686 return;
6687 }
6688 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6689#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006690 mbedtls_sha512_init( &sha512 );
6691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006693
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006694 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006695
6696 /*
6697 * TLSv1.2:
6698 * hash = PRF( master, finished_label,
6699 * Hash( handshake ) )[0.11]
6700 */
6701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006703 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6704 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006705#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006706
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006707 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006708 mbedtls_sha512_free( &sha512 );
6709#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006710
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006711 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006712 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006715
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006716 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006719}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006720#endif /* MBEDTLS_SHA512_C */
6721#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006723static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006724{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006726
6727 /*
6728 * Free our handshake params
6729 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006730 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006731 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006732 ssl->handshake = NULL;
6733
6734 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006735 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006736 */
6737 if( ssl->transform )
6738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739 mbedtls_ssl_transform_free( ssl->transform );
6740 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006741 }
6742 ssl->transform = ssl->transform_negotiate;
6743 ssl->transform_negotiate = NULL;
6744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006746}
6747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006749{
6750 int resume = ssl->handshake->resume;
6751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754#if defined(MBEDTLS_SSL_RENEGOTIATION)
6755 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006757 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006758 ssl->renego_records_seen = 0;
6759 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006760#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006761
6762 /*
6763 * Free the previous session and switch in the current one
6764 */
Paul Bakker0a597072012-09-25 21:55:46 +00006765 if( ssl->session )
6766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006767#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006768 /* RFC 7366 3.1: keep the EtM state */
6769 ssl->session_negotiate->encrypt_then_mac =
6770 ssl->session->encrypt_then_mac;
6771#endif
6772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006773 mbedtls_ssl_session_free( ssl->session );
6774 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006775 }
6776 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006777 ssl->session_negotiate = NULL;
6778
Paul Bakker0a597072012-09-25 21:55:46 +00006779 /*
6780 * Add cache entry
6781 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006782 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006783 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006784 resume == 0 )
6785 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006786 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006788 }
Paul Bakker0a597072012-09-25 21:55:46 +00006789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006791 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006792 ssl->handshake->flight != NULL )
6793 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006794 /* Cancel handshake timer */
6795 ssl_set_timer( ssl, 0 );
6796
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006797 /* Keep last flight around in case we need to resend it:
6798 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006800 }
6801 else
6802#endif
6803 ssl_handshake_wrapup_free_hs_transform( ssl );
6804
Paul Bakker48916f92012-09-16 19:57:18 +00006805 ssl->state++;
6806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006808}
6809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006811{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006812 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006815
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006816 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006817
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006818 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006819
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006820 /*
6821 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6822 * may define some other value. Currently (early 2016), no defined
6823 * ciphersuite does this (and this is unlikely to change as activity has
6824 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006829 ssl->verify_data_len = hash_len;
6830 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006831#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006832
Paul Bakker5121ce52009-01-03 21:22:43 +00006833 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6835 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006836
6837 /*
6838 * In case of session resuming, invert the client and server
6839 * ChangeCipherSpec messages order.
6840 */
Paul Bakker0a597072012-09-25 21:55:46 +00006841 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006844 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006845 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006846#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006847#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006848 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006849 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006850#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006851 }
6852 else
6853 ssl->state++;
6854
Paul Bakker48916f92012-09-16 19:57:18 +00006855 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006856 * Switch to our negotiated transform and session parameters for outbound
6857 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006858 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006859 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006862 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006863 {
6864 unsigned char i;
6865
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006866 /* Remember current epoch settings for resending */
6867 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006868 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006869
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006870 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006871 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006872
6873 /* Increment epoch */
6874 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006875 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006876 break;
6877
6878 /* The loop goes to its end iff the counter is wrapping */
6879 if( i == 0 )
6880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6882 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006883 }
6884 }
6885 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006886#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006887 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006888
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006889 ssl->transform_out = ssl->transform_negotiate;
6890 ssl->session_out = ssl->session_negotiate;
6891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6893 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006895 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006897 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6898 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006899 }
6900 }
6901#endif
6902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006903#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006904 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006905 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006906#endif
6907
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006908 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006909 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006910 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006911 return( ret );
6912 }
6913
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006914#if defined(MBEDTLS_SSL_PROTO_DTLS)
6915 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6916 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6917 {
6918 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6919 return( ret );
6920 }
6921#endif
6922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006924
6925 return( 0 );
6926}
6927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006929#define SSL_MAX_HASH_LEN 36
6930#else
6931#define SSL_MAX_HASH_LEN 12
6932#endif
6933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006934int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006935{
Paul Bakker23986e52011-04-24 08:57:21 +00006936 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006937 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006938 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006941
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006942 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006943
Hanno Becker327c93b2018-08-15 13:56:18 +01006944 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006947 return( ret );
6948 }
6949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006953 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6954 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006956 }
6957
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006958 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959#if defined(MBEDTLS_SSL_PROTO_SSL3)
6960 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006961 hash_len = 36;
6962 else
6963#endif
6964 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6967 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006970 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6971 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006973 }
6974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006975 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006976 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006979 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6980 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006981 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006982 }
6983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006985 ssl->verify_data_len = hash_len;
6986 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006987#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006988
Paul Bakker0a597072012-09-25 21:55:46 +00006989 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006992 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006994#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006996 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006998#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006999 }
7000 else
7001 ssl->state++;
7002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007003#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007004 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007006#endif
7007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007009
7010 return( 0 );
7011}
7012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007014{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007015 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007017#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7018 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7019 mbedtls_md5_init( &handshake->fin_md5 );
7020 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007021 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7022 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007023#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7025#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007026#if defined(MBEDTLS_USE_PSA_CRYPTO)
7027 handshake->fin_sha256_psa = psa_hash_operation_init();
7028 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007031 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007032#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007033#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007034#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007035#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007036 handshake->fin_sha384_psa = psa_hash_operation_init();
7037 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007038#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007039 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007040 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007041#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007043#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007044
7045 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007046
7047#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7048 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7049 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7050#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007052#if defined(MBEDTLS_DHM_C)
7053 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007054#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007055#if defined(MBEDTLS_ECDH_C)
7056 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007057#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007058#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007059 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007060#if defined(MBEDTLS_SSL_CLI_C)
7061 handshake->ecjpake_cache = NULL;
7062 handshake->ecjpake_cache_len = 0;
7063#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007064#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007065
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007066#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007067 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007068#endif
7069
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007070#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7071 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7072#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007073}
7074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7080 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082 mbedtls_md_init( &transform->md_ctx_enc );
7083 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007084}
7085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007086void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007087{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007089}
7090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007092{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007093 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007094 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007095 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007096 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007098 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007099 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007100
7101 /*
7102 * Either the pointers are now NULL or cleared properly and can be freed.
7103 * Now allocate missing structures.
7104 */
7105 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007106 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007107 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007108 }
Paul Bakker48916f92012-09-16 19:57:18 +00007109
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007110 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007111 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007112 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007113 }
Paul Bakker48916f92012-09-16 19:57:18 +00007114
Paul Bakker82788fb2014-10-20 13:59:19 +02007115 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007116 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007117 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007118 }
Paul Bakker48916f92012-09-16 19:57:18 +00007119
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007120 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007121 if( ssl->handshake == NULL ||
7122 ssl->transform_negotiate == NULL ||
7123 ssl->session_negotiate == NULL )
7124 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127 mbedtls_free( ssl->handshake );
7128 mbedtls_free( ssl->transform_negotiate );
7129 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007130
7131 ssl->handshake = NULL;
7132 ssl->transform_negotiate = NULL;
7133 ssl->session_negotiate = NULL;
7134
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007135 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007136 }
7137
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007138 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007140 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007141 ssl_handshake_params_init( ssl->handshake );
7142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007143#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007144 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7145 {
7146 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007147
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007148 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7149 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7150 else
7151 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007152
7153 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007154 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007155#endif
7156
Paul Bakker48916f92012-09-16 19:57:18 +00007157 return( 0 );
7158}
7159
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007160#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007161/* Dummy cookie callbacks for defaults */
7162static int ssl_cookie_write_dummy( void *ctx,
7163 unsigned char **p, unsigned char *end,
7164 const unsigned char *cli_id, size_t cli_id_len )
7165{
7166 ((void) ctx);
7167 ((void) p);
7168 ((void) end);
7169 ((void) cli_id);
7170 ((void) cli_id_len);
7171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007173}
7174
7175static int ssl_cookie_check_dummy( void *ctx,
7176 const unsigned char *cookie, size_t cookie_len,
7177 const unsigned char *cli_id, size_t cli_id_len )
7178{
7179 ((void) ctx);
7180 ((void) cookie);
7181 ((void) cookie_len);
7182 ((void) cli_id);
7183 ((void) cli_id_len);
7184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007186}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007187#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007188
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007189/* Once ssl->out_hdr as the address of the beginning of the
7190 * next outgoing record is set, deduce the other pointers.
7191 *
7192 * Note: For TLS, we save the implicit record sequence number
7193 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7194 * and the caller has to make sure there's space for this.
7195 */
7196
7197static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7198 mbedtls_ssl_transform *transform )
7199{
7200#if defined(MBEDTLS_SSL_PROTO_DTLS)
7201 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7202 {
7203 ssl->out_ctr = ssl->out_hdr + 3;
7204 ssl->out_len = ssl->out_hdr + 11;
7205 ssl->out_iv = ssl->out_hdr + 13;
7206 }
7207 else
7208#endif
7209 {
7210 ssl->out_ctr = ssl->out_hdr - 8;
7211 ssl->out_len = ssl->out_hdr + 3;
7212 ssl->out_iv = ssl->out_hdr + 5;
7213 }
7214
7215 /* Adjust out_msg to make space for explicit IV, if used. */
7216 if( transform != NULL &&
7217 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7218 {
7219 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7220 }
7221 else
7222 ssl->out_msg = ssl->out_iv;
7223}
7224
7225/* Once ssl->in_hdr as the address of the beginning of the
7226 * next incoming record is set, deduce the other pointers.
7227 *
7228 * Note: For TLS, we save the implicit record sequence number
7229 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7230 * and the caller has to make sure there's space for this.
7231 */
7232
7233static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7234 mbedtls_ssl_transform *transform )
7235{
7236#if defined(MBEDTLS_SSL_PROTO_DTLS)
7237 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7238 {
7239 ssl->in_ctr = ssl->in_hdr + 3;
7240 ssl->in_len = ssl->in_hdr + 11;
7241 ssl->in_iv = ssl->in_hdr + 13;
7242 }
7243 else
7244#endif
7245 {
7246 ssl->in_ctr = ssl->in_hdr - 8;
7247 ssl->in_len = ssl->in_hdr + 3;
7248 ssl->in_iv = ssl->in_hdr + 5;
7249 }
7250
7251 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7252 if( transform != NULL &&
7253 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7254 {
7255 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7256 }
7257 else
7258 ssl->in_msg = ssl->in_iv;
7259}
7260
Paul Bakker5121ce52009-01-03 21:22:43 +00007261/*
7262 * Initialize an SSL context
7263 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007264void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7265{
7266 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7267}
7268
7269/*
7270 * Setup an SSL context
7271 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007272
7273static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7274{
7275 /* Set the incoming and outgoing record pointers. */
7276#if defined(MBEDTLS_SSL_PROTO_DTLS)
7277 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7278 {
7279 ssl->out_hdr = ssl->out_buf;
7280 ssl->in_hdr = ssl->in_buf;
7281 }
7282 else
7283#endif /* MBEDTLS_SSL_PROTO_DTLS */
7284 {
7285 ssl->out_hdr = ssl->out_buf + 8;
7286 ssl->in_hdr = ssl->in_buf + 8;
7287 }
7288
7289 /* Derive other internal pointers. */
7290 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7291 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7292}
7293
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007294int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007295 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007296{
Paul Bakker48916f92012-09-16 19:57:18 +00007297 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007298
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007299 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007300
7301 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007302 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007303 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007304
7305 /* Set to NULL in case of an error condition */
7306 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007307
Angus Grattond8213d02016-05-25 20:56:48 +10007308 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7309 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007310 {
Angus Grattond8213d02016-05-25 20:56:48 +10007311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007312 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007313 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007314 }
7315
7316 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7317 if( ssl->out_buf == NULL )
7318 {
7319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007320 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007321 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007322 }
7323
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007324 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007325
Paul Bakker48916f92012-09-16 19:57:18 +00007326 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007327 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007328
7329 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007330
7331error:
7332 mbedtls_free( ssl->in_buf );
7333 mbedtls_free( ssl->out_buf );
7334
7335 ssl->conf = NULL;
7336
7337 ssl->in_buf = NULL;
7338 ssl->out_buf = NULL;
7339
7340 ssl->in_hdr = NULL;
7341 ssl->in_ctr = NULL;
7342 ssl->in_len = NULL;
7343 ssl->in_iv = NULL;
7344 ssl->in_msg = NULL;
7345
7346 ssl->out_hdr = NULL;
7347 ssl->out_ctr = NULL;
7348 ssl->out_len = NULL;
7349 ssl->out_iv = NULL;
7350 ssl->out_msg = NULL;
7351
k-stachowiak9f7798e2018-07-31 16:52:32 +02007352 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007353}
7354
7355/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007356 * Reset an initialized and used SSL context for re-use while retaining
7357 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007358 *
7359 * If partial is non-zero, keep data in the input buffer and client ID.
7360 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007361 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007362static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007363{
Paul Bakker48916f92012-09-16 19:57:18 +00007364 int ret;
7365
Hanno Becker7e772132018-08-10 12:38:21 +01007366#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7367 !defined(MBEDTLS_SSL_SRV_C)
7368 ((void) partial);
7369#endif
7370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007372
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007373 /* Cancel any possibly running timer */
7374 ssl_set_timer( ssl, 0 );
7375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376#if defined(MBEDTLS_SSL_RENEGOTIATION)
7377 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007378 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007379
7380 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7382 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007383#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007385
Paul Bakker7eb013f2011-10-06 12:37:39 +00007386 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007387 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007388
7389 ssl->in_msgtype = 0;
7390 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007391#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007392 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007393 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007394#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007396 ssl_dtls_replay_reset( ssl );
7397#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007398
7399 ssl->in_hslen = 0;
7400 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007401
7402 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007403
7404 ssl->out_msgtype = 0;
7405 ssl->out_msglen = 0;
7406 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7408 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007409 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007410#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007411
Hanno Becker19859472018-08-06 09:40:20 +01007412 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7413
Paul Bakker48916f92012-09-16 19:57:18 +00007414 ssl->transform_in = NULL;
7415 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007416
Hanno Becker78640902018-08-13 16:35:15 +01007417 ssl->session_in = NULL;
7418 ssl->session_out = NULL;
7419
Angus Grattond8213d02016-05-25 20:56:48 +10007420 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007421
7422#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007423 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007424#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7425 {
7426 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007427 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007428 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007430#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7431 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7434 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7437 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007438 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007439 }
7440#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007441
Paul Bakker48916f92012-09-16 19:57:18 +00007442 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007444 mbedtls_ssl_transform_free( ssl->transform );
7445 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007446 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007447 }
Paul Bakker48916f92012-09-16 19:57:18 +00007448
Paul Bakkerc0463502013-02-14 11:19:38 +01007449 if( ssl->session )
7450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451 mbedtls_ssl_session_free( ssl->session );
7452 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007453 ssl->session = NULL;
7454 }
7455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007456#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007457 ssl->alpn_chosen = NULL;
7458#endif
7459
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007460#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007461#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007462 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007463#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007464 {
7465 mbedtls_free( ssl->cli_id );
7466 ssl->cli_id = NULL;
7467 ssl->cli_id_len = 0;
7468 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007469#endif
7470
Paul Bakker48916f92012-09-16 19:57:18 +00007471 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7472 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007473
7474 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007475}
7476
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007477/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007478 * Reset an initialized and used SSL context for re-use while retaining
7479 * all application-set variables, function pointers and data.
7480 */
7481int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7482{
7483 return( ssl_session_reset_int( ssl, 0 ) );
7484}
7485
7486/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007487 * SSL set accessors
7488 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007489void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007490{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007491 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007492}
7493
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007494void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007495{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007496 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007497}
7498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007500void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007501{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007502 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007503}
7504#endif
7505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007507void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007508{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007509 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007510}
7511#endif
7512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007513#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007514
Hanno Becker1841b0a2018-08-24 11:13:57 +01007515void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7516 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007517{
7518 ssl->disable_datagram_packing = !allow_packing;
7519}
7520
7521void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7522 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007523{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007524 conf->hs_timeout_min = min;
7525 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007526}
7527#endif
7528
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007529void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007530{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007531 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007532}
7533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007535void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007536 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007537 void *p_vrfy )
7538{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007539 conf->f_vrfy = f_vrfy;
7540 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007541}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007542#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007543
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007544void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007545 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007546 void *p_rng )
7547{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007548 conf->f_rng = f_rng;
7549 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007550}
7551
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007552void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007553 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007554 void *p_dbg )
7555{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007556 conf->f_dbg = f_dbg;
7557 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007558}
7559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007561 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007562 mbedtls_ssl_send_t *f_send,
7563 mbedtls_ssl_recv_t *f_recv,
7564 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007565{
7566 ssl->p_bio = p_bio;
7567 ssl->f_send = f_send;
7568 ssl->f_recv = f_recv;
7569 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007570}
7571
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007572#if defined(MBEDTLS_SSL_PROTO_DTLS)
7573void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7574{
7575 ssl->mtu = mtu;
7576}
7577#endif
7578
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007579void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007580{
7581 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007582}
7583
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007584void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7585 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007586 mbedtls_ssl_set_timer_t *f_set_timer,
7587 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007588{
7589 ssl->p_timer = p_timer;
7590 ssl->f_set_timer = f_set_timer;
7591 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007592
7593 /* Make sure we start with no timer running */
7594 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007595}
7596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007597#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007598void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007599 void *p_cache,
7600 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7601 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007602{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007603 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007604 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007605 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007606}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609#if defined(MBEDTLS_SSL_CLI_C)
7610int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007611{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007612 int ret;
7613
7614 if( ssl == NULL ||
7615 session == NULL ||
7616 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007617 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007620 }
7621
7622 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7623 return( ret );
7624
Paul Bakker0a597072012-09-25 21:55:46 +00007625 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007626
7627 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007628}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007630
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007631void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007632 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007633{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007634 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7635 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7636 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7637 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007638}
7639
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007640void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007641 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007642 int major, int minor )
7643{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007644 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007645 return;
7646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007647 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007648 return;
7649
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007650 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007651}
7652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007654void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007655 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007656{
7657 conf->cert_profile = profile;
7658}
7659
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007660/* Append a new keycert entry to a (possibly empty) list */
7661static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7662 mbedtls_x509_crt *cert,
7663 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007664{
niisato8ee24222018-06-25 19:05:48 +09007665 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007666
niisato8ee24222018-06-25 19:05:48 +09007667 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7668 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007669 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007670
niisato8ee24222018-06-25 19:05:48 +09007671 new_cert->cert = cert;
7672 new_cert->key = key;
7673 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007674
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007675 /* Update head is the list was null, else add to the end */
7676 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007677 {
niisato8ee24222018-06-25 19:05:48 +09007678 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007679 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007680 else
7681 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007682 mbedtls_ssl_key_cert *cur = *head;
7683 while( cur->next != NULL )
7684 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007685 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007686 }
7687
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007688 return( 0 );
7689}
7690
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007691int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007692 mbedtls_x509_crt *own_cert,
7693 mbedtls_pk_context *pk_key )
7694{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007695 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007696}
7697
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007698void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007699 mbedtls_x509_crt *ca_chain,
7700 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007701{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007702 conf->ca_chain = ca_chain;
7703 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007704}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007706
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007707#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7708int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7709 mbedtls_x509_crt *own_cert,
7710 mbedtls_pk_context *pk_key )
7711{
7712 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7713 own_cert, pk_key ) );
7714}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007715
7716void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7717 mbedtls_x509_crt *ca_chain,
7718 mbedtls_x509_crl *ca_crl )
7719{
7720 ssl->handshake->sni_ca_chain = ca_chain;
7721 ssl->handshake->sni_ca_crl = ca_crl;
7722}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007723
7724void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7725 int authmode )
7726{
7727 ssl->handshake->sni_authmode = authmode;
7728}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007729#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7730
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007731#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007732/*
7733 * Set EC J-PAKE password for current handshake
7734 */
7735int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7736 const unsigned char *pw,
7737 size_t pw_len )
7738{
7739 mbedtls_ecjpake_role role;
7740
Janos Follath8eb64132016-06-03 15:40:57 +01007741 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007742 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7743
7744 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7745 role = MBEDTLS_ECJPAKE_SERVER;
7746 else
7747 role = MBEDTLS_ECJPAKE_CLIENT;
7748
7749 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7750 role,
7751 MBEDTLS_MD_SHA256,
7752 MBEDTLS_ECP_DP_SECP256R1,
7753 pw, pw_len ) );
7754}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007755#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007758
7759static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
7760{
7761 /* Remove reference to existing PSK, if any. */
7762#if defined(MBEDTLS_USE_PSA_CRYPTO)
7763 if( conf->psk_opaque != 0 )
7764 {
7765 /* The maintenance of the PSK key slot is the
7766 * user's responsibility. */
7767 conf->psk_opaque = 0;
7768 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00007769 /* This and the following branch should never
7770 * be taken simultaenously as we maintain the
7771 * invariant that raw and opaque PSKs are never
7772 * configured simultaneously. As a safeguard,
7773 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007774#endif /* MBEDTLS_USE_PSA_CRYPTO */
7775 if( conf->psk != NULL )
7776 {
7777 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
7778
7779 mbedtls_free( conf->psk );
7780 conf->psk = NULL;
7781 conf->psk_len = 0;
7782 }
7783
7784 /* Remove reference to PSK identity, if any. */
7785 if( conf->psk_identity != NULL )
7786 {
7787 mbedtls_free( conf->psk_identity );
7788 conf->psk_identity = NULL;
7789 conf->psk_identity_len = 0;
7790 }
7791}
7792
Hanno Becker7390c712018-11-15 13:33:04 +00007793/* This function assumes that PSK identity in the SSL config is unset.
7794 * It checks that the provided identity is well-formed and attempts
7795 * to make a copy of it in the SSL config.
7796 * On failure, the PSK identity in the config remains unset. */
7797static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
7798 unsigned char const *psk_identity,
7799 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007800{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007801 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00007802 if( psk_identity == NULL ||
7803 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007804 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007805 {
7806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7807 }
7808
Hanno Becker7390c712018-11-15 13:33:04 +00007809 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
7810 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007811 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02007812
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007813 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007814 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02007815
7816 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02007817}
7818
Hanno Becker7390c712018-11-15 13:33:04 +00007819int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
7820 const unsigned char *psk, size_t psk_len,
7821 const unsigned char *psk_identity, size_t psk_identity_len )
7822{
7823 int ret;
7824 /* Remove opaque/raw PSK + PSK Identity */
7825 ssl_conf_remove_psk( conf );
7826
7827 /* Check and set raw PSK */
7828 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
7829 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7830 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
7831 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7832 conf->psk_len = psk_len;
7833 memcpy( conf->psk, psk, conf->psk_len );
7834
7835 /* Check and set PSK Identity */
7836 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
7837 if( ret != 0 )
7838 ssl_conf_remove_psk( conf );
7839
7840 return( ret );
7841}
7842
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007843static void ssl_remove_psk( mbedtls_ssl_context *ssl )
7844{
7845#if defined(MBEDTLS_USE_PSA_CRYPTO)
7846 if( ssl->handshake->psk_opaque != 0 )
7847 {
7848 ssl->handshake->psk_opaque = 0;
7849 }
7850 else
7851#endif /* MBEDTLS_USE_PSA_CRYPTO */
7852 if( ssl->handshake->psk != NULL )
7853 {
7854 mbedtls_platform_zeroize( ssl->handshake->psk,
7855 ssl->handshake->psk_len );
7856 mbedtls_free( ssl->handshake->psk );
7857 ssl->handshake->psk_len = 0;
7858 }
7859}
7860
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007861int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7862 const unsigned char *psk, size_t psk_len )
7863{
7864 if( psk == NULL || ssl->handshake == NULL )
7865 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7866
7867 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7868 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7869
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007870 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007871
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007872 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007873 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007874
7875 ssl->handshake->psk_len = psk_len;
7876 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7877
7878 return( 0 );
7879}
7880
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007881#if defined(MBEDTLS_USE_PSA_CRYPTO)
7882int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007883 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007884 const unsigned char *psk_identity,
7885 size_t psk_identity_len )
7886{
Hanno Becker7390c712018-11-15 13:33:04 +00007887 int ret;
7888 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007889 ssl_conf_remove_psk( conf );
7890
Hanno Becker7390c712018-11-15 13:33:04 +00007891 /* Check and set opaque PSK */
7892 if( psk_slot == 0 )
7893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007894 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00007895
7896 /* Check and set PSK Identity */
7897 ret = ssl_conf_set_psk_identity( conf, psk_identity,
7898 psk_identity_len );
7899 if( ret != 0 )
7900 ssl_conf_remove_psk( conf );
7901
7902 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007903}
7904
7905int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007906 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007907{
7908 if( psk_slot == 0 || ssl->handshake == NULL )
7909 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7910
7911 ssl_remove_psk( ssl );
7912 ssl->handshake->psk_opaque = psk_slot;
7913 return( 0 );
7914}
7915#endif /* MBEDTLS_USE_PSA_CRYPTO */
7916
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007917void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007919 size_t),
7920 void *p_psk )
7921{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007922 conf->f_psk = f_psk;
7923 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007924}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007926
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007927#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007928
7929#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007930int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007931{
7932 int ret;
7933
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007934 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7935 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7936 {
7937 mbedtls_mpi_free( &conf->dhm_P );
7938 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007939 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007940 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007941
7942 return( 0 );
7943}
Hanno Becker470a8c42017-10-04 15:28:46 +01007944#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007945
Hanno Beckera90658f2017-10-04 15:29:08 +01007946int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7947 const unsigned char *dhm_P, size_t P_len,
7948 const unsigned char *dhm_G, size_t G_len )
7949{
7950 int ret;
7951
7952 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7953 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7954 {
7955 mbedtls_mpi_free( &conf->dhm_P );
7956 mbedtls_mpi_free( &conf->dhm_G );
7957 return( ret );
7958 }
7959
7960 return( 0 );
7961}
Paul Bakker5121ce52009-01-03 21:22:43 +00007962
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007963int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007964{
7965 int ret;
7966
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007967 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7968 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7969 {
7970 mbedtls_mpi_free( &conf->dhm_P );
7971 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007972 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007973 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007974
7975 return( 0 );
7976}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007977#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007978
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007979#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7980/*
7981 * Set the minimum length for Diffie-Hellman parameters
7982 */
7983void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7984 unsigned int bitlen )
7985{
7986 conf->dhm_min_bitlen = bitlen;
7987}
7988#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7989
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007990#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007991/*
7992 * Set allowed/preferred hashes for handshake signatures
7993 */
7994void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7995 const int *hashes )
7996{
7997 conf->sig_hashes = hashes;
7998}
Hanno Becker947194e2017-04-07 13:25:49 +01007999#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008000
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008001#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008002/*
8003 * Set the allowed elliptic curves
8004 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008005void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008006 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008007{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008008 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008009}
Hanno Becker947194e2017-04-07 13:25:49 +01008010#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008011
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008012#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008014{
Hanno Becker947194e2017-04-07 13:25:49 +01008015 /* Initialize to suppress unnecessary compiler warning */
8016 size_t hostname_len = 0;
8017
8018 /* Check if new hostname is valid before
8019 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008020 if( hostname != NULL )
8021 {
8022 hostname_len = strlen( hostname );
8023
8024 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8025 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8026 }
8027
8028 /* Now it's clear that we will overwrite the old hostname,
8029 * so we can free it safely */
8030
8031 if( ssl->hostname != NULL )
8032 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008033 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008034 mbedtls_free( ssl->hostname );
8035 }
8036
8037 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008038
Paul Bakker5121ce52009-01-03 21:22:43 +00008039 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008040 {
8041 ssl->hostname = NULL;
8042 }
8043 else
8044 {
8045 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008046 if( ssl->hostname == NULL )
8047 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008048
Hanno Becker947194e2017-04-07 13:25:49 +01008049 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008050
Hanno Becker947194e2017-04-07 13:25:49 +01008051 ssl->hostname[hostname_len] = '\0';
8052 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008053
8054 return( 0 );
8055}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008056#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008057
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008058#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008059void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008060 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008061 const unsigned char *, size_t),
8062 void *p_sni )
8063{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008064 conf->f_sni = f_sni;
8065 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008066}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008067#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008069#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008070int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008071{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008072 size_t cur_len, tot_len;
8073 const char **p;
8074
8075 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008076 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8077 * MUST NOT be truncated."
8078 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008079 */
8080 tot_len = 0;
8081 for( p = protos; *p != NULL; p++ )
8082 {
8083 cur_len = strlen( *p );
8084 tot_len += cur_len;
8085
8086 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008087 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008088 }
8089
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008090 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008091
8092 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008093}
8094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008095const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008096{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008097 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008099#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008100
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008101void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008102{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008103 conf->max_major_ver = major;
8104 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008105}
8106
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008107void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008108{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008109 conf->min_major_ver = major;
8110 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008111}
8112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008113#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008114void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008115{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008116 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008117}
8118#endif
8119
Janos Follath088ce432017-04-10 12:42:31 +01008120#if defined(MBEDTLS_SSL_SRV_C)
8121void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8122 char cert_req_ca_list )
8123{
8124 conf->cert_req_ca_list = cert_req_ca_list;
8125}
8126#endif
8127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008128#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008129void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008130{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008131 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008132}
8133#endif
8134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008136void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008137{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008138 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008139}
8140#endif
8141
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008142#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008143void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008144{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008145 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008146}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008147#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008150int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008151{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008152 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008153 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008156 }
8157
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008158 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008159
8160 return( 0 );
8161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008162#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008165void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008166{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008167 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008168}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008169#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008171#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008172void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008173{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008174 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008175}
8176#endif
8177
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008178void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008179{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008180 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008181}
8182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008184void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008185{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008186 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008187}
8188
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008189void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008190{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008191 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008192}
8193
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008194void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008195 const unsigned char period[8] )
8196{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008197 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008198}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008202#if defined(MBEDTLS_SSL_CLI_C)
8203void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008204{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008205 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008206}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008207#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008208
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008209#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008210void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8211 mbedtls_ssl_ticket_write_t *f_ticket_write,
8212 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8213 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008214{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008215 conf->f_ticket_write = f_ticket_write;
8216 conf->f_ticket_parse = f_ticket_parse;
8217 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008218}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008219#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008221
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008222#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8223void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8224 mbedtls_ssl_export_keys_t *f_export_keys,
8225 void *p_export_keys )
8226{
8227 conf->f_export_keys = f_export_keys;
8228 conf->p_export_keys = p_export_keys;
8229}
8230#endif
8231
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008232#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008233void mbedtls_ssl_conf_async_private_cb(
8234 mbedtls_ssl_config *conf,
8235 mbedtls_ssl_async_sign_t *f_async_sign,
8236 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8237 mbedtls_ssl_async_resume_t *f_async_resume,
8238 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008239 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008240{
8241 conf->f_async_sign_start = f_async_sign;
8242 conf->f_async_decrypt_start = f_async_decrypt;
8243 conf->f_async_resume = f_async_resume;
8244 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008245 conf->p_async_config_data = async_config_data;
8246}
8247
Gilles Peskine8f97af72018-04-26 11:46:10 +02008248void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8249{
8250 return( conf->p_async_config_data );
8251}
8252
Gilles Peskine1febfef2018-04-30 11:54:39 +02008253void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008254{
8255 if( ssl->handshake == NULL )
8256 return( NULL );
8257 else
8258 return( ssl->handshake->user_async_ctx );
8259}
8260
Gilles Peskine1febfef2018-04-30 11:54:39 +02008261void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008262 void *ctx )
8263{
8264 if( ssl->handshake != NULL )
8265 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008266}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008267#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008268
Paul Bakker5121ce52009-01-03 21:22:43 +00008269/*
8270 * SSL get accessors
8271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008272size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008273{
8274 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8275}
8276
Hanno Becker8b170a02017-10-10 11:51:19 +01008277int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8278{
8279 /*
8280 * Case A: We're currently holding back
8281 * a message for further processing.
8282 */
8283
8284 if( ssl->keep_current_message == 1 )
8285 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008286 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008287 return( 1 );
8288 }
8289
8290 /*
8291 * Case B: Further records are pending in the current datagram.
8292 */
8293
8294#if defined(MBEDTLS_SSL_PROTO_DTLS)
8295 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8296 ssl->in_left > ssl->next_record_offset )
8297 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008298 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008299 return( 1 );
8300 }
8301#endif /* MBEDTLS_SSL_PROTO_DTLS */
8302
8303 /*
8304 * Case C: A handshake message is being processed.
8305 */
8306
Hanno Becker8b170a02017-10-10 11:51:19 +01008307 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8308 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008309 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008310 return( 1 );
8311 }
8312
8313 /*
8314 * Case D: An application data message is being processed
8315 */
8316 if( ssl->in_offt != NULL )
8317 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008318 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008319 return( 1 );
8320 }
8321
8322 /*
8323 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008324 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008325 * we implement support for multiple alerts in single records.
8326 */
8327
8328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8329 return( 0 );
8330}
8331
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008332uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008333{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008334 if( ssl->session != NULL )
8335 return( ssl->session->verify_result );
8336
8337 if( ssl->session_negotiate != NULL )
8338 return( ssl->session_negotiate->verify_result );
8339
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008340 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008341}
8342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008343const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008344{
Paul Bakker926c8e42013-03-06 10:23:34 +01008345 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008346 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008348 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008349}
8350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008352{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008353#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008354 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008355 {
8356 switch( ssl->minor_ver )
8357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008359 return( "DTLSv1.0" );
8360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008361 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008362 return( "DTLSv1.2" );
8363
8364 default:
8365 return( "unknown (DTLS)" );
8366 }
8367 }
8368#endif
8369
Paul Bakker43ca69c2011-01-15 17:35:19 +00008370 switch( ssl->minor_ver )
8371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008373 return( "SSLv3.0" );
8374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008375 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008376 return( "TLSv1.0" );
8377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008378 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008379 return( "TLSv1.1" );
8380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008381 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008382 return( "TLSv1.2" );
8383
Paul Bakker43ca69c2011-01-15 17:35:19 +00008384 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008385 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008386 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008387}
8388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008389int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008390{
Hanno Becker3136ede2018-08-17 15:28:19 +01008391 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008392 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008393 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008394
Hanno Becker78640902018-08-13 16:35:15 +01008395 if( transform == NULL )
8396 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008398#if defined(MBEDTLS_ZLIB_SUPPORT)
8399 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8400 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008401#endif
8402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405 case MBEDTLS_MODE_GCM:
8406 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008407 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008409 transform_expansion = transform->minlen;
8410 break;
8411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008412 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008413
8414 block_size = mbedtls_cipher_get_block_size(
8415 &transform->cipher_ctx_enc );
8416
Hanno Becker3136ede2018-08-17 15:28:19 +01008417 /* Expansion due to the addition of the MAC. */
8418 transform_expansion += transform->maclen;
8419
8420 /* Expansion due to the addition of CBC padding;
8421 * Theoretically up to 256 bytes, but we never use
8422 * more than the block size of the underlying cipher. */
8423 transform_expansion += block_size;
8424
8425 /* For TLS 1.1 or higher, an explicit IV is added
8426 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008427#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8428 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008429 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008430#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008431
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008432 break;
8433
8434 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008436 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008437 }
8438
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008439 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008440}
8441
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008442#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8443size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8444{
8445 size_t max_len;
8446
8447 /*
8448 * Assume mfl_code is correct since it was checked when set
8449 */
Angus Grattond8213d02016-05-25 20:56:48 +10008450 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008451
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008452 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008453 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008454 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008455 {
Angus Grattond8213d02016-05-25 20:56:48 +10008456 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008457 }
8458
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008459 /* During a handshake, use the value being negotiated */
8460 if( ssl->session_negotiate != NULL &&
8461 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8462 {
8463 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8464 }
8465
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008466 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008467}
8468#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8469
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008470#if defined(MBEDTLS_SSL_PROTO_DTLS)
8471static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8472{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008473 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8474 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8475 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8476 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8477 return ( 0 );
8478
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008479 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8480 return( ssl->mtu );
8481
8482 if( ssl->mtu == 0 )
8483 return( ssl->handshake->mtu );
8484
8485 return( ssl->mtu < ssl->handshake->mtu ?
8486 ssl->mtu : ssl->handshake->mtu );
8487}
8488#endif /* MBEDTLS_SSL_PROTO_DTLS */
8489
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008490int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8491{
8492 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8493
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008494#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8495 !defined(MBEDTLS_SSL_PROTO_DTLS)
8496 (void) ssl;
8497#endif
8498
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008499#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8500 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8501
8502 if( max_len > mfl )
8503 max_len = mfl;
8504#endif
8505
8506#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008507 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008508 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008509 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008510 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8511 const size_t overhead = (size_t) ret;
8512
8513 if( ret < 0 )
8514 return( ret );
8515
8516 if( mtu <= overhead )
8517 {
8518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8519 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8520 }
8521
8522 if( max_len > mtu - overhead )
8523 max_len = mtu - overhead;
8524 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008525#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008526
Hanno Becker0defedb2018-08-10 12:35:02 +01008527#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8528 !defined(MBEDTLS_SSL_PROTO_DTLS)
8529 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008530#endif
8531
8532 return( (int) max_len );
8533}
8534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008535#if defined(MBEDTLS_X509_CRT_PARSE_C)
8536const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008537{
8538 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008539 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008540
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008541 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008542}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008543#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008545#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00008546int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8547 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008548{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008549 if( ssl == NULL ||
8550 dst == NULL ||
8551 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008552 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008554 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008555 }
8556
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008557 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008558}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008559#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008560
Paul Bakker5121ce52009-01-03 21:22:43 +00008561/*
Paul Bakker1961b702013-01-25 14:49:24 +01008562 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008565{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008566 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008567
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008568 if( ssl == NULL || ssl->conf == NULL )
8569 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008571#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008572 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008573 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008574#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008575#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008576 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008577 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008578#endif
8579
Paul Bakker1961b702013-01-25 14:49:24 +01008580 return( ret );
8581}
8582
8583/*
8584 * Perform the SSL handshake
8585 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008586int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008587{
8588 int ret = 0;
8589
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008590 if( ssl == NULL || ssl->conf == NULL )
8591 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008597 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008598
8599 if( ret != 0 )
8600 break;
8601 }
8602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008604
8605 return( ret );
8606}
8607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008608#if defined(MBEDTLS_SSL_RENEGOTIATION)
8609#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008610/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008611 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008614{
8615 int ret;
8616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008618
8619 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008620 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8621 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008622
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008623 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008624 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008625 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008626 return( ret );
8627 }
8628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008630
8631 return( 0 );
8632}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008633#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008634
8635/*
8636 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637 * - any side: calling mbedtls_ssl_renegotiate(),
8638 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8639 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008640 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008641 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008642 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008644static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008645{
8646 int ret;
8647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008649
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008650 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8651 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008652
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008653 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8654 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008655#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008656 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008657 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008658 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008659 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008660 ssl->handshake->out_msg_seq = 1;
8661 else
8662 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008663 }
8664#endif
8665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008666 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8667 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008669 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008671 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008672 return( ret );
8673 }
8674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008676
8677 return( 0 );
8678}
8679
8680/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008681 * Renegotiate current connection on client,
8682 * or request renegotiation on server
8683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008684int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008685{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008687
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008688 if( ssl == NULL || ssl->conf == NULL )
8689 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008691#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008692 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008693 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008695 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8696 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008698 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008699
8700 /* Did we already try/start sending HelloRequest? */
8701 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008702 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008703
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008704 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008705 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008706#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008708#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008709 /*
8710 * On client, either start the renegotiation process or,
8711 * if already in progress, continue the handshake
8712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008713 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008715 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8716 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008717
8718 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008720 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008721 return( ret );
8722 }
8723 }
8724 else
8725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008726 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008728 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008729 return( ret );
8730 }
8731 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008732#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008733
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008734 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008735}
8736
8737/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008738 * Check record counters and renegotiate if they're above the limit.
8739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008740static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008741{
Andres AG2196c7f2016-12-15 17:01:16 +00008742 size_t ep_len = ssl_ep_len( ssl );
8743 int in_ctr_cmp;
8744 int out_ctr_cmp;
8745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008746 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8747 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008748 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008749 {
8750 return( 0 );
8751 }
8752
Andres AG2196c7f2016-12-15 17:01:16 +00008753 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8754 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008755 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008756 ssl->conf->renego_period + ep_len, 8 - ep_len );
8757
8758 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008759 {
8760 return( 0 );
8761 }
8762
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008764 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008765}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008766#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008767
8768/*
8769 * Receive application data decrypted from the SSL layer
8770 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008771int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008772{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008773 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008774 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008775
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008776 if( ssl == NULL || ssl->conf == NULL )
8777 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008782 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008784 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008785 return( ret );
8786
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008787 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008788 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008789 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008790 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008791 return( ret );
8792 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008793 }
8794#endif
8795
Hanno Becker4a810fb2017-05-24 16:27:30 +01008796 /*
8797 * Check if renegotiation is necessary and/or handshake is
8798 * in process. If yes, perform/continue, and fall through
8799 * if an unexpected packet is received while the client
8800 * is waiting for the ServerHello.
8801 *
8802 * (There is no equivalent to the last condition on
8803 * the server-side as it is not treated as within
8804 * a handshake while waiting for the ClientHello
8805 * after a renegotiation request.)
8806 */
8807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008808#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008809 ret = ssl_check_ctr_renegotiate( ssl );
8810 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8811 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008813 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008814 return( ret );
8815 }
8816#endif
8817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008818 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008820 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008821 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8822 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008824 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008825 return( ret );
8826 }
8827 }
8828
Hanno Beckere41158b2017-10-23 13:30:32 +01008829 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008830 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008831 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008832 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008833 if( ssl->f_get_timer != NULL &&
8834 ssl->f_get_timer( ssl->p_timer ) == -1 )
8835 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008836 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008837 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008838
Hanno Becker327c93b2018-08-15 13:56:18 +01008839 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008840 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008841 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8842 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008843
Hanno Becker4a810fb2017-05-24 16:27:30 +01008844 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8845 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008846 }
8847
8848 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008849 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008850 {
8851 /*
8852 * OpenSSL sends empty messages to randomize the IV
8853 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008854 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008856 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008857 return( 0 );
8858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008859 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008860 return( ret );
8861 }
8862 }
8863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008864 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008867
Hanno Becker4a810fb2017-05-24 16:27:30 +01008868 /*
8869 * - For client-side, expect SERVER_HELLO_REQUEST.
8870 * - For server-side, expect CLIENT_HELLO.
8871 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8872 */
8873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008874#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008875 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008876 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008877 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008880
8881 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008882#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008883 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008884 {
8885 continue;
8886 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008887#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008888 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008889 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008890#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008891
Hanno Becker4a810fb2017-05-24 16:27:30 +01008892#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008893 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008894 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008897
8898 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008899#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008900 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008901 {
8902 continue;
8903 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008904#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008905 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008906 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008907#endif /* MBEDTLS_SSL_SRV_C */
8908
Hanno Becker21df7f92017-10-17 11:03:26 +01008909#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008910 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008911 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8912 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8913 ssl->conf->allow_legacy_renegotiation ==
8914 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8915 {
8916 /*
8917 * Accept renegotiation request
8918 */
Paul Bakker48916f92012-09-16 19:57:18 +00008919
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008920 /* DTLS clients need to know renego is server-initiated */
8921#if defined(MBEDTLS_SSL_PROTO_DTLS)
8922 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8923 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8924 {
8925 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8926 }
8927#endif
8928 ret = ssl_start_renegotiation( ssl );
8929 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8930 ret != 0 )
8931 {
8932 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8933 return( ret );
8934 }
8935 }
8936 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008937#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008938 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008939 /*
8940 * Refuse renegotiation
8941 */
8942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008943 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008945#if defined(MBEDTLS_SSL_PROTO_SSL3)
8946 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008947 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008948 /* SSLv3 does not have a "no_renegotiation" warning, so
8949 we send a fatal alert and abort the connection. */
8950 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8951 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8952 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008953 }
8954 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008955#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8956#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8957 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8958 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008960 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8961 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8962 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008963 {
8964 return( ret );
8965 }
Paul Bakker48916f92012-09-16 19:57:18 +00008966 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008967 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008968#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8969 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8972 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008973 }
Paul Bakker48916f92012-09-16 19:57:18 +00008974 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008975
Hanno Becker90333da2017-10-10 11:27:13 +01008976 /* At this point, we don't know whether the renegotiation has been
8977 * completed or not. The cases to consider are the following:
8978 * 1) The renegotiation is complete. In this case, no new record
8979 * has been read yet.
8980 * 2) The renegotiation is incomplete because the client received
8981 * an application data record while awaiting the ServerHello.
8982 * 3) The renegotiation is incomplete because the client received
8983 * a non-handshake, non-application data message while awaiting
8984 * the ServerHello.
8985 * In each of these case, looping will be the proper action:
8986 * - For 1), the next iteration will read a new record and check
8987 * if it's application data.
8988 * - For 2), the loop condition isn't satisfied as application data
8989 * is present, hence continue is the same as break
8990 * - For 3), the loop condition is satisfied and read_record
8991 * will re-deliver the message that was held back by the client
8992 * when expecting the ServerHello.
8993 */
8994 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008995 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008996#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008997 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008998 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008999 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009000 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009001 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009004 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009005 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009006 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009007 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009008 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009009#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009011 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9012 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009015 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009016 }
9017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009018 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9021 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009022 }
9023
9024 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009025
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009026 /* We're going to return something now, cancel timer,
9027 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009028 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009029 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009030
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009031#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009032 /* If we requested renego but received AppData, resend HelloRequest.
9033 * Do it now, after setting in_offt, to avoid taking this branch
9034 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009035#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009036 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009037 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009038 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009039 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009041 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009042 return( ret );
9043 }
9044 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009045#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009046#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009047 }
9048
9049 n = ( len < ssl->in_msglen )
9050 ? len : ssl->in_msglen;
9051
9052 memcpy( buf, ssl->in_offt, n );
9053 ssl->in_msglen -= n;
9054
9055 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009056 {
9057 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009058 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009059 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009060 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009061 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009062 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009063 /* more data available */
9064 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009065 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009068
Paul Bakker23986e52011-04-24 08:57:21 +00009069 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009070}
9071
9072/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009073 * Send application data to be encrypted by the SSL layer, taking care of max
9074 * fragment length and buffer size.
9075 *
9076 * According to RFC 5246 Section 6.2.1:
9077 *
9078 * Zero-length fragments of Application data MAY be sent as they are
9079 * potentially useful as a traffic analysis countermeasure.
9080 *
9081 * Therefore, it is possible that the input message length is 0 and the
9082 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009083 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009084static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009085 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009086{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009087 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9088 const size_t max_len = (size_t) ret;
9089
9090 if( ret < 0 )
9091 {
9092 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9093 return( ret );
9094 }
9095
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009096 if( len > max_len )
9097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009098#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009099 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009102 "maximum fragment length: %d > %d",
9103 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009104 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009105 }
9106 else
9107#endif
9108 len = max_len;
9109 }
Paul Bakker887bd502011-06-08 13:10:54 +00009110
Paul Bakker5121ce52009-01-03 21:22:43 +00009111 if( ssl->out_left != 0 )
9112 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009113 /*
9114 * The user has previously tried to send the data and
9115 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9116 * written. In this case, we expect the high-level write function
9117 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009119 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009121 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009122 return( ret );
9123 }
9124 }
Paul Bakker887bd502011-06-08 13:10:54 +00009125 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009126 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009127 /*
9128 * The user is trying to send a message the first time, so we need to
9129 * copy the data into the internal buffers and setup the data structure
9130 * to keep track of partial writes
9131 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009132 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009133 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009134 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009135
Hanno Becker67bc7c32018-08-06 11:33:50 +01009136 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009138 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009139 return( ret );
9140 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009141 }
9142
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009143 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009144}
9145
9146/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009147 * Write application data, doing 1/n-1 splitting if necessary.
9148 *
9149 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009150 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009151 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009153#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009154static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009155 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009156{
9157 int ret;
9158
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009159 if( ssl->conf->cbc_record_splitting ==
9160 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009161 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009162 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9163 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9164 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009165 {
9166 return( ssl_write_real( ssl, buf, len ) );
9167 }
9168
9169 if( ssl->split_done == 0 )
9170 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009171 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009172 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009173 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009174 }
9175
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009176 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9177 return( ret );
9178 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009179
9180 return( ret + 1 );
9181}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009182#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009183
9184/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009185 * Write application data (public-facing wrapper)
9186 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009187int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009188{
9189 int ret;
9190
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009192
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009193 if( ssl == NULL || ssl->conf == NULL )
9194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9195
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009196#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009197 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9198 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009199 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009200 return( ret );
9201 }
9202#endif
9203
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009204 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009205 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009206 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009207 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009208 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009209 return( ret );
9210 }
9211 }
9212
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009213#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009214 ret = ssl_write_split( ssl, buf, len );
9215#else
9216 ret = ssl_write_real( ssl, buf, len );
9217#endif
9218
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009220
9221 return( ret );
9222}
9223
9224/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009225 * Notify the peer that the connection is being closed
9226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009227int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009228{
9229 int ret;
9230
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009231 if( ssl == NULL || ssl->conf == NULL )
9232 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009235
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009236 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009237 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009239 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009241 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9242 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9243 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009245 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009246 return( ret );
9247 }
9248 }
9249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009251
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009252 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009253}
9254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009255void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009256{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009257 if( transform == NULL )
9258 return;
9259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009260#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009261 deflateEnd( &transform->ctx_deflate );
9262 inflateEnd( &transform->ctx_inflate );
9263#endif
9264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009265 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9266 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009268 mbedtls_md_free( &transform->md_ctx_enc );
9269 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009270
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009271 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009272}
9273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009274#if defined(MBEDTLS_X509_CRT_PARSE_C)
9275static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009276{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009277 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009278
9279 while( cur != NULL )
9280 {
9281 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009282 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009283 cur = next;
9284 }
9285}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009286#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009287
Hanno Becker0271f962018-08-16 13:23:47 +01009288#if defined(MBEDTLS_SSL_PROTO_DTLS)
9289
9290static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9291{
9292 unsigned offset;
9293 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9294
9295 if( hs == NULL )
9296 return;
9297
Hanno Becker283f5ef2018-08-24 09:34:47 +01009298 ssl_free_buffered_record( ssl );
9299
Hanno Becker0271f962018-08-16 13:23:47 +01009300 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009301 ssl_buffering_free_slot( ssl, offset );
9302}
9303
9304static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9305 uint8_t slot )
9306{
9307 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9308 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009309
9310 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9311 return;
9312
Hanno Beckere605b192018-08-21 15:59:07 +01009313 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009314 {
Hanno Beckere605b192018-08-21 15:59:07 +01009315 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009316 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009317 mbedtls_free( hs_buf->data );
9318 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009319 }
9320}
9321
9322#endif /* MBEDTLS_SSL_PROTO_DTLS */
9323
Gilles Peskine9b562d52018-04-25 20:32:43 +02009324void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009325{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009326 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9327
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009328 if( handshake == NULL )
9329 return;
9330
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009331#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9332 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9333 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009334 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009335 handshake->async_in_progress = 0;
9336 }
9337#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9338
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009339#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9340 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9341 mbedtls_md5_free( &handshake->fin_md5 );
9342 mbedtls_sha1_free( &handshake->fin_sha1 );
9343#endif
9344#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9345#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009346#if defined(MBEDTLS_USE_PSA_CRYPTO)
9347 psa_hash_abort( &handshake->fin_sha256_psa );
9348#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009349 mbedtls_sha256_free( &handshake->fin_sha256 );
9350#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009351#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009352#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009353#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009354 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009355#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009356 mbedtls_sha512_free( &handshake->fin_sha512 );
9357#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009358#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009359#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009361#if defined(MBEDTLS_DHM_C)
9362 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009363#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009364#if defined(MBEDTLS_ECDH_C)
9365 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009366#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009367#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009368 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009369#if defined(MBEDTLS_SSL_CLI_C)
9370 mbedtls_free( handshake->ecjpake_cache );
9371 handshake->ecjpake_cache = NULL;
9372 handshake->ecjpake_cache_len = 0;
9373#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009374#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009375
Janos Follath4ae5c292016-02-10 11:27:43 +00009376#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9377 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009378 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009379 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009380#endif
9381
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009382#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9383 if( handshake->psk != NULL )
9384 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009385 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009386 mbedtls_free( handshake->psk );
9387 }
9388#endif
9389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9391 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009392 /*
9393 * Free only the linked list wrapper, not the keys themselves
9394 * since the belong to the SNI callback
9395 */
9396 if( handshake->sni_key_cert != NULL )
9397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009398 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009399
9400 while( cur != NULL )
9401 {
9402 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009403 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009404 cur = next;
9405 }
9406 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009407#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009408
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009409#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009410 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009411#endif
9412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009413#if defined(MBEDTLS_SSL_PROTO_DTLS)
9414 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009415 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009416 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009417#endif
9418
Hanno Becker4a63ed42019-01-08 11:39:35 +00009419#if defined(MBEDTLS_ECDH_C) && \
9420 defined(MBEDTLS_USE_PSA_CRYPTO)
9421 psa_destroy_key( handshake->ecdh_psa_privkey );
9422#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9423
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009424 mbedtls_platform_zeroize( handshake,
9425 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009426}
9427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009429{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009430 if( session == NULL )
9431 return;
9432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009433#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009434 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009435#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009436
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009437#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009438 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009439#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009440
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009441 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009442}
9443
Paul Bakker5121ce52009-01-03 21:22:43 +00009444/*
9445 * Free an SSL context
9446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009447void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009448{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009449 if( ssl == NULL )
9450 return;
9451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009453
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009454 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009455 {
Angus Grattond8213d02016-05-25 20:56:48 +10009456 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009457 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009458 }
9459
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009460 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009461 {
Angus Grattond8213d02016-05-25 20:56:48 +10009462 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009463 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009464 }
9465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009467 if( ssl->compress_buf != NULL )
9468 {
Angus Grattond8213d02016-05-25 20:56:48 +10009469 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009470 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009471 }
9472#endif
9473
Paul Bakker48916f92012-09-16 19:57:18 +00009474 if( ssl->transform )
9475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009476 mbedtls_ssl_transform_free( ssl->transform );
9477 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009478 }
9479
9480 if( ssl->handshake )
9481 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009482 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009483 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9484 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486 mbedtls_free( ssl->handshake );
9487 mbedtls_free( ssl->transform_negotiate );
9488 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009489 }
9490
Paul Bakkerc0463502013-02-14 11:19:38 +01009491 if( ssl->session )
9492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009493 mbedtls_ssl_session_free( ssl->session );
9494 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009495 }
9496
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009497#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009498 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009499 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009500 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009502 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009503#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009505#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9506 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9509 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009510 }
9511#endif
9512
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009513#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009514 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009515#endif
9516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009518
Paul Bakker86f04f42013-02-14 11:20:09 +01009519 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009520 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009521}
9522
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009523/*
9524 * Initialze mbedtls_ssl_config
9525 */
9526void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9527{
9528 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9529}
9530
Simon Butcherc97b6972015-12-27 23:48:17 +00009531#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009532static int ssl_preset_default_hashes[] = {
9533#if defined(MBEDTLS_SHA512_C)
9534 MBEDTLS_MD_SHA512,
9535 MBEDTLS_MD_SHA384,
9536#endif
9537#if defined(MBEDTLS_SHA256_C)
9538 MBEDTLS_MD_SHA256,
9539 MBEDTLS_MD_SHA224,
9540#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009541#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009542 MBEDTLS_MD_SHA1,
9543#endif
9544 MBEDTLS_MD_NONE
9545};
Simon Butcherc97b6972015-12-27 23:48:17 +00009546#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009547
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009548static int ssl_preset_suiteb_ciphersuites[] = {
9549 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9550 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9551 0
9552};
9553
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009554#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009555static int ssl_preset_suiteb_hashes[] = {
9556 MBEDTLS_MD_SHA256,
9557 MBEDTLS_MD_SHA384,
9558 MBEDTLS_MD_NONE
9559};
9560#endif
9561
9562#if defined(MBEDTLS_ECP_C)
9563static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9564 MBEDTLS_ECP_DP_SECP256R1,
9565 MBEDTLS_ECP_DP_SECP384R1,
9566 MBEDTLS_ECP_DP_NONE
9567};
9568#endif
9569
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009570/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009571 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009572 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009573int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009574 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009575{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009576#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009577 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009578#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009579
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009580 /* Use the functions here so that they are covered in tests,
9581 * but otherwise access member directly for efficiency */
9582 mbedtls_ssl_conf_endpoint( conf, endpoint );
9583 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009584
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009585 /*
9586 * Things that are common to all presets
9587 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009588#if defined(MBEDTLS_SSL_CLI_C)
9589 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9590 {
9591 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9592#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9593 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9594#endif
9595 }
9596#endif
9597
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009598#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009599 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009600#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009601
9602#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9603 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9604#endif
9605
9606#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9607 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9608#endif
9609
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009610#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9611 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9612#endif
9613
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009614#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009615 conf->f_cookie_write = ssl_cookie_write_dummy;
9616 conf->f_cookie_check = ssl_cookie_check_dummy;
9617#endif
9618
9619#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9620 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9621#endif
9622
Janos Follath088ce432017-04-10 12:42:31 +01009623#if defined(MBEDTLS_SSL_SRV_C)
9624 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9625#endif
9626
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009627#if defined(MBEDTLS_SSL_PROTO_DTLS)
9628 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9629 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9630#endif
9631
9632#if defined(MBEDTLS_SSL_RENEGOTIATION)
9633 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009634 memset( conf->renego_period, 0x00, 2 );
9635 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009636#endif
9637
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009638#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9639 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9640 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009641 const unsigned char dhm_p[] =
9642 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9643 const unsigned char dhm_g[] =
9644 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9645
Hanno Beckera90658f2017-10-04 15:29:08 +01009646 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9647 dhm_p, sizeof( dhm_p ),
9648 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009649 {
9650 return( ret );
9651 }
9652 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009653#endif
9654
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009655 /*
9656 * Preset-specific defaults
9657 */
9658 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009659 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009660 /*
9661 * NSA Suite B
9662 */
9663 case MBEDTLS_SSL_PRESET_SUITEB:
9664 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9665 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9666 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9667 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9668
9669 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9670 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9671 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9672 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9673 ssl_preset_suiteb_ciphersuites;
9674
9675#if defined(MBEDTLS_X509_CRT_PARSE_C)
9676 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009677#endif
9678
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009679#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009680 conf->sig_hashes = ssl_preset_suiteb_hashes;
9681#endif
9682
9683#if defined(MBEDTLS_ECP_C)
9684 conf->curve_list = ssl_preset_suiteb_curves;
9685#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009686 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009687
9688 /*
9689 * Default
9690 */
9691 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009692 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9693 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9694 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9695 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9696 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9697 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9698 MBEDTLS_SSL_MIN_MINOR_VERSION :
9699 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009700 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9701 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9702
9703#if defined(MBEDTLS_SSL_PROTO_DTLS)
9704 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9705 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9706#endif
9707
9708 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9709 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9710 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9711 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9712 mbedtls_ssl_list_ciphersuites();
9713
9714#if defined(MBEDTLS_X509_CRT_PARSE_C)
9715 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9716#endif
9717
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009718#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009719 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009720#endif
9721
9722#if defined(MBEDTLS_ECP_C)
9723 conf->curve_list = mbedtls_ecp_grp_id_list();
9724#endif
9725
9726#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9727 conf->dhm_min_bitlen = 1024;
9728#endif
9729 }
9730
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009731 return( 0 );
9732}
9733
9734/*
9735 * Free mbedtls_ssl_config
9736 */
9737void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9738{
9739#if defined(MBEDTLS_DHM_C)
9740 mbedtls_mpi_free( &conf->dhm_P );
9741 mbedtls_mpi_free( &conf->dhm_G );
9742#endif
9743
9744#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9745 if( conf->psk != NULL )
9746 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009747 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009748 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009749 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009750 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009751 }
9752
9753 if( conf->psk_identity != NULL )
9754 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009755 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009756 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009757 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009758 conf->psk_identity_len = 0;
9759 }
9760#endif
9761
9762#if defined(MBEDTLS_X509_CRT_PARSE_C)
9763 ssl_key_cert_free( conf->key_cert );
9764#endif
9765
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009766 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009767}
9768
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009769#if defined(MBEDTLS_PK_C) && \
9770 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009771/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009772 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009773 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009775{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009776#if defined(MBEDTLS_RSA_C)
9777 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9778 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009779#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009780#if defined(MBEDTLS_ECDSA_C)
9781 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9782 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009783#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009784 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009785}
9786
Hanno Becker7e5437a2017-04-28 17:15:26 +01009787unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9788{
9789 switch( type ) {
9790 case MBEDTLS_PK_RSA:
9791 return( MBEDTLS_SSL_SIG_RSA );
9792 case MBEDTLS_PK_ECDSA:
9793 case MBEDTLS_PK_ECKEY:
9794 return( MBEDTLS_SSL_SIG_ECDSA );
9795 default:
9796 return( MBEDTLS_SSL_SIG_ANON );
9797 }
9798}
9799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009800mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009801{
9802 switch( sig )
9803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009804#if defined(MBEDTLS_RSA_C)
9805 case MBEDTLS_SSL_SIG_RSA:
9806 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009807#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808#if defined(MBEDTLS_ECDSA_C)
9809 case MBEDTLS_SSL_SIG_ECDSA:
9810 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009811#endif
9812 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009813 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009814 }
9815}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009816#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009817
Hanno Becker7e5437a2017-04-28 17:15:26 +01009818#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9819 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9820
9821/* Find an entry in a signature-hash set matching a given hash algorithm. */
9822mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9823 mbedtls_pk_type_t sig_alg )
9824{
9825 switch( sig_alg )
9826 {
9827 case MBEDTLS_PK_RSA:
9828 return( set->rsa );
9829 case MBEDTLS_PK_ECDSA:
9830 return( set->ecdsa );
9831 default:
9832 return( MBEDTLS_MD_NONE );
9833 }
9834}
9835
9836/* Add a signature-hash-pair to a signature-hash set */
9837void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9838 mbedtls_pk_type_t sig_alg,
9839 mbedtls_md_type_t md_alg )
9840{
9841 switch( sig_alg )
9842 {
9843 case MBEDTLS_PK_RSA:
9844 if( set->rsa == MBEDTLS_MD_NONE )
9845 set->rsa = md_alg;
9846 break;
9847
9848 case MBEDTLS_PK_ECDSA:
9849 if( set->ecdsa == MBEDTLS_MD_NONE )
9850 set->ecdsa = md_alg;
9851 break;
9852
9853 default:
9854 break;
9855 }
9856}
9857
9858/* Allow exactly one hash algorithm for each signature. */
9859void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9860 mbedtls_md_type_t md_alg )
9861{
9862 set->rsa = md_alg;
9863 set->ecdsa = md_alg;
9864}
9865
9866#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9867 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9868
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009869/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009870 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009872mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009873{
9874 switch( hash )
9875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009876#if defined(MBEDTLS_MD5_C)
9877 case MBEDTLS_SSL_HASH_MD5:
9878 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009879#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009880#if defined(MBEDTLS_SHA1_C)
9881 case MBEDTLS_SSL_HASH_SHA1:
9882 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009883#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009884#if defined(MBEDTLS_SHA256_C)
9885 case MBEDTLS_SSL_HASH_SHA224:
9886 return( MBEDTLS_MD_SHA224 );
9887 case MBEDTLS_SSL_HASH_SHA256:
9888 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009889#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009890#if defined(MBEDTLS_SHA512_C)
9891 case MBEDTLS_SSL_HASH_SHA384:
9892 return( MBEDTLS_MD_SHA384 );
9893 case MBEDTLS_SSL_HASH_SHA512:
9894 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009895#endif
9896 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009897 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009898 }
9899}
9900
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009901/*
9902 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9903 */
9904unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9905{
9906 switch( md )
9907 {
9908#if defined(MBEDTLS_MD5_C)
9909 case MBEDTLS_MD_MD5:
9910 return( MBEDTLS_SSL_HASH_MD5 );
9911#endif
9912#if defined(MBEDTLS_SHA1_C)
9913 case MBEDTLS_MD_SHA1:
9914 return( MBEDTLS_SSL_HASH_SHA1 );
9915#endif
9916#if defined(MBEDTLS_SHA256_C)
9917 case MBEDTLS_MD_SHA224:
9918 return( MBEDTLS_SSL_HASH_SHA224 );
9919 case MBEDTLS_MD_SHA256:
9920 return( MBEDTLS_SSL_HASH_SHA256 );
9921#endif
9922#if defined(MBEDTLS_SHA512_C)
9923 case MBEDTLS_MD_SHA384:
9924 return( MBEDTLS_SSL_HASH_SHA384 );
9925 case MBEDTLS_MD_SHA512:
9926 return( MBEDTLS_SSL_HASH_SHA512 );
9927#endif
9928 default:
9929 return( MBEDTLS_SSL_HASH_NONE );
9930 }
9931}
9932
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009933#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009934/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009935 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009936 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009937 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009938int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009939{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009940 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009941
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009942 if( ssl->conf->curve_list == NULL )
9943 return( -1 );
9944
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009945 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009946 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009947 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009948
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009949 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009950}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009951#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009952
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009953#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009954/*
9955 * Check if a hash proposed by the peer is in our list.
9956 * Return 0 if we're willing to use it, -1 otherwise.
9957 */
9958int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9959 mbedtls_md_type_t md )
9960{
9961 const int *cur;
9962
9963 if( ssl->conf->sig_hashes == NULL )
9964 return( -1 );
9965
9966 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9967 if( *cur == (int) md )
9968 return( 0 );
9969
9970 return( -1 );
9971}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009972#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009974#if defined(MBEDTLS_X509_CRT_PARSE_C)
9975int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9976 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009977 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009978 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009979{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009980 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009981#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009982 int usage = 0;
9983#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009984#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009985 const char *ext_oid;
9986 size_t ext_len;
9987#endif
9988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009989#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9990 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009991 ((void) cert);
9992 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009993 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009994#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009996#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9997 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009998 {
9999 /* Server part of the key exchange */
10000 switch( ciphersuite->key_exchange )
10001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010002 case MBEDTLS_KEY_EXCHANGE_RSA:
10003 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010004 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010005 break;
10006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010007 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10008 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10009 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10010 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010011 break;
10012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010013 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10014 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010015 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010016 break;
10017
10018 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010019 case MBEDTLS_KEY_EXCHANGE_NONE:
10020 case MBEDTLS_KEY_EXCHANGE_PSK:
10021 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10022 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010023 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010024 usage = 0;
10025 }
10026 }
10027 else
10028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010029 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10030 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010031 }
10032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010033 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010034 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010035 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010036 ret = -1;
10037 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010038#else
10039 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010040#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010042#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10043 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010045 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10046 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010047 }
10048 else
10049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010050 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10051 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010052 }
10053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010054 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010055 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010056 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010057 ret = -1;
10058 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010059#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010060
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010061 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010062}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010063#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010064
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010065/*
10066 * Convert version numbers to/from wire format
10067 * and, for DTLS, to/from TLS equivalent.
10068 *
10069 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010070 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010071 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10072 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10073 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010074void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010075 unsigned char ver[2] )
10076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010077#if defined(MBEDTLS_SSL_PROTO_DTLS)
10078 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010081 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10082
10083 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10084 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10085 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010086 else
10087#else
10088 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010089#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010090 {
10091 ver[0] = (unsigned char) major;
10092 ver[1] = (unsigned char) minor;
10093 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010094}
10095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010096void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010097 const unsigned char ver[2] )
10098{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099#if defined(MBEDTLS_SSL_PROTO_DTLS)
10100 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010101 {
10102 *major = 255 - ver[0] + 2;
10103 *minor = 255 - ver[1] + 1;
10104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010105 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010106 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10107 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010108 else
10109#else
10110 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010111#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010112 {
10113 *major = ver[0];
10114 *minor = ver[1];
10115 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010116}
10117
Simon Butcher99000142016-10-13 17:21:01 +010010118int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10119{
10120#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10121 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10122 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10123
10124 switch( md )
10125 {
10126#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10127#if defined(MBEDTLS_MD5_C)
10128 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010129 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010130#endif
10131#if defined(MBEDTLS_SHA1_C)
10132 case MBEDTLS_SSL_HASH_SHA1:
10133 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10134 break;
10135#endif
10136#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10137#if defined(MBEDTLS_SHA512_C)
10138 case MBEDTLS_SSL_HASH_SHA384:
10139 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10140 break;
10141#endif
10142#if defined(MBEDTLS_SHA256_C)
10143 case MBEDTLS_SSL_HASH_SHA256:
10144 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10145 break;
10146#endif
10147 default:
10148 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10149 }
10150
10151 return 0;
10152#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10153 (void) ssl;
10154 (void) md;
10155
10156 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10157#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10158}
10159
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010160#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10161 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10162int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10163 unsigned char *output,
10164 unsigned char *data, size_t data_len )
10165{
10166 int ret = 0;
10167 mbedtls_md5_context mbedtls_md5;
10168 mbedtls_sha1_context mbedtls_sha1;
10169
10170 mbedtls_md5_init( &mbedtls_md5 );
10171 mbedtls_sha1_init( &mbedtls_sha1 );
10172
10173 /*
10174 * digitally-signed struct {
10175 * opaque md5_hash[16];
10176 * opaque sha_hash[20];
10177 * };
10178 *
10179 * md5_hash
10180 * MD5(ClientHello.random + ServerHello.random
10181 * + ServerParams);
10182 * sha_hash
10183 * SHA(ClientHello.random + ServerHello.random
10184 * + ServerParams);
10185 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010186 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010187 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010189 goto exit;
10190 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010191 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010192 ssl->handshake->randbytes, 64 ) ) != 0 )
10193 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010194 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010195 goto exit;
10196 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010197 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010198 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010199 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010200 goto exit;
10201 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010202 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010203 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010204 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010205 goto exit;
10206 }
10207
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010208 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010209 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010210 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010211 goto exit;
10212 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010213 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010214 ssl->handshake->randbytes, 64 ) ) != 0 )
10215 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010216 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010217 goto exit;
10218 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010219 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010220 data_len ) ) != 0 )
10221 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010222 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010223 goto exit;
10224 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010225 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010226 output + 16 ) ) != 0 )
10227 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010229 goto exit;
10230 }
10231
10232exit:
10233 mbedtls_md5_free( &mbedtls_md5 );
10234 mbedtls_sha1_free( &mbedtls_sha1 );
10235
10236 if( ret != 0 )
10237 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10238 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10239
10240 return( ret );
10241
10242}
10243#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10244 MBEDTLS_SSL_PROTO_TLS1_1 */
10245
10246#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10247 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010248
10249#if defined(MBEDTLS_USE_PSA_CRYPTO)
10250int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10251 unsigned char *hash, size_t *hashlen,
10252 unsigned char *data, size_t data_len,
10253 mbedtls_md_type_t md_alg )
10254{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010255 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010256 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010257 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10258
Andrzej Kurek5615dab2019-01-16 05:26:25 -050010259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010260
10261 if( ( status = psa_hash_setup( &hash_operation,
10262 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010263 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010264 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010265 goto exit;
10266 }
10267
Andrzej Kurek814feff2019-01-14 04:35:19 -050010268 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10269 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010270 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010271 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010272 goto exit;
10273 }
10274
Andrzej Kurek814feff2019-01-14 04:35:19 -050010275 if( ( status = psa_hash_update( &hash_operation,
10276 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010277 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010278 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010279 goto exit;
10280 }
10281
Andrzej Kurek814feff2019-01-14 04:35:19 -050010282 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10283 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010284 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010285 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010286 goto exit;
10287 }
10288
10289exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010290 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010291 {
10292 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10293 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010294 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010295 {
10296 case PSA_ERROR_NOT_SUPPORTED:
10297 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010298 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010299 case PSA_ERROR_BUFFER_TOO_SMALL:
10300 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10301 case PSA_ERROR_INSUFFICIENT_MEMORY:
10302 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10303 default:
10304 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10305 }
10306 }
10307 return( 0 );
10308}
10309
10310#else
10311
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010312int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010313 unsigned char *hash, size_t *hashlen,
10314 unsigned char *data, size_t data_len,
10315 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010316{
10317 int ret = 0;
10318 mbedtls_md_context_t ctx;
10319 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010320 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010321
Andrzej Kurek5615dab2019-01-16 05:26:25 -050010322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010323
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010324 mbedtls_md_init( &ctx );
10325
10326 /*
10327 * digitally-signed struct {
10328 * opaque client_random[32];
10329 * opaque server_random[32];
10330 * ServerDHParams params;
10331 * };
10332 */
10333 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10334 {
10335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10336 goto exit;
10337 }
10338 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10339 {
10340 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10341 goto exit;
10342 }
10343 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10344 {
10345 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10346 goto exit;
10347 }
10348 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10349 {
10350 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10351 goto exit;
10352 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010353 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010354 {
10355 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10356 goto exit;
10357 }
10358
10359exit:
10360 mbedtls_md_free( &ctx );
10361
10362 if( ret != 0 )
10363 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10364 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10365
10366 return( ret );
10367}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010368#endif /* MBEDTLS_USE_PSA_CRYPTO */
10369
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010370#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10371 MBEDTLS_SSL_PROTO_TLS1_2 */
10372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010373#endif /* MBEDTLS_SSL_TLS_C */