blob: 6239d67c00c3a609313bdca62e3efdf60561b70c [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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005560#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
5561 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
5562 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
5563 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
5564 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
5565 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
5566 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005567/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005568int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005569{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005574 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5575 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005576 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5577 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005580 ssl->state++;
5581 return( 0 );
5582 }
5583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5585 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005586}
5587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005589{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005590 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5595 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005596 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5597 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005600 ssl->state++;
5601 return( 0 );
5602 }
5603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5605 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005606}
Gilles Peskinef9828522017-05-03 12:28:43 +02005607
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005608#else
Gilles Peskinef9828522017-05-03 12:28:43 +02005609/* Some certificate support -> implement write and parse */
5610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005611int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005612{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005614 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615 const mbedtls_x509_crt *crt;
5616 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005620 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5621 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005622 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5623 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005626 ssl->state++;
5627 return( 0 );
5628 }
5629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005630#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005631 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005632 {
5633 if( ssl->client_auth == 0 )
5634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005636 ssl->state++;
5637 return( 0 );
5638 }
5639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005641 /*
5642 * If using SSLv3 and got no cert, send an Alert message
5643 * (otherwise an empty Certificate message will be sent).
5644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5646 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005647 {
5648 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5650 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5651 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005654 goto write_msg;
5655 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005656#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005657 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658#endif /* MBEDTLS_SSL_CLI_C */
5659#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005660 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005662 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5665 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005666 }
5667 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005668#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005670 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005671
5672 /*
5673 * 0 . 0 handshake type
5674 * 1 . 3 handshake length
5675 * 4 . 6 length of all certs
5676 * 7 . 9 length of cert. 1
5677 * 10 . n-1 peer certificate
5678 * n . n+2 length of cert. 2
5679 * n+3 . ... upper level cert, etc.
5680 */
5681 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005682 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005683
Paul Bakker29087132010-03-21 21:03:34 +00005684 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005685 {
5686 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005687 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005690 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005692 }
5693
5694 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5695 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5696 ssl->out_msg[i + 2] = (unsigned char)( n );
5697
5698 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5699 i += n; crt = crt->next;
5700 }
5701
5702 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5703 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5704 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5705
5706 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5708 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005709
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005710#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005711write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005712#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005713
5714 ssl->state++;
5715
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005716 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005717 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005718 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005719 return( ret );
5720 }
5721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005723
Paul Bakkered27a042013-04-18 22:46:23 +02005724 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005725}
5726
Hanno Becker84879e32019-01-31 07:44:03 +00005727#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005728static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5729 unsigned char *crt_buf,
5730 size_t crt_buf_len )
5731{
5732 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5733
5734 if( peer_crt == NULL )
5735 return( -1 );
5736
5737 if( peer_crt->raw.len != crt_buf_len )
5738 return( -1 );
5739
Hanno Becker46f34d02019-02-08 14:00:04 +00005740 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005741}
Hanno Becker84879e32019-01-31 07:44:03 +00005742#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005743
Hanno Becker1294a0b2019-02-05 12:38:15 +00005744static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5745{
5746 if( session->peer_cert != NULL )
5747 {
5748 mbedtls_x509_crt_free( session->peer_cert );
5749 mbedtls_free( session->peer_cert );
5750 session->peer_cert = NULL;
5751 }
5752}
5753
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005754/*
5755 * Once the certificate message is read, parse it into a cert chain and
5756 * perform basic checks, but leave actual verification to the caller
5757 */
5758static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005759{
Hanno Beckera028c5b2019-02-05 12:38:45 +00005760 int ret, crt_cnt=0;
Paul Bakker23986e52011-04-24 08:57:21 +00005761 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005762 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005763
Paul Bakker5121ce52009-01-03 21:22:43 +00005764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005765 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005768 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5769 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005770 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005771 }
5772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5774 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005783
Paul Bakker5121ce52009-01-03 21:22:43 +00005784 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005786 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005787 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005788
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005789 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005790 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005793 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5794 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005795 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005796 }
5797
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005798 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5799 i += 3;
5800
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005801 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005802 while( i < ssl->in_hslen )
5803 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005804 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02005805 if ( i + 3 > ssl->in_hslen ) {
5806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005807 mbedtls_ssl_send_alert_message( ssl,
5808 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5809 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02005810 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5811 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005812 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5813 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005814 if( ssl->in_msg[i] != 0 )
5815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005817 mbedtls_ssl_send_alert_message( ssl,
5818 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5819 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005820 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005821 }
5822
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005823 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005824 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5825 | (unsigned int) ssl->in_msg[i + 2];
5826 i += 3;
5827
5828 if( n < 128 || i + n > ssl->in_hslen )
5829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005831 mbedtls_ssl_send_alert_message( ssl,
5832 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5833 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005834 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005835 }
5836
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005837 /* Check if we're handling the first CRT in the chain. */
Hanno Beckera028c5b2019-02-05 12:38:45 +00005838 if( crt_cnt++ == 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005839 {
Hanno Becker46f34d02019-02-08 14:00:04 +00005840 /* During client-side renegotiation, check that the server's
5841 * end-CRTs hasn't changed compared to the initial handshake,
5842 * mitigating the triple handshake attack. On success, reuse
5843 * the original end-CRT instead of parsing it again. */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005844#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5845 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5846 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
5847 {
5848 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005849 if( ssl_check_peer_crt_unchanged( ssl,
5850 &ssl->in_msg[i],
5851 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005852 {
5853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005854 mbedtls_ssl_send_alert_message( ssl,
5855 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5856 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005857 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5858 }
5859
Hanno Becker60848e62019-02-05 15:06:15 +00005860 /* Now we can safely free the original chain. */
Hanno Becker1294a0b2019-02-05 12:38:15 +00005861 ssl_clear_peer_cert( ssl->session );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005862
Hanno Becker60848e62019-02-05 15:06:15 +00005863 /* Intentional fallthrough. */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005864 }
5865#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5866
5867 /* Outside of client-side renegotiation, create a fresh X.509 CRT
5868 * instance to parse the end-CRT into. */
5869
5870 ssl->session_negotiate->peer_cert =
5871 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
5872 if( ssl->session_negotiate->peer_cert == NULL )
5873 {
5874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
5875 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005876 mbedtls_ssl_send_alert_message( ssl,
5877 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5878 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005879 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
5880 }
5881
5882 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
5883
5884 /* Intentional fall through */
5885 }
5886
5887 /* Parse the next certificate in the chain. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005889 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005890 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005891 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005892 case 0: /*ok*/
5893 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5894 /* Ignore certificate with an unknown algorithm: maybe a
5895 prior certificate was already trusted. */
5896 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005897
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005898 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5899 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5900 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005901
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005902 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5903 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5904 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005905
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005906 default:
5907 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5908 crt_parse_der_failed:
5909 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
5910 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
5911 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005912 }
5913
5914 i += n;
5915 }
5916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005917 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005918 return( 0 );
5919}
5920
Hanno Becker4a55f632019-02-05 12:49:06 +00005921#if defined(MBEDTLS_SSL_SRV_C)
5922static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
5923{
5924 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5925 return( -1 );
5926
5927#if defined(MBEDTLS_SSL_PROTO_SSL3)
5928 /*
5929 * Check if the client sent an empty certificate
5930 */
5931 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
5932 {
5933 if( ssl->in_msglen == 2 &&
5934 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5935 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5936 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5937 {
5938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
5939 return( 0 );
5940 }
5941
5942 return( -1 );
5943 }
5944#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5945
5946#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5947 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5948 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5949 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5950 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5951 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
5952 {
5953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
5954 return( 0 );
5955 }
5956
5957 return( -1 );
5958#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5959 MBEDTLS_SSL_PROTO_TLS1_2 */
5960}
5961#endif /* MBEDTLS_SSL_SRV_C */
5962
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005963int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
5964{
5965 int ret;
5966 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
5967 ssl->transform_negotiate->ciphersuite_info;
5968#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5969 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
5970 ? ssl->handshake->sni_authmode
5971 : ssl->conf->authmode;
5972#else
5973 const int authmode = ssl->conf->authmode;
5974#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02005975 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005976
5977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
5978
5979 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5980 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
5981 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5982 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
5983 {
5984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5985 ssl->state++;
5986 return( 0 );
5987 }
5988
5989#if defined(MBEDTLS_SSL_SRV_C)
5990 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5991 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5992 {
5993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
5994 ssl->state++;
5995 return( 0 );
5996 }
5997
5998 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5999 authmode == MBEDTLS_SSL_VERIFY_NONE )
6000 {
6001 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006003
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006004 ssl->state++;
6005 return( 0 );
6006 }
6007#endif
6008
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006009#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6010 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006011 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006012 {
6013 goto crt_verify;
6014 }
6015#endif
6016
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006017 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006018 {
6019 /* mbedtls_ssl_read_record may have sent an alert already. We
6020 let it decide whether to alert. */
6021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6022 return( ret );
6023 }
6024
Hanno Becker4a55f632019-02-05 12:49:06 +00006025#if defined(MBEDTLS_SSL_SRV_C)
6026 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6027 {
6028 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
6029 ssl->state++;
6030
6031 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
6032 return( 0 );
6033
6034 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
6035 }
6036#endif /* MBEDTLS_SSL_SRV_C */
6037
Hanno Becker7a955a02019-02-05 13:08:01 +00006038 /* In case we tried to reuse a session but it failed. */
6039 ssl_clear_peer_cert( ssl->session_negotiate );
6040
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006041 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
6042 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006043 ssl->state++;
6044 return( ret );
6045 }
6046
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006047#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6048 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006049 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006050
6051crt_verify:
6052 if( ssl->handshake->ecrs_enabled)
6053 rs_ctx = &ssl->handshake->ecrs_ctx;
6054#endif
6055
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006056 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006057 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006058 mbedtls_x509_crt *ca_chain;
6059 mbedtls_x509_crl *ca_crl;
6060
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006061#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006062 if( ssl->handshake->sni_ca_chain != NULL )
6063 {
6064 ca_chain = ssl->handshake->sni_ca_chain;
6065 ca_crl = ssl->handshake->sni_ca_crl;
6066 }
6067 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006068#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006069 {
6070 ca_chain = ssl->conf->ca_chain;
6071 ca_crl = ssl->conf->ca_crl;
6072 }
6073
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006074 /*
6075 * Main check: verify certificate
6076 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006077 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006078 ssl->session_negotiate->peer_cert,
6079 ca_chain, ca_crl,
6080 ssl->conf->cert_profile,
6081 ssl->hostname,
6082 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006083 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00006084
6085 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006087 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006088 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006089
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006090#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6091 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006092 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006093#endif
6094
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006095 /*
6096 * Secondary checks: always done, but change 'ret' only if it was 0
6097 */
6098
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006099#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006101 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006102
6103 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006104 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006105 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006106 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006107 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006110 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006111 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006112 }
6113 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006114#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006116 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006117 ciphersuite_info,
6118 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006119 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006122 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006123 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006124 }
6125
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006126 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6127 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6128 * with details encoded in the verification flags. All other kinds
6129 * of error codes, including those from the user provided f_vrfy
6130 * functions, are treated as fatal and lead to a failure of
6131 * ssl_parse_certificate even if verification was optional. */
6132 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6133 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6134 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6135 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006136 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006137 }
6138
6139 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6140 {
6141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6142 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6143 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006144
6145 if( ret != 0 )
6146 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006147 uint8_t alert;
6148
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006149 /* The certificate may have been rejected for several reasons.
6150 Pick one and send the corresponding alert. Which alert to send
6151 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006152 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6153 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6154 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6155 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6156 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6157 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6158 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6159 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6160 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6161 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6162 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6163 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6164 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6165 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6166 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6167 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6168 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6169 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6170 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6171 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006172 else
6173 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006174 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6175 alert );
6176 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006177
Hanno Beckere6706e62017-05-15 16:05:15 +01006178#if defined(MBEDTLS_DEBUG_C)
6179 if( ssl->session_negotiate->verify_result != 0 )
6180 {
6181 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6182 ssl->session_negotiate->verify_result ) );
6183 }
6184 else
6185 {
6186 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6187 }
6188#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006189 }
6190
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006191 ssl->state++;
6192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006194
6195 return( ret );
6196}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006197#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
6198 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
6199 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
6200 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
6201 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
6202 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
6203 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006205int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006206{
6207 int ret;
6208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006211 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006212 ssl->out_msglen = 1;
6213 ssl->out_msg[0] = 1;
6214
Paul Bakker5121ce52009-01-03 21:22:43 +00006215 ssl->state++;
6216
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006217 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006218 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006219 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006220 return( ret );
6221 }
6222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006224
6225 return( 0 );
6226}
6227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006228int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006229{
6230 int ret;
6231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006233
Hanno Becker327c93b2018-08-15 13:56:18 +01006234 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006236 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006237 return( ret );
6238 }
6239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006240 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006243 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6244 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006245 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006246 }
6247
Hanno Beckere678eaa2018-08-21 14:57:46 +01006248 /* CCS records are only accepted if they have length 1 and content '1',
6249 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006250
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006251 /*
6252 * Switch to our negotiated transform and session parameters for inbound
6253 * data.
6254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006255 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006256 ssl->transform_in = ssl->transform_negotiate;
6257 ssl->session_in = ssl->session_negotiate;
6258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006260 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006263 ssl_dtls_replay_reset( ssl );
6264#endif
6265
6266 /* Increment epoch */
6267 if( ++ssl->in_epoch == 0 )
6268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006270 /* This is highly unlikely to happen for legitimate reasons, so
6271 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006272 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006273 }
6274 }
6275 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006276#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006277 memset( ssl->in_ctr, 0, 8 );
6278
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006279 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006281#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6282 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006286 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006287 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6288 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006290 }
6291 }
6292#endif
6293
Paul Bakker5121ce52009-01-03 21:22:43 +00006294 ssl->state++;
6295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006297
6298 return( 0 );
6299}
6300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006301void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6302 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006303{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006304 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6307 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6308 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006309 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006310 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006311#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6313#if defined(MBEDTLS_SHA512_C)
6314 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006315 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6316 else
6317#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318#if defined(MBEDTLS_SHA256_C)
6319 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006320 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006321 else
6322#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006326 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006327 }
Paul Bakker380da532012-04-18 16:10:25 +00006328}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006330void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006331{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006332#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6333 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006334 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6335 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006336#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6338#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006339#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006340 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006341 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6342#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006343 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006344#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006345#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006346#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006347#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006348 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006349 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006350#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006351 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006352#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006353#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006354#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006355}
6356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006357static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006358 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006359{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006360#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6361 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006362 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6363 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006364#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006365#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6366#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006367#if defined(MBEDTLS_USE_PSA_CRYPTO)
6368 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6369#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006370 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006371#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006372#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006373#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006374#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006375 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006376#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006377 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006378#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006379#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006380#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006381}
6382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6384 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6385static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006386 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006387{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006388 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6389 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006390}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006391#endif
Paul Bakker380da532012-04-18 16:10:25 +00006392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006393#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6394#if defined(MBEDTLS_SHA256_C)
6395static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006396 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006397{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006398#if defined(MBEDTLS_USE_PSA_CRYPTO)
6399 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6400#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006401 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006402#endif
Paul Bakker380da532012-04-18 16:10:25 +00006403}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006404#endif
Paul Bakker380da532012-04-18 16:10:25 +00006405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006406#if defined(MBEDTLS_SHA512_C)
6407static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006408 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006409{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006410#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006411 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006412#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006413 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006414#endif
Paul Bakker380da532012-04-18 16:10:25 +00006415}
Paul Bakker769075d2012-11-24 11:26:46 +01006416#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006417#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006419#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006420static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006421 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006422{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006423 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006424 mbedtls_md5_context md5;
6425 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006426
Paul Bakker5121ce52009-01-03 21:22:43 +00006427 unsigned char padbuf[48];
6428 unsigned char md5sum[16];
6429 unsigned char sha1sum[20];
6430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006431 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006432 if( !session )
6433 session = ssl->session;
6434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006436
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006437 mbedtls_md5_init( &md5 );
6438 mbedtls_sha1_init( &sha1 );
6439
6440 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6441 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006442
6443 /*
6444 * SSLv3:
6445 * hash =
6446 * MD5( master + pad2 +
6447 * MD5( handshake + sender + master + pad1 ) )
6448 * + SHA1( master + pad2 +
6449 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006450 */
6451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006453 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6454 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006455#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006457#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006458 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6459 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006460#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006462 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006463 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006464
Paul Bakker1ef83d62012-04-11 12:09:53 +00006465 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006466
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006467 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6468 mbedtls_md5_update_ret( &md5, session->master, 48 );
6469 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6470 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006471
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006472 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6473 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6474 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6475 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006476
Paul Bakker1ef83d62012-04-11 12:09:53 +00006477 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006478
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006479 mbedtls_md5_starts_ret( &md5 );
6480 mbedtls_md5_update_ret( &md5, session->master, 48 );
6481 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6482 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6483 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006484
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006485 mbedtls_sha1_starts_ret( &sha1 );
6486 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6487 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6488 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6489 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006491 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006492
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006493 mbedtls_md5_free( &md5 );
6494 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006495
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006496 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6497 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6498 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006501}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006502#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006504#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006505static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006507{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006508 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006509 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006510 mbedtls_md5_context md5;
6511 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006512 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006514 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006515 if( !session )
6516 session = ssl->session;
6517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006519
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006520 mbedtls_md5_init( &md5 );
6521 mbedtls_sha1_init( &sha1 );
6522
6523 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6524 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006525
Paul Bakker1ef83d62012-04-11 12:09:53 +00006526 /*
6527 * TLSv1:
6528 * hash = PRF( master, finished_label,
6529 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6530 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006532#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006533 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6534 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006535#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006537#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006538 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6539 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006540#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006542 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006543 ? "client finished"
6544 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006545
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006546 mbedtls_md5_finish_ret( &md5, padbuf );
6547 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006548
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006549 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006550 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006553
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006554 mbedtls_md5_free( &md5 );
6555 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006556
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006557 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006560}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006561#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6564#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006565static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006567{
6568 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006569 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006570 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006571#if defined(MBEDTLS_USE_PSA_CRYPTO)
6572 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006573 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006574 psa_status_t status;
6575#else
6576 mbedtls_sha256_context sha256;
6577#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006580 if( !session )
6581 session = ssl->session;
6582
Andrzej Kurekeb342242019-01-29 09:14:33 -05006583 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6584 ? "client finished"
6585 : "server finished";
6586
6587#if defined(MBEDTLS_USE_PSA_CRYPTO)
6588 sha256_psa = psa_hash_operation_init();
6589
6590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
6591
6592 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
6593 if( status != PSA_SUCCESS )
6594 {
6595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6596 return;
6597 }
6598
6599 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
6600 if( status != PSA_SUCCESS )
6601 {
6602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6603 return;
6604 }
6605 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
6606#else
6607
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006608 mbedtls_sha256_init( &sha256 );
6609
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006611
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006612 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006613
6614 /*
6615 * TLSv1.2:
6616 * hash = PRF( master, finished_label,
6617 * Hash( handshake ) )[0.11]
6618 */
6619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006620#if !defined(MBEDTLS_SHA256_ALT)
6621 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006622 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006623#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006624
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006625 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006626 mbedtls_sha256_free( &sha256 );
6627#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006628
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006629 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006630 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006632 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006633
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006634 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006637}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006640#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006641static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006642 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006643{
6644 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006645 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006646 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05006647#if defined(MBEDTLS_USE_PSA_CRYPTO)
6648 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00006649 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05006650 psa_status_t status;
6651#else
6652 mbedtls_sha512_context sha512;
6653#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006656 if( !session )
6657 session = ssl->session;
6658
Andrzej Kurekeb342242019-01-29 09:14:33 -05006659 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
6660 ? "client finished"
6661 : "server finished";
6662
6663#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006664 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05006665
6666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
6667
Andrzej Kurek972fba52019-01-30 03:29:12 -05006668 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006669 if( status != PSA_SUCCESS )
6670 {
6671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
6672 return;
6673 }
6674
Andrzej Kurek972fba52019-01-30 03:29:12 -05006675 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006676 if( status != PSA_SUCCESS )
6677 {
6678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
6679 return;
6680 }
6681 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
6682#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006683 mbedtls_sha512_init( &sha512 );
6684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006686
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006687 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006688
6689 /*
6690 * TLSv1.2:
6691 * hash = PRF( master, finished_label,
6692 * Hash( handshake ) )[0.11]
6693 */
6694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006695#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006696 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
6697 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006698#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006699
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006700 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006701 mbedtls_sha512_free( &sha512 );
6702#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00006703
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006704 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006705 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006708
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006709 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00006712}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713#endif /* MBEDTLS_SHA512_C */
6714#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00006715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006716static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006717{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006719
6720 /*
6721 * Free our handshake params
6722 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02006723 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006724 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00006725 ssl->handshake = NULL;
6726
6727 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006728 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00006729 */
6730 if( ssl->transform )
6731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006732 mbedtls_ssl_transform_free( ssl->transform );
6733 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006734 }
6735 ssl->transform = ssl->transform_negotiate;
6736 ssl->transform_negotiate = NULL;
6737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006738 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006739}
6740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006741void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006742{
6743 int resume = ssl->handshake->resume;
6744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006747#if defined(MBEDTLS_SSL_RENEGOTIATION)
6748 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006750 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006751 ssl->renego_records_seen = 0;
6752 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006753#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006754
6755 /*
6756 * Free the previous session and switch in the current one
6757 */
Paul Bakker0a597072012-09-25 21:55:46 +00006758 if( ssl->session )
6759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01006761 /* RFC 7366 3.1: keep the EtM state */
6762 ssl->session_negotiate->encrypt_then_mac =
6763 ssl->session->encrypt_then_mac;
6764#endif
6765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766 mbedtls_ssl_session_free( ssl->session );
6767 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00006768 }
6769 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006770 ssl->session_negotiate = NULL;
6771
Paul Bakker0a597072012-09-25 21:55:46 +00006772 /*
6773 * Add cache entry
6774 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006775 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02006776 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006777 resume == 0 )
6778 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006779 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02006781 }
Paul Bakker0a597072012-09-25 21:55:46 +00006782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006784 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006785 ssl->handshake->flight != NULL )
6786 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006787 /* Cancel handshake timer */
6788 ssl_set_timer( ssl, 0 );
6789
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006790 /* Keep last flight around in case we need to resend it:
6791 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006793 }
6794 else
6795#endif
6796 ssl_handshake_wrapup_free_hs_transform( ssl );
6797
Paul Bakker48916f92012-09-16 19:57:18 +00006798 ssl->state++;
6799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006800 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006801}
6802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006804{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006805 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006808
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006809 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01006810
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006811 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006812
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01006813 /*
6814 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
6815 * may define some other value. Currently (early 2016), no defined
6816 * ciphersuite does this (and this is unlikely to change as activity has
6817 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
6818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006822 ssl->verify_data_len = hash_len;
6823 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006824#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006825
Paul Bakker5121ce52009-01-03 21:22:43 +00006826 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006827 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6828 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006829
6830 /*
6831 * In case of session resuming, invert the client and server
6832 * ChangeCipherSpec messages order.
6833 */
Paul Bakker0a597072012-09-25 21:55:46 +00006834 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006837 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006839#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006841 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006842 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006843#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006844 }
6845 else
6846 ssl->state++;
6847
Paul Bakker48916f92012-09-16 19:57:18 +00006848 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006849 * Switch to our negotiated transform and session parameters for outbound
6850 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00006851 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01006853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006854#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006855 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006856 {
6857 unsigned char i;
6858
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006859 /* Remember current epoch settings for resending */
6860 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01006861 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006862
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006863 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01006864 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006865
6866 /* Increment epoch */
6867 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01006868 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006869 break;
6870
6871 /* The loop goes to its end iff the counter is wrapping */
6872 if( i == 0 )
6873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
6875 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006876 }
6877 }
6878 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006879#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01006880 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006881
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006882 ssl->transform_out = ssl->transform_negotiate;
6883 ssl->session_out = ssl->session_negotiate;
6884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6886 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006888 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01006889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
6891 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01006892 }
6893 }
6894#endif
6895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006897 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02006899#endif
6900
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006901 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006902 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006903 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006904 return( ret );
6905 }
6906
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02006907#if defined(MBEDTLS_SSL_PROTO_DTLS)
6908 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6909 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
6910 {
6911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
6912 return( ret );
6913 }
6914#endif
6915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006917
6918 return( 0 );
6919}
6920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006921#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006922#define SSL_MAX_HASH_LEN 36
6923#else
6924#define SSL_MAX_HASH_LEN 12
6925#endif
6926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006927int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006928{
Paul Bakker23986e52011-04-24 08:57:21 +00006929 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02006930 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006931 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00006932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006934
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006935 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006936
Hanno Becker327c93b2018-08-15 13:56:18 +01006937 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006940 return( ret );
6941 }
6942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006946 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6947 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006949 }
6950
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006951 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952#if defined(MBEDTLS_SSL_PROTO_SSL3)
6953 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00006954 hash_len = 36;
6955 else
6956#endif
6957 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00006958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
6960 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006963 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6964 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006966 }
6967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00006969 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006972 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6973 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006975 }
6976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006978 ssl->verify_data_len = hash_len;
6979 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006980#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006981
Paul Bakker0a597072012-09-25 21:55:46 +00006982 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006985 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006987#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006989 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006990 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006991#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006992 }
6993 else
6994 ssl->state++;
6995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006996#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006997 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006998 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006999#endif
7000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007002
7003 return( 0 );
7004}
7005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007006static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007007{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007008 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007010#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7011 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7012 mbedtls_md5_init( &handshake->fin_md5 );
7013 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007014 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7015 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007016#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007017#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7018#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007019#if defined(MBEDTLS_USE_PSA_CRYPTO)
7020 handshake->fin_sha256_psa = psa_hash_operation_init();
7021 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007024 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007025#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007026#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007027#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007028#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007029 handshake->fin_sha384_psa = psa_hash_operation_init();
7030 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007031#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007033 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007034#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007035#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007036#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007037
7038 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007039
7040#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7041 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7042 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7043#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007045#if defined(MBEDTLS_DHM_C)
7046 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007047#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007048#if defined(MBEDTLS_ECDH_C)
7049 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007050#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007051#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007052 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007053#if defined(MBEDTLS_SSL_CLI_C)
7054 handshake->ecjpake_cache = NULL;
7055 handshake->ecjpake_cache_len = 0;
7056#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007057#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007058
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007059#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007060 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007061#endif
7062
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007063#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7064 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7065#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007066}
7067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007069{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007070 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007072 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7073 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075 mbedtls_md_init( &transform->md_ctx_enc );
7076 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007077}
7078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007080{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007082}
7083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007085{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007086 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007087 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007089 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007091 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007092 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007093
7094 /*
7095 * Either the pointers are now NULL or cleared properly and can be freed.
7096 * Now allocate missing structures.
7097 */
7098 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007099 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007100 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007101 }
Paul Bakker48916f92012-09-16 19:57:18 +00007102
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007103 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007104 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007105 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007106 }
Paul Bakker48916f92012-09-16 19:57:18 +00007107
Paul Bakker82788fb2014-10-20 13:59:19 +02007108 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007109 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007110 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007111 }
Paul Bakker48916f92012-09-16 19:57:18 +00007112
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007113 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007114 if( ssl->handshake == NULL ||
7115 ssl->transform_negotiate == NULL ||
7116 ssl->session_negotiate == NULL )
7117 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007120 mbedtls_free( ssl->handshake );
7121 mbedtls_free( ssl->transform_negotiate );
7122 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007123
7124 ssl->handshake = NULL;
7125 ssl->transform_negotiate = NULL;
7126 ssl->session_negotiate = NULL;
7127
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007128 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007129 }
7130
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007131 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007132 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007133 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007134 ssl_handshake_params_init( ssl->handshake );
7135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007136#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007137 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7138 {
7139 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007140
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007141 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7142 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7143 else
7144 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007145
7146 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007147 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007148#endif
7149
Paul Bakker48916f92012-09-16 19:57:18 +00007150 return( 0 );
7151}
7152
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007153#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007154/* Dummy cookie callbacks for defaults */
7155static int ssl_cookie_write_dummy( void *ctx,
7156 unsigned char **p, unsigned char *end,
7157 const unsigned char *cli_id, size_t cli_id_len )
7158{
7159 ((void) ctx);
7160 ((void) p);
7161 ((void) end);
7162 ((void) cli_id);
7163 ((void) cli_id_len);
7164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007166}
7167
7168static int ssl_cookie_check_dummy( void *ctx,
7169 const unsigned char *cookie, size_t cookie_len,
7170 const unsigned char *cli_id, size_t cli_id_len )
7171{
7172 ((void) ctx);
7173 ((void) cookie);
7174 ((void) cookie_len);
7175 ((void) cli_id);
7176 ((void) cli_id_len);
7177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007178 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007179}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007180#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007181
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007182/* Once ssl->out_hdr as the address of the beginning of the
7183 * next outgoing record is set, deduce the other pointers.
7184 *
7185 * Note: For TLS, we save the implicit record sequence number
7186 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7187 * and the caller has to make sure there's space for this.
7188 */
7189
7190static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7191 mbedtls_ssl_transform *transform )
7192{
7193#if defined(MBEDTLS_SSL_PROTO_DTLS)
7194 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7195 {
7196 ssl->out_ctr = ssl->out_hdr + 3;
7197 ssl->out_len = ssl->out_hdr + 11;
7198 ssl->out_iv = ssl->out_hdr + 13;
7199 }
7200 else
7201#endif
7202 {
7203 ssl->out_ctr = ssl->out_hdr - 8;
7204 ssl->out_len = ssl->out_hdr + 3;
7205 ssl->out_iv = ssl->out_hdr + 5;
7206 }
7207
7208 /* Adjust out_msg to make space for explicit IV, if used. */
7209 if( transform != NULL &&
7210 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7211 {
7212 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7213 }
7214 else
7215 ssl->out_msg = ssl->out_iv;
7216}
7217
7218/* Once ssl->in_hdr as the address of the beginning of the
7219 * next incoming record is set, deduce the other pointers.
7220 *
7221 * Note: For TLS, we save the implicit record sequence number
7222 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7223 * and the caller has to make sure there's space for this.
7224 */
7225
7226static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7227 mbedtls_ssl_transform *transform )
7228{
7229#if defined(MBEDTLS_SSL_PROTO_DTLS)
7230 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7231 {
7232 ssl->in_ctr = ssl->in_hdr + 3;
7233 ssl->in_len = ssl->in_hdr + 11;
7234 ssl->in_iv = ssl->in_hdr + 13;
7235 }
7236 else
7237#endif
7238 {
7239 ssl->in_ctr = ssl->in_hdr - 8;
7240 ssl->in_len = ssl->in_hdr + 3;
7241 ssl->in_iv = ssl->in_hdr + 5;
7242 }
7243
7244 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7245 if( transform != NULL &&
7246 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7247 {
7248 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7249 }
7250 else
7251 ssl->in_msg = ssl->in_iv;
7252}
7253
Paul Bakker5121ce52009-01-03 21:22:43 +00007254/*
7255 * Initialize an SSL context
7256 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007257void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7258{
7259 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7260}
7261
7262/*
7263 * Setup an SSL context
7264 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007265
7266static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7267{
7268 /* Set the incoming and outgoing record pointers. */
7269#if defined(MBEDTLS_SSL_PROTO_DTLS)
7270 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7271 {
7272 ssl->out_hdr = ssl->out_buf;
7273 ssl->in_hdr = ssl->in_buf;
7274 }
7275 else
7276#endif /* MBEDTLS_SSL_PROTO_DTLS */
7277 {
7278 ssl->out_hdr = ssl->out_buf + 8;
7279 ssl->in_hdr = ssl->in_buf + 8;
7280 }
7281
7282 /* Derive other internal pointers. */
7283 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7284 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7285}
7286
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007287int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007288 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007289{
Paul Bakker48916f92012-09-16 19:57:18 +00007290 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007291
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007292 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007293
7294 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007295 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007296 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007297
7298 /* Set to NULL in case of an error condition */
7299 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007300
Angus Grattond8213d02016-05-25 20:56:48 +10007301 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7302 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007303 {
Angus Grattond8213d02016-05-25 20:56:48 +10007304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007305 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007306 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007307 }
7308
7309 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7310 if( ssl->out_buf == NULL )
7311 {
7312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007313 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007314 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007315 }
7316
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007317 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007318
Paul Bakker48916f92012-09-16 19:57:18 +00007319 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007320 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007321
7322 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007323
7324error:
7325 mbedtls_free( ssl->in_buf );
7326 mbedtls_free( ssl->out_buf );
7327
7328 ssl->conf = NULL;
7329
7330 ssl->in_buf = NULL;
7331 ssl->out_buf = NULL;
7332
7333 ssl->in_hdr = NULL;
7334 ssl->in_ctr = NULL;
7335 ssl->in_len = NULL;
7336 ssl->in_iv = NULL;
7337 ssl->in_msg = NULL;
7338
7339 ssl->out_hdr = NULL;
7340 ssl->out_ctr = NULL;
7341 ssl->out_len = NULL;
7342 ssl->out_iv = NULL;
7343 ssl->out_msg = NULL;
7344
k-stachowiak9f7798e2018-07-31 16:52:32 +02007345 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007346}
7347
7348/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007349 * Reset an initialized and used SSL context for re-use while retaining
7350 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007351 *
7352 * If partial is non-zero, keep data in the input buffer and client ID.
7353 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007354 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007355static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007356{
Paul Bakker48916f92012-09-16 19:57:18 +00007357 int ret;
7358
Hanno Becker7e772132018-08-10 12:38:21 +01007359#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7360 !defined(MBEDTLS_SSL_SRV_C)
7361 ((void) partial);
7362#endif
7363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007365
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007366 /* Cancel any possibly running timer */
7367 ssl_set_timer( ssl, 0 );
7368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369#if defined(MBEDTLS_SSL_RENEGOTIATION)
7370 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007371 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007372
7373 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7375 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007376#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007378
Paul Bakker7eb013f2011-10-06 12:37:39 +00007379 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007380 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007381
7382 ssl->in_msgtype = 0;
7383 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007385 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007386 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007387#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007389 ssl_dtls_replay_reset( ssl );
7390#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007391
7392 ssl->in_hslen = 0;
7393 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007394
7395 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007396
7397 ssl->out_msgtype = 0;
7398 ssl->out_msglen = 0;
7399 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007400#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7401 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007402 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007403#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007404
Hanno Becker19859472018-08-06 09:40:20 +01007405 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7406
Paul Bakker48916f92012-09-16 19:57:18 +00007407 ssl->transform_in = NULL;
7408 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007409
Hanno Becker78640902018-08-13 16:35:15 +01007410 ssl->session_in = NULL;
7411 ssl->session_out = NULL;
7412
Angus Grattond8213d02016-05-25 20:56:48 +10007413 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007414
7415#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007416 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007417#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7418 {
7419 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007420 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007421 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7424 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7427 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007429 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7430 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007431 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007432 }
7433#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007434
Paul Bakker48916f92012-09-16 19:57:18 +00007435 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007437 mbedtls_ssl_transform_free( ssl->transform );
7438 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007439 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007440 }
Paul Bakker48916f92012-09-16 19:57:18 +00007441
Paul Bakkerc0463502013-02-14 11:19:38 +01007442 if( ssl->session )
7443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007444 mbedtls_ssl_session_free( ssl->session );
7445 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007446 ssl->session = NULL;
7447 }
7448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007450 ssl->alpn_chosen = NULL;
7451#endif
7452
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007453#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007454#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007455 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007456#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007457 {
7458 mbedtls_free( ssl->cli_id );
7459 ssl->cli_id = NULL;
7460 ssl->cli_id_len = 0;
7461 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007462#endif
7463
Paul Bakker48916f92012-09-16 19:57:18 +00007464 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7465 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007466
7467 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007468}
7469
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007470/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007471 * Reset an initialized and used SSL context for re-use while retaining
7472 * all application-set variables, function pointers and data.
7473 */
7474int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7475{
7476 return( ssl_session_reset_int( ssl, 0 ) );
7477}
7478
7479/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007480 * SSL set accessors
7481 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007482void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007483{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007484 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007485}
7486
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007487void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007488{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007489 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007490}
7491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007492#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007493void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007494{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007495 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007496}
7497#endif
7498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007500void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007501{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007502 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007503}
7504#endif
7505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007507
Hanno Becker1841b0a2018-08-24 11:13:57 +01007508void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7509 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007510{
7511 ssl->disable_datagram_packing = !allow_packing;
7512}
7513
7514void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7515 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007516{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007517 conf->hs_timeout_min = min;
7518 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007519}
7520#endif
7521
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007522void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007523{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007524 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007525}
7526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007528void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007529 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007530 void *p_vrfy )
7531{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007532 conf->f_vrfy = f_vrfy;
7533 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007535#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007536
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007537void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007538 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007539 void *p_rng )
7540{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007541 conf->f_rng = f_rng;
7542 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007543}
7544
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007545void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007546 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007547 void *p_dbg )
7548{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007549 conf->f_dbg = f_dbg;
7550 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007551}
7552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007554 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007555 mbedtls_ssl_send_t *f_send,
7556 mbedtls_ssl_recv_t *f_recv,
7557 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007558{
7559 ssl->p_bio = p_bio;
7560 ssl->f_send = f_send;
7561 ssl->f_recv = f_recv;
7562 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007563}
7564
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007565#if defined(MBEDTLS_SSL_PROTO_DTLS)
7566void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7567{
7568 ssl->mtu = mtu;
7569}
7570#endif
7571
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007572void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007573{
7574 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007575}
7576
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007577void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7578 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007579 mbedtls_ssl_set_timer_t *f_set_timer,
7580 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007581{
7582 ssl->p_timer = p_timer;
7583 ssl->f_set_timer = f_set_timer;
7584 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007585
7586 /* Make sure we start with no timer running */
7587 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007588}
7589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007591void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007592 void *p_cache,
7593 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7594 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007595{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007596 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007597 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007598 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007599}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602#if defined(MBEDTLS_SSL_CLI_C)
7603int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007604{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007605 int ret;
7606
7607 if( ssl == NULL ||
7608 session == NULL ||
7609 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007610 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007613 }
7614
7615 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7616 return( ret );
7617
Paul Bakker0a597072012-09-25 21:55:46 +00007618 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007619
7620 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007621}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007623
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007624void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007625 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007626{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007627 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7628 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7629 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7630 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007631}
7632
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007633void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007634 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007635 int major, int minor )
7636{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007637 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007638 return;
7639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007641 return;
7642
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007643 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007644}
7645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007646#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007647void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007648 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007649{
7650 conf->cert_profile = profile;
7651}
7652
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007653/* Append a new keycert entry to a (possibly empty) list */
7654static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7655 mbedtls_x509_crt *cert,
7656 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007657{
niisato8ee24222018-06-25 19:05:48 +09007658 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007659
niisato8ee24222018-06-25 19:05:48 +09007660 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7661 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007662 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007663
niisato8ee24222018-06-25 19:05:48 +09007664 new_cert->cert = cert;
7665 new_cert->key = key;
7666 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007667
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007668 /* Update head is the list was null, else add to the end */
7669 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007670 {
niisato8ee24222018-06-25 19:05:48 +09007671 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007672 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007673 else
7674 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007675 mbedtls_ssl_key_cert *cur = *head;
7676 while( cur->next != NULL )
7677 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007678 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007679 }
7680
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007681 return( 0 );
7682}
7683
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007684int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007685 mbedtls_x509_crt *own_cert,
7686 mbedtls_pk_context *pk_key )
7687{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007688 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007689}
7690
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007691void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007692 mbedtls_x509_crt *ca_chain,
7693 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007694{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007695 conf->ca_chain = ca_chain;
7696 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007698#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007699
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007700#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7701int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7702 mbedtls_x509_crt *own_cert,
7703 mbedtls_pk_context *pk_key )
7704{
7705 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7706 own_cert, pk_key ) );
7707}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007708
7709void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7710 mbedtls_x509_crt *ca_chain,
7711 mbedtls_x509_crl *ca_crl )
7712{
7713 ssl->handshake->sni_ca_chain = ca_chain;
7714 ssl->handshake->sni_ca_crl = ca_crl;
7715}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007716
7717void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7718 int authmode )
7719{
7720 ssl->handshake->sni_authmode = authmode;
7721}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007722#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7723
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007724#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007725/*
7726 * Set EC J-PAKE password for current handshake
7727 */
7728int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7729 const unsigned char *pw,
7730 size_t pw_len )
7731{
7732 mbedtls_ecjpake_role role;
7733
Janos Follath8eb64132016-06-03 15:40:57 +01007734 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007735 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7736
7737 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7738 role = MBEDTLS_ECJPAKE_SERVER;
7739 else
7740 role = MBEDTLS_ECJPAKE_CLIENT;
7741
7742 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7743 role,
7744 MBEDTLS_MD_SHA256,
7745 MBEDTLS_ECP_DP_SECP256R1,
7746 pw, pw_len ) );
7747}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007748#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007750#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007751
7752static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
7753{
7754 /* Remove reference to existing PSK, if any. */
7755#if defined(MBEDTLS_USE_PSA_CRYPTO)
7756 if( conf->psk_opaque != 0 )
7757 {
7758 /* The maintenance of the PSK key slot is the
7759 * user's responsibility. */
7760 conf->psk_opaque = 0;
7761 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00007762 /* This and the following branch should never
7763 * be taken simultaenously as we maintain the
7764 * invariant that raw and opaque PSKs are never
7765 * configured simultaneously. As a safeguard,
7766 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007767#endif /* MBEDTLS_USE_PSA_CRYPTO */
7768 if( conf->psk != NULL )
7769 {
7770 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
7771
7772 mbedtls_free( conf->psk );
7773 conf->psk = NULL;
7774 conf->psk_len = 0;
7775 }
7776
7777 /* Remove reference to PSK identity, if any. */
7778 if( conf->psk_identity != NULL )
7779 {
7780 mbedtls_free( conf->psk_identity );
7781 conf->psk_identity = NULL;
7782 conf->psk_identity_len = 0;
7783 }
7784}
7785
Hanno Becker7390c712018-11-15 13:33:04 +00007786/* This function assumes that PSK identity in the SSL config is unset.
7787 * It checks that the provided identity is well-formed and attempts
7788 * to make a copy of it in the SSL config.
7789 * On failure, the PSK identity in the config remains unset. */
7790static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
7791 unsigned char const *psk_identity,
7792 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007793{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007794 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00007795 if( psk_identity == NULL ||
7796 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007797 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007798 {
7799 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7800 }
7801
Hanno Becker7390c712018-11-15 13:33:04 +00007802 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
7803 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007804 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02007805
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007806 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007807 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02007808
7809 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02007810}
7811
Hanno Becker7390c712018-11-15 13:33:04 +00007812int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
7813 const unsigned char *psk, size_t psk_len,
7814 const unsigned char *psk_identity, size_t psk_identity_len )
7815{
7816 int ret;
7817 /* Remove opaque/raw PSK + PSK Identity */
7818 ssl_conf_remove_psk( conf );
7819
7820 /* Check and set raw PSK */
7821 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
7822 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7823 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
7824 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7825 conf->psk_len = psk_len;
7826 memcpy( conf->psk, psk, conf->psk_len );
7827
7828 /* Check and set PSK Identity */
7829 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
7830 if( ret != 0 )
7831 ssl_conf_remove_psk( conf );
7832
7833 return( ret );
7834}
7835
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007836static void ssl_remove_psk( mbedtls_ssl_context *ssl )
7837{
7838#if defined(MBEDTLS_USE_PSA_CRYPTO)
7839 if( ssl->handshake->psk_opaque != 0 )
7840 {
7841 ssl->handshake->psk_opaque = 0;
7842 }
7843 else
7844#endif /* MBEDTLS_USE_PSA_CRYPTO */
7845 if( ssl->handshake->psk != NULL )
7846 {
7847 mbedtls_platform_zeroize( ssl->handshake->psk,
7848 ssl->handshake->psk_len );
7849 mbedtls_free( ssl->handshake->psk );
7850 ssl->handshake->psk_len = 0;
7851 }
7852}
7853
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007854int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7855 const unsigned char *psk, size_t psk_len )
7856{
7857 if( psk == NULL || ssl->handshake == NULL )
7858 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7859
7860 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7861 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7862
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007863 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007864
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007865 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007866 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007867
7868 ssl->handshake->psk_len = psk_len;
7869 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7870
7871 return( 0 );
7872}
7873
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007874#if defined(MBEDTLS_USE_PSA_CRYPTO)
7875int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007876 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007877 const unsigned char *psk_identity,
7878 size_t psk_identity_len )
7879{
Hanno Becker7390c712018-11-15 13:33:04 +00007880 int ret;
7881 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007882 ssl_conf_remove_psk( conf );
7883
Hanno Becker7390c712018-11-15 13:33:04 +00007884 /* Check and set opaque PSK */
7885 if( psk_slot == 0 )
7886 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007887 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00007888
7889 /* Check and set PSK Identity */
7890 ret = ssl_conf_set_psk_identity( conf, psk_identity,
7891 psk_identity_len );
7892 if( ret != 0 )
7893 ssl_conf_remove_psk( conf );
7894
7895 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007896}
7897
7898int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007899 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007900{
7901 if( psk_slot == 0 || ssl->handshake == NULL )
7902 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7903
7904 ssl_remove_psk( ssl );
7905 ssl->handshake->psk_opaque = psk_slot;
7906 return( 0 );
7907}
7908#endif /* MBEDTLS_USE_PSA_CRYPTO */
7909
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007910void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007912 size_t),
7913 void *p_psk )
7914{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007915 conf->f_psk = f_psk;
7916 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007917}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007919
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007920#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007921
7922#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007923int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007924{
7925 int ret;
7926
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007927 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7928 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7929 {
7930 mbedtls_mpi_free( &conf->dhm_P );
7931 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007932 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007933 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007934
7935 return( 0 );
7936}
Hanno Becker470a8c42017-10-04 15:28:46 +01007937#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007938
Hanno Beckera90658f2017-10-04 15:29:08 +01007939int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7940 const unsigned char *dhm_P, size_t P_len,
7941 const unsigned char *dhm_G, size_t G_len )
7942{
7943 int ret;
7944
7945 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7946 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7947 {
7948 mbedtls_mpi_free( &conf->dhm_P );
7949 mbedtls_mpi_free( &conf->dhm_G );
7950 return( ret );
7951 }
7952
7953 return( 0 );
7954}
Paul Bakker5121ce52009-01-03 21:22:43 +00007955
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007956int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007957{
7958 int ret;
7959
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007960 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7961 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7962 {
7963 mbedtls_mpi_free( &conf->dhm_P );
7964 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007965 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007966 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007967
7968 return( 0 );
7969}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007970#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007971
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007972#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7973/*
7974 * Set the minimum length for Diffie-Hellman parameters
7975 */
7976void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7977 unsigned int bitlen )
7978{
7979 conf->dhm_min_bitlen = bitlen;
7980}
7981#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7982
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007983#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007984/*
7985 * Set allowed/preferred hashes for handshake signatures
7986 */
7987void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7988 const int *hashes )
7989{
7990 conf->sig_hashes = hashes;
7991}
Hanno Becker947194e2017-04-07 13:25:49 +01007992#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007993
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007994#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007995/*
7996 * Set the allowed elliptic curves
7997 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007998void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007999 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008000{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008001 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008002}
Hanno Becker947194e2017-04-07 13:25:49 +01008003#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008004
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008005#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008006int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008007{
Hanno Becker947194e2017-04-07 13:25:49 +01008008 /* Initialize to suppress unnecessary compiler warning */
8009 size_t hostname_len = 0;
8010
8011 /* Check if new hostname is valid before
8012 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008013 if( hostname != NULL )
8014 {
8015 hostname_len = strlen( hostname );
8016
8017 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8018 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8019 }
8020
8021 /* Now it's clear that we will overwrite the old hostname,
8022 * so we can free it safely */
8023
8024 if( ssl->hostname != NULL )
8025 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008026 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008027 mbedtls_free( ssl->hostname );
8028 }
8029
8030 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008031
Paul Bakker5121ce52009-01-03 21:22:43 +00008032 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008033 {
8034 ssl->hostname = NULL;
8035 }
8036 else
8037 {
8038 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008039 if( ssl->hostname == NULL )
8040 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008041
Hanno Becker947194e2017-04-07 13:25:49 +01008042 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008043
Hanno Becker947194e2017-04-07 13:25:49 +01008044 ssl->hostname[hostname_len] = '\0';
8045 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008046
8047 return( 0 );
8048}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008049#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008050
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008051#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008052void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008053 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008054 const unsigned char *, size_t),
8055 void *p_sni )
8056{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008057 conf->f_sni = f_sni;
8058 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008060#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008062#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008063int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008064{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008065 size_t cur_len, tot_len;
8066 const char **p;
8067
8068 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008069 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8070 * MUST NOT be truncated."
8071 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008072 */
8073 tot_len = 0;
8074 for( p = protos; *p != NULL; p++ )
8075 {
8076 cur_len = strlen( *p );
8077 tot_len += cur_len;
8078
8079 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008081 }
8082
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008083 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008084
8085 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008086}
8087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008088const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008089{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008090 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008091}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008092#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008093
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008094void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008095{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008096 conf->max_major_ver = major;
8097 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008098}
8099
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008100void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008101{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008102 conf->min_major_ver = major;
8103 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008104}
8105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008106#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008107void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008108{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008109 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008110}
8111#endif
8112
Janos Follath088ce432017-04-10 12:42:31 +01008113#if defined(MBEDTLS_SSL_SRV_C)
8114void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8115 char cert_req_ca_list )
8116{
8117 conf->cert_req_ca_list = cert_req_ca_list;
8118}
8119#endif
8120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008121#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008122void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008123{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008124 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008125}
8126#endif
8127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008128#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008129void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008130{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008131 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008132}
8133#endif
8134
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008135#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008136void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008137{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008138 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008139}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008140#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008142#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008143int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008144{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008146 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008148 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008149 }
8150
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008151 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008152
8153 return( 0 );
8154}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008155#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008157#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008158void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008159{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008160 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008162#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008165void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008166{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008167 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008168}
8169#endif
8170
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008171void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008172{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008173 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008174}
8175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008177void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008178{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008179 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008180}
8181
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008182void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008183{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008184 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008185}
8186
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008187void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008188 const unsigned char period[8] )
8189{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008190 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008191}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008192#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008195#if defined(MBEDTLS_SSL_CLI_C)
8196void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008197{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008198 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008199}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008200#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008201
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008202#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008203void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8204 mbedtls_ssl_ticket_write_t *f_ticket_write,
8205 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8206 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008207{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008208 conf->f_ticket_write = f_ticket_write;
8209 conf->f_ticket_parse = f_ticket_parse;
8210 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008211}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008212#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008213#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008214
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008215#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8216void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8217 mbedtls_ssl_export_keys_t *f_export_keys,
8218 void *p_export_keys )
8219{
8220 conf->f_export_keys = f_export_keys;
8221 conf->p_export_keys = p_export_keys;
8222}
8223#endif
8224
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008225#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008226void mbedtls_ssl_conf_async_private_cb(
8227 mbedtls_ssl_config *conf,
8228 mbedtls_ssl_async_sign_t *f_async_sign,
8229 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8230 mbedtls_ssl_async_resume_t *f_async_resume,
8231 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008232 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008233{
8234 conf->f_async_sign_start = f_async_sign;
8235 conf->f_async_decrypt_start = f_async_decrypt;
8236 conf->f_async_resume = f_async_resume;
8237 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008238 conf->p_async_config_data = async_config_data;
8239}
8240
Gilles Peskine8f97af72018-04-26 11:46:10 +02008241void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8242{
8243 return( conf->p_async_config_data );
8244}
8245
Gilles Peskine1febfef2018-04-30 11:54:39 +02008246void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008247{
8248 if( ssl->handshake == NULL )
8249 return( NULL );
8250 else
8251 return( ssl->handshake->user_async_ctx );
8252}
8253
Gilles Peskine1febfef2018-04-30 11:54:39 +02008254void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008255 void *ctx )
8256{
8257 if( ssl->handshake != NULL )
8258 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008259}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008260#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008261
Paul Bakker5121ce52009-01-03 21:22:43 +00008262/*
8263 * SSL get accessors
8264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008265size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008266{
8267 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8268}
8269
Hanno Becker8b170a02017-10-10 11:51:19 +01008270int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8271{
8272 /*
8273 * Case A: We're currently holding back
8274 * a message for further processing.
8275 */
8276
8277 if( ssl->keep_current_message == 1 )
8278 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008279 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008280 return( 1 );
8281 }
8282
8283 /*
8284 * Case B: Further records are pending in the current datagram.
8285 */
8286
8287#if defined(MBEDTLS_SSL_PROTO_DTLS)
8288 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8289 ssl->in_left > ssl->next_record_offset )
8290 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008292 return( 1 );
8293 }
8294#endif /* MBEDTLS_SSL_PROTO_DTLS */
8295
8296 /*
8297 * Case C: A handshake message is being processed.
8298 */
8299
Hanno Becker8b170a02017-10-10 11:51:19 +01008300 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8301 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008302 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008303 return( 1 );
8304 }
8305
8306 /*
8307 * Case D: An application data message is being processed
8308 */
8309 if( ssl->in_offt != NULL )
8310 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008311 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008312 return( 1 );
8313 }
8314
8315 /*
8316 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008317 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008318 * we implement support for multiple alerts in single records.
8319 */
8320
8321 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8322 return( 0 );
8323}
8324
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008325uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008326{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008327 if( ssl->session != NULL )
8328 return( ssl->session->verify_result );
8329
8330 if( ssl->session_negotiate != NULL )
8331 return( ssl->session_negotiate->verify_result );
8332
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008333 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008334}
8335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008336const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008337{
Paul Bakker926c8e42013-03-06 10:23:34 +01008338 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008339 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008342}
8343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008344const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008345{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008347 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008348 {
8349 switch( ssl->minor_ver )
8350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008352 return( "DTLSv1.0" );
8353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008355 return( "DTLSv1.2" );
8356
8357 default:
8358 return( "unknown (DTLS)" );
8359 }
8360 }
8361#endif
8362
Paul Bakker43ca69c2011-01-15 17:35:19 +00008363 switch( ssl->minor_ver )
8364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008366 return( "SSLv3.0" );
8367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008368 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008369 return( "TLSv1.0" );
8370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008371 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008372 return( "TLSv1.1" );
8373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008374 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008375 return( "TLSv1.2" );
8376
Paul Bakker43ca69c2011-01-15 17:35:19 +00008377 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008378 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008379 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008380}
8381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008382int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008383{
Hanno Becker3136ede2018-08-17 15:28:19 +01008384 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008385 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008386 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008387
Hanno Becker78640902018-08-13 16:35:15 +01008388 if( transform == NULL )
8389 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391#if defined(MBEDTLS_ZLIB_SUPPORT)
8392 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8393 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008394#endif
8395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008396 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008398 case MBEDTLS_MODE_GCM:
8399 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008400 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008401 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008402 transform_expansion = transform->minlen;
8403 break;
8404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008406
8407 block_size = mbedtls_cipher_get_block_size(
8408 &transform->cipher_ctx_enc );
8409
Hanno Becker3136ede2018-08-17 15:28:19 +01008410 /* Expansion due to the addition of the MAC. */
8411 transform_expansion += transform->maclen;
8412
8413 /* Expansion due to the addition of CBC padding;
8414 * Theoretically up to 256 bytes, but we never use
8415 * more than the block size of the underlying cipher. */
8416 transform_expansion += block_size;
8417
8418 /* For TLS 1.1 or higher, an explicit IV is added
8419 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008420#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8421 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008422 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008423#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008424
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008425 break;
8426
8427 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008429 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008430 }
8431
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008432 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008433}
8434
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008435#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8436size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8437{
8438 size_t max_len;
8439
8440 /*
8441 * Assume mfl_code is correct since it was checked when set
8442 */
Angus Grattond8213d02016-05-25 20:56:48 +10008443 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008444
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008445 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008446 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008447 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008448 {
Angus Grattond8213d02016-05-25 20:56:48 +10008449 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008450 }
8451
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008452 /* During a handshake, use the value being negotiated */
8453 if( ssl->session_negotiate != NULL &&
8454 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8455 {
8456 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8457 }
8458
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008459 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008460}
8461#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8462
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008463#if defined(MBEDTLS_SSL_PROTO_DTLS)
8464static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8465{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008466 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8467 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8468 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8469 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8470 return ( 0 );
8471
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008472 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8473 return( ssl->mtu );
8474
8475 if( ssl->mtu == 0 )
8476 return( ssl->handshake->mtu );
8477
8478 return( ssl->mtu < ssl->handshake->mtu ?
8479 ssl->mtu : ssl->handshake->mtu );
8480}
8481#endif /* MBEDTLS_SSL_PROTO_DTLS */
8482
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008483int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8484{
8485 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8486
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008487#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8488 !defined(MBEDTLS_SSL_PROTO_DTLS)
8489 (void) ssl;
8490#endif
8491
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008492#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8493 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8494
8495 if( max_len > mfl )
8496 max_len = mfl;
8497#endif
8498
8499#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008500 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008501 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008502 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008503 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8504 const size_t overhead = (size_t) ret;
8505
8506 if( ret < 0 )
8507 return( ret );
8508
8509 if( mtu <= overhead )
8510 {
8511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8512 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8513 }
8514
8515 if( max_len > mtu - overhead )
8516 max_len = mtu - overhead;
8517 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008518#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008519
Hanno Becker0defedb2018-08-10 12:35:02 +01008520#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8521 !defined(MBEDTLS_SSL_PROTO_DTLS)
8522 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008523#endif
8524
8525 return( (int) max_len );
8526}
8527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008528#if defined(MBEDTLS_X509_CRT_PARSE_C)
8529const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008530{
8531 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008532 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008533
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008534 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008535}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008536#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008538#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00008539int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8540 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008541{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008542 if( ssl == NULL ||
8543 dst == NULL ||
8544 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008545 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008548 }
8549
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008550 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008551}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008552#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008553
Paul Bakker5121ce52009-01-03 21:22:43 +00008554/*
Paul Bakker1961b702013-01-25 14:49:24 +01008555 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008556 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008557int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008558{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008559 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008560
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008561 if( ssl == NULL || ssl->conf == NULL )
8562 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008565 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008566 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008567#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008568#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008569 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008570 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008571#endif
8572
Paul Bakker1961b702013-01-25 14:49:24 +01008573 return( ret );
8574}
8575
8576/*
8577 * Perform the SSL handshake
8578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008579int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008580{
8581 int ret = 0;
8582
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008583 if( ssl == NULL || ssl->conf == NULL )
8584 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008588 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008590 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008591
8592 if( ret != 0 )
8593 break;
8594 }
8595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008597
8598 return( ret );
8599}
8600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008601#if defined(MBEDTLS_SSL_RENEGOTIATION)
8602#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008603/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008604 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008606static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008607{
8608 int ret;
8609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008611
8612 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8614 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008615
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008616 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008617 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008618 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008619 return( ret );
8620 }
8621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008623
8624 return( 0 );
8625}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008626#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008627
8628/*
8629 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008630 * - any side: calling mbedtls_ssl_renegotiate(),
8631 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8632 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008633 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008634 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008635 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008638{
8639 int ret;
8640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008642
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008643 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8644 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008645
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008646 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8647 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008648#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008649 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008650 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008651 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008652 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008653 ssl->handshake->out_msg_seq = 1;
8654 else
8655 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008656 }
8657#endif
8658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008659 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8660 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008662 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008664 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008665 return( ret );
8666 }
8667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008669
8670 return( 0 );
8671}
8672
8673/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008674 * Renegotiate current connection on client,
8675 * or request renegotiation on server
8676 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008677int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008678{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008679 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008680
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008681 if( ssl == NULL || ssl->conf == NULL )
8682 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008684#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008685 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008686 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008688 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8689 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008691 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008692
8693 /* Did we already try/start sending HelloRequest? */
8694 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008695 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008696
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008697 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008698 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008699#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008701#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008702 /*
8703 * On client, either start the renegotiation process or,
8704 * if already in progress, continue the handshake
8705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008706 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008708 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8709 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008710
8711 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008713 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008714 return( ret );
8715 }
8716 }
8717 else
8718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008719 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008721 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008722 return( ret );
8723 }
8724 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008725#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008726
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008727 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008728}
8729
8730/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008731 * Check record counters and renegotiate if they're above the limit.
8732 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008733static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008734{
Andres AG2196c7f2016-12-15 17:01:16 +00008735 size_t ep_len = ssl_ep_len( ssl );
8736 int in_ctr_cmp;
8737 int out_ctr_cmp;
8738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008739 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8740 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008741 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008742 {
8743 return( 0 );
8744 }
8745
Andres AG2196c7f2016-12-15 17:01:16 +00008746 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8747 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008748 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008749 ssl->conf->renego_period + ep_len, 8 - ep_len );
8750
8751 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008752 {
8753 return( 0 );
8754 }
8755
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008757 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008758}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008759#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008760
8761/*
8762 * Receive application data decrypted from the SSL layer
8763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008764int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008765{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008766 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008767 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008768
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008769 if( ssl == NULL || ssl->conf == NULL )
8770 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008774#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008775 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008777 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008778 return( ret );
8779
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008780 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008782 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008783 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008784 return( ret );
8785 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008786 }
8787#endif
8788
Hanno Becker4a810fb2017-05-24 16:27:30 +01008789 /*
8790 * Check if renegotiation is necessary and/or handshake is
8791 * in process. If yes, perform/continue, and fall through
8792 * if an unexpected packet is received while the client
8793 * is waiting for the ServerHello.
8794 *
8795 * (There is no equivalent to the last condition on
8796 * the server-side as it is not treated as within
8797 * a handshake while waiting for the ClientHello
8798 * after a renegotiation request.)
8799 */
8800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008801#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008802 ret = ssl_check_ctr_renegotiate( ssl );
8803 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8804 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008806 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008807 return( ret );
8808 }
8809#endif
8810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008811 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008813 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008814 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8815 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008818 return( ret );
8819 }
8820 }
8821
Hanno Beckere41158b2017-10-23 13:30:32 +01008822 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008823 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008824 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008825 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008826 if( ssl->f_get_timer != NULL &&
8827 ssl->f_get_timer( ssl->p_timer ) == -1 )
8828 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008829 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008830 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008831
Hanno Becker327c93b2018-08-15 13:56:18 +01008832 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008833 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008834 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8835 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008836
Hanno Becker4a810fb2017-05-24 16:27:30 +01008837 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8838 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008839 }
8840
8841 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008842 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008843 {
8844 /*
8845 * OpenSSL sends empty messages to randomize the IV
8846 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008847 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008849 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008850 return( 0 );
8851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008852 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008853 return( ret );
8854 }
8855 }
8856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008857 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008860
Hanno Becker4a810fb2017-05-24 16:27:30 +01008861 /*
8862 * - For client-side, expect SERVER_HELLO_REQUEST.
8863 * - For server-side, expect CLIENT_HELLO.
8864 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8865 */
8866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008867#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008868 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008869 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008870 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008873
8874 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008875#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008876 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008877 {
8878 continue;
8879 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008880#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008881 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008882 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008883#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008884
Hanno Becker4a810fb2017-05-24 16:27:30 +01008885#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008886 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008887 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008890
8891 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008892#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008893 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008894 {
8895 continue;
8896 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008897#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008898 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008899 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008900#endif /* MBEDTLS_SSL_SRV_C */
8901
Hanno Becker21df7f92017-10-17 11:03:26 +01008902#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008903 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008904 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8905 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8906 ssl->conf->allow_legacy_renegotiation ==
8907 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8908 {
8909 /*
8910 * Accept renegotiation request
8911 */
Paul Bakker48916f92012-09-16 19:57:18 +00008912
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008913 /* DTLS clients need to know renego is server-initiated */
8914#if defined(MBEDTLS_SSL_PROTO_DTLS)
8915 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8916 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8917 {
8918 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8919 }
8920#endif
8921 ret = ssl_start_renegotiation( ssl );
8922 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8923 ret != 0 )
8924 {
8925 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8926 return( ret );
8927 }
8928 }
8929 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008930#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008931 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008932 /*
8933 * Refuse renegotiation
8934 */
8935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008936 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008938#if defined(MBEDTLS_SSL_PROTO_SSL3)
8939 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008940 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008941 /* SSLv3 does not have a "no_renegotiation" warning, so
8942 we send a fatal alert and abort the connection. */
8943 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8944 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8945 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008946 }
8947 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008948#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8949#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8950 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8951 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008953 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8954 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8955 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008956 {
8957 return( ret );
8958 }
Paul Bakker48916f92012-09-16 19:57:18 +00008959 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008960 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008961#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8962 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8965 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008966 }
Paul Bakker48916f92012-09-16 19:57:18 +00008967 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008968
Hanno Becker90333da2017-10-10 11:27:13 +01008969 /* At this point, we don't know whether the renegotiation has been
8970 * completed or not. The cases to consider are the following:
8971 * 1) The renegotiation is complete. In this case, no new record
8972 * has been read yet.
8973 * 2) The renegotiation is incomplete because the client received
8974 * an application data record while awaiting the ServerHello.
8975 * 3) The renegotiation is incomplete because the client received
8976 * a non-handshake, non-application data message while awaiting
8977 * the ServerHello.
8978 * In each of these case, looping will be the proper action:
8979 * - For 1), the next iteration will read a new record and check
8980 * if it's application data.
8981 * - For 2), the loop condition isn't satisfied as application data
8982 * is present, hence continue is the same as break
8983 * - For 3), the loop condition is satisfied and read_record
8984 * will re-deliver the message that was held back by the client
8985 * when expecting the ServerHello.
8986 */
8987 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008988 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008989#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008990 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008991 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008992 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008993 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008994 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008997 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008998 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008999 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009000 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009001 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009002#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009004 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9005 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009008 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009009 }
9010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009011 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9014 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009015 }
9016
9017 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009018
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009019 /* We're going to return something now, cancel timer,
9020 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009021 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009022 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009023
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009024#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009025 /* If we requested renego but received AppData, resend HelloRequest.
9026 * Do it now, after setting in_offt, to avoid taking this branch
9027 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009028#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009029 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009030 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009031 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009032 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009034 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009035 return( ret );
9036 }
9037 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009038#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009039#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009040 }
9041
9042 n = ( len < ssl->in_msglen )
9043 ? len : ssl->in_msglen;
9044
9045 memcpy( buf, ssl->in_offt, n );
9046 ssl->in_msglen -= n;
9047
9048 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009049 {
9050 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009051 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009052 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009053 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009054 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009055 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009056 /* more data available */
9057 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009058 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009061
Paul Bakker23986e52011-04-24 08:57:21 +00009062 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009063}
9064
9065/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009066 * Send application data to be encrypted by the SSL layer, taking care of max
9067 * fragment length and buffer size.
9068 *
9069 * According to RFC 5246 Section 6.2.1:
9070 *
9071 * Zero-length fragments of Application data MAY be sent as they are
9072 * potentially useful as a traffic analysis countermeasure.
9073 *
9074 * Therefore, it is possible that the input message length is 0 and the
9075 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009076 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009077static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009078 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009079{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009080 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9081 const size_t max_len = (size_t) ret;
9082
9083 if( ret < 0 )
9084 {
9085 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9086 return( ret );
9087 }
9088
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009089 if( len > max_len )
9090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009091#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009092 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009095 "maximum fragment length: %d > %d",
9096 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009097 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009098 }
9099 else
9100#endif
9101 len = max_len;
9102 }
Paul Bakker887bd502011-06-08 13:10:54 +00009103
Paul Bakker5121ce52009-01-03 21:22:43 +00009104 if( ssl->out_left != 0 )
9105 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009106 /*
9107 * The user has previously tried to send the data and
9108 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9109 * written. In this case, we expect the high-level write function
9110 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009112 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009115 return( ret );
9116 }
9117 }
Paul Bakker887bd502011-06-08 13:10:54 +00009118 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009119 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009120 /*
9121 * The user is trying to send a message the first time, so we need to
9122 * copy the data into the internal buffers and setup the data structure
9123 * to keep track of partial writes
9124 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009125 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009126 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009127 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009128
Hanno Becker67bc7c32018-08-06 11:33:50 +01009129 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009131 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009132 return( ret );
9133 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009134 }
9135
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009136 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009137}
9138
9139/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009140 * Write application data, doing 1/n-1 splitting if necessary.
9141 *
9142 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009143 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009144 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009146#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009147static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009148 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009149{
9150 int ret;
9151
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009152 if( ssl->conf->cbc_record_splitting ==
9153 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009154 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009155 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9156 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9157 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009158 {
9159 return( ssl_write_real( ssl, buf, len ) );
9160 }
9161
9162 if( ssl->split_done == 0 )
9163 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009164 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009165 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009166 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009167 }
9168
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009169 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9170 return( ret );
9171 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009172
9173 return( ret + 1 );
9174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009175#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009176
9177/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009178 * Write application data (public-facing wrapper)
9179 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009180int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009181{
9182 int ret;
9183
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009185
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009186 if( ssl == NULL || ssl->conf == NULL )
9187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9188
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009189#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009190 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9191 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009192 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009193 return( ret );
9194 }
9195#endif
9196
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009197 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009198 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009199 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009200 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009202 return( ret );
9203 }
9204 }
9205
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009206#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009207 ret = ssl_write_split( ssl, buf, len );
9208#else
9209 ret = ssl_write_real( ssl, buf, len );
9210#endif
9211
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009213
9214 return( ret );
9215}
9216
9217/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009218 * Notify the peer that the connection is being closed
9219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009220int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009221{
9222 int ret;
9223
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009224 if( ssl == NULL || ssl->conf == NULL )
9225 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009228
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009229 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009230 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009232 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009234 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9235 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9236 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009238 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009239 return( ret );
9240 }
9241 }
9242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009244
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009245 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009246}
9247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009248void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009249{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009250 if( transform == NULL )
9251 return;
9252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009253#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009254 deflateEnd( &transform->ctx_deflate );
9255 inflateEnd( &transform->ctx_inflate );
9256#endif
9257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009258 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9259 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009261 mbedtls_md_free( &transform->md_ctx_enc );
9262 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009263
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009264 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009265}
9266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009267#if defined(MBEDTLS_X509_CRT_PARSE_C)
9268static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009269{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009270 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009271
9272 while( cur != NULL )
9273 {
9274 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009275 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009276 cur = next;
9277 }
9278}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009279#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009280
Hanno Becker0271f962018-08-16 13:23:47 +01009281#if defined(MBEDTLS_SSL_PROTO_DTLS)
9282
9283static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9284{
9285 unsigned offset;
9286 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9287
9288 if( hs == NULL )
9289 return;
9290
Hanno Becker283f5ef2018-08-24 09:34:47 +01009291 ssl_free_buffered_record( ssl );
9292
Hanno Becker0271f962018-08-16 13:23:47 +01009293 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009294 ssl_buffering_free_slot( ssl, offset );
9295}
9296
9297static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9298 uint8_t slot )
9299{
9300 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9301 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009302
9303 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9304 return;
9305
Hanno Beckere605b192018-08-21 15:59:07 +01009306 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009307 {
Hanno Beckere605b192018-08-21 15:59:07 +01009308 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009309 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009310 mbedtls_free( hs_buf->data );
9311 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009312 }
9313}
9314
9315#endif /* MBEDTLS_SSL_PROTO_DTLS */
9316
Gilles Peskine9b562d52018-04-25 20:32:43 +02009317void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009318{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009319 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9320
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009321 if( handshake == NULL )
9322 return;
9323
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009324#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9325 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9326 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009327 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009328 handshake->async_in_progress = 0;
9329 }
9330#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9331
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009332#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9333 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9334 mbedtls_md5_free( &handshake->fin_md5 );
9335 mbedtls_sha1_free( &handshake->fin_sha1 );
9336#endif
9337#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9338#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009339#if defined(MBEDTLS_USE_PSA_CRYPTO)
9340 psa_hash_abort( &handshake->fin_sha256_psa );
9341#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009342 mbedtls_sha256_free( &handshake->fin_sha256 );
9343#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009344#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009345#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009346#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009347 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009348#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009349 mbedtls_sha512_free( &handshake->fin_sha512 );
9350#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009351#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009352#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009354#if defined(MBEDTLS_DHM_C)
9355 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009356#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357#if defined(MBEDTLS_ECDH_C)
9358 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009359#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009360#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009361 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009362#if defined(MBEDTLS_SSL_CLI_C)
9363 mbedtls_free( handshake->ecjpake_cache );
9364 handshake->ecjpake_cache = NULL;
9365 handshake->ecjpake_cache_len = 0;
9366#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009367#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009368
Janos Follath4ae5c292016-02-10 11:27:43 +00009369#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9370 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009371 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009372 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009373#endif
9374
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009375#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9376 if( handshake->psk != NULL )
9377 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009378 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009379 mbedtls_free( handshake->psk );
9380 }
9381#endif
9382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009383#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9384 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009385 /*
9386 * Free only the linked list wrapper, not the keys themselves
9387 * since the belong to the SNI callback
9388 */
9389 if( handshake->sni_key_cert != NULL )
9390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009391 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009392
9393 while( cur != NULL )
9394 {
9395 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009396 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009397 cur = next;
9398 }
9399 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009400#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009401
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009402#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009403 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009404#endif
9405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009406#if defined(MBEDTLS_SSL_PROTO_DTLS)
9407 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009408 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009409 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009410#endif
9411
Hanno Becker4a63ed42019-01-08 11:39:35 +00009412#if defined(MBEDTLS_ECDH_C) && \
9413 defined(MBEDTLS_USE_PSA_CRYPTO)
9414 psa_destroy_key( handshake->ecdh_psa_privkey );
9415#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9416
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009417 mbedtls_platform_zeroize( handshake,
9418 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009419}
9420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009421void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009422{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009423 if( session == NULL )
9424 return;
9425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009426#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009427 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009428#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009429
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009430#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009431 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009432#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009433
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009434 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009435}
9436
Paul Bakker5121ce52009-01-03 21:22:43 +00009437/*
9438 * Free an SSL context
9439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009440void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009441{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009442 if( ssl == NULL )
9443 return;
9444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009446
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009447 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009448 {
Angus Grattond8213d02016-05-25 20:56:48 +10009449 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009450 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009451 }
9452
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009453 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009454 {
Angus Grattond8213d02016-05-25 20:56:48 +10009455 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009456 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009457 }
9458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009459#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009460 if( ssl->compress_buf != NULL )
9461 {
Angus Grattond8213d02016-05-25 20:56:48 +10009462 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009463 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009464 }
9465#endif
9466
Paul Bakker48916f92012-09-16 19:57:18 +00009467 if( ssl->transform )
9468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009469 mbedtls_ssl_transform_free( ssl->transform );
9470 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009471 }
9472
9473 if( ssl->handshake )
9474 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009475 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009476 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9477 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009479 mbedtls_free( ssl->handshake );
9480 mbedtls_free( ssl->transform_negotiate );
9481 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009482 }
9483
Paul Bakkerc0463502013-02-14 11:19:38 +01009484 if( ssl->session )
9485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486 mbedtls_ssl_session_free( ssl->session );
9487 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009488 }
9489
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009490#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009491 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009492 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009493 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009494 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009495 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009496#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009498#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9499 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9502 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009503 }
9504#endif
9505
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009506#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009508#endif
9509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009511
Paul Bakker86f04f42013-02-14 11:20:09 +01009512 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009513 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009514}
9515
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009516/*
9517 * Initialze mbedtls_ssl_config
9518 */
9519void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9520{
9521 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9522}
9523
Simon Butcherc97b6972015-12-27 23:48:17 +00009524#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009525static int ssl_preset_default_hashes[] = {
9526#if defined(MBEDTLS_SHA512_C)
9527 MBEDTLS_MD_SHA512,
9528 MBEDTLS_MD_SHA384,
9529#endif
9530#if defined(MBEDTLS_SHA256_C)
9531 MBEDTLS_MD_SHA256,
9532 MBEDTLS_MD_SHA224,
9533#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009534#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009535 MBEDTLS_MD_SHA1,
9536#endif
9537 MBEDTLS_MD_NONE
9538};
Simon Butcherc97b6972015-12-27 23:48:17 +00009539#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009540
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009541static int ssl_preset_suiteb_ciphersuites[] = {
9542 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9543 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9544 0
9545};
9546
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009547#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009548static int ssl_preset_suiteb_hashes[] = {
9549 MBEDTLS_MD_SHA256,
9550 MBEDTLS_MD_SHA384,
9551 MBEDTLS_MD_NONE
9552};
9553#endif
9554
9555#if defined(MBEDTLS_ECP_C)
9556static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9557 MBEDTLS_ECP_DP_SECP256R1,
9558 MBEDTLS_ECP_DP_SECP384R1,
9559 MBEDTLS_ECP_DP_NONE
9560};
9561#endif
9562
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009563/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009564 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009565 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009566int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009567 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009568{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009569#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009570 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009571#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009572
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009573 /* Use the functions here so that they are covered in tests,
9574 * but otherwise access member directly for efficiency */
9575 mbedtls_ssl_conf_endpoint( conf, endpoint );
9576 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009577
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009578 /*
9579 * Things that are common to all presets
9580 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009581#if defined(MBEDTLS_SSL_CLI_C)
9582 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9583 {
9584 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9585#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9586 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9587#endif
9588 }
9589#endif
9590
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009591#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009592 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009593#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009594
9595#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9596 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9597#endif
9598
9599#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9600 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9601#endif
9602
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009603#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9604 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9605#endif
9606
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009607#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009608 conf->f_cookie_write = ssl_cookie_write_dummy;
9609 conf->f_cookie_check = ssl_cookie_check_dummy;
9610#endif
9611
9612#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9613 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9614#endif
9615
Janos Follath088ce432017-04-10 12:42:31 +01009616#if defined(MBEDTLS_SSL_SRV_C)
9617 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9618#endif
9619
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009620#if defined(MBEDTLS_SSL_PROTO_DTLS)
9621 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9622 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9623#endif
9624
9625#if defined(MBEDTLS_SSL_RENEGOTIATION)
9626 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009627 memset( conf->renego_period, 0x00, 2 );
9628 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009629#endif
9630
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009631#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9632 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9633 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009634 const unsigned char dhm_p[] =
9635 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9636 const unsigned char dhm_g[] =
9637 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9638
Hanno Beckera90658f2017-10-04 15:29:08 +01009639 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9640 dhm_p, sizeof( dhm_p ),
9641 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009642 {
9643 return( ret );
9644 }
9645 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009646#endif
9647
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009648 /*
9649 * Preset-specific defaults
9650 */
9651 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009652 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009653 /*
9654 * NSA Suite B
9655 */
9656 case MBEDTLS_SSL_PRESET_SUITEB:
9657 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9658 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9659 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9660 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9661
9662 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9663 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9664 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9665 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9666 ssl_preset_suiteb_ciphersuites;
9667
9668#if defined(MBEDTLS_X509_CRT_PARSE_C)
9669 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009670#endif
9671
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009672#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009673 conf->sig_hashes = ssl_preset_suiteb_hashes;
9674#endif
9675
9676#if defined(MBEDTLS_ECP_C)
9677 conf->curve_list = ssl_preset_suiteb_curves;
9678#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009679 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009680
9681 /*
9682 * Default
9683 */
9684 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009685 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9686 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9687 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9688 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9689 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9690 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9691 MBEDTLS_SSL_MIN_MINOR_VERSION :
9692 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009693 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9694 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9695
9696#if defined(MBEDTLS_SSL_PROTO_DTLS)
9697 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9698 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9699#endif
9700
9701 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9702 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9703 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9704 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9705 mbedtls_ssl_list_ciphersuites();
9706
9707#if defined(MBEDTLS_X509_CRT_PARSE_C)
9708 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9709#endif
9710
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009711#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009712 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009713#endif
9714
9715#if defined(MBEDTLS_ECP_C)
9716 conf->curve_list = mbedtls_ecp_grp_id_list();
9717#endif
9718
9719#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9720 conf->dhm_min_bitlen = 1024;
9721#endif
9722 }
9723
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009724 return( 0 );
9725}
9726
9727/*
9728 * Free mbedtls_ssl_config
9729 */
9730void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9731{
9732#if defined(MBEDTLS_DHM_C)
9733 mbedtls_mpi_free( &conf->dhm_P );
9734 mbedtls_mpi_free( &conf->dhm_G );
9735#endif
9736
9737#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9738 if( conf->psk != NULL )
9739 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009740 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009741 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009742 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009743 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009744 }
9745
9746 if( conf->psk_identity != NULL )
9747 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009748 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009749 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009750 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009751 conf->psk_identity_len = 0;
9752 }
9753#endif
9754
9755#if defined(MBEDTLS_X509_CRT_PARSE_C)
9756 ssl_key_cert_free( conf->key_cert );
9757#endif
9758
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009759 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009760}
9761
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009762#if defined(MBEDTLS_PK_C) && \
9763 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009764/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009765 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009766 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009768{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769#if defined(MBEDTLS_RSA_C)
9770 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9771 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009772#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773#if defined(MBEDTLS_ECDSA_C)
9774 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9775 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009776#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009777 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009778}
9779
Hanno Becker7e5437a2017-04-28 17:15:26 +01009780unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9781{
9782 switch( type ) {
9783 case MBEDTLS_PK_RSA:
9784 return( MBEDTLS_SSL_SIG_RSA );
9785 case MBEDTLS_PK_ECDSA:
9786 case MBEDTLS_PK_ECKEY:
9787 return( MBEDTLS_SSL_SIG_ECDSA );
9788 default:
9789 return( MBEDTLS_SSL_SIG_ANON );
9790 }
9791}
9792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009793mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009794{
9795 switch( sig )
9796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009797#if defined(MBEDTLS_RSA_C)
9798 case MBEDTLS_SSL_SIG_RSA:
9799 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009800#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801#if defined(MBEDTLS_ECDSA_C)
9802 case MBEDTLS_SSL_SIG_ECDSA:
9803 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009804#endif
9805 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009807 }
9808}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009809#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009810
Hanno Becker7e5437a2017-04-28 17:15:26 +01009811#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9812 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9813
9814/* Find an entry in a signature-hash set matching a given hash algorithm. */
9815mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9816 mbedtls_pk_type_t sig_alg )
9817{
9818 switch( sig_alg )
9819 {
9820 case MBEDTLS_PK_RSA:
9821 return( set->rsa );
9822 case MBEDTLS_PK_ECDSA:
9823 return( set->ecdsa );
9824 default:
9825 return( MBEDTLS_MD_NONE );
9826 }
9827}
9828
9829/* Add a signature-hash-pair to a signature-hash set */
9830void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9831 mbedtls_pk_type_t sig_alg,
9832 mbedtls_md_type_t md_alg )
9833{
9834 switch( sig_alg )
9835 {
9836 case MBEDTLS_PK_RSA:
9837 if( set->rsa == MBEDTLS_MD_NONE )
9838 set->rsa = md_alg;
9839 break;
9840
9841 case MBEDTLS_PK_ECDSA:
9842 if( set->ecdsa == MBEDTLS_MD_NONE )
9843 set->ecdsa = md_alg;
9844 break;
9845
9846 default:
9847 break;
9848 }
9849}
9850
9851/* Allow exactly one hash algorithm for each signature. */
9852void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9853 mbedtls_md_type_t md_alg )
9854{
9855 set->rsa = md_alg;
9856 set->ecdsa = md_alg;
9857}
9858
9859#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9860 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9861
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009862/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009863 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009865mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009866{
9867 switch( hash )
9868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009869#if defined(MBEDTLS_MD5_C)
9870 case MBEDTLS_SSL_HASH_MD5:
9871 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009872#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009873#if defined(MBEDTLS_SHA1_C)
9874 case MBEDTLS_SSL_HASH_SHA1:
9875 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009876#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009877#if defined(MBEDTLS_SHA256_C)
9878 case MBEDTLS_SSL_HASH_SHA224:
9879 return( MBEDTLS_MD_SHA224 );
9880 case MBEDTLS_SSL_HASH_SHA256:
9881 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009882#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883#if defined(MBEDTLS_SHA512_C)
9884 case MBEDTLS_SSL_HASH_SHA384:
9885 return( MBEDTLS_MD_SHA384 );
9886 case MBEDTLS_SSL_HASH_SHA512:
9887 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009888#endif
9889 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009890 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009891 }
9892}
9893
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009894/*
9895 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9896 */
9897unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9898{
9899 switch( md )
9900 {
9901#if defined(MBEDTLS_MD5_C)
9902 case MBEDTLS_MD_MD5:
9903 return( MBEDTLS_SSL_HASH_MD5 );
9904#endif
9905#if defined(MBEDTLS_SHA1_C)
9906 case MBEDTLS_MD_SHA1:
9907 return( MBEDTLS_SSL_HASH_SHA1 );
9908#endif
9909#if defined(MBEDTLS_SHA256_C)
9910 case MBEDTLS_MD_SHA224:
9911 return( MBEDTLS_SSL_HASH_SHA224 );
9912 case MBEDTLS_MD_SHA256:
9913 return( MBEDTLS_SSL_HASH_SHA256 );
9914#endif
9915#if defined(MBEDTLS_SHA512_C)
9916 case MBEDTLS_MD_SHA384:
9917 return( MBEDTLS_SSL_HASH_SHA384 );
9918 case MBEDTLS_MD_SHA512:
9919 return( MBEDTLS_SSL_HASH_SHA512 );
9920#endif
9921 default:
9922 return( MBEDTLS_SSL_HASH_NONE );
9923 }
9924}
9925
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009926#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009927/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009928 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009929 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009930 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009931int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009932{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009933 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009934
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009935 if( ssl->conf->curve_list == NULL )
9936 return( -1 );
9937
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009938 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009939 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009940 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009941
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009942 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009943}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009944#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009945
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009946#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009947/*
9948 * Check if a hash proposed by the peer is in our list.
9949 * Return 0 if we're willing to use it, -1 otherwise.
9950 */
9951int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9952 mbedtls_md_type_t md )
9953{
9954 const int *cur;
9955
9956 if( ssl->conf->sig_hashes == NULL )
9957 return( -1 );
9958
9959 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9960 if( *cur == (int) md )
9961 return( 0 );
9962
9963 return( -1 );
9964}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009965#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009967#if defined(MBEDTLS_X509_CRT_PARSE_C)
9968int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9969 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009970 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009971 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009972{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009973 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009974#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009975 int usage = 0;
9976#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009977#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009978 const char *ext_oid;
9979 size_t ext_len;
9980#endif
9981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009982#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9983 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009984 ((void) cert);
9985 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009986 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009987#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009989#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9990 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009991 {
9992 /* Server part of the key exchange */
9993 switch( ciphersuite->key_exchange )
9994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009995 case MBEDTLS_KEY_EXCHANGE_RSA:
9996 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009997 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009998 break;
9999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010000 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10001 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10002 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10003 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010004 break;
10005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010006 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10007 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010008 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010009 break;
10010
10011 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010012 case MBEDTLS_KEY_EXCHANGE_NONE:
10013 case MBEDTLS_KEY_EXCHANGE_PSK:
10014 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10015 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010016 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010017 usage = 0;
10018 }
10019 }
10020 else
10021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010022 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10023 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010024 }
10025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010026 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010027 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010028 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010029 ret = -1;
10030 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010031#else
10032 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010033#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010035#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10036 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010038 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10039 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010040 }
10041 else
10042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010043 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10044 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010045 }
10046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010047 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010048 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010049 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010050 ret = -1;
10051 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010052#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010053
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010054 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010055}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010056#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010057
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010058/*
10059 * Convert version numbers to/from wire format
10060 * and, for DTLS, to/from TLS equivalent.
10061 *
10062 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010063 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010064 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10065 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010067void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010068 unsigned char ver[2] )
10069{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010070#if defined(MBEDTLS_SSL_PROTO_DTLS)
10071 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010073 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010074 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10075
10076 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10077 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10078 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010079 else
10080#else
10081 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010082#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010083 {
10084 ver[0] = (unsigned char) major;
10085 ver[1] = (unsigned char) minor;
10086 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010087}
10088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010089void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010090 const unsigned char ver[2] )
10091{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010092#if defined(MBEDTLS_SSL_PROTO_DTLS)
10093 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010094 {
10095 *major = 255 - ver[0] + 2;
10096 *minor = 255 - ver[1] + 1;
10097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010098 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010099 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10100 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010101 else
10102#else
10103 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010104#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010105 {
10106 *major = ver[0];
10107 *minor = ver[1];
10108 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010109}
10110
Simon Butcher99000142016-10-13 17:21:01 +010010111int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10112{
10113#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10114 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10115 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10116
10117 switch( md )
10118 {
10119#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10120#if defined(MBEDTLS_MD5_C)
10121 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010122 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010123#endif
10124#if defined(MBEDTLS_SHA1_C)
10125 case MBEDTLS_SSL_HASH_SHA1:
10126 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10127 break;
10128#endif
10129#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10130#if defined(MBEDTLS_SHA512_C)
10131 case MBEDTLS_SSL_HASH_SHA384:
10132 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10133 break;
10134#endif
10135#if defined(MBEDTLS_SHA256_C)
10136 case MBEDTLS_SSL_HASH_SHA256:
10137 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10138 break;
10139#endif
10140 default:
10141 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10142 }
10143
10144 return 0;
10145#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10146 (void) ssl;
10147 (void) md;
10148
10149 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10150#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10151}
10152
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010153#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10154 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10155int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10156 unsigned char *output,
10157 unsigned char *data, size_t data_len )
10158{
10159 int ret = 0;
10160 mbedtls_md5_context mbedtls_md5;
10161 mbedtls_sha1_context mbedtls_sha1;
10162
10163 mbedtls_md5_init( &mbedtls_md5 );
10164 mbedtls_sha1_init( &mbedtls_sha1 );
10165
10166 /*
10167 * digitally-signed struct {
10168 * opaque md5_hash[16];
10169 * opaque sha_hash[20];
10170 * };
10171 *
10172 * md5_hash
10173 * MD5(ClientHello.random + ServerHello.random
10174 * + ServerParams);
10175 * sha_hash
10176 * SHA(ClientHello.random + ServerHello.random
10177 * + ServerParams);
10178 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010179 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010180 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010181 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010182 goto exit;
10183 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010184 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010185 ssl->handshake->randbytes, 64 ) ) != 0 )
10186 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010187 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010188 goto exit;
10189 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010190 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010191 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010193 goto exit;
10194 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010195 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010196 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010198 goto exit;
10199 }
10200
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010201 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010202 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010203 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010204 goto exit;
10205 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010206 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010207 ssl->handshake->randbytes, 64 ) ) != 0 )
10208 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010209 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010210 goto exit;
10211 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010212 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010213 data_len ) ) != 0 )
10214 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010215 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010216 goto exit;
10217 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010218 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010219 output + 16 ) ) != 0 )
10220 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010221 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010222 goto exit;
10223 }
10224
10225exit:
10226 mbedtls_md5_free( &mbedtls_md5 );
10227 mbedtls_sha1_free( &mbedtls_sha1 );
10228
10229 if( ret != 0 )
10230 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10231 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10232
10233 return( ret );
10234
10235}
10236#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10237 MBEDTLS_SSL_PROTO_TLS1_1 */
10238
10239#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10240 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010241
10242#if defined(MBEDTLS_USE_PSA_CRYPTO)
10243int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10244 unsigned char *hash, size_t *hashlen,
10245 unsigned char *data, size_t data_len,
10246 mbedtls_md_type_t md_alg )
10247{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010248 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010249 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010250 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10251
Andrzej Kurek5615dab2019-01-16 05:26:25 -050010252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010253
10254 if( ( status = psa_hash_setup( &hash_operation,
10255 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010256 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010257 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010258 goto exit;
10259 }
10260
Andrzej Kurek814feff2019-01-14 04:35:19 -050010261 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10262 64 ) ) != 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_update", 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,
10269 data, data_len ) ) != 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_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10276 hashlen ) ) != 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_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010279 goto exit;
10280 }
10281
10282exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010283 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010284 {
10285 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10286 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010287 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010288 {
10289 case PSA_ERROR_NOT_SUPPORTED:
10290 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010291 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010292 case PSA_ERROR_BUFFER_TOO_SMALL:
10293 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10294 case PSA_ERROR_INSUFFICIENT_MEMORY:
10295 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10296 default:
10297 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10298 }
10299 }
10300 return( 0 );
10301}
10302
10303#else
10304
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010305int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010306 unsigned char *hash, size_t *hashlen,
10307 unsigned char *data, size_t data_len,
10308 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010309{
10310 int ret = 0;
10311 mbedtls_md_context_t ctx;
10312 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010313 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010314
Andrzej Kurek5615dab2019-01-16 05:26:25 -050010315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010316
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010317 mbedtls_md_init( &ctx );
10318
10319 /*
10320 * digitally-signed struct {
10321 * opaque client_random[32];
10322 * opaque server_random[32];
10323 * ServerDHParams params;
10324 * };
10325 */
10326 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10327 {
10328 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10329 goto exit;
10330 }
10331 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10332 {
10333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10334 goto exit;
10335 }
10336 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10337 {
10338 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10339 goto exit;
10340 }
10341 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10342 {
10343 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10344 goto exit;
10345 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010346 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010347 {
10348 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10349 goto exit;
10350 }
10351
10352exit:
10353 mbedtls_md_free( &ctx );
10354
10355 if( ret != 0 )
10356 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10357 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10358
10359 return( ret );
10360}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010361#endif /* MBEDTLS_USE_PSA_CRYPTO */
10362
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010363#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10364 MBEDTLS_SSL_PROTO_TLS1_2 */
10365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010366#endif /* MBEDTLS_SSL_TLS_C */