blob: ac652d2d785f449ce2834b77323e0fda46866dde [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Andrzej Kurekc929a822019-01-14 03:51:11 -050062#if defined(MBEDTLS_USE_PSA_CRYPTO)
63#include "mbedtls/psa_util.h"
64#endif
65
Hanno Becker2a43f6f2018-08-10 11:12:52 +010066static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010067static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010068
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081/*
82 * Start a timer.
83 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020087 if( ssl->f_set_timer == NULL )
88 return;
89
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
91 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020099 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200100 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200101
102 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 {
104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200106 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
108 return( 0 );
109}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200110
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100111static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
112 mbedtls_ssl_transform *transform );
113static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
114 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100115
116#define SSL_DONT_FORCE_FLUSH 0
117#define SSL_FORCE_FLUSH 1
118
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200119#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100120
Hanno Beckerd5847772018-08-28 10:09:23 +0100121/* Forward declarations for functions related to message buffering. */
122static void ssl_buffering_free( mbedtls_ssl_context *ssl );
123static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
124 uint8_t slot );
125static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
126static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
127static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
128static int ssl_buffer_message( mbedtls_ssl_context *ssl );
129static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100130static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100131
Hanno Beckera67dee22018-08-22 10:05:20 +0100132static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100133static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100134{
Hanno Becker11682cc2018-08-22 14:41:02 +0100135 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100136
137 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100138 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100139
140 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
141}
142
Hanno Becker67bc7c32018-08-06 11:33:50 +0100143static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
144{
Hanno Becker11682cc2018-08-22 14:41:02 +0100145 size_t const bytes_written = ssl->out_left;
146 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100147
148 /* Double-check that the write-index hasn't gone
149 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100150 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100151 {
152 /* Should never happen... */
153 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
154 }
155
156 return( (int) ( mtu - bytes_written ) );
157}
158
159static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
160{
161 int ret;
162 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400163 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100164
165#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
166 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
167
168 if( max_len > mfl )
169 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100170
171 /* By the standard (RFC 6066 Sect. 4), the MFL extension
172 * only limits the maximum record payload size, so in theory
173 * we would be allowed to pack multiple records of payload size
174 * MFL into a single datagram. However, this would mean that there's
175 * no way to explicitly communicate MTU restrictions to the peer.
176 *
177 * The following reduction of max_len makes sure that we never
178 * write datagrams larger than MFL + Record Expansion Overhead.
179 */
180 if( max_len <= ssl->out_left )
181 return( 0 );
182
183 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100184#endif
185
186 ret = ssl_get_remaining_space_in_datagram( ssl );
187 if( ret < 0 )
188 return( ret );
189 remaining = (size_t) ret;
190
191 ret = mbedtls_ssl_get_record_expansion( ssl );
192 if( ret < 0 )
193 return( ret );
194 expansion = (size_t) ret;
195
196 if( remaining <= expansion )
197 return( 0 );
198
199 remaining -= expansion;
200 if( remaining >= max_len )
201 remaining = max_len;
202
203 return( (int) remaining );
204}
205
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200206/*
207 * Double the retransmit timeout value, within the allowed range,
208 * returning -1 if the maximum value has already been reached.
209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200211{
212 uint32_t new_timeout;
213
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200214 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200215 return( -1 );
216
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200217 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
218 * in the following way: after the initial transmission and a first
219 * retransmission, back off to a temporary estimated MTU of 508 bytes.
220 * This value is guaranteed to be deliverable (if not guaranteed to be
221 * delivered) of any compliant IPv4 (and IPv6) network, and should work
222 * on most non-IP stacks too. */
223 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400224 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200225 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
227 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200228
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200229 new_timeout = 2 * ssl->handshake->retransmit_timeout;
230
231 /* Avoid arithmetic overflow and range overflow */
232 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200233 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200234 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200235 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200236 }
237
238 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200240 ssl->handshake->retransmit_timeout ) );
241
242 return( 0 );
243}
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200246{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200247 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200249 ssl->handshake->retransmit_timeout ) );
250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200254/*
255 * Convert max_fragment_length codes to length.
256 * RFC 6066 says:
257 * enum{
258 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
259 * } MaxFragmentLength;
260 * and we add 0 -> extension unused
261 */
Angus Grattond8213d02016-05-25 20:56:48 +1000262static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200263{
Angus Grattond8213d02016-05-25 20:56:48 +1000264 switch( mfl )
265 {
266 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
267 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
268 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
269 return 512;
270 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
271 return 1024;
272 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
273 return 2048;
274 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
275 return 4096;
276 default:
277 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
278 }
279}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200281
Hanno Becker52055ae2019-02-06 14:30:46 +0000282int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
283 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_ssl_session_free( dst );
286 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200289 if( src->peer_cert != NULL )
290 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200291 int ret;
292
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200293 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200294 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200295 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200300 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303 dst->peer_cert = NULL;
304 return( ret );
305 }
306 }
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}
322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
324int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200325 const unsigned char *key_enc, const unsigned char *key_dec,
326 size_t keylen,
327 const unsigned char *iv_enc, const unsigned char *iv_dec,
328 size_t ivlen,
329 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200330 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
332int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
333int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
334int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
335int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
336#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000337
Paul Bakker5121ce52009-01-03 21:22:43 +0000338/*
339 * Key material generation
340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200342static int ssl3_prf( const unsigned char *secret, size_t slen,
343 const char *label,
344 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000345 unsigned char *dstbuf, size_t dlen )
346{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100347 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000348 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200349 mbedtls_md5_context md5;
350 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000351 unsigned char padding[16];
352 unsigned char sha1sum[20];
353 ((void)label);
354
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200355 mbedtls_md5_init( &md5 );
356 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200357
Paul Bakker5f70b252012-09-13 14:23:06 +0000358 /*
359 * SSLv3:
360 * block =
361 * MD5( secret + SHA1( 'A' + secret + random ) ) +
362 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
363 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
364 * ...
365 */
366 for( i = 0; i < dlen / 16; i++ )
367 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200368 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000369
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100370 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100371 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100372 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100373 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100374 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100375 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100376 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100377 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100378 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100379 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000380
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100381 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100382 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100383 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100384 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100385 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100386 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100387 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100388 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000389 }
390
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100391exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200392 mbedtls_md5_free( &md5 );
393 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000394
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500395 mbedtls_platform_zeroize( padding, sizeof( padding ) );
396 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000397
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100398 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000399}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200403static int tls1_prf( const unsigned char *secret, size_t slen,
404 const char *label,
405 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000406 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000407{
Paul Bakker23986e52011-04-24 08:57:21 +0000408 size_t nb, hs;
409 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200410 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 unsigned char tmp[128];
412 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 const mbedtls_md_info_t *md_info;
414 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100415 int ret;
416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000418
419 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000421
422 hs = ( slen + 1 ) / 2;
423 S1 = secret;
424 S2 = secret + slen - hs;
425
426 nb = strlen( label );
427 memcpy( tmp + 20, label, nb );
428 memcpy( tmp + 20 + nb, random, rlen );
429 nb += rlen;
430
431 /*
432 * First compute P_md5(secret,label+random)[0..dlen]
433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
435 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100438 return( ret );
439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
441 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
442 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000443
444 for( i = 0; i < dlen; i += 16 )
445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_md_hmac_reset ( &md_ctx );
447 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
448 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_md_hmac_reset ( &md_ctx );
451 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
452 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
454 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
455
456 for( j = 0; j < k; j++ )
457 dstbuf[i + j] = h_i[j];
458 }
459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100461
Paul Bakker5121ce52009-01-03 21:22:43 +0000462 /*
463 * XOR out with P_sha1(secret,label+random)[0..dlen]
464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
466 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100469 return( ret );
470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
472 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
473 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000474
475 for( i = 0; i < dlen; i += 20 )
476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_md_hmac_reset ( &md_ctx );
478 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
479 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 mbedtls_md_hmac_reset ( &md_ctx );
482 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
483 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
485 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
486
487 for( j = 0; j < k; j++ )
488 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
489 }
490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100492
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500493 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
494 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496 return( 0 );
497}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500501#if defined(MBEDTLS_USE_PSA_CRYPTO)
502static int tls_prf_generic( mbedtls_md_type_t md_type,
503 const unsigned char *secret, size_t slen,
504 const char *label,
505 const unsigned char *random, size_t rlen,
506 unsigned char *dstbuf, size_t dlen )
507{
508 psa_status_t status;
509 psa_algorithm_t alg;
510 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500511 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500512 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
513
Andrzej Kurek2f760752019-01-28 08:08:15 -0500514 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500515 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500516
Andrzej Kurekc929a822019-01-14 03:51:11 -0500517 if( md_type == MBEDTLS_MD_SHA384 )
518 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
519 else
520 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
521
Andrzej Kurek2f760752019-01-28 08:08:15 -0500522 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500523 psa_key_policy_set_usage( &policy,
524 PSA_KEY_USAGE_DERIVE,
525 alg );
526 status = psa_set_key_policy( master_slot, &policy );
527 if( status != PSA_SUCCESS )
528 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
529
530 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
531 if( status != PSA_SUCCESS )
532 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
533
534 status = psa_key_derivation( &generator,
535 master_slot, alg,
536 random, rlen,
537 (unsigned char const *) label,
538 (size_t) strlen( label ),
539 dlen );
540 if( status != PSA_SUCCESS )
541 {
542 psa_generator_abort( &generator );
543 psa_destroy_key( master_slot );
544 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
545 }
546
547 status = psa_generator_read( &generator, dstbuf, dlen );
548 if( status != PSA_SUCCESS )
549 {
550 psa_generator_abort( &generator );
551 psa_destroy_key( master_slot );
552 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
553 }
554
555 status = psa_generator_abort( &generator );
556 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500557 {
558 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500559 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500560 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500561
562 status = psa_destroy_key( master_slot );
563 if( status != PSA_SUCCESS )
564 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
565
Andrzej Kurek33171262019-01-15 03:25:18 -0500566 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500567}
568
569#else /* MBEDTLS_USE_PSA_CRYPTO */
570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100572 const unsigned char *secret, size_t slen,
573 const char *label,
574 const unsigned char *random, size_t rlen,
575 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000576{
577 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100578 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000579 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
581 const mbedtls_md_info_t *md_info;
582 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100583 int ret;
584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100591
592 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000594
595 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100596 memcpy( tmp + md_len, label, nb );
597 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000598 nb += rlen;
599
600 /*
601 * Compute P_<hash>(secret, label + random)[0..dlen]
602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100604 return( ret );
605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
607 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
608 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100609
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100610 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_md_hmac_reset ( &md_ctx );
613 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
614 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_md_hmac_reset ( &md_ctx );
617 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
618 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000619
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100620 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000621
622 for( j = 0; j < k; j++ )
623 dstbuf[i + j] = h_i[j];
624 }
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100627
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500628 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
629 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000630
631 return( 0 );
632}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500633#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100635static int tls_prf_sha256( const unsigned char *secret, size_t slen,
636 const char *label,
637 const unsigned char *random, size_t rlen,
638 unsigned char *dstbuf, size_t dlen )
639{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100641 label, random, rlen, dstbuf, dlen ) );
642}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200646static int tls_prf_sha384( const unsigned char *secret, size_t slen,
647 const char *label,
648 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000649 unsigned char *dstbuf, size_t dlen )
650{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100652 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000653}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654#endif /* MBEDTLS_SHA512_C */
655#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
660 defined(MBEDTLS_SSL_PROTO_TLS1_1)
661static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200662#endif
Paul Bakker380da532012-04-18 16:10:25 +0000663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664#if defined(MBEDTLS_SSL_PROTO_SSL3)
665static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
666static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200667#endif
668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
670static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
671static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200672#endif
673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
675#if defined(MBEDTLS_SHA256_C)
676static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
677static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
678static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200679#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#if defined(MBEDTLS_SHA512_C)
682static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
683static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
684static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100685#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000687
Hanno Becker7d0a5692018-10-23 15:26:22 +0100688#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) && \
689 defined(MBEDTLS_USE_PSA_CRYPTO)
690static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
691{
692 if( ssl->conf->f_psk != NULL )
693 {
694 /* If we've used a callback to select the PSK,
695 * the static configuration is irrelevant. */
696 if( ssl->handshake->psk_opaque != 0 )
697 return( 1 );
698
699 return( 0 );
700 }
701
702 if( ssl->conf->psk_opaque != 0 )
703 return( 1 );
704
705 return( 0 );
706}
707#endif /* MBEDTLS_USE_PSA_CRYPTO &&
708 MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000711{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200712 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000713#if defined(MBEDTLS_USE_PSA_CRYPTO)
714 int psa_fallthrough;
715#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000716 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000717 unsigned char keyblk[256];
718 unsigned char *key1;
719 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100720 unsigned char *mac_enc;
721 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000722 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200723 size_t iv_copy_len;
Hanno Beckerf704bef2018-11-16 15:21:18 +0000724 size_t taglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 const mbedtls_cipher_info_t *cipher_info;
726 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100727
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000728 /* cf. RFC 5246, Section 8.1:
729 * "The master secret is always exactly 48 bytes in length." */
730 size_t const master_secret_len = 48;
731
Hanno Becker35b23c72018-10-23 12:10:41 +0100732#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
733 unsigned char session_hash[48];
734#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_ssl_session *session = ssl->session_negotiate;
737 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
738 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100743 if( cipher_info == NULL )
744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100746 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100748 }
749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100751 if( md_info == NULL )
752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100754 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100756 }
757
Paul Bakker5121ce52009-01-03 21:22:43 +0000758 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000759 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761#if defined(MBEDTLS_SSL_PROTO_SSL3)
762 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000763 {
Paul Bakker48916f92012-09-16 19:57:18 +0000764 handshake->tls_prf = ssl3_prf;
765 handshake->calc_verify = ssl_calc_verify_ssl;
766 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000767 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200768 else
769#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
771 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000772 {
Paul Bakker48916f92012-09-16 19:57:18 +0000773 handshake->tls_prf = tls1_prf;
774 handshake->calc_verify = ssl_calc_verify_tls;
775 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000776 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200777 else
778#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
780#if defined(MBEDTLS_SHA512_C)
781 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
782 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000783 {
Paul Bakker48916f92012-09-16 19:57:18 +0000784 handshake->tls_prf = tls_prf_sha384;
785 handshake->calc_verify = ssl_calc_verify_tls_sha384;
786 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000787 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000788 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200789#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#if defined(MBEDTLS_SHA256_C)
791 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000792 {
Paul Bakker48916f92012-09-16 19:57:18 +0000793 handshake->tls_prf = tls_prf_sha256;
794 handshake->calc_verify = ssl_calc_verify_tls_sha256;
795 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000796 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200797 else
798#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
802 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200803 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000804
805 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000806 * SSLv3:
807 * master =
808 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
809 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
810 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200811 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200812 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000813 * master = PRF( premaster, "master secret", randbytes )[0..47]
814 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100815 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000816 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100817 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
818 }
819 else
820 {
821 /* The label for the KDF used for key expansion.
822 * This is either "master secret" or "extended master secret"
823 * depending on whether the Extended Master Secret extension
824 * is used. */
825 char const *lbl = "master secret";
826
827 /* The salt for the KDF used for key expansion.
828 * - If the Extended Master Secret extension is not used,
829 * this is ClientHello.Random + ServerHello.Random
830 * (see Sect. 8.1 in RFC 5246).
831 * - If the Extended Master Secret extension is used,
832 * this is the transcript of the handshake so far.
833 * (see Sect. 4 in RFC 7627). */
834 unsigned char const *salt = handshake->randbytes;
835 size_t salt_len = 64;
836
837#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
838 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
839 ssl->transform_negotiate->ciphersuite_info;
840 mbedtls_md_type_t const md_type = ciphersuite_info->mac;
841#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Paul Bakker5121ce52009-01-03 21:22:43 +0000842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
844 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200847
Hanno Becker35b23c72018-10-23 12:10:41 +0100848 lbl = "extended master secret";
849 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200850 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
852 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854#if defined(MBEDTLS_SHA512_C)
Hanno Becker35b23c72018-10-23 12:10:41 +0100855 if( md_type == MBEDTLS_MD_SHA384 )
856 salt_len = 48;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200857 else
Hanno Becker35b23c72018-10-23 12:10:41 +0100858#endif /* MBEDTLS_SHA512_C */
859 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200860 }
861 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100863 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200864
Hanno Becker35b23c72018-10-23 12:10:41 +0100865 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200866 }
Hanno Becker35b23c72018-10-23 12:10:41 +0100867#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
868
Hanno Becker7d0a5692018-10-23 15:26:22 +0100869#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
870 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
871 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
872 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
873 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100874 {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100875 /* Perform PSK-to-MS expansion in a single step. */
876 psa_status_t status;
877 psa_algorithm_t alg;
878 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500879 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100880
881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
882
883 psk = ssl->conf->psk_opaque;
884 if( ssl->handshake->psk_opaque != 0 )
885 psk = ssl->handshake->psk_opaque;
886
887 if( md_type == MBEDTLS_MD_SHA384 )
888 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
889 else
890 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
891
892 status = psa_key_derivation( &generator, psk, alg,
893 salt, salt_len,
894 (unsigned char const *) lbl,
895 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000896 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100897 if( status != PSA_SUCCESS )
898 {
899 psa_generator_abort( &generator );
900 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
901 }
902
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000903 status = psa_generator_read( &generator, session->master,
904 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100905 if( status != PSA_SUCCESS )
906 {
907 psa_generator_abort( &generator );
908 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
909 }
910
911 status = psa_generator_abort( &generator );
912 if( status != PSA_SUCCESS )
913 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100914 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100915 else
916#endif
917 {
918 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
919 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000920 session->master,
921 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +0100922 if( ret != 0 )
923 {
924 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
925 return( ret );
926 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200927
Hanno Becker7d0a5692018-10-23 15:26:22 +0100928 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
929 handshake->premaster,
930 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +0100931
Hanno Becker7d0a5692018-10-23 15:26:22 +0100932 mbedtls_platform_zeroize( handshake->premaster,
933 sizeof(handshake->premaster) );
934 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000935 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000936
937 /*
938 * Swap the client and server random values.
939 */
Paul Bakker48916f92012-09-16 19:57:18 +0000940 memcpy( tmp, handshake->randbytes, 64 );
941 memcpy( handshake->randbytes, tmp + 32, 32 );
942 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500943 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000944
945 /*
946 * SSLv3:
947 * key block =
948 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
949 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
950 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
951 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
952 * ...
953 *
954 * TLSv1:
955 * key block = PRF( master, "key expansion", randbytes )
956 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100957 ret = handshake->tls_prf( session->master, 48, "key expansion",
958 handshake->randbytes, 64, keyblk, 256 );
959 if( ret != 0 )
960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100962 return( ret );
963 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
966 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
967 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
968 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
969 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000970
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500971 mbedtls_platform_zeroize( handshake->randbytes,
972 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000973
974 /*
975 * Determine the appropriate key, IV and MAC length.
976 */
Paul Bakker68884e32013-01-07 18:20:04 +0100977
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200978 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200981 cipher_info->mode == MBEDTLS_MODE_CCM ||
982 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000983 {
Hanno Beckerf704bef2018-11-16 15:21:18 +0000984 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200985
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200986 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000987 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200988
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200989 /* All modes haves 96-bit IVs;
990 * GCM and CCM has 4 implicit and 8 explicit bytes
991 * ChachaPoly has all 12 bytes implicit
992 */
Paul Bakker68884e32013-01-07 18:20:04 +0100993 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200994 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
995 transform->fixed_ivlen = 12;
996 else
997 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200998
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200999 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
1000 taglen = transform->ciphersuite_info->flags &
1001 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
1002
1003
1004 /* Minimum length of encrypted record */
1005 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
1006 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001007 }
1008 else
1009 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001010 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1012 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001015 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001016 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001018 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001019 mac_key_len = mbedtls_md_get_size( md_info );
1020 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001023 /*
1024 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1025 * (rfc 6066 page 13 or rfc 2104 section 4),
1026 * so we only need to adjust the length here.
1027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001031
1032#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1033 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001034 * HMAC implementation which also truncates the key
1035 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001036 mac_key_len = transform->maclen;
1037#endif
1038 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001040
1041 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001042 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001043
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001044 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001046 transform->minlen = transform->maclen;
1047 else
Paul Bakker68884e32013-01-07 18:20:04 +01001048 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001049 /*
1050 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001051 * 1. if EtM is in use: one block plus MAC
1052 * otherwise: * first multiple of blocklen greater than maclen
1053 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1056 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001057 {
1058 transform->minlen = transform->maclen
1059 + cipher_info->block_size;
1060 }
1061 else
1062#endif
1063 {
1064 transform->minlen = transform->maclen
1065 + cipher_info->block_size
1066 - transform->maclen % cipher_info->block_size;
1067 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1070 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1071 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001072 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001073 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001074#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1076 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1077 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001078 {
1079 transform->minlen += transform->ivlen;
1080 }
1081 else
1082#endif
1083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1085 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001086 }
Paul Bakker68884e32013-01-07 18:20:04 +01001087 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 }
1089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001091 transform->keylen, transform->minlen, transform->ivlen,
1092 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001093
1094 /*
1095 * Finally setup the cipher contexts, IVs and MAC secrets.
1096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001098 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001099 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001100 key1 = keyblk + mac_key_len * 2;
1101 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001102
Paul Bakker68884e32013-01-07 18:20:04 +01001103 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001104 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001105
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001106 /*
1107 * This is not used in TLS v1.1.
1108 */
Paul Bakker48916f92012-09-16 19:57:18 +00001109 iv_copy_len = ( transform->fixed_ivlen ) ?
1110 transform->fixed_ivlen : transform->ivlen;
1111 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
1112 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001113 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001114 }
1115 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116#endif /* MBEDTLS_SSL_CLI_C */
1117#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001118 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001119 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001120 key1 = keyblk + mac_key_len * 2 + transform->keylen;
1121 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001122
Hanno Becker81c7b182017-11-09 18:39:33 +00001123 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001124 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001126 /*
1127 * This is not used in TLS v1.1.
1128 */
Paul Bakker48916f92012-09-16 19:57:18 +00001129 iv_copy_len = ( transform->fixed_ivlen ) ?
1130 transform->fixed_ivlen : transform->ivlen;
1131 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
1132 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001133 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001134 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001135 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1139 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001140 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142#if defined(MBEDTLS_SSL_PROTO_SSL3)
1143 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001144 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001145 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001149 }
1150
Hanno Becker81c7b182017-11-09 18:39:33 +00001151 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1152 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001153 }
1154 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1156#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1157 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1158 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001159 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001160 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1161 For AEAD-based ciphersuites, there is nothing to do here. */
1162 if( mac_key_len != 0 )
1163 {
1164 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1165 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1166 }
Paul Bakker68884e32013-01-07 18:20:04 +01001167 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001168 else
1169#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1172 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001173 }
Paul Bakker68884e32013-01-07 18:20:04 +01001174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1176 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001177 {
1178 int ret = 0;
1179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001183 transform->iv_enc, transform->iv_dec,
1184 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001185 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001186 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1189 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001190 }
1191 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001193
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001194#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1195 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001196 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001197 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1198 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +00001199 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001200 iv_copy_len );
1201 }
1202#endif
1203
Hanno Beckerf704bef2018-11-16 15:21:18 +00001204#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001205
1206 /* Only use PSA-based ciphers for TLS-1.2.
1207 * That's relevant at least for TLS-1.0, where
1208 * we assume that mbedtls_cipher_crypt() updates
1209 * the structure field for the IV, which the PSA-based
1210 * implementation currently doesn't. */
1211#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1212 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001213 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001214 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
1215 cipher_info, taglen );
1216 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1217 {
1218 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1219 return( ret );
1220 }
1221
1222 if( ret == 0 )
1223 {
1224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based encryption cipher context" ) );
1225 psa_fallthrough = 0;
1226 }
1227 else
1228 {
1229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1230 psa_fallthrough = 1;
1231 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001232 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001233 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001234 psa_fallthrough = 1;
1235#else
1236 psa_fallthrough = 1;
1237#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001238
Hanno Beckercb1cc802018-11-17 22:27:38 +00001239 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001240#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001241 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001242 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001243 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001244 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001245 return( ret );
1246 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001247
Hanno Beckerf704bef2018-11-16 15:21:18 +00001248#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001249 /* Only use PSA-based ciphers for TLS-1.2.
1250 * That's relevant at least for TLS-1.0, where
1251 * we assume that mbedtls_cipher_crypt() updates
1252 * the structure field for the IV, which the PSA-based
1253 * implementation currently doesn't. */
1254#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1255 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001256 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001257 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
1258 cipher_info, taglen );
1259 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1260 {
1261 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
1262 return( ret );
1263 }
1264
1265 if( ret == 0 )
1266 {
1267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Successfully setup PSA-based decryption cipher context" ) );
1268 psa_fallthrough = 0;
1269 }
1270 else
1271 {
1272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1273 psa_fallthrough = 1;
1274 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001275 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001276 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001277 psa_fallthrough = 1;
1278#else
1279 psa_fallthrough = 1;
1280#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001281
Hanno Beckercb1cc802018-11-17 22:27:38 +00001282 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001283#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001284 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001285 cipher_info ) ) != 0 )
1286 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001287 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001288 return( ret );
1289 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001292 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001296 return( ret );
1297 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001300 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001304 return( ret );
1305 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307#if defined(MBEDTLS_CIPHER_MODE_CBC)
1308 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1311 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001314 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001315 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1318 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001321 return( ret );
1322 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001325
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001326 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001329 // Initialize compression
1330 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001332 {
Paul Bakker16770332013-10-11 09:59:44 +02001333 if( ssl->compress_buf == NULL )
1334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001336 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001337 if( ssl->compress_buf == NULL )
1338 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001340 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001341 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001342 }
1343 }
1344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001346
Paul Bakker48916f92012-09-16 19:57:18 +00001347 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1348 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001349
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001350 if( deflateInit( &transform->ctx_deflate,
1351 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001352 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1355 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001356 }
1357 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
1362 return( 0 );
1363}
1364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#if defined(MBEDTLS_SSL_PROTO_SSL3)
1366void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001367{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001368 mbedtls_md5_context md5;
1369 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 unsigned char pad_1[48];
1371 unsigned char pad_2[48];
1372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001374
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001375 mbedtls_md5_init( &md5 );
1376 mbedtls_sha1_init( &sha1 );
1377
1378 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1379 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001380
Paul Bakker380da532012-04-18 16:10:25 +00001381 memset( pad_1, 0x36, 48 );
1382 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001383
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001384 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1385 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1386 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001387
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001388 mbedtls_md5_starts_ret( &md5 );
1389 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1390 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1391 mbedtls_md5_update_ret( &md5, hash, 16 );
1392 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001393
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001394 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1395 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1396 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001397
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001398 mbedtls_sha1_starts_ret( &sha1 );
1399 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1400 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1401 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1402 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001406
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001407 mbedtls_md5_free( &md5 );
1408 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001409
Paul Bakker380da532012-04-18 16:10:25 +00001410 return;
1411}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1415void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001416{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001417 mbedtls_md5_context md5;
1418 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001421
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001422 mbedtls_md5_init( &md5 );
1423 mbedtls_sha1_init( &sha1 );
1424
1425 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1426 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001427
Andrzej Kurekeb342242019-01-29 09:14:33 -05001428 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001429 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001433
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001434 mbedtls_md5_free( &md5 );
1435 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001436
Paul Bakker380da532012-04-18 16:10:25 +00001437 return;
1438}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1442#if defined(MBEDTLS_SHA256_C)
1443void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001444{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001445#if defined(MBEDTLS_USE_PSA_CRYPTO)
1446 size_t hash_size;
1447 psa_status_t status;
1448 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1449
1450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1451 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1452 if( status != PSA_SUCCESS )
1453 {
1454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1455 return;
1456 }
1457
1458 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1459 if( status != PSA_SUCCESS )
1460 {
1461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1462 return;
1463 }
1464 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1466#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001467 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001468
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001469 mbedtls_sha256_init( &sha256 );
1470
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001472
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001473 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001474 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001478
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001479 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001480#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001481 return;
1482}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#if defined(MBEDTLS_SHA512_C)
1486void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001487{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001488#if defined(MBEDTLS_USE_PSA_CRYPTO)
1489 size_t hash_size;
1490 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001491 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001492
1493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001494 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001495 if( status != PSA_SUCCESS )
1496 {
1497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1498 return;
1499 }
1500
Andrzej Kurek972fba52019-01-30 03:29:12 -05001501 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001502 if( status != PSA_SUCCESS )
1503 {
1504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1505 return;
1506 }
1507 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1509#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001510 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001511
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001512 mbedtls_sha512_init( &sha512 );
1513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001515
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001516 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001517 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001521
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001522 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001523#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001524 return;
1525}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#endif /* MBEDTLS_SHA512_C */
1527#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1530int 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 +02001531{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001532 unsigned char *p = ssl->handshake->premaster;
1533 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001534 const unsigned char *psk = ssl->conf->psk;
1535 size_t psk_len = ssl->conf->psk_len;
1536
1537 /* If the psk callback was called, use its result */
1538 if( ssl->handshake->psk != NULL )
1539 {
1540 psk = ssl->handshake->psk;
1541 psk_len = ssl->handshake->psk_len;
1542 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001543
1544 /*
1545 * PMS = struct {
1546 * opaque other_secret<0..2^16-1>;
1547 * opaque psk<0..2^16-1>;
1548 * };
1549 * with "other_secret" depending on the particular key exchange
1550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1552 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001553 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001554 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001556
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001557 *(p++) = (unsigned char)( psk_len >> 8 );
1558 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001559
1560 if( end < p || (size_t)( end - p ) < psk_len )
1561 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1562
1563 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001564 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001565 }
1566 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1568#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1569 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001570 {
1571 /*
1572 * other_secret already set by the ClientKeyExchange message,
1573 * and is 48 bytes long
1574 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001575 if( end - p < 2 )
1576 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1577
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001578 *p++ = 0;
1579 *p++ = 48;
1580 p += 48;
1581 }
1582 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1584#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1585 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001586 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001587 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001588 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001589
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001590 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001592 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001593 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001596 return( ret );
1597 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001598 *(p++) = (unsigned char)( len >> 8 );
1599 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001600 p += len;
1601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001603 }
1604 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1606#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1607 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001608 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001609 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001610 size_t zlen;
1611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001613 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001614 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001617 return( ret );
1618 }
1619
1620 *(p++) = (unsigned char)( zlen >> 8 );
1621 *(p++) = (unsigned char)( zlen );
1622 p += zlen;
1623
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001624 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1625 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001626 }
1627 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1631 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001632 }
1633
1634 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001635 if( end - p < 2 )
1636 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001637
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001638 *(p++) = (unsigned char)( psk_len >> 8 );
1639 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001640
1641 if( end < p || (size_t)( end - p ) < psk_len )
1642 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1643
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001644 memcpy( p, psk, psk_len );
1645 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001646
1647 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1648
1649 return( 0 );
1650}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001654/*
1655 * SSLv3.0 MAC functions
1656 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001657#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001658static void ssl_mac( mbedtls_md_context_t *md_ctx,
1659 const unsigned char *secret,
1660 const unsigned char *buf, size_t len,
1661 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001662 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001663{
1664 unsigned char header[11];
1665 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001666 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1668 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001669
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001670 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001672 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001673 else
Paul Bakker68884e32013-01-07 18:20:04 +01001674 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001675
1676 memcpy( header, ctr, 8 );
1677 header[ 8] = (unsigned char) type;
1678 header[ 9] = (unsigned char)( len >> 8 );
1679 header[10] = (unsigned char)( len );
1680
Paul Bakker68884e32013-01-07 18:20:04 +01001681 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 mbedtls_md_starts( md_ctx );
1683 mbedtls_md_update( md_ctx, secret, md_size );
1684 mbedtls_md_update( md_ctx, padding, padlen );
1685 mbedtls_md_update( md_ctx, header, 11 );
1686 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001687 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001688
Paul Bakker68884e32013-01-07 18:20:04 +01001689 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 mbedtls_md_starts( md_ctx );
1691 mbedtls_md_update( md_ctx, secret, md_size );
1692 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001693 mbedtls_md_update( md_ctx, out, md_size );
1694 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001695}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1699 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001700 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001701#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001702#endif
1703
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001704/* The function below is only used in the Lucky 13 counter-measure in
1705 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1706#if defined(SSL_SOME_MODES_USE_MAC) && \
1707 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1708 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1709 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1710/* This function makes sure every byte in the memory region is accessed
1711 * (in ascending addresses order) */
1712static void ssl_read_memory( unsigned char *p, size_t len )
1713{
1714 unsigned char acc = 0;
1715 volatile unsigned char force;
1716
1717 for( ; len != 0; p++, len-- )
1718 acc ^= *p;
1719
1720 force = acc;
1721 (void) force;
1722}
1723#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1724
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001725/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001726 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001729{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001731 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001734
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001735 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1738 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001739 }
1740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001744 ssl->out_msg, ssl->out_msglen );
1745
Paul Bakker5121ce52009-01-03 21:22:43 +00001746 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001747 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001748 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001749#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 if( mode == MBEDTLS_MODE_STREAM ||
1751 ( mode == MBEDTLS_MODE_CBC
1752#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1753 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001754#endif
1755 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757#if defined(MBEDTLS_SSL_PROTO_SSL3)
1758 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001759 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001760 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001761
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001762 ssl_mac( &ssl->transform_out->md_ctx_enc,
1763 ssl->transform_out->mac_enc,
1764 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001765 ssl->out_ctr, ssl->out_msgtype,
1766 mac );
1767
1768 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001769 }
1770 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001771#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1773 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1774 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001775 {
Hanno Becker992b6872017-11-09 18:57:39 +00001776 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1779 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1780 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1781 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001782 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001783 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001785
1786 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001787 }
1788 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001789#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1792 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001793 }
1794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001796 ssl->out_msg + ssl->out_msglen,
1797 ssl->transform_out->maclen );
1798
1799 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001800 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001801 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001802#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001803
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001804 /*
1805 * Encrypt
1806 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1808 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001809 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001810 int ret;
1811 size_t olen = 0;
1812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001814 "including %d bytes of padding",
1815 ssl->out_msglen, 0 ) );
1816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001818 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001819 ssl->transform_out->ivlen,
1820 ssl->out_msg, ssl->out_msglen,
1821 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001824 return( ret );
1825 }
1826
1827 if( ssl->out_msglen != olen )
1828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1830 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001831 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001832 }
Paul Bakker68884e32013-01-07 18:20:04 +01001833 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001835#if defined(MBEDTLS_GCM_C) || \
1836 defined(MBEDTLS_CCM_C) || \
1837 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001839 mode == MBEDTLS_MODE_CCM ||
1840 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001841 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001842 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001843 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001844 unsigned char *enc_msg;
1845 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001846 unsigned char iv[12];
1847 mbedtls_ssl_transform *transform = ssl->transform_out;
1848 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001850 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001851
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001852 /*
1853 * Prepare additional authenticated data
1854 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001855 memcpy( add_data, ssl->out_ctr, 8 );
1856 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001858 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001859 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1860 add_data[12] = ssl->out_msglen & 0xFF;
1861
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001862 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001863
Paul Bakker68884e32013-01-07 18:20:04 +01001864 /*
1865 * Generate IV
1866 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001867 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1868 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001869 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001870 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1871 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1872 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1873
1874 }
1875 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1876 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001877 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001878 unsigned char i;
1879
1880 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1881
1882 for( i = 0; i < 8; i++ )
1883 iv[i+4] ^= ssl->out_ctr[i];
1884 }
1885 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001886 {
1887 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1889 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001890 }
1891
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001892 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1893 iv, transform->ivlen );
1894 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1895 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001896
Paul Bakker68884e32013-01-07 18:20:04 +01001897 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001898 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001899 */
1900 enc_msg = ssl->out_msg;
1901 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001902 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001905 "including 0 bytes of padding",
1906 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001907
Paul Bakker68884e32013-01-07 18:20:04 +01001908 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001909 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001910 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001911 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1912 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001913 add_data, 13,
1914 enc_msg, enc_msglen,
1915 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001916 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001919 return( ret );
1920 }
1921
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001922 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1925 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001926 }
1927
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001928 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001929 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001932 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1935#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001936 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001938 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001939 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001940 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001941 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001942
Paul Bakker48916f92012-09-16 19:57:18 +00001943 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1944 ssl->transform_out->ivlen;
1945 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001946 padlen = 0;
1947
1948 for( i = 0; i <= padlen; i++ )
1949 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1950
1951 ssl->out_msglen += padlen + 1;
1952
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001953 enc_msglen = ssl->out_msglen;
1954 enc_msg = ssl->out_msg;
1955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001957 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001958 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1959 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001960 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001962 {
1963 /*
1964 * Generate IV
1965 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001966 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001967 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001968 if( ret != 0 )
1969 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001970
Paul Bakker92be97b2013-01-02 17:30:03 +01001971 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001972 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001973
1974 /*
1975 * Fix pointer positions and message length with added IV
1976 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001977 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001978 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001979 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001980 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001984 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001985 ssl->out_msglen, ssl->transform_out->ivlen,
1986 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001989 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001990 ssl->transform_out->ivlen,
1991 enc_msg, enc_msglen,
1992 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001995 return( ret );
1996 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001997
Paul Bakkercca5b812013-08-31 17:40:26 +02001998 if( enc_msglen != olen )
1999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2001 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002002 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2005 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002006 {
2007 /*
2008 * Save IV in SSL3 and TLS1
2009 */
2010 memcpy( ssl->transform_out->iv_enc,
2011 ssl->transform_out->cipher_ctx_enc.iv,
2012 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002013 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002014#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002017 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002018 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002019 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2020
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002021 /*
2022 * MAC(MAC_write_key, seq_num +
2023 * TLSCipherText.type +
2024 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002025 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002026 * IV + // except for TLS 1.0
2027 * ENC(content + padding + padding_length));
2028 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002029 unsigned char pseudo_hdr[13];
2030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002032
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002033 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
2034 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002035 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
2036 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
2037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
2041 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002042 ssl->out_iv, ssl->out_msglen );
Hanno Becker3d8c9072018-01-05 16:24:22 +00002043 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002045
Hanno Becker3d8c9072018-01-05 16:24:22 +00002046 memcpy( ssl->out_iv + ssl->out_msglen, mac,
2047 ssl->transform_out->maclen );
2048
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002049 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002050 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002051 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002054 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002056 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2059 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002060 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002061
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002062 /* Make extra sure authentication was performed, exactly once */
2063 if( auth_done != 1 )
2064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2066 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002067 }
2068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002070
2071 return( 0 );
2072}
2073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002077 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002078#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002079 size_t padlen = 0, correct = 1;
2080#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002083
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002084 if( ssl->session_in == NULL || ssl->transform_in == NULL )
2085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2087 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002088 }
2089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002091
Paul Bakker48916f92012-09-16 19:57:18 +00002092 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00002095 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 }
2098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2100 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002101 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002102 int ret;
2103 size_t olen = 0;
2104
Paul Bakker68884e32013-01-07 18:20:04 +01002105 padlen = 0;
2106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002108 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002109 ssl->transform_in->ivlen,
2110 ssl->in_msg, ssl->in_msglen,
2111 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002114 return( ret );
2115 }
2116
2117 if( ssl->in_msglen != olen )
2118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2120 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002121 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 }
Paul Bakker68884e32013-01-07 18:20:04 +01002123 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002125#if defined(MBEDTLS_GCM_C) || \
2126 defined(MBEDTLS_CCM_C) || \
2127 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002129 mode == MBEDTLS_MODE_CCM ||
2130 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002131 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002132 int ret;
2133 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002134 unsigned char *dec_msg;
2135 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002136 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002137 unsigned char iv[12];
2138 mbedtls_ssl_transform *transform = ssl->transform_in;
2139 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002141 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002142
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002143 /*
2144 * Compute and update sizes
2145 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02002146 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002149 "+ taglen (%d)", ssl->in_msglen,
2150 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002152 }
2153 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
2154
Paul Bakker68884e32013-01-07 18:20:04 +01002155 dec_msg = ssl->in_msg;
2156 dec_msg_result = ssl->in_msg;
2157 ssl->in_msglen = dec_msglen;
2158
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002159 /*
2160 * Prepare additional authenticated data
2161 */
Paul Bakker68884e32013-01-07 18:20:04 +01002162 memcpy( add_data, ssl->in_ctr, 8 );
2163 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002165 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01002166 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
2167 add_data[12] = ssl->in_msglen & 0xFF;
2168
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002169 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01002170
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002171 /*
2172 * Prepare IV
2173 */
2174 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2175 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002176 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002177 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2178 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002179
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002180 }
2181 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2182 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002183 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002184 unsigned char i;
2185
2186 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2187
2188 for( i = 0; i < 8; i++ )
2189 iv[i+4] ^= ssl->in_ctr[i];
2190 }
2191 else
2192 {
2193 /* Reminder if we ever add an AEAD mode with a different size */
2194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2196 }
2197
2198 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002200
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002201 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002202 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002205 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002206 add_data, 13,
2207 dec_msg, dec_msglen,
2208 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002209 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2214 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002215
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002216 return( ret );
2217 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002218 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002219
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002220 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2223 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002224 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002225 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002226 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2228#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002229 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002231 {
Paul Bakker45829992013-01-03 14:52:21 +01002232 /*
2233 * Decrypt and check the padding
2234 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002235 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002236 unsigned char *dec_msg;
2237 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00002238 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002239 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002240 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002241
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 /*
Paul Bakker45829992013-01-03 14:52:21 +01002243 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
2246 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01002247 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002248#endif
Paul Bakker45829992013-01-03 14:52:21 +01002249
2250 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
2251 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
2252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002254 "+ 1 ) ( + expl IV )", ssl->in_msglen,
2255 ssl->transform_in->ivlen,
2256 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002258 }
2259
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002260 dec_msglen = ssl->in_msglen;
2261 dec_msg = ssl->in_msg;
2262 dec_msg_result = ssl->in_msg;
2263
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002264 /*
2265 * Authenticate before decrypt if enabled
2266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
2268 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002269 {
Hanno Becker992b6872017-11-09 18:57:39 +00002270 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002271 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002274
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002275 dec_msglen -= ssl->transform_in->maclen;
2276 ssl->in_msglen -= ssl->transform_in->maclen;
2277
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002278 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
2279 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
2280 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
2281 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
2282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
2286 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002287 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00002288 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002292 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002293 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002294 ssl->transform_in->maclen );
2295
Hanno Becker992b6872017-11-09 18:57:39 +00002296 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
2297 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002302 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002303 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002304 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002306
2307 /*
2308 * Check length sanity
2309 */
2310 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
2311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002313 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002315 }
2316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002318 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002319 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002322 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002323 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00002324 dec_msglen -= ssl->transform_in->ivlen;
2325 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002326
Paul Bakker48916f92012-09-16 19:57:18 +00002327 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01002328 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002329 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02002333 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002334 ssl->transform_in->ivlen,
2335 dec_msg, dec_msglen,
2336 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002339 return( ret );
2340 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002341
Paul Bakkercca5b812013-08-31 17:40:26 +02002342 if( dec_msglen != olen )
2343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2345 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002346 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2349 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002350 {
2351 /*
2352 * Save IV in SSL3 and TLS1
2353 */
2354 memcpy( ssl->transform_in->iv_dec,
2355 ssl->transform_in->cipher_ctx_dec.iv,
2356 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002358#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002359
2360 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002361
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002362 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002363 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365#if defined(MBEDTLS_SSL_DEBUG_ALL)
2366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002367 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002368#endif
Paul Bakker45829992013-01-03 14:52:21 +01002369 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002370 correct = 0;
2371 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373#if defined(MBEDTLS_SSL_PROTO_SSL3)
2374 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002375 {
Paul Bakker48916f92012-09-16 19:57:18 +00002376 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378#if defined(MBEDTLS_SSL_DEBUG_ALL)
2379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002381 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002382#endif
Paul Bakker45829992013-01-03 14:52:21 +01002383 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002384 }
2385 }
2386 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2388#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2389 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2390 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 {
2392 /*
Paul Bakker45829992013-01-03 14:52:21 +01002393 * TLSv1+: always check the padding up to the first failure
2394 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002395 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002396 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002397 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002398 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002399
Paul Bakker956c9e02013-12-19 14:42:28 +01002400 /*
2401 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002402 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002403 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002404 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002405 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002406 *
2407 * In both cases we reset padding_idx to a safe value (0) to
2408 * prevent out-of-buffer reads.
2409 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002410 correct &= ( padlen <= ssl->in_msglen );
2411 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002412 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002413
2414 padding_idx *= correct;
2415
Angus Grattonb512bc12018-06-19 15:57:50 +10002416 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002417 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002418 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002419 pad_count += real_count *
2420 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2421 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002422
2423 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002426 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002428#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002429 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002431 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2433 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2436 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002437 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002438
2439 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002440 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002441 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002443 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2446 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002447 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002448
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002449#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002451 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002452#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002453
2454 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002455 * Authenticate if not done yet.
2456 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002458#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002459 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002460 {
Hanno Becker992b6872017-11-09 18:57:39 +00002461 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002462
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002463 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002464
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002465 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2466 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468#if defined(MBEDTLS_SSL_PROTO_SSL3)
2469 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002470 {
2471 ssl_mac( &ssl->transform_in->md_ctx_dec,
2472 ssl->transform_in->mac_dec,
2473 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002474 ssl->in_ctr, ssl->in_msgtype,
2475 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002476 }
2477 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2479#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2480 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2481 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002482 {
2483 /*
2484 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002485 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002486 *
2487 * Known timing attacks:
2488 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2489 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002490 * To compensate for different timings for the MAC calculation
2491 * depending on how much padding was removed (which is determined
2492 * by padlen), process extra_run more blocks through the hash
2493 * function.
2494 *
2495 * The formula in the paper is
2496 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2497 * where L1 is the size of the header plus the decrypted message
2498 * plus CBC padding and L2 is the size of the header plus the
2499 * decrypted message. This is for an underlying hash function
2500 * with 64-byte blocks.
2501 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2502 * correctly. We round down instead of up, so -56 is the correct
2503 * value for our calculations instead of -55.
2504 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002505 * Repeat the formula rather than defining a block_size variable.
2506 * This avoids requiring division by a variable at runtime
2507 * (which would be marginally less efficient and would require
2508 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002509 */
2510 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002511
2512 /*
2513 * The next two sizes are the minimum and maximum values of
2514 * in_msglen over all padlen values.
2515 *
2516 * They're independent of padlen, since we previously did
2517 * in_msglen -= padlen.
2518 *
2519 * Note that max_len + maclen is never more than the buffer
2520 * length, as we previously did in_msglen -= maclen too.
2521 */
2522 const size_t max_len = ssl->in_msglen + padlen;
2523 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2524
Gilles Peskine20b44082018-05-29 14:06:49 +02002525 switch( ssl->transform_in->ciphersuite_info->mac )
2526 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002527#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2528 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002529 case MBEDTLS_MD_MD5:
2530 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002531 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002532 /* 8 bytes of message size, 64-byte compression blocks */
2533 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2534 ( 13 + ssl->in_msglen + 8 ) / 64;
2535 break;
2536#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002537#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002538 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002539 /* 16 bytes of message size, 128-byte compression blocks */
2540 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2541 ( 13 + ssl->in_msglen + 16 ) / 128;
2542 break;
2543#endif
2544 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002546 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2547 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002548
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002549 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2552 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2553 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2554 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002555 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002556 /* Make sure we access everything even when padlen > 0. This
2557 * makes the synchronisation requirements for just-in-time
2558 * Prime+Probe attacks much tighter and hopefully impractical. */
2559 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002560 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002561
2562 /* Call mbedtls_md_process at least once due to cache attacks
2563 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002564 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002568
2569 /* Make sure we access all the memory that could contain the MAC,
2570 * before we check it in the next code block. This makes the
2571 * synchronisation requirements for just-in-time Prime+Probe
2572 * attacks much tighter and hopefully impractical. */
2573 ssl_read_memory( ssl->in_msg + min_len,
2574 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002575 }
2576 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2578 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2581 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002583
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002584#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002585 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2586 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2587 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002588#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002589
Hanno Becker992b6872017-11-09 18:57:39 +00002590 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2591 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593#if defined(MBEDTLS_SSL_DEBUG_ALL)
2594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002595#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002596 correct = 0;
2597 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002598 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002599 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002600
2601 /*
2602 * Finally check the correct flag
2603 */
2604 if( correct == 0 )
2605 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002606#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002607
2608 /* Make extra sure authentication was performed, exactly once */
2609 if( auth_done != 1 )
2610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2612 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002613 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002614
2615 if( ssl->in_msglen == 0 )
2616 {
Angus Gratton34817922018-06-19 15:58:22 +10002617#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2618 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2619 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2620 {
2621 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2623 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2624 }
2625#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2626
Paul Bakker5121ce52009-01-03 21:22:43 +00002627 ssl->nb_zero++;
2628
2629 /*
2630 * Three or more empty messages may be a DoS attack
2631 * (excessive CPU consumption).
2632 */
2633 if( ssl->nb_zero > 3 )
2634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 }
2639 }
2640 else
2641 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002644 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002645 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002646 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002647 }
2648 else
2649#endif
2650 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002651 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002652 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2653 if( ++ssl->in_ctr[i - 1] != 0 )
2654 break;
2655
2656 /* The loop goes to its end iff the counter is wrapping */
2657 if( i == ssl_ep_len( ssl ) )
2658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2660 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002661 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002662 }
2663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
2666 return( 0 );
2667}
2668
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002669#undef MAC_NONE
2670#undef MAC_PLAINTEXT
2671#undef MAC_CIPHERTEXT
2672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002674/*
2675 * Compression/decompression functions
2676 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002678{
2679 int ret;
2680 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002681 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002682 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002683 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002686
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002687 if( len_pre == 0 )
2688 return( 0 );
2689
Paul Bakker2770fbd2012-07-03 13:30:23 +00002690 memcpy( msg_pre, ssl->out_msg, len_pre );
2691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002693 ssl->out_msglen ) );
2694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002696 ssl->out_msg, ssl->out_msglen );
2697
Paul Bakker48916f92012-09-16 19:57:18 +00002698 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2699 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2700 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002701 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002702
Paul Bakker48916f92012-09-16 19:57:18 +00002703 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002704 if( ret != Z_OK )
2705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2707 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002708 }
2709
Angus Grattond8213d02016-05-25 20:56:48 +10002710 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002711 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002714 ssl->out_msglen ) );
2715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002717 ssl->out_msg, ssl->out_msglen );
2718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002720
2721 return( 0 );
2722}
2723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002725{
2726 int ret;
2727 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002728 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002729 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002730 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002733
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002734 if( len_pre == 0 )
2735 return( 0 );
2736
Paul Bakker2770fbd2012-07-03 13:30:23 +00002737 memcpy( msg_pre, ssl->in_msg, len_pre );
2738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002740 ssl->in_msglen ) );
2741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002743 ssl->in_msg, ssl->in_msglen );
2744
Paul Bakker48916f92012-09-16 19:57:18 +00002745 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2746 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2747 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002748 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002749 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002750
Paul Bakker48916f92012-09-16 19:57:18 +00002751 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002752 if( ret != Z_OK )
2753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2755 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002756 }
2757
Angus Grattond8213d02016-05-25 20:56:48 +10002758 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002759 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002762 ssl->in_msglen ) );
2763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002765 ssl->in_msg, ssl->in_msglen );
2766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002768
2769 return( 0 );
2770}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2774static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776#if defined(MBEDTLS_SSL_PROTO_DTLS)
2777static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002778{
2779 /* If renegotiation is not enforced, retransmit until we would reach max
2780 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002781 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002782 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002783 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002784 unsigned char doublings = 1;
2785
2786 while( ratio != 0 )
2787 {
2788 ++doublings;
2789 ratio >>= 1;
2790 }
2791
2792 if( ++ssl->renego_records_seen > doublings )
2793 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002795 return( 0 );
2796 }
2797 }
2798
2799 return( ssl_write_hello_request( ssl ) );
2800}
2801#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002803
Paul Bakker5121ce52009-01-03 21:22:43 +00002804/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002805 * Fill the input message buffer by appending data to it.
2806 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002807 *
2808 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2809 * available (from this read and/or a previous one). Otherwise, an error code
2810 * is returned (possibly EOF or WANT_READ).
2811 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002812 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2813 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2814 * since we always read a whole datagram at once.
2815 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002816 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002817 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002820{
Paul Bakker23986e52011-04-24 08:57:21 +00002821 int ret;
2822 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002825
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002826 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002829 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002831 }
2832
Angus Grattond8213d02016-05-25 20:56:48 +10002833 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2836 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002837 }
2838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002840 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002841 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002842 uint32_t timeout;
2843
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002844 /* Just to be sure */
2845 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2846 {
2847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2848 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2849 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2850 }
2851
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002852 /*
2853 * The point is, we need to always read a full datagram at once, so we
2854 * sometimes read more then requested, and handle the additional data.
2855 * It could be the rest of the current record (while fetching the
2856 * header) and/or some other records in the same datagram.
2857 */
2858
2859 /*
2860 * Move to the next record in the already read datagram if applicable
2861 */
2862 if( ssl->next_record_offset != 0 )
2863 {
2864 if( ssl->in_left < ssl->next_record_offset )
2865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2867 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002868 }
2869
2870 ssl->in_left -= ssl->next_record_offset;
2871
2872 if( ssl->in_left != 0 )
2873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002875 ssl->next_record_offset ) );
2876 memmove( ssl->in_hdr,
2877 ssl->in_hdr + ssl->next_record_offset,
2878 ssl->in_left );
2879 }
2880
2881 ssl->next_record_offset = 0;
2882 }
2883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002885 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002886
2887 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002888 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002889 */
2890 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002893 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002894 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002895
2896 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002897 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002898 * are not at the beginning of a new record, the caller did something
2899 * wrong.
2900 */
2901 if( ssl->in_left != 0 )
2902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2904 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002905 }
2906
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002907 /*
2908 * Don't even try to read if time's out already.
2909 * This avoids by-passing the timer when repeatedly receiving messages
2910 * that will end up being dropped.
2911 */
2912 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002913 {
2914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002915 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002916 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002917 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002918 {
Angus Grattond8213d02016-05-25 20:56:48 +10002919 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002922 timeout = ssl->handshake->retransmit_timeout;
2923 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002924 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002927
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002928 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002929 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2930 timeout );
2931 else
2932 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002935
2936 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002938 }
2939
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002940 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002943 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002946 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002947 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002950 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002951 }
2952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002956 return( ret );
2957 }
2958
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002959 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002960 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002962 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002964 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002965 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002968 return( ret );
2969 }
2970
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002971 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002972 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002974 }
2975
Paul Bakker5121ce52009-01-03 21:22:43 +00002976 if( ret < 0 )
2977 return( ret );
2978
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002979 ssl->in_left = ret;
2980 }
2981 else
2982#endif
2983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002985 ssl->in_left, nb_want ) );
2986
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002987 while( ssl->in_left < nb_want )
2988 {
2989 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002990
2991 if( ssl_check_timer( ssl ) != 0 )
2992 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2993 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002994 {
2995 if( ssl->f_recv_timeout != NULL )
2996 {
2997 ret = ssl->f_recv_timeout( ssl->p_bio,
2998 ssl->in_hdr + ssl->in_left, len,
2999 ssl->conf->read_timeout );
3000 }
3001 else
3002 {
3003 ret = ssl->f_recv( ssl->p_bio,
3004 ssl->in_hdr + ssl->in_left, len );
3005 }
3006 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003009 ssl->in_left, nb_want ) );
3010 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003011
3012 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003014
3015 if( ret < 0 )
3016 return( ret );
3017
mohammad160352aecb92018-03-28 23:41:40 -07003018 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003019 {
Darryl Green11999bb2018-03-13 15:22:58 +00003020 MBEDTLS_SSL_DEBUG_MSG( 1,
3021 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003022 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003023 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3024 }
3025
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003026 ssl->in_left += ret;
3027 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003028 }
3029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003031
3032 return( 0 );
3033}
3034
3035/*
3036 * Flush any data not yet written
3037 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003039{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003040 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003041 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003044
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003045 if( ssl->f_send == NULL )
3046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003048 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003050 }
3051
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003052 /* Avoid incrementing counter if data is flushed */
3053 if( ssl->out_left == 0 )
3054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003056 return( 0 );
3057 }
3058
Paul Bakker5121ce52009-01-03 21:22:43 +00003059 while( ssl->out_left > 0 )
3060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3062 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003063
Hanno Becker2b1e3542018-08-06 11:19:13 +01003064 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003065 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003068
3069 if( ret <= 0 )
3070 return( ret );
3071
mohammad160352aecb92018-03-28 23:41:40 -07003072 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003073 {
Darryl Green11999bb2018-03-13 15:22:58 +00003074 MBEDTLS_SSL_DEBUG_MSG( 1,
3075 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003076 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003077 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3078 }
3079
Paul Bakker5121ce52009-01-03 21:22:43 +00003080 ssl->out_left -= ret;
3081 }
3082
Hanno Becker2b1e3542018-08-06 11:19:13 +01003083#if defined(MBEDTLS_SSL_PROTO_DTLS)
3084 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003085 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003086 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003087 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003088 else
3089#endif
3090 {
3091 ssl->out_hdr = ssl->out_buf + 8;
3092 }
3093 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003096
3097 return( 0 );
3098}
3099
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003100/*
3101 * Functions to handle the DTLS retransmission state machine
3102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003104/*
3105 * Append current handshake message to current outgoing flight
3106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003108{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3111 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3112 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003113
3114 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003115 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003116 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003119 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003120 }
3121
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003122 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003123 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003126 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003127 }
3128
3129 /* Copy current handshake message with headers */
3130 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3131 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003132 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003133 msg->next = NULL;
3134
3135 /* Append to the current flight */
3136 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003137 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003138 else
3139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003141 while( cur->next != NULL )
3142 cur = cur->next;
3143 cur->next = msg;
3144 }
3145
Hanno Becker3b235902018-08-06 09:54:53 +01003146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003147 return( 0 );
3148}
3149
3150/*
3151 * Free the current flight of handshake messages
3152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003154{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 mbedtls_ssl_flight_item *cur = flight;
3156 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003157
3158 while( cur != NULL )
3159 {
3160 next = cur->next;
3161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 mbedtls_free( cur->p );
3163 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003164
3165 cur = next;
3166 }
3167}
3168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3170static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003171#endif
3172
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003173/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003174 * Swap transform_out and out_ctr with the alternative ones
3175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003177{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003179 unsigned char tmp_out_ctr[8];
3180
3181 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003184 return;
3185 }
3186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003188
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003189 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003190 tmp_transform = ssl->transform_out;
3191 ssl->transform_out = ssl->handshake->alt_transform_out;
3192 ssl->handshake->alt_transform_out = tmp_transform;
3193
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003194 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003195 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3196 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003197 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003198
3199 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003200 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3203 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3208 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003209 }
3210 }
3211#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003212}
3213
3214/*
3215 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003216 */
3217int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3218{
3219 int ret = 0;
3220
3221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3222
3223 ret = mbedtls_ssl_flight_transmit( ssl );
3224
3225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3226
3227 return( ret );
3228}
3229
3230/*
3231 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003232 *
3233 * Need to remember the current message in case flush_output returns
3234 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003235 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003236 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003237int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003238{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003239 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003243 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003245
3246 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003247 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003248 ssl_swap_epochs( ssl );
3249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003251 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003252
3253 while( ssl->handshake->cur_msg != NULL )
3254 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003255 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003256 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003257
Hanno Beckere1dcb032018-08-17 16:47:58 +01003258 int const is_finished =
3259 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3260 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3261
Hanno Becker04da1892018-08-14 13:22:10 +01003262 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3263 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3264
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003265 /* Swap epochs before sending Finished: we can't do it after
3266 * sending ChangeCipherSpec, in case write returns WANT_READ.
3267 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003268 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003269 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003271 ssl_swap_epochs( ssl );
3272 }
3273
Hanno Becker67bc7c32018-08-06 11:33:50 +01003274 ret = ssl_get_remaining_payload_in_datagram( ssl );
3275 if( ret < 0 )
3276 return( ret );
3277 max_frag_len = (size_t) ret;
3278
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003279 /* CCS is copied as is, while HS messages may need fragmentation */
3280 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3281 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003282 if( max_frag_len == 0 )
3283 {
3284 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3285 return( ret );
3286
3287 continue;
3288 }
3289
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003290 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003291 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003292 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003293
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003294 /* Update position inside current message */
3295 ssl->handshake->cur_msg_p += cur->len;
3296 }
3297 else
3298 {
3299 const unsigned char * const p = ssl->handshake->cur_msg_p;
3300 const size_t hs_len = cur->len - 12;
3301 const size_t frag_off = p - ( cur->p + 12 );
3302 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003303 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003304
Hanno Beckere1dcb032018-08-17 16:47:58 +01003305 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003306 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003307 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003308 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003309
Hanno Becker67bc7c32018-08-06 11:33:50 +01003310 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3311 return( ret );
3312
3313 continue;
3314 }
3315 max_hs_frag_len = max_frag_len - 12;
3316
3317 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3318 max_hs_frag_len : rem_len;
3319
3320 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003321 {
3322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003323 (unsigned) cur_hs_frag_len,
3324 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003325 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003326
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003327 /* Messages are stored with handshake headers as if not fragmented,
3328 * copy beginning of headers then fill fragmentation fields.
3329 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3330 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003331
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003332 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3333 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3334 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3335
Hanno Becker67bc7c32018-08-06 11:33:50 +01003336 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3337 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3338 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003339
3340 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3341
Hanno Becker3f7b9732018-08-28 09:53:25 +01003342 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003343 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3344 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003345 ssl->out_msgtype = cur->type;
3346
3347 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003348 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003349 }
3350
3351 /* If done with the current message move to the next one if any */
3352 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3353 {
3354 if( cur->next != NULL )
3355 {
3356 ssl->handshake->cur_msg = cur->next;
3357 ssl->handshake->cur_msg_p = cur->next->p + 12;
3358 }
3359 else
3360 {
3361 ssl->handshake->cur_msg = NULL;
3362 ssl->handshake->cur_msg_p = NULL;
3363 }
3364 }
3365
3366 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003367 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003370 return( ret );
3371 }
3372 }
3373
Hanno Becker67bc7c32018-08-06 11:33:50 +01003374 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3375 return( ret );
3376
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003377 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3379 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003380 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003383 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3384 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003385
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003387
3388 return( 0 );
3389}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003390
3391/*
3392 * To be called when the last message of an incoming flight is received.
3393 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003395{
3396 /* We won't need to resend that one any more */
3397 ssl_flight_free( ssl->handshake->flight );
3398 ssl->handshake->flight = NULL;
3399 ssl->handshake->cur_msg = NULL;
3400
3401 /* The next incoming flight will start with this msg_seq */
3402 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3403
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003404 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003405 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003406
Hanno Becker0271f962018-08-16 13:23:47 +01003407 /* Clear future message buffering structure. */
3408 ssl_buffering_free( ssl );
3409
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003410 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003411 ssl_set_timer( ssl, 0 );
3412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3414 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003417 }
3418 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003420}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003421
3422/*
3423 * To be called when the last message of an outgoing flight is send.
3424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003426{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003427 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003428 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3431 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003434 }
3435 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003437}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003439
Paul Bakker5121ce52009-01-03 21:22:43 +00003440/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003441 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003442 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003443
3444/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003445 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003446 *
3447 * - fill in handshake headers
3448 * - update handshake checksum
3449 * - DTLS: save message for resending
3450 * - then pass to the record layer
3451 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003452 * DTLS: except for HelloRequest, messages are only queued, and will only be
3453 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003454 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003455 * Inputs:
3456 * - ssl->out_msglen: 4 + actual handshake message len
3457 * (4 is the size of handshake headers for TLS)
3458 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3459 * - ssl->out_msg + 4: the handshake message body
3460 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003461 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003462 * - ssl->out_msglen: the length of the record contents
3463 * (including handshake headers but excluding record headers)
3464 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003465 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003466int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003467{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003468 int ret;
3469 const size_t hs_len = ssl->out_msglen - 4;
3470 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003471
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3473
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003474 /*
3475 * Sanity checks
3476 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003477 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003478 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3479 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003480 /* In SSLv3, the client might send a NoCertificate alert. */
3481#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3482 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3483 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3484 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3485#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3486 {
3487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3488 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3489 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003490 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003491
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003492 /* Whenever we send anything different from a
3493 * HelloRequest we should be in a handshake - double check. */
3494 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3495 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003496 ssl->handshake == NULL )
3497 {
3498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3499 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3500 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003503 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003504 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003506 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3508 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003509 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003510#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003511
Hanno Beckerb50a2532018-08-06 11:52:54 +01003512 /* Double-check that we did not exceed the bounds
3513 * of the outgoing record buffer.
3514 * This should never fail as the various message
3515 * writing functions must obey the bounds of the
3516 * outgoing record buffer, but better be safe.
3517 *
3518 * Note: We deliberately do not check for the MTU or MFL here.
3519 */
3520 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3521 {
3522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3523 "size %u, maximum %u",
3524 (unsigned) ssl->out_msglen,
3525 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3526 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3527 }
3528
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003529 /*
3530 * Fill handshake headers
3531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003533 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003534 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3535 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3536 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003537
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003538 /*
3539 * DTLS has additional fields in the Handshake layer,
3540 * between the length field and the actual payload:
3541 * uint16 message_seq;
3542 * uint24 fragment_offset;
3543 * uint24 fragment_length;
3544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003546 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003547 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003548 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003549 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003550 {
3551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3552 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003553 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003554 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003555 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3556 }
3557
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003558 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003559 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003560
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003561 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003562 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003563 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003564 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3565 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3566 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003567 }
3568 else
3569 {
3570 ssl->out_msg[4] = 0;
3571 ssl->out_msg[5] = 0;
3572 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003573
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003574 /* Handshake hashes are computed without fragmentation,
3575 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003576 memset( ssl->out_msg + 6, 0x00, 3 );
3577 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003578 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003580
Hanno Becker0207e532018-08-28 10:28:28 +01003581 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003582 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3583 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003584 }
3585
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003586 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003588 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003589 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3590 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003591 {
3592 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003595 return( ret );
3596 }
3597 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003598 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003599#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003600 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003601 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003602 {
3603 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3604 return( ret );
3605 }
3606 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003607
3608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3609
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003610 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003611}
3612
3613/*
3614 * Record layer functions
3615 */
3616
3617/*
3618 * Write current record.
3619 *
3620 * Uses:
3621 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3622 * - ssl->out_msglen: length of the record content (excl headers)
3623 * - ssl->out_msg: record content
3624 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003625int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003626{
3627 int ret, done = 0;
3628 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003629 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003630
3631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003634 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003636 {
3637 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003639 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003640 return( ret );
3641 }
3642
3643 len = ssl->out_msglen;
3644 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003645#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3648 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 ret = mbedtls_ssl_hw_record_write( ssl );
3653 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3656 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003657 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003658
3659 if( ret == 0 )
3660 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003661 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003663 if( !done )
3664 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003665 unsigned i;
3666 size_t protected_record_size;
3667
Paul Bakker05ef8352012-05-08 09:17:57 +00003668 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003670 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003671
Hanno Becker19859472018-08-06 09:40:20 +01003672 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003673 ssl->out_len[0] = (unsigned char)( len >> 8 );
3674 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003675
Paul Bakker48916f92012-09-16 19:57:18 +00003676 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003677 {
3678 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003680 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003681 return( ret );
3682 }
3683
3684 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003685 ssl->out_len[0] = (unsigned char)( len >> 8 );
3686 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003687 }
3688
Hanno Becker2b1e3542018-08-06 11:19:13 +01003689 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3690
3691#if defined(MBEDTLS_SSL_PROTO_DTLS)
3692 /* In case of DTLS, double-check that we don't exceed
3693 * the remaining space in the datagram. */
3694 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3695 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003696 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003697 if( ret < 0 )
3698 return( ret );
3699
3700 if( protected_record_size > (size_t) ret )
3701 {
3702 /* Should never happen */
3703 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3704 }
3705 }
3706#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003709 "version = [%d:%d], msglen = %d",
3710 ssl->out_hdr[0], ssl->out_hdr[1],
3711 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003714 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003715
3716 ssl->out_left += protected_record_size;
3717 ssl->out_hdr += protected_record_size;
3718 ssl_update_out_pointers( ssl, ssl->transform_out );
3719
Hanno Becker04484622018-08-06 09:49:38 +01003720 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3721 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3722 break;
3723
3724 /* The loop goes to its end iff the counter is wrapping */
3725 if( i == ssl_ep_len( ssl ) )
3726 {
3727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3728 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3729 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003730 }
3731
Hanno Becker67bc7c32018-08-06 11:33:50 +01003732#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003733 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3734 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003735 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003736 size_t remaining;
3737 ret = ssl_get_remaining_payload_in_datagram( ssl );
3738 if( ret < 0 )
3739 {
3740 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3741 ret );
3742 return( ret );
3743 }
3744
3745 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003746 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003747 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003748 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003749 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003750 else
3751 {
Hanno Becker513815a2018-08-20 11:56:09 +01003752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003753 }
3754 }
3755#endif /* MBEDTLS_SSL_PROTO_DTLS */
3756
3757 if( ( flush == SSL_FORCE_FLUSH ) &&
3758 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003761 return( ret );
3762 }
3763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003765
3766 return( 0 );
3767}
3768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003770
3771static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3772{
3773 if( ssl->in_msglen < ssl->in_hslen ||
3774 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3775 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3776 {
3777 return( 1 );
3778 }
3779 return( 0 );
3780}
Hanno Becker44650b72018-08-16 12:51:11 +01003781
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003782static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003783{
3784 return( ( ssl->in_msg[9] << 16 ) |
3785 ( ssl->in_msg[10] << 8 ) |
3786 ssl->in_msg[11] );
3787}
3788
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003789static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003790{
3791 return( ( ssl->in_msg[6] << 16 ) |
3792 ( ssl->in_msg[7] << 8 ) |
3793 ssl->in_msg[8] );
3794}
3795
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003796static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003797{
3798 uint32_t msg_len, frag_off, frag_len;
3799
3800 msg_len = ssl_get_hs_total_len( ssl );
3801 frag_off = ssl_get_hs_frag_off( ssl );
3802 frag_len = ssl_get_hs_frag_len( ssl );
3803
3804 if( frag_off > msg_len )
3805 return( -1 );
3806
3807 if( frag_len > msg_len - frag_off )
3808 return( -1 );
3809
3810 if( frag_len + 12 > ssl->in_msglen )
3811 return( -1 );
3812
3813 return( 0 );
3814}
3815
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003816/*
3817 * Mark bits in bitmask (used for DTLS HS reassembly)
3818 */
3819static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3820{
3821 unsigned int start_bits, end_bits;
3822
3823 start_bits = 8 - ( offset % 8 );
3824 if( start_bits != 8 )
3825 {
3826 size_t first_byte_idx = offset / 8;
3827
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003828 /* Special case */
3829 if( len <= start_bits )
3830 {
3831 for( ; len != 0; len-- )
3832 mask[first_byte_idx] |= 1 << ( start_bits - len );
3833
3834 /* Avoid potential issues with offset or len becoming invalid */
3835 return;
3836 }
3837
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003838 offset += start_bits; /* Now offset % 8 == 0 */
3839 len -= start_bits;
3840
3841 for( ; start_bits != 0; start_bits-- )
3842 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3843 }
3844
3845 end_bits = len % 8;
3846 if( end_bits != 0 )
3847 {
3848 size_t last_byte_idx = ( offset + len ) / 8;
3849
3850 len -= end_bits; /* Now len % 8 == 0 */
3851
3852 for( ; end_bits != 0; end_bits-- )
3853 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3854 }
3855
3856 memset( mask + offset / 8, 0xFF, len / 8 );
3857}
3858
3859/*
3860 * Check that bitmask is full
3861 */
3862static int ssl_bitmask_check( unsigned char *mask, size_t len )
3863{
3864 size_t i;
3865
3866 for( i = 0; i < len / 8; i++ )
3867 if( mask[i] != 0xFF )
3868 return( -1 );
3869
3870 for( i = 0; i < len % 8; i++ )
3871 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3872 return( -1 );
3873
3874 return( 0 );
3875}
3876
Hanno Becker56e205e2018-08-16 09:06:12 +01003877/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003878static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003879 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003880{
Hanno Becker56e205e2018-08-16 09:06:12 +01003881 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003882
Hanno Becker56e205e2018-08-16 09:06:12 +01003883 alloc_len = 12; /* Handshake header */
3884 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003885
Hanno Beckerd07df862018-08-16 09:14:58 +01003886 if( add_bitmap )
3887 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003888
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003889 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003890}
Hanno Becker56e205e2018-08-16 09:06:12 +01003891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003892#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003893
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003894static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003895{
3896 return( ( ssl->in_msg[1] << 16 ) |
3897 ( ssl->in_msg[2] << 8 ) |
3898 ssl->in_msg[3] );
3899}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003900
Simon Butcher99000142016-10-13 17:21:01 +01003901int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003902{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003903 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003906 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003907 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003908 }
3909
Hanno Becker12555c62018-08-16 12:47:53 +01003910 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003913 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003914 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003917 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003918 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003919 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003920 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003921
Hanno Becker44650b72018-08-16 12:51:11 +01003922 if( ssl_check_hs_header( ssl ) != 0 )
3923 {
3924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3925 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3926 }
3927
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003928 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003929 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3930 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3931 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3932 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003933 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003934 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3935 {
3936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3937 recv_msg_seq,
3938 ssl->handshake->in_msg_seq ) );
3939 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3940 }
3941
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003942 /* Retransmit only on last message from previous flight, to avoid
3943 * too many retransmissions.
3944 * Besides, No sane server ever retransmits HelloVerifyRequest */
3945 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003946 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003949 "message_seq = %d, start_of_flight = %d",
3950 recv_msg_seq,
3951 ssl->handshake->in_flight_start_seq ) );
3952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003953 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003956 return( ret );
3957 }
3958 }
3959 else
3960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003962 "message_seq = %d, expected = %d",
3963 recv_msg_seq,
3964 ssl->handshake->in_msg_seq ) );
3965 }
3966
Hanno Becker90333da2017-10-10 11:27:13 +01003967 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003968 }
3969 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003970
Hanno Becker6d97ef52018-08-16 13:09:04 +01003971 /* Message reassembly is handled alongside buffering of future
3972 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003973 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003974 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003975 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003978 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003979 }
3980 }
3981 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003983 /* With TLS we don't handle fragmentation (for now) */
3984 if( ssl->in_msglen < ssl->in_hslen )
3985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3987 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003988 }
3989
Simon Butcher99000142016-10-13 17:21:01 +01003990 return( 0 );
3991}
3992
3993void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3994{
Hanno Becker0271f962018-08-16 13:23:47 +01003995 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003996
Hanno Becker0271f962018-08-16 13:23:47 +01003997 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003998 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003999 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004000 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004001
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004002 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004003#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004004 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004005 ssl->handshake != NULL )
4006 {
Hanno Becker0271f962018-08-16 13:23:47 +01004007 unsigned offset;
4008 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004009
Hanno Becker0271f962018-08-16 13:23:47 +01004010 /* Increment handshake sequence number */
4011 hs->in_msg_seq++;
4012
4013 /*
4014 * Clear up handshake buffering and reassembly structure.
4015 */
4016
4017 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004018 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004019
4020 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004021 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4022 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004023 offset++, hs_buf++ )
4024 {
4025 *hs_buf = *(hs_buf + 1);
4026 }
4027
4028 /* Create a fresh last entry */
4029 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004030 }
4031#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004032}
4033
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004034/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004035 * DTLS anti-replay: RFC 6347 4.1.2.6
4036 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004037 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4038 * Bit n is set iff record number in_window_top - n has been seen.
4039 *
4040 * Usually, in_window_top is the last record number seen and the lsb of
4041 * in_window is set. The only exception is the initial state (record number 0
4042 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004043 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004044#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4045static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004046{
4047 ssl->in_window_top = 0;
4048 ssl->in_window = 0;
4049}
4050
4051static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4052{
4053 return( ( (uint64_t) buf[0] << 40 ) |
4054 ( (uint64_t) buf[1] << 32 ) |
4055 ( (uint64_t) buf[2] << 24 ) |
4056 ( (uint64_t) buf[3] << 16 ) |
4057 ( (uint64_t) buf[4] << 8 ) |
4058 ( (uint64_t) buf[5] ) );
4059}
4060
4061/*
4062 * Return 0 if sequence number is acceptable, -1 otherwise
4063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004065{
4066 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4067 uint64_t bit;
4068
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004069 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004070 return( 0 );
4071
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004072 if( rec_seqnum > ssl->in_window_top )
4073 return( 0 );
4074
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004075 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004076
4077 if( bit >= 64 )
4078 return( -1 );
4079
4080 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4081 return( -1 );
4082
4083 return( 0 );
4084}
4085
4086/*
4087 * Update replay window on new validated record
4088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004090{
4091 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4092
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004093 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004094 return;
4095
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004096 if( rec_seqnum > ssl->in_window_top )
4097 {
4098 /* Update window_top and the contents of the window */
4099 uint64_t shift = rec_seqnum - ssl->in_window_top;
4100
4101 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004102 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004103 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004104 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004105 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004106 ssl->in_window |= 1;
4107 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004108
4109 ssl->in_window_top = rec_seqnum;
4110 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004111 else
4112 {
4113 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004114 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004115
4116 if( bit < 64 ) /* Always true, but be extra sure */
4117 ssl->in_window |= (uint64_t) 1 << bit;
4118 }
4119}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004120#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004121
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004122#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004123/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004124static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4125
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004126/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004127 * Without any SSL context, check if a datagram looks like a ClientHello with
4128 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004129 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004130 *
4131 * - if cookie is valid, return 0
4132 * - if ClientHello looks superficially valid but cookie is not,
4133 * fill obuf and set olen, then
4134 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4135 * - otherwise return a specific error code
4136 */
4137static int ssl_check_dtls_clihlo_cookie(
4138 mbedtls_ssl_cookie_write_t *f_cookie_write,
4139 mbedtls_ssl_cookie_check_t *f_cookie_check,
4140 void *p_cookie,
4141 const unsigned char *cli_id, size_t cli_id_len,
4142 const unsigned char *in, size_t in_len,
4143 unsigned char *obuf, size_t buf_len, size_t *olen )
4144{
4145 size_t sid_len, cookie_len;
4146 unsigned char *p;
4147
4148 if( f_cookie_write == NULL || f_cookie_check == NULL )
4149 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4150
4151 /*
4152 * Structure of ClientHello with record and handshake headers,
4153 * and expected values. We don't need to check a lot, more checks will be
4154 * done when actually parsing the ClientHello - skipping those checks
4155 * avoids code duplication and does not make cookie forging any easier.
4156 *
4157 * 0-0 ContentType type; copied, must be handshake
4158 * 1-2 ProtocolVersion version; copied
4159 * 3-4 uint16 epoch; copied, must be 0
4160 * 5-10 uint48 sequence_number; copied
4161 * 11-12 uint16 length; (ignored)
4162 *
4163 * 13-13 HandshakeType msg_type; (ignored)
4164 * 14-16 uint24 length; (ignored)
4165 * 17-18 uint16 message_seq; copied
4166 * 19-21 uint24 fragment_offset; copied, must be 0
4167 * 22-24 uint24 fragment_length; (ignored)
4168 *
4169 * 25-26 ProtocolVersion client_version; (ignored)
4170 * 27-58 Random random; (ignored)
4171 * 59-xx SessionID session_id; 1 byte len + sid_len content
4172 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4173 * ...
4174 *
4175 * Minimum length is 61 bytes.
4176 */
4177 if( in_len < 61 ||
4178 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4179 in[3] != 0 || in[4] != 0 ||
4180 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4181 {
4182 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4183 }
4184
4185 sid_len = in[59];
4186 if( sid_len > in_len - 61 )
4187 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4188
4189 cookie_len = in[60 + sid_len];
4190 if( cookie_len > in_len - 60 )
4191 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4192
4193 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4194 cli_id, cli_id_len ) == 0 )
4195 {
4196 /* Valid cookie */
4197 return( 0 );
4198 }
4199
4200 /*
4201 * If we get here, we've got an invalid cookie, let's prepare HVR.
4202 *
4203 * 0-0 ContentType type; copied
4204 * 1-2 ProtocolVersion version; copied
4205 * 3-4 uint16 epoch; copied
4206 * 5-10 uint48 sequence_number; copied
4207 * 11-12 uint16 length; olen - 13
4208 *
4209 * 13-13 HandshakeType msg_type; hello_verify_request
4210 * 14-16 uint24 length; olen - 25
4211 * 17-18 uint16 message_seq; copied
4212 * 19-21 uint24 fragment_offset; copied
4213 * 22-24 uint24 fragment_length; olen - 25
4214 *
4215 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4216 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4217 *
4218 * Minimum length is 28.
4219 */
4220 if( buf_len < 28 )
4221 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4222
4223 /* Copy most fields and adapt others */
4224 memcpy( obuf, in, 25 );
4225 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4226 obuf[25] = 0xfe;
4227 obuf[26] = 0xff;
4228
4229 /* Generate and write actual cookie */
4230 p = obuf + 28;
4231 if( f_cookie_write( p_cookie,
4232 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4233 {
4234 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4235 }
4236
4237 *olen = p - obuf;
4238
4239 /* Go back and fill length fields */
4240 obuf[27] = (unsigned char)( *olen - 28 );
4241
4242 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4243 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4244 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4245
4246 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4247 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4248
4249 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4250}
4251
4252/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004253 * Handle possible client reconnect with the same UDP quadruplet
4254 * (RFC 6347 Section 4.2.8).
4255 *
4256 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4257 * that looks like a ClientHello.
4258 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004259 * - if the input looks like a ClientHello without cookies,
4260 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004261 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004262 * - if the input looks like a ClientHello with a valid cookie,
4263 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004264 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004265 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004266 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004267 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004268 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4269 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004270 */
4271static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4272{
4273 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004274 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004275
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004276 ret = ssl_check_dtls_clihlo_cookie(
4277 ssl->conf->f_cookie_write,
4278 ssl->conf->f_cookie_check,
4279 ssl->conf->p_cookie,
4280 ssl->cli_id, ssl->cli_id_len,
4281 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004282 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004283
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004284 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4285
4286 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004287 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004288 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004289 * If the error is permanent we'll catch it later,
4290 * if it's not, then hopefully it'll work next time. */
4291 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4292
4293 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004294 }
4295
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004296 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004297 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004298 /* Got a valid cookie, partially reset context */
4299 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4300 {
4301 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4302 return( ret );
4303 }
4304
4305 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004306 }
4307
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004308 return( ret );
4309}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004310#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004311
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004312/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004313 * ContentType type;
4314 * ProtocolVersion version;
4315 * uint16 epoch; // DTLS only
4316 * uint48 sequence_number; // DTLS only
4317 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004318 *
4319 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004320 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004321 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4322 *
4323 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004324 * 1. proceed with the record if this function returns 0
4325 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4326 * 3. return CLIENT_RECONNECT if this function return that value
4327 * 4. drop the whole datagram if this function returns anything else.
4328 * Point 2 is needed when the peer is resending, and we have already received
4329 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004332{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004333 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004335 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 +02004336
Paul Bakker5121ce52009-01-03 21:22:43 +00004337 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004338 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004339 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004342 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004343 ssl->in_msgtype,
4344 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004345
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004346 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004347 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4348 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4349 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4350 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004353
4354#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004355 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4356 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004357 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4358#endif /* MBEDTLS_SSL_PROTO_DTLS */
4359 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4360 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004362 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004363 }
4364
4365 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004366 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4369 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004370 }
4371
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004372 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4375 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004376 }
4377
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004378 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004379 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004380 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4383 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004384 }
4385
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004386 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004387 * DTLS-related tests.
4388 * Check epoch before checking length constraint because
4389 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4390 * message gets duplicated before the corresponding Finished message,
4391 * the second ChangeCipherSpec should be discarded because it belongs
4392 * to an old epoch, but not because its length is shorter than
4393 * the minimum record length for packets using the new record transform.
4394 * Note that these two kinds of failures are handled differently,
4395 * as an unexpected record is silently skipped but an invalid
4396 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004397 */
4398#if defined(MBEDTLS_SSL_PROTO_DTLS)
4399 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4400 {
4401 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4402
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004403 /* Check epoch (and sequence number) with DTLS */
4404 if( rec_epoch != ssl->in_epoch )
4405 {
4406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4407 "expected %d, received %d",
4408 ssl->in_epoch, rec_epoch ) );
4409
4410#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4411 /*
4412 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4413 * access the first byte of record content (handshake type), as we
4414 * have an active transform (possibly iv_len != 0), so use the
4415 * fact that the record header len is 13 instead.
4416 */
4417 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4418 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4419 rec_epoch == 0 &&
4420 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4421 ssl->in_left > 13 &&
4422 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4423 {
4424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4425 "from the same port" ) );
4426 return( ssl_handle_possible_reconnect( ssl ) );
4427 }
4428 else
4429#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004430 {
4431 /* Consider buffering the record. */
4432 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4433 {
4434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4435 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4436 }
4437
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004438 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004439 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004440 }
4441
4442#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4443 /* Replay detection only works for the current epoch */
4444 if( rec_epoch == ssl->in_epoch &&
4445 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4446 {
4447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4448 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4449 }
4450#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004451
Hanno Becker52c6dc62017-05-26 16:07:36 +01004452 /* Drop unexpected ApplicationData records,
4453 * except at the beginning of renegotiations */
4454 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4455 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4456#if defined(MBEDTLS_SSL_RENEGOTIATION)
4457 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4458 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4459#endif
4460 )
4461 {
4462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4463 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4464 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004465 }
4466#endif /* MBEDTLS_SSL_PROTO_DTLS */
4467
Hanno Becker52c6dc62017-05-26 16:07:36 +01004468
4469 /* Check length against bounds of the current transform and version */
4470 if( ssl->transform_in == NULL )
4471 {
4472 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004473 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004474 {
4475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4476 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4477 }
4478 }
4479 else
4480 {
4481 if( ssl->in_msglen < ssl->transform_in->minlen )
4482 {
4483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4484 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4485 }
4486
4487#if defined(MBEDTLS_SSL_PROTO_SSL3)
4488 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004489 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004490 {
4491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4492 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4493 }
4494#endif
4495#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4496 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4497 /*
4498 * TLS encrypted messages can have up to 256 bytes of padding
4499 */
4500 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4501 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004502 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004503 {
4504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4505 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4506 }
4507#endif
4508 }
4509
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004510 return( 0 );
4511}
Paul Bakker5121ce52009-01-03 21:22:43 +00004512
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004513/*
4514 * If applicable, decrypt (and decompress) record content
4515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004517{
4518 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004520 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4521 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004523#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4524 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004528 ret = mbedtls_ssl_hw_record_read( ssl );
4529 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004531 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4532 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004533 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004534
4535 if( ret == 0 )
4536 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004537 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004538#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004539 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004540 {
4541 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004543 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004544 return( ret );
4545 }
4546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004548 ssl->in_msg, ssl->in_msglen );
4549
Angus Grattond8213d02016-05-25 20:56:48 +10004550 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4553 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004554 }
4555 }
4556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004557#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004558 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004559 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004560 {
4561 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004563 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004564 return( ret );
4565 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004566 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004567#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004569#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004570 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004572 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004573 }
4574#endif
4575
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004576 return( 0 );
4577}
4578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004579static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004580
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004581/*
4582 * Read a record.
4583 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004584 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4585 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4586 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004587 */
Hanno Becker1097b342018-08-15 14:09:41 +01004588
4589/* Helper functions for mbedtls_ssl_read_record(). */
4590static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004591static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4592static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004593
Hanno Becker327c93b2018-08-15 13:56:18 +01004594int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004595 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004596{
4597 int ret;
4598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004600
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004601 if( ssl->keep_current_message == 0 )
4602 {
4603 do {
Simon Butcher99000142016-10-13 17:21:01 +01004604
Hanno Becker26994592018-08-15 14:14:59 +01004605 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004606 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004607 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004608
Hanno Beckere74d5562018-08-15 14:26:08 +01004609 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004610 {
Hanno Becker40f50842018-08-15 14:48:01 +01004611#if defined(MBEDTLS_SSL_PROTO_DTLS)
4612 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004613
Hanno Becker40f50842018-08-15 14:48:01 +01004614 /* We only check for buffered messages if the
4615 * current datagram is fully consumed. */
4616 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004617 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004618 {
Hanno Becker40f50842018-08-15 14:48:01 +01004619 if( ssl_load_buffered_message( ssl ) == 0 )
4620 have_buffered = 1;
4621 }
4622
4623 if( have_buffered == 0 )
4624#endif /* MBEDTLS_SSL_PROTO_DTLS */
4625 {
4626 ret = ssl_get_next_record( ssl );
4627 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4628 continue;
4629
4630 if( ret != 0 )
4631 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004632 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004633 return( ret );
4634 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004635 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004636 }
4637
4638 ret = mbedtls_ssl_handle_message_type( ssl );
4639
Hanno Becker40f50842018-08-15 14:48:01 +01004640#if defined(MBEDTLS_SSL_PROTO_DTLS)
4641 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4642 {
4643 /* Buffer future message */
4644 ret = ssl_buffer_message( ssl );
4645 if( ret != 0 )
4646 return( ret );
4647
4648 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4649 }
4650#endif /* MBEDTLS_SSL_PROTO_DTLS */
4651
Hanno Becker90333da2017-10-10 11:27:13 +01004652 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4653 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004654
4655 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004656 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004657 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004658 return( ret );
4659 }
4660
Hanno Becker327c93b2018-08-15 13:56:18 +01004661 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004662 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004663 {
4664 mbedtls_ssl_update_handshake_status( ssl );
4665 }
Simon Butcher99000142016-10-13 17:21:01 +01004666 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004667 else
Simon Butcher99000142016-10-13 17:21:01 +01004668 {
Hanno Becker02f59072018-08-15 14:00:24 +01004669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004670 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004671 }
4672
4673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4674
4675 return( 0 );
4676}
4677
Hanno Becker40f50842018-08-15 14:48:01 +01004678#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004679static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004680{
Hanno Becker40f50842018-08-15 14:48:01 +01004681 if( ssl->in_left > ssl->next_record_offset )
4682 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004683
Hanno Becker40f50842018-08-15 14:48:01 +01004684 return( 0 );
4685}
4686
4687static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4688{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004689 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004690 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004691 int ret = 0;
4692
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004693 if( hs == NULL )
4694 return( -1 );
4695
Hanno Beckere00ae372018-08-20 09:39:42 +01004696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4697
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004698 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4699 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4700 {
4701 /* Check if we have seen a ChangeCipherSpec before.
4702 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004703 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004704 {
4705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4706 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004707 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004708 }
4709
Hanno Becker39b8bc92018-08-28 17:17:13 +01004710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004711 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4712 ssl->in_msglen = 1;
4713 ssl->in_msg[0] = 1;
4714
4715 /* As long as they are equal, the exact value doesn't matter. */
4716 ssl->in_left = 0;
4717 ssl->next_record_offset = 0;
4718
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004719 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004720 goto exit;
4721 }
Hanno Becker37f95322018-08-16 13:55:32 +01004722
Hanno Beckerb8f50142018-08-28 10:01:34 +01004723#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004724 /* Debug only */
4725 {
4726 unsigned offset;
4727 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4728 {
4729 hs_buf = &hs->buffering.hs[offset];
4730 if( hs_buf->is_valid == 1 )
4731 {
4732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4733 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004734 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004735 }
4736 }
4737 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004738#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004739
4740 /* Check if we have buffered and/or fully reassembled the
4741 * next handshake message. */
4742 hs_buf = &hs->buffering.hs[0];
4743 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4744 {
4745 /* Synthesize a record containing the buffered HS message. */
4746 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4747 ( hs_buf->data[2] << 8 ) |
4748 hs_buf->data[3];
4749
4750 /* Double-check that we haven't accidentally buffered
4751 * a message that doesn't fit into the input buffer. */
4752 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4753 {
4754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4755 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4756 }
4757
4758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4759 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4760 hs_buf->data, msg_len + 12 );
4761
4762 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4763 ssl->in_hslen = msg_len + 12;
4764 ssl->in_msglen = msg_len + 12;
4765 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4766
4767 ret = 0;
4768 goto exit;
4769 }
4770 else
4771 {
4772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4773 hs->in_msg_seq ) );
4774 }
4775
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004776 ret = -1;
4777
4778exit:
4779
4780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4781 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004782}
4783
Hanno Beckera02b0b42018-08-21 17:20:27 +01004784static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4785 size_t desired )
4786{
4787 int offset;
4788 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4790 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004791
Hanno Becker01315ea2018-08-21 17:22:17 +01004792 /* Get rid of future records epoch first, if such exist. */
4793 ssl_free_buffered_record( ssl );
4794
4795 /* Check if we have enough space available now. */
4796 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4797 hs->buffering.total_bytes_buffered ) )
4798 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004800 return( 0 );
4801 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004802
Hanno Becker4f432ad2018-08-28 10:02:32 +01004803 /* We don't have enough space to buffer the next expected handshake
4804 * message. Remove buffers used for future messages to gain space,
4805 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004806 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4807 offset >= 0; offset-- )
4808 {
4809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4810 offset ) );
4811
Hanno Beckerb309b922018-08-23 13:18:05 +01004812 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004813
4814 /* Check if we have enough space available now. */
4815 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4816 hs->buffering.total_bytes_buffered ) )
4817 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004819 return( 0 );
4820 }
4821 }
4822
4823 return( -1 );
4824}
4825
Hanno Becker40f50842018-08-15 14:48:01 +01004826static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4827{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004828 int ret = 0;
4829 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4830
4831 if( hs == NULL )
4832 return( 0 );
4833
4834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4835
4836 switch( ssl->in_msgtype )
4837 {
4838 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004840
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004841 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004842 break;
4843
4844 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004845 {
4846 unsigned recv_msg_seq_offset;
4847 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4848 mbedtls_ssl_hs_buffer *hs_buf;
4849 size_t msg_len = ssl->in_hslen - 12;
4850
4851 /* We should never receive an old handshake
4852 * message - double-check nonetheless. */
4853 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4854 {
4855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4856 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4857 }
4858
4859 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4860 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4861 {
4862 /* Silently ignore -- message too far in the future */
4863 MBEDTLS_SSL_DEBUG_MSG( 2,
4864 ( "Ignore future HS message with sequence number %u, "
4865 "buffering window %u - %u",
4866 recv_msg_seq, ssl->handshake->in_msg_seq,
4867 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4868
4869 goto exit;
4870 }
4871
4872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4873 recv_msg_seq, recv_msg_seq_offset ) );
4874
4875 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4876
4877 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004878 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004879 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004880 size_t reassembly_buf_sz;
4881
Hanno Becker37f95322018-08-16 13:55:32 +01004882 hs_buf->is_fragmented =
4883 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4884
4885 /* We copy the message back into the input buffer
4886 * after reassembly, so check that it's not too large.
4887 * This is an implementation-specific limitation
4888 * and not one from the standard, hence it is not
4889 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004890 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004891 {
4892 /* Ignore message */
4893 goto exit;
4894 }
4895
Hanno Beckere0b150f2018-08-21 15:51:03 +01004896 /* Check if we have enough space to buffer the message. */
4897 if( hs->buffering.total_bytes_buffered >
4898 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4899 {
4900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4901 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4902 }
4903
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004904 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4905 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004906
4907 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4908 hs->buffering.total_bytes_buffered ) )
4909 {
4910 if( recv_msg_seq_offset > 0 )
4911 {
4912 /* If we can't buffer a future message because
4913 * of space limitations -- ignore. */
4914 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",
4915 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4916 (unsigned) hs->buffering.total_bytes_buffered ) );
4917 goto exit;
4918 }
Hanno Beckere1801392018-08-21 16:51:05 +01004919 else
4920 {
4921 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",
4922 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4923 (unsigned) hs->buffering.total_bytes_buffered ) );
4924 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004925
Hanno Beckera02b0b42018-08-21 17:20:27 +01004926 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004927 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004928 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",
4929 (unsigned) msg_len,
4930 (unsigned) reassembly_buf_sz,
4931 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004932 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004933 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4934 goto exit;
4935 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004936 }
4937
4938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
4939 msg_len ) );
4940
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004941 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4942 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004943 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004944 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004945 goto exit;
4946 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004947 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004948
4949 /* Prepare final header: copy msg_type, length and message_seq,
4950 * then add standardised fragment_offset and fragment_length */
4951 memcpy( hs_buf->data, ssl->in_msg, 6 );
4952 memset( hs_buf->data + 6, 0, 3 );
4953 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4954
4955 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004956
4957 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004958 }
4959 else
4960 {
4961 /* Make sure msg_type and length are consistent */
4962 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4963 {
4964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4965 /* Ignore */
4966 goto exit;
4967 }
4968 }
4969
Hanno Becker4422bbb2018-08-20 09:40:19 +01004970 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004971 {
4972 size_t frag_len, frag_off;
4973 unsigned char * const msg = hs_buf->data + 12;
4974
4975 /*
4976 * Check and copy current fragment
4977 */
4978
4979 /* Validation of header fields already done in
4980 * mbedtls_ssl_prepare_handshake_record(). */
4981 frag_off = ssl_get_hs_frag_off( ssl );
4982 frag_len = ssl_get_hs_frag_len( ssl );
4983
4984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
4985 frag_off, frag_len ) );
4986 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4987
4988 if( hs_buf->is_fragmented )
4989 {
4990 unsigned char * const bitmask = msg + msg_len;
4991 ssl_bitmask_set( bitmask, frag_off, frag_len );
4992 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4993 msg_len ) == 0 );
4994 }
4995 else
4996 {
4997 hs_buf->is_complete = 1;
4998 }
4999
5000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5001 hs_buf->is_complete ? "" : "not yet " ) );
5002 }
5003
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005004 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005005 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005006
5007 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005008 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005009 break;
5010 }
5011
5012exit:
5013
5014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5015 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005016}
5017#endif /* MBEDTLS_SSL_PROTO_DTLS */
5018
Hanno Becker1097b342018-08-15 14:09:41 +01005019static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005020{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005021 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005022 * Consume last content-layer message and potentially
5023 * update in_msglen which keeps track of the contents'
5024 * consumption state.
5025 *
5026 * (1) Handshake messages:
5027 * Remove last handshake message, move content
5028 * and adapt in_msglen.
5029 *
5030 * (2) Alert messages:
5031 * Consume whole record content, in_msglen = 0.
5032 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005033 * (3) Change cipher spec:
5034 * Consume whole record content, in_msglen = 0.
5035 *
5036 * (4) Application data:
5037 * Don't do anything - the record layer provides
5038 * the application data as a stream transport
5039 * and consumes through mbedtls_ssl_read only.
5040 *
5041 */
5042
5043 /* Case (1): Handshake messages */
5044 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005045 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005046 /* Hard assertion to be sure that no application data
5047 * is in flight, as corrupting ssl->in_msglen during
5048 * ssl->in_offt != NULL is fatal. */
5049 if( ssl->in_offt != NULL )
5050 {
5051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5052 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5053 }
5054
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005055 /*
5056 * Get next Handshake message in the current record
5057 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005058
Hanno Becker4a810fb2017-05-24 16:27:30 +01005059 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005060 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005061 * current handshake content: If DTLS handshake
5062 * fragmentation is used, that's the fragment
5063 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005064 * size here is faulty and should be changed at
5065 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005066 * (2) While it doesn't seem to cause problems, one
5067 * has to be very careful not to assume that in_hslen
5068 * is always <= in_msglen in a sensible communication.
5069 * Again, it's wrong for DTLS handshake fragmentation.
5070 * The following check is therefore mandatory, and
5071 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005072 * Additionally, ssl->in_hslen might be arbitrarily out of
5073 * bounds after handling a DTLS message with an unexpected
5074 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005075 */
5076 if( ssl->in_hslen < ssl->in_msglen )
5077 {
5078 ssl->in_msglen -= ssl->in_hslen;
5079 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5080 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005081
Hanno Becker4a810fb2017-05-24 16:27:30 +01005082 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5083 ssl->in_msg, ssl->in_msglen );
5084 }
5085 else
5086 {
5087 ssl->in_msglen = 0;
5088 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005089
Hanno Becker4a810fb2017-05-24 16:27:30 +01005090 ssl->in_hslen = 0;
5091 }
5092 /* Case (4): Application data */
5093 else if( ssl->in_offt != NULL )
5094 {
5095 return( 0 );
5096 }
5097 /* Everything else (CCS & Alerts) */
5098 else
5099 {
5100 ssl->in_msglen = 0;
5101 }
5102
Hanno Becker1097b342018-08-15 14:09:41 +01005103 return( 0 );
5104}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005105
Hanno Beckere74d5562018-08-15 14:26:08 +01005106static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5107{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005108 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005109 return( 1 );
5110
5111 return( 0 );
5112}
5113
Hanno Becker5f066e72018-08-16 14:56:31 +01005114#if defined(MBEDTLS_SSL_PROTO_DTLS)
5115
5116static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5117{
5118 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5119 if( hs == NULL )
5120 return;
5121
Hanno Becker01315ea2018-08-21 17:22:17 +01005122 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005123 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005124 hs->buffering.total_bytes_buffered -=
5125 hs->buffering.future_record.len;
5126
5127 mbedtls_free( hs->buffering.future_record.data );
5128 hs->buffering.future_record.data = NULL;
5129 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005130}
5131
5132static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5133{
5134 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5135 unsigned char * rec;
5136 size_t rec_len;
5137 unsigned rec_epoch;
5138
5139 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5140 return( 0 );
5141
5142 if( hs == NULL )
5143 return( 0 );
5144
Hanno Becker5f066e72018-08-16 14:56:31 +01005145 rec = hs->buffering.future_record.data;
5146 rec_len = hs->buffering.future_record.len;
5147 rec_epoch = hs->buffering.future_record.epoch;
5148
5149 if( rec == NULL )
5150 return( 0 );
5151
Hanno Becker4cb782d2018-08-20 11:19:05 +01005152 /* Only consider loading future records if the
5153 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005154 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005155 return( 0 );
5156
Hanno Becker5f066e72018-08-16 14:56:31 +01005157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5158
5159 if( rec_epoch != ssl->in_epoch )
5160 {
5161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5162 goto exit;
5163 }
5164
5165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5166
5167 /* Double-check that the record is not too large */
5168 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5169 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5170 {
5171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5172 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5173 }
5174
5175 memcpy( ssl->in_hdr, rec, rec_len );
5176 ssl->in_left = rec_len;
5177 ssl->next_record_offset = 0;
5178
5179 ssl_free_buffered_record( ssl );
5180
5181exit:
5182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5183 return( 0 );
5184}
5185
5186static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5187{
5188 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5189 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005190 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005191
5192 /* Don't buffer future records outside handshakes. */
5193 if( hs == NULL )
5194 return( 0 );
5195
5196 /* Only buffer handshake records (we are only interested
5197 * in Finished messages). */
5198 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5199 return( 0 );
5200
5201 /* Don't buffer more than one future epoch record. */
5202 if( hs->buffering.future_record.data != NULL )
5203 return( 0 );
5204
Hanno Becker01315ea2018-08-21 17:22:17 +01005205 /* Don't buffer record if there's not enough buffering space remaining. */
5206 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5207 hs->buffering.total_bytes_buffered ) )
5208 {
5209 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",
5210 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5211 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005212 return( 0 );
5213 }
5214
Hanno Becker5f066e72018-08-16 14:56:31 +01005215 /* Buffer record */
5216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5217 ssl->in_epoch + 1 ) );
5218 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5219 rec_hdr_len + ssl->in_msglen );
5220
5221 /* ssl_parse_record_header() only considers records
5222 * of the next epoch as candidates for buffering. */
5223 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005224 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005225
5226 hs->buffering.future_record.data =
5227 mbedtls_calloc( 1, hs->buffering.future_record.len );
5228 if( hs->buffering.future_record.data == NULL )
5229 {
5230 /* If we run out of RAM trying to buffer a
5231 * record from the next epoch, just ignore. */
5232 return( 0 );
5233 }
5234
Hanno Becker01315ea2018-08-21 17:22:17 +01005235 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005236
Hanno Becker01315ea2018-08-21 17:22:17 +01005237 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005238 return( 0 );
5239}
5240
5241#endif /* MBEDTLS_SSL_PROTO_DTLS */
5242
Hanno Beckere74d5562018-08-15 14:26:08 +01005243static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005244{
5245 int ret;
5246
Hanno Becker5f066e72018-08-16 14:56:31 +01005247#if defined(MBEDTLS_SSL_PROTO_DTLS)
5248 /* We might have buffered a future record; if so,
5249 * and if the epoch matches now, load it.
5250 * On success, this call will set ssl->in_left to
5251 * the length of the buffered record, so that
5252 * the calls to ssl_fetch_input() below will
5253 * essentially be no-ops. */
5254 ret = ssl_load_buffered_record( ssl );
5255 if( ret != 0 )
5256 return( ret );
5257#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005259 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005261 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005262 return( ret );
5263 }
5264
5265 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005267#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005268 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5269 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005270 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005271 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5272 {
5273 ret = ssl_buffer_future_record( ssl );
5274 if( ret != 0 )
5275 return( ret );
5276
5277 /* Fall through to handling of unexpected records */
5278 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5279 }
5280
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005281 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5282 {
5283 /* Skip unexpected record (but not whole datagram) */
5284 ssl->next_record_offset = ssl->in_msglen
5285 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005286
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5288 "(header)" ) );
5289 }
5290 else
5291 {
5292 /* Skip invalid record and the rest of the datagram */
5293 ssl->next_record_offset = 0;
5294 ssl->in_left = 0;
5295
5296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5297 "(header)" ) );
5298 }
5299
5300 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005301 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005302 }
5303#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005304 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005305 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005306
5307 /*
5308 * Read and optionally decrypt the message contents
5309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005310 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5311 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005313 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005314 return( ret );
5315 }
5316
5317 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005318#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005319 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005321 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005322 if( ssl->next_record_offset < ssl->in_left )
5323 {
5324 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5325 }
5326 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005327 else
5328#endif
5329 ssl->in_left = 0;
5330
5331 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005333#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005334 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005335 {
5336 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5338 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005339 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005340 /* Except when waiting for Finished as a bad mac here
5341 * probably means something went wrong in the handshake
5342 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5343 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5344 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5345 {
5346#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5347 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5348 {
5349 mbedtls_ssl_send_alert_message( ssl,
5350 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5351 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5352 }
5353#endif
5354 return( ret );
5355 }
5356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005357#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005358 if( ssl->conf->badmac_limit != 0 &&
5359 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5362 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005363 }
5364#endif
5365
Hanno Becker4a810fb2017-05-24 16:27:30 +01005366 /* As above, invalid records cause
5367 * dismissal of the whole datagram. */
5368
5369 ssl->next_record_offset = 0;
5370 ssl->in_left = 0;
5371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005373 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005374 }
5375
5376 return( ret );
5377 }
5378 else
5379#endif
5380 {
5381 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005382#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5383 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005385 mbedtls_ssl_send_alert_message( ssl,
5386 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5387 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005388 }
5389#endif
5390 return( ret );
5391 }
5392 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005393
Simon Butcher99000142016-10-13 17:21:01 +01005394 return( 0 );
5395}
5396
5397int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5398{
5399 int ret;
5400
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005401 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005402 * Handle particular types of records
5403 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005404 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005405 {
Simon Butcher99000142016-10-13 17:21:01 +01005406 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5407 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005408 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005409 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005410 }
5411
Hanno Beckere678eaa2018-08-21 14:57:46 +01005412 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005413 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005414 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005415 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5417 ssl->in_msglen ) );
5418 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005419 }
5420
Hanno Beckere678eaa2018-08-21 14:57:46 +01005421 if( ssl->in_msg[0] != 1 )
5422 {
5423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5424 ssl->in_msg[0] ) );
5425 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5426 }
5427
5428#if defined(MBEDTLS_SSL_PROTO_DTLS)
5429 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5430 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5431 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5432 {
5433 if( ssl->handshake == NULL )
5434 {
5435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5436 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5437 }
5438
5439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5440 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5441 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005442#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005443 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005445 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005446 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005447 if( ssl->in_msglen != 2 )
5448 {
5449 /* Note: Standard allows for more than one 2 byte alert
5450 to be packed in a single message, but Mbed TLS doesn't
5451 currently support this. */
5452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5453 ssl->in_msglen ) );
5454 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5455 }
5456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005458 ssl->in_msg[0], ssl->in_msg[1] ) );
5459
5460 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005461 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005463 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005466 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005468 }
5469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005470 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5471 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5474 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005475 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005476
5477#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5478 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5479 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5480 {
Hanno Becker90333da2017-10-10 11:27:13 +01005481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005482 /* Will be handled when trying to parse ServerHello */
5483 return( 0 );
5484 }
5485#endif
5486
5487#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5488 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5489 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5490 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5491 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5492 {
5493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5494 /* Will be handled in mbedtls_ssl_parse_certificate() */
5495 return( 0 );
5496 }
5497#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5498
5499 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005500 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005501 }
5502
Hanno Beckerc76c6192017-06-06 10:03:17 +01005503#if defined(MBEDTLS_SSL_PROTO_DTLS)
5504 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5505 ssl->handshake != NULL &&
5506 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5507 {
5508 ssl_handshake_wrapup_free_hs_transform( ssl );
5509 }
5510#endif
5511
Paul Bakker5121ce52009-01-03 21:22:43 +00005512 return( 0 );
5513}
5514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005515int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005516{
5517 int ret;
5518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005519 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5520 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5521 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005522 {
5523 return( ret );
5524 }
5525
5526 return( 0 );
5527}
5528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005529int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005530 unsigned char level,
5531 unsigned char message )
5532{
5533 int ret;
5534
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005535 if( ssl == NULL || ssl->conf == NULL )
5536 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005539 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005541 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005542 ssl->out_msglen = 2;
5543 ssl->out_msg[0] = level;
5544 ssl->out_msg[1] = message;
5545
Hanno Becker67bc7c32018-08-06 11:33:50 +01005546 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005548 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005549 return( ret );
5550 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005552
5553 return( 0 );
5554}
5555
Paul Bakker5121ce52009-01-03 21:22:43 +00005556/*
5557 * Handshake functions
5558 */
Hanno Becker21489932019-02-05 13:20:55 +00005559#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005560/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005561int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005562{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005566
Hanno Becker7177a882019-02-05 13:36:46 +00005567 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005570 ssl->state++;
5571 return( 0 );
5572 }
5573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5575 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005576}
5577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005578int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005579{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005580 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005583
Hanno Becker7177a882019-02-05 13:36:46 +00005584 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005587 ssl->state++;
5588 return( 0 );
5589 }
5590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5592 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005593}
Gilles Peskinef9828522017-05-03 12:28:43 +02005594
Hanno Becker21489932019-02-05 13:20:55 +00005595#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005596/* Some certificate support -> implement write and parse */
5597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005599{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005600 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005601 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 const mbedtls_x509_crt *crt;
5603 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005606
Hanno Becker7177a882019-02-05 13:36:46 +00005607 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005610 ssl->state++;
5611 return( 0 );
5612 }
5613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005615 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005616 {
5617 if( ssl->client_auth == 0 )
5618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005620 ssl->state++;
5621 return( 0 );
5622 }
5623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005624#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005625 /*
5626 * If using SSLv3 and got no cert, send an Alert message
5627 * (otherwise an empty Certificate message will be sent).
5628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5630 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005631 {
5632 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5634 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5635 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005638 goto write_msg;
5639 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005641 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005642#endif /* MBEDTLS_SSL_CLI_C */
5643#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005644 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005646 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
5649 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005650 }
5651 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005652#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005654 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005655
5656 /*
5657 * 0 . 0 handshake type
5658 * 1 . 3 handshake length
5659 * 4 . 6 length of all certs
5660 * 7 . 9 length of cert. 1
5661 * 10 . n-1 peer certificate
5662 * n . n+2 length of cert. 2
5663 * n+3 . ... upper level cert, etc.
5664 */
5665 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005666 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005667
Paul Bakker29087132010-03-21 21:03:34 +00005668 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005669 {
5670 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10005671 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00005672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10005674 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005675 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005676 }
5677
5678 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
5679 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
5680 ssl->out_msg[i + 2] = (unsigned char)( n );
5681
5682 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
5683 i += n; crt = crt->next;
5684 }
5685
5686 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
5687 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
5688 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
5689
5690 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5692 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005693
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005694#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005695write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005696#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005697
5698 ssl->state++;
5699
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005700 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005701 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005702 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005703 return( ret );
5704 }
5705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005707
Paul Bakkered27a042013-04-18 22:46:23 +02005708 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005709}
5710
Hanno Becker84879e32019-01-31 07:44:03 +00005711#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005712static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
5713 unsigned char *crt_buf,
5714 size_t crt_buf_len )
5715{
5716 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
5717
5718 if( peer_crt == NULL )
5719 return( -1 );
5720
5721 if( peer_crt->raw.len != crt_buf_len )
5722 return( -1 );
5723
Hanno Becker46f34d02019-02-08 14:00:04 +00005724 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005725}
Hanno Becker84879e32019-01-31 07:44:03 +00005726#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005727
Hanno Becker1294a0b2019-02-05 12:38:15 +00005728static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5729{
5730 if( session->peer_cert != NULL )
5731 {
5732 mbedtls_x509_crt_free( session->peer_cert );
5733 mbedtls_free( session->peer_cert );
5734 session->peer_cert = NULL;
5735 }
5736}
5737
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005738/*
5739 * Once the certificate message is read, parse it into a cert chain and
5740 * perform basic checks, but leave actual verification to the caller
5741 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005742static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
5743 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00005744{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005745 int ret;
5746#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5747 int crt_cnt=0;
5748#endif
Paul Bakker23986e52011-04-24 08:57:21 +00005749 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02005750 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00005751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005752 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005755 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5756 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005757 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005758 }
5759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005760 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
5761 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005764 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5765 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005766 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005767 }
5768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005770
Paul Bakker5121ce52009-01-03 21:22:43 +00005771 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005772 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00005773 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005774 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00005775
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005776 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005777 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005780 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5781 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005783 }
5784
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005785 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
5786 i += 3;
5787
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005788 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005789 while( i < ssl->in_hslen )
5790 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005791 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02005792 if ( i + 3 > ssl->in_hslen ) {
5793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005794 mbedtls_ssl_send_alert_message( ssl,
5795 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5796 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02005797 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5798 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005799 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
5800 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005801 if( ssl->in_msg[i] != 0 )
5802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005804 mbedtls_ssl_send_alert_message( ssl,
5805 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5806 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005807 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005808 }
5809
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005810 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005811 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5812 | (unsigned int) ssl->in_msg[i + 2];
5813 i += 3;
5814
5815 if( n < 128 || i + n > ssl->in_hslen )
5816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00005818 mbedtls_ssl_send_alert_message( ssl,
5819 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5820 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005822 }
5823
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005824 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005825#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
5826 if( crt_cnt++ == 0 &&
5827 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5828 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005829 {
Hanno Becker46f34d02019-02-08 14:00:04 +00005830 /* During client-side renegotiation, check that the server's
5831 * end-CRTs hasn't changed compared to the initial handshake,
5832 * mitigating the triple handshake attack. On success, reuse
5833 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005834 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
5835 if( ssl_check_peer_crt_unchanged( ssl,
5836 &ssl->in_msg[i],
5837 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005838 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
5840 mbedtls_ssl_send_alert_message( ssl,
5841 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5842 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
5843 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005844 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005845
5846 /* Now we can safely free the original chain. */
5847 ssl_clear_peer_cert( ssl->session );
5848 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005849#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
5850
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005851 /* Parse the next certificate in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005852 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005853 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005854 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005855 case 0: /*ok*/
5856 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5857 /* Ignore certificate with an unknown algorithm: maybe a
5858 prior certificate was already trusted. */
5859 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005860
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005861 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5862 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5863 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005864
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005865 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5866 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5867 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005868
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00005869 default:
5870 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5871 crt_parse_der_failed:
5872 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
5873 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
5874 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005875 }
5876
5877 i += n;
5878 }
5879
Hanno Beckerc7bd7802019-02-05 15:37:23 +00005880 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005881 return( 0 );
5882}
5883
Hanno Becker4a55f632019-02-05 12:49:06 +00005884#if defined(MBEDTLS_SSL_SRV_C)
5885static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
5886{
5887 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5888 return( -1 );
5889
5890#if defined(MBEDTLS_SSL_PROTO_SSL3)
5891 /*
5892 * Check if the client sent an empty certificate
5893 */
5894 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
5895 {
5896 if( ssl->in_msglen == 2 &&
5897 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
5898 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5899 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5900 {
5901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
5902 return( 0 );
5903 }
5904
5905 return( -1 );
5906 }
5907#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5908
5909#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5910 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5911 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
5912 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5913 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
5914 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
5915 {
5916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
5917 return( 0 );
5918 }
5919
5920 return( -1 );
5921#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
5922 MBEDTLS_SSL_PROTO_TLS1_2 */
5923}
5924#endif /* MBEDTLS_SSL_SRV_C */
5925
Hanno Becker28f2fcd2019-02-07 10:11:07 +00005926/* Check if a certificate message is expected.
5927 * Return either
5928 * - SSL_CERTIFICATE_EXPECTED, or
5929 * - SSL_CERTIFICATE_SKIP
5930 * indicating whether a Certificate message is expected or not.
5931 */
5932#define SSL_CERTIFICATE_EXPECTED 0
5933#define SSL_CERTIFICATE_SKIP 1
5934static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
5935 int authmode )
5936{
5937 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5938 ssl->transform_negotiate->ciphersuite_info;
5939
5940 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
5941 return( SSL_CERTIFICATE_SKIP );
5942
5943#if defined(MBEDTLS_SSL_SRV_C)
5944 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
5945 {
5946 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
5947 return( SSL_CERTIFICATE_SKIP );
5948
5949 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
5950 {
5951 /* NOTE: Is it intentional that we set verify_result
5952 * to SKIP_VERIFY on server-side only? */
5953 ssl->session_negotiate->verify_result =
5954 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
5955 return( SSL_CERTIFICATE_SKIP );
5956 }
5957 }
5958#endif /* MBEDTLS_SSL_SRV_C */
5959
5960 return( SSL_CERTIFICATE_EXPECTED );
5961}
5962
Hanno Becker68636192019-02-05 14:36:34 +00005963static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
5964 int authmode,
5965 mbedtls_x509_crt *chain,
5966 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02005967{
Hanno Becker6bdfab22019-02-05 13:11:17 +00005968 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00005969 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5970 ssl->transform_negotiate->ciphersuite_info;
Hanno Becker68636192019-02-05 14:36:34 +00005971 mbedtls_x509_crt *ca_chain;
5972 mbedtls_x509_crl *ca_crl;
5973
5974 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
5975 return( 0 );
5976
5977#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5978 if( ssl->handshake->sni_ca_chain != NULL )
5979 {
5980 ca_chain = ssl->handshake->sni_ca_chain;
5981 ca_crl = ssl->handshake->sni_ca_crl;
5982 }
5983 else
5984#endif
5985 {
5986 ca_chain = ssl->conf->ca_chain;
5987 ca_crl = ssl->conf->ca_crl;
5988 }
5989
5990 /*
5991 * Main check: verify certificate
5992 */
5993 ret = mbedtls_x509_crt_verify_restartable(
5994 chain,
5995 ca_chain, ca_crl,
5996 ssl->conf->cert_profile,
5997 ssl->hostname,
5998 &ssl->session_negotiate->verify_result,
5999 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6000
6001 if( ret != 0 )
6002 {
6003 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6004 }
6005
6006#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6007 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6008 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6009#endif
6010
6011 /*
6012 * Secondary checks: always done, but change 'ret' only if it was 0
6013 */
6014
6015#if defined(MBEDTLS_ECP_C)
6016 {
6017 const mbedtls_pk_context *pk = &chain->pk;
6018
6019 /* If certificate uses an EC key, make sure the curve is OK */
6020 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6021 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6022 {
6023 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6024
6025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6026 if( ret == 0 )
6027 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6028 }
6029 }
6030#endif /* MBEDTLS_ECP_C */
6031
6032 if( mbedtls_ssl_check_cert_usage( chain,
6033 ciphersuite_info,
6034 ! ssl->conf->endpoint,
6035 &ssl->session_negotiate->verify_result ) != 0 )
6036 {
6037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6038 if( ret == 0 )
6039 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6040 }
6041
6042 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6043 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6044 * with details encoded in the verification flags. All other kinds
6045 * of error codes, including those from the user provided f_vrfy
6046 * functions, are treated as fatal and lead to a failure of
6047 * ssl_parse_certificate even if verification was optional. */
6048 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6049 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6050 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6051 {
6052 ret = 0;
6053 }
6054
6055 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6056 {
6057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6058 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6059 }
6060
6061 if( ret != 0 )
6062 {
6063 uint8_t alert;
6064
6065 /* The certificate may have been rejected for several reasons.
6066 Pick one and send the corresponding alert. Which alert to send
6067 may be a subject of debate in some cases. */
6068 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6069 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6070 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6071 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6072 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6073 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6074 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6075 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6076 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6077 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6078 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6079 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6080 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6081 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6082 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6083 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6084 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6085 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6086 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6087 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6088 else
6089 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6090 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6091 alert );
6092 }
6093
6094#if defined(MBEDTLS_DEBUG_C)
6095 if( ssl->session_negotiate->verify_result != 0 )
6096 {
6097 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6098 ssl->session_negotiate->verify_result ) );
6099 }
6100 else
6101 {
6102 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6103 }
6104#endif /* MBEDTLS_DEBUG_C */
6105
6106 return( ret );
6107}
6108
6109int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6110{
6111 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006112 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006113#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6114 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6115 ? ssl->handshake->sni_authmode
6116 : ssl->conf->authmode;
6117#else
6118 const int authmode = ssl->conf->authmode;
6119#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006120 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006121
6122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6123
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006124 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6125 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006126 {
6127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006128 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006129 }
6130
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006131#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6132 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006133 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006134 {
6135 goto crt_verify;
6136 }
6137#endif
6138
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006139 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006140 {
6141 /* mbedtls_ssl_read_record may have sent an alert already. We
6142 let it decide whether to alert. */
6143 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6144 return( ret );
6145 }
6146
Hanno Becker4a55f632019-02-05 12:49:06 +00006147#if defined(MBEDTLS_SSL_SRV_C)
6148 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6149 {
6150 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006151
6152 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006153 ret = 0;
6154 else
6155 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006156
Hanno Becker6bdfab22019-02-05 13:11:17 +00006157 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006158 }
6159#endif /* MBEDTLS_SSL_SRV_C */
6160
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006161 /* Clear existing peer CRT structure in case we tried to
6162 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006163 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006164 ssl->session_negotiate->peer_cert =
6165 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6166 if( ssl->session_negotiate->peer_cert == NULL )
6167 {
6168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6169 sizeof( mbedtls_x509_crt ) ) );
6170 mbedtls_ssl_send_alert_message( ssl,
6171 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6172 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6173 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6174 }
6175 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Hanno Becker7a955a02019-02-05 13:08:01 +00006176
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006177 ret = ssl_parse_certificate_chain( ssl, ssl->session_negotiate->peer_cert );
6178 if( ret != 0 )
Hanno Beckerfcd9e712019-02-05 14:35:46 +00006179 return( ret );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006180
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006181#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6182 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006183 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006184
6185crt_verify:
6186 if( ssl->handshake->ecrs_enabled)
6187 rs_ctx = &ssl->handshake->ecrs_ctx;
6188#endif
6189
Hanno Becker68636192019-02-05 14:36:34 +00006190 ret = ssl_parse_certificate_verify( ssl, authmode,
6191 ssl->session_negotiate->peer_cert,
6192 rs_ctx );
6193 if( ret != 0 )
6194 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006197
Hanno Becker6bdfab22019-02-05 13:11:17 +00006198exit:
6199
6200 ssl->state++;
Paul Bakker5121ce52009-01-03 21:22:43 +00006201 return( ret );
6202}
Hanno Becker21489932019-02-05 13:20:55 +00006203#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__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
Hanno Becker52055ae2019-02-06 14:30:46 +00007615 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
7616 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007617 return( ret );
7618
Paul Bakker0a597072012-09-25 21:55:46 +00007619 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007620
7621 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007622}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007624
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007625void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007626 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007627{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007628 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7629 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7630 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7631 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007632}
7633
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007634void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007635 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007636 int major, int minor )
7637{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007639 return;
7640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007642 return;
7643
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007644 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007645}
7646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007647#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007648void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007649 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007650{
7651 conf->cert_profile = profile;
7652}
7653
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007654/* Append a new keycert entry to a (possibly empty) list */
7655static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
7656 mbedtls_x509_crt *cert,
7657 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007658{
niisato8ee24222018-06-25 19:05:48 +09007659 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007660
niisato8ee24222018-06-25 19:05:48 +09007661 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
7662 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007663 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007664
niisato8ee24222018-06-25 19:05:48 +09007665 new_cert->cert = cert;
7666 new_cert->key = key;
7667 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007668
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007669 /* Update head is the list was null, else add to the end */
7670 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01007671 {
niisato8ee24222018-06-25 19:05:48 +09007672 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01007673 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007674 else
7675 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007676 mbedtls_ssl_key_cert *cur = *head;
7677 while( cur->next != NULL )
7678 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09007679 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007680 }
7681
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007682 return( 0 );
7683}
7684
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007685int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02007686 mbedtls_x509_crt *own_cert,
7687 mbedtls_pk_context *pk_key )
7688{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02007689 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02007690}
7691
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007692void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007693 mbedtls_x509_crt *ca_chain,
7694 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007695{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01007696 conf->ca_chain = ca_chain;
7697 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00007698}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007699#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00007700
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007701#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7702int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
7703 mbedtls_x509_crt *own_cert,
7704 mbedtls_pk_context *pk_key )
7705{
7706 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
7707 own_cert, pk_key ) );
7708}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02007709
7710void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
7711 mbedtls_x509_crt *ca_chain,
7712 mbedtls_x509_crl *ca_crl )
7713{
7714 ssl->handshake->sni_ca_chain = ca_chain;
7715 ssl->handshake->sni_ca_crl = ca_crl;
7716}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007717
7718void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
7719 int authmode )
7720{
7721 ssl->handshake->sni_authmode = authmode;
7722}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02007723#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
7724
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007725#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007726/*
7727 * Set EC J-PAKE password for current handshake
7728 */
7729int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
7730 const unsigned char *pw,
7731 size_t pw_len )
7732{
7733 mbedtls_ecjpake_role role;
7734
Janos Follath8eb64132016-06-03 15:40:57 +01007735 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007736 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7737
7738 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
7739 role = MBEDTLS_ECJPAKE_SERVER;
7740 else
7741 role = MBEDTLS_ECJPAKE_CLIENT;
7742
7743 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
7744 role,
7745 MBEDTLS_MD_SHA256,
7746 MBEDTLS_ECP_DP_SECP256R1,
7747 pw, pw_len ) );
7748}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007749#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02007750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007752
7753static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
7754{
7755 /* Remove reference to existing PSK, if any. */
7756#if defined(MBEDTLS_USE_PSA_CRYPTO)
7757 if( conf->psk_opaque != 0 )
7758 {
7759 /* The maintenance of the PSK key slot is the
7760 * user's responsibility. */
7761 conf->psk_opaque = 0;
7762 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00007763 /* This and the following branch should never
7764 * be taken simultaenously as we maintain the
7765 * invariant that raw and opaque PSKs are never
7766 * configured simultaneously. As a safeguard,
7767 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007768#endif /* MBEDTLS_USE_PSA_CRYPTO */
7769 if( conf->psk != NULL )
7770 {
7771 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
7772
7773 mbedtls_free( conf->psk );
7774 conf->psk = NULL;
7775 conf->psk_len = 0;
7776 }
7777
7778 /* Remove reference to PSK identity, if any. */
7779 if( conf->psk_identity != NULL )
7780 {
7781 mbedtls_free( conf->psk_identity );
7782 conf->psk_identity = NULL;
7783 conf->psk_identity_len = 0;
7784 }
7785}
7786
Hanno Becker7390c712018-11-15 13:33:04 +00007787/* This function assumes that PSK identity in the SSL config is unset.
7788 * It checks that the provided identity is well-formed and attempts
7789 * to make a copy of it in the SSL config.
7790 * On failure, the PSK identity in the config remains unset. */
7791static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
7792 unsigned char const *psk_identity,
7793 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007794{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007795 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00007796 if( psk_identity == NULL ||
7797 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10007798 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02007799 {
7800 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7801 }
7802
Hanno Becker7390c712018-11-15 13:33:04 +00007803 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
7804 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007805 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02007806
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007807 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01007808 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02007809
7810 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02007811}
7812
Hanno Becker7390c712018-11-15 13:33:04 +00007813int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
7814 const unsigned char *psk, size_t psk_len,
7815 const unsigned char *psk_identity, size_t psk_identity_len )
7816{
7817 int ret;
7818 /* Remove opaque/raw PSK + PSK Identity */
7819 ssl_conf_remove_psk( conf );
7820
7821 /* Check and set raw PSK */
7822 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
7823 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7824 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
7825 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7826 conf->psk_len = psk_len;
7827 memcpy( conf->psk, psk, conf->psk_len );
7828
7829 /* Check and set PSK Identity */
7830 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
7831 if( ret != 0 )
7832 ssl_conf_remove_psk( conf );
7833
7834 return( ret );
7835}
7836
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007837static void ssl_remove_psk( mbedtls_ssl_context *ssl )
7838{
7839#if defined(MBEDTLS_USE_PSA_CRYPTO)
7840 if( ssl->handshake->psk_opaque != 0 )
7841 {
7842 ssl->handshake->psk_opaque = 0;
7843 }
7844 else
7845#endif /* MBEDTLS_USE_PSA_CRYPTO */
7846 if( ssl->handshake->psk != NULL )
7847 {
7848 mbedtls_platform_zeroize( ssl->handshake->psk,
7849 ssl->handshake->psk_len );
7850 mbedtls_free( ssl->handshake->psk );
7851 ssl->handshake->psk_len = 0;
7852 }
7853}
7854
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007855int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
7856 const unsigned char *psk, size_t psk_len )
7857{
7858 if( psk == NULL || ssl->handshake == NULL )
7859 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7860
7861 if( psk_len > MBEDTLS_PSK_MAX_LEN )
7862 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7863
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007864 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007865
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007866 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007867 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007868
7869 ssl->handshake->psk_len = psk_len;
7870 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
7871
7872 return( 0 );
7873}
7874
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007875#if defined(MBEDTLS_USE_PSA_CRYPTO)
7876int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007877 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007878 const unsigned char *psk_identity,
7879 size_t psk_identity_len )
7880{
Hanno Becker7390c712018-11-15 13:33:04 +00007881 int ret;
7882 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007883 ssl_conf_remove_psk( conf );
7884
Hanno Becker7390c712018-11-15 13:33:04 +00007885 /* Check and set opaque PSK */
7886 if( psk_slot == 0 )
7887 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007888 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00007889
7890 /* Check and set PSK Identity */
7891 ret = ssl_conf_set_psk_identity( conf, psk_identity,
7892 psk_identity_len );
7893 if( ret != 0 )
7894 ssl_conf_remove_psk( conf );
7895
7896 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007897}
7898
7899int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05007900 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01007901{
7902 if( psk_slot == 0 || ssl->handshake == NULL )
7903 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7904
7905 ssl_remove_psk( ssl );
7906 ssl->handshake->psk_opaque = psk_slot;
7907 return( 0 );
7908}
7909#endif /* MBEDTLS_USE_PSA_CRYPTO */
7910
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007911void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02007913 size_t),
7914 void *p_psk )
7915{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007916 conf->f_psk = f_psk;
7917 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02007918}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00007920
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007921#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01007922
7923#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007924int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00007925{
7926 int ret;
7927
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007928 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
7929 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
7930 {
7931 mbedtls_mpi_free( &conf->dhm_P );
7932 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00007933 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007934 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007935
7936 return( 0 );
7937}
Hanno Becker470a8c42017-10-04 15:28:46 +01007938#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007939
Hanno Beckera90658f2017-10-04 15:29:08 +01007940int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
7941 const unsigned char *dhm_P, size_t P_len,
7942 const unsigned char *dhm_G, size_t G_len )
7943{
7944 int ret;
7945
7946 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
7947 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
7948 {
7949 mbedtls_mpi_free( &conf->dhm_P );
7950 mbedtls_mpi_free( &conf->dhm_G );
7951 return( ret );
7952 }
7953
7954 return( 0 );
7955}
Paul Bakker5121ce52009-01-03 21:22:43 +00007956
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007957int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00007958{
7959 int ret;
7960
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007961 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
7962 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
7963 {
7964 mbedtls_mpi_free( &conf->dhm_P );
7965 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00007966 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01007967 }
Paul Bakker1b57b062011-01-06 15:48:19 +00007968
7969 return( 0 );
7970}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007971#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00007972
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007973#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7974/*
7975 * Set the minimum length for Diffie-Hellman parameters
7976 */
7977void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
7978 unsigned int bitlen )
7979{
7980 conf->dhm_min_bitlen = bitlen;
7981}
7982#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
7983
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02007984#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007985/*
7986 * Set allowed/preferred hashes for handshake signatures
7987 */
7988void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
7989 const int *hashes )
7990{
7991 conf->sig_hashes = hashes;
7992}
Hanno Becker947194e2017-04-07 13:25:49 +01007993#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02007994
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007995#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01007996/*
7997 * Set the allowed elliptic curves
7998 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007999void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008000 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008001{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008002 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008003}
Hanno Becker947194e2017-04-07 13:25:49 +01008004#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008005
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008006#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008007int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008008{
Hanno Becker947194e2017-04-07 13:25:49 +01008009 /* Initialize to suppress unnecessary compiler warning */
8010 size_t hostname_len = 0;
8011
8012 /* Check if new hostname is valid before
8013 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008014 if( hostname != NULL )
8015 {
8016 hostname_len = strlen( hostname );
8017
8018 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8019 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8020 }
8021
8022 /* Now it's clear that we will overwrite the old hostname,
8023 * so we can free it safely */
8024
8025 if( ssl->hostname != NULL )
8026 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008027 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008028 mbedtls_free( ssl->hostname );
8029 }
8030
8031 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008032
Paul Bakker5121ce52009-01-03 21:22:43 +00008033 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008034 {
8035 ssl->hostname = NULL;
8036 }
8037 else
8038 {
8039 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008040 if( ssl->hostname == NULL )
8041 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008042
Hanno Becker947194e2017-04-07 13:25:49 +01008043 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008044
Hanno Becker947194e2017-04-07 13:25:49 +01008045 ssl->hostname[hostname_len] = '\0';
8046 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008047
8048 return( 0 );
8049}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008050#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008051
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008052#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008053void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008054 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008055 const unsigned char *, size_t),
8056 void *p_sni )
8057{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008058 conf->f_sni = f_sni;
8059 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008060}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008061#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008063#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008064int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008065{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008066 size_t cur_len, tot_len;
8067 const char **p;
8068
8069 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008070 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8071 * MUST NOT be truncated."
8072 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008073 */
8074 tot_len = 0;
8075 for( p = protos; *p != NULL; p++ )
8076 {
8077 cur_len = strlen( *p );
8078 tot_len += cur_len;
8079
8080 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008082 }
8083
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008084 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008085
8086 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008087}
8088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008089const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008090{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008091 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008092}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008093#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008094
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008095void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008096{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008097 conf->max_major_ver = major;
8098 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008099}
8100
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008101void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008102{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008103 conf->min_major_ver = major;
8104 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008105}
8106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008107#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008108void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008109{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008110 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008111}
8112#endif
8113
Janos Follath088ce432017-04-10 12:42:31 +01008114#if defined(MBEDTLS_SSL_SRV_C)
8115void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8116 char cert_req_ca_list )
8117{
8118 conf->cert_req_ca_list = cert_req_ca_list;
8119}
8120#endif
8121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008122#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008123void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008124{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008125 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008126}
8127#endif
8128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008130void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008131{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008132 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008133}
8134#endif
8135
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008136#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008137void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008138{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008139 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008140}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008141#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008143#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008144int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008145{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008147 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008150 }
8151
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008152 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008153
8154 return( 0 );
8155}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008158#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008159void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008160{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008161 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008162}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008163#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008166void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008167{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008168 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008169}
8170#endif
8171
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008172void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008173{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008174 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008175}
8176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008177#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008178void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008179{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008180 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008181}
8182
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008183void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008184{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008185 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008186}
8187
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008188void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008189 const unsigned char period[8] )
8190{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008191 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008192}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008193#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008195#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008196#if defined(MBEDTLS_SSL_CLI_C)
8197void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008198{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008199 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008200}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008201#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008202
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008203#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008204void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8205 mbedtls_ssl_ticket_write_t *f_ticket_write,
8206 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8207 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008208{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008209 conf->f_ticket_write = f_ticket_write;
8210 conf->f_ticket_parse = f_ticket_parse;
8211 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008212}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008213#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008215
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008216#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8217void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8218 mbedtls_ssl_export_keys_t *f_export_keys,
8219 void *p_export_keys )
8220{
8221 conf->f_export_keys = f_export_keys;
8222 conf->p_export_keys = p_export_keys;
8223}
8224#endif
8225
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008226#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008227void mbedtls_ssl_conf_async_private_cb(
8228 mbedtls_ssl_config *conf,
8229 mbedtls_ssl_async_sign_t *f_async_sign,
8230 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8231 mbedtls_ssl_async_resume_t *f_async_resume,
8232 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008233 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008234{
8235 conf->f_async_sign_start = f_async_sign;
8236 conf->f_async_decrypt_start = f_async_decrypt;
8237 conf->f_async_resume = f_async_resume;
8238 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008239 conf->p_async_config_data = async_config_data;
8240}
8241
Gilles Peskine8f97af72018-04-26 11:46:10 +02008242void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8243{
8244 return( conf->p_async_config_data );
8245}
8246
Gilles Peskine1febfef2018-04-30 11:54:39 +02008247void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008248{
8249 if( ssl->handshake == NULL )
8250 return( NULL );
8251 else
8252 return( ssl->handshake->user_async_ctx );
8253}
8254
Gilles Peskine1febfef2018-04-30 11:54:39 +02008255void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008256 void *ctx )
8257{
8258 if( ssl->handshake != NULL )
8259 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008260}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008261#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008262
Paul Bakker5121ce52009-01-03 21:22:43 +00008263/*
8264 * SSL get accessors
8265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008266size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008267{
8268 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8269}
8270
Hanno Becker8b170a02017-10-10 11:51:19 +01008271int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8272{
8273 /*
8274 * Case A: We're currently holding back
8275 * a message for further processing.
8276 */
8277
8278 if( ssl->keep_current_message == 1 )
8279 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008280 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008281 return( 1 );
8282 }
8283
8284 /*
8285 * Case B: Further records are pending in the current datagram.
8286 */
8287
8288#if defined(MBEDTLS_SSL_PROTO_DTLS)
8289 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8290 ssl->in_left > ssl->next_record_offset )
8291 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008292 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008293 return( 1 );
8294 }
8295#endif /* MBEDTLS_SSL_PROTO_DTLS */
8296
8297 /*
8298 * Case C: A handshake message is being processed.
8299 */
8300
Hanno Becker8b170a02017-10-10 11:51:19 +01008301 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8302 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008303 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008304 return( 1 );
8305 }
8306
8307 /*
8308 * Case D: An application data message is being processed
8309 */
8310 if( ssl->in_offt != NULL )
8311 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008312 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008313 return( 1 );
8314 }
8315
8316 /*
8317 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008318 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008319 * we implement support for multiple alerts in single records.
8320 */
8321
8322 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8323 return( 0 );
8324}
8325
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008326uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008327{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008328 if( ssl->session != NULL )
8329 return( ssl->session->verify_result );
8330
8331 if( ssl->session_negotiate != NULL )
8332 return( ssl->session_negotiate->verify_result );
8333
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008334 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008335}
8336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008338{
Paul Bakker926c8e42013-03-06 10:23:34 +01008339 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008340 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008342 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008343}
8344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008346{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008348 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008349 {
8350 switch( ssl->minor_ver )
8351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008352 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008353 return( "DTLSv1.0" );
8354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008356 return( "DTLSv1.2" );
8357
8358 default:
8359 return( "unknown (DTLS)" );
8360 }
8361 }
8362#endif
8363
Paul Bakker43ca69c2011-01-15 17:35:19 +00008364 switch( ssl->minor_ver )
8365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008366 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008367 return( "SSLv3.0" );
8368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008369 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008370 return( "TLSv1.0" );
8371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008373 return( "TLSv1.1" );
8374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008375 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008376 return( "TLSv1.2" );
8377
Paul Bakker43ca69c2011-01-15 17:35:19 +00008378 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008379 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008380 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008381}
8382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008383int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008384{
Hanno Becker3136ede2018-08-17 15:28:19 +01008385 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008386 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008387 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008388
Hanno Becker78640902018-08-13 16:35:15 +01008389 if( transform == NULL )
8390 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008392#if defined(MBEDTLS_ZLIB_SUPPORT)
8393 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8394 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008395#endif
8396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008397 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399 case MBEDTLS_MODE_GCM:
8400 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008401 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008402 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008403 transform_expansion = transform->minlen;
8404 break;
8405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008406 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008407
8408 block_size = mbedtls_cipher_get_block_size(
8409 &transform->cipher_ctx_enc );
8410
Hanno Becker3136ede2018-08-17 15:28:19 +01008411 /* Expansion due to the addition of the MAC. */
8412 transform_expansion += transform->maclen;
8413
8414 /* Expansion due to the addition of CBC padding;
8415 * Theoretically up to 256 bytes, but we never use
8416 * more than the block size of the underlying cipher. */
8417 transform_expansion += block_size;
8418
8419 /* For TLS 1.1 or higher, an explicit IV is added
8420 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008421#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8422 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008423 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008424#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008425
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008426 break;
8427
8428 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008429 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008430 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008431 }
8432
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02008433 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008434}
8435
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008436#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8437size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8438{
8439 size_t max_len;
8440
8441 /*
8442 * Assume mfl_code is correct since it was checked when set
8443 */
Angus Grattond8213d02016-05-25 20:56:48 +10008444 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008445
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008446 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008447 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008448 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008449 {
Angus Grattond8213d02016-05-25 20:56:48 +10008450 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008451 }
8452
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008453 /* During a handshake, use the value being negotiated */
8454 if( ssl->session_negotiate != NULL &&
8455 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8456 {
8457 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8458 }
8459
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008460 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008461}
8462#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8463
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008464#if defined(MBEDTLS_SSL_PROTO_DTLS)
8465static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8466{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008467 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8468 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8469 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8470 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8471 return ( 0 );
8472
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008473 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8474 return( ssl->mtu );
8475
8476 if( ssl->mtu == 0 )
8477 return( ssl->handshake->mtu );
8478
8479 return( ssl->mtu < ssl->handshake->mtu ?
8480 ssl->mtu : ssl->handshake->mtu );
8481}
8482#endif /* MBEDTLS_SSL_PROTO_DTLS */
8483
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008484int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8485{
8486 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8487
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008488#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8489 !defined(MBEDTLS_SSL_PROTO_DTLS)
8490 (void) ssl;
8491#endif
8492
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008493#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8494 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8495
8496 if( max_len > mfl )
8497 max_len = mfl;
8498#endif
8499
8500#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008501 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008502 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008503 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008504 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8505 const size_t overhead = (size_t) ret;
8506
8507 if( ret < 0 )
8508 return( ret );
8509
8510 if( mtu <= overhead )
8511 {
8512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8513 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8514 }
8515
8516 if( max_len > mtu - overhead )
8517 max_len = mtu - overhead;
8518 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008519#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008520
Hanno Becker0defedb2018-08-10 12:35:02 +01008521#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8522 !defined(MBEDTLS_SSL_PROTO_DTLS)
8523 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008524#endif
8525
8526 return( (int) max_len );
8527}
8528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008529#if defined(MBEDTLS_X509_CRT_PARSE_C)
8530const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008531{
8532 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008533 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008534
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008535 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008536}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008537#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008539#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00008540int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8541 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008542{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008543 if( ssl == NULL ||
8544 dst == NULL ||
8545 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008546 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008548 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008549 }
8550
Hanno Becker52055ae2019-02-06 14:30:46 +00008551 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008552}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008553#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008554
Paul Bakker5121ce52009-01-03 21:22:43 +00008555/*
Paul Bakker1961b702013-01-25 14:49:24 +01008556 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00008557 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008558int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008559{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008560 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00008561
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008562 if( ssl == NULL || ssl->conf == NULL )
8563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008565#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008566 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008567 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008568#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008569#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008570 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008571 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00008572#endif
8573
Paul Bakker1961b702013-01-25 14:49:24 +01008574 return( ret );
8575}
8576
8577/*
8578 * Perform the SSL handshake
8579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008580int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01008581{
8582 int ret = 0;
8583
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008584 if( ssl == NULL || ssl->conf == NULL )
8585 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01008588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008589 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01008590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008591 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01008592
8593 if( ret != 0 )
8594 break;
8595 }
8596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008598
8599 return( ret );
8600}
8601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008602#if defined(MBEDTLS_SSL_RENEGOTIATION)
8603#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00008604/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008605 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00008606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008607static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008608{
8609 int ret;
8610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008612
8613 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8615 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008616
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008617 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008618 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008619 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008620 return( ret );
8621 }
8622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008623 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008624
8625 return( 0 );
8626}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008627#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008628
8629/*
8630 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008631 * - any side: calling mbedtls_ssl_renegotiate(),
8632 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
8633 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02008634 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008635 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008636 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008638static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008639{
8640 int ret;
8641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008643
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008644 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8645 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008646
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008647 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
8648 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008649#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008650 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008651 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008652 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008653 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02008654 ssl->handshake->out_msg_seq = 1;
8655 else
8656 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02008657 }
8658#endif
8659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008660 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
8661 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00008662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008663 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008665 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00008666 return( ret );
8667 }
8668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008670
8671 return( 0 );
8672}
8673
8674/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008675 * Renegotiate current connection on client,
8676 * or request renegotiation on server
8677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008678int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008679{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008680 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008681
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008682 if( ssl == NULL || ssl->conf == NULL )
8683 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008685#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008686 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008687 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008689 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8690 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008692 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008693
8694 /* Did we already try/start sending HelloRequest? */
8695 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008696 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02008697
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008698 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008699 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008700#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008702#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008703 /*
8704 * On client, either start the renegotiation process or,
8705 * if already in progress, continue the handshake
8706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008707 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008709 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
8710 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008711
8712 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
8713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008714 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008715 return( ret );
8716 }
8717 }
8718 else
8719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008720 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008722 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008723 return( ret );
8724 }
8725 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008726#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01008727
Paul Bakker37ce0ff2013-10-31 14:32:04 +01008728 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01008729}
8730
8731/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008732 * Check record counters and renegotiate if they're above the limit.
8733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008734static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008735{
Andres AG2196c7f2016-12-15 17:01:16 +00008736 size_t ep_len = ssl_ep_len( ssl );
8737 int in_ctr_cmp;
8738 int out_ctr_cmp;
8739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008740 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
8741 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008742 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008743 {
8744 return( 0 );
8745 }
8746
Andres AG2196c7f2016-12-15 17:01:16 +00008747 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
8748 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01008749 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00008750 ssl->conf->renego_period + ep_len, 8 - ep_len );
8751
8752 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008753 {
8754 return( 0 );
8755 }
8756
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008758 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008759}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008760#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008761
8762/*
8763 * Receive application data decrypted from the SSL layer
8764 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008765int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008766{
Hanno Becker4a810fb2017-05-24 16:27:30 +01008767 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00008768 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00008769
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008770 if( ssl == NULL || ssl->conf == NULL )
8771 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008775#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008776 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008778 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008779 return( ret );
8780
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008781 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008782 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008783 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008784 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02008785 return( ret );
8786 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008787 }
8788#endif
8789
Hanno Becker4a810fb2017-05-24 16:27:30 +01008790 /*
8791 * Check if renegotiation is necessary and/or handshake is
8792 * in process. If yes, perform/continue, and fall through
8793 * if an unexpected packet is received while the client
8794 * is waiting for the ServerHello.
8795 *
8796 * (There is no equivalent to the last condition on
8797 * the server-side as it is not treated as within
8798 * a handshake while waiting for the ClientHello
8799 * after a renegotiation request.)
8800 */
8801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008802#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008803 ret = ssl_check_ctr_renegotiate( ssl );
8804 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8805 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008807 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01008808 return( ret );
8809 }
8810#endif
8811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008814 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01008815 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8816 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008818 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008819 return( ret );
8820 }
8821 }
8822
Hanno Beckere41158b2017-10-23 13:30:32 +01008823 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01008824 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008825 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008826 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008827 if( ssl->f_get_timer != NULL &&
8828 ssl->f_get_timer( ssl->p_timer ) == -1 )
8829 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008830 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02008831 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008832
Hanno Becker327c93b2018-08-15 13:56:18 +01008833 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008834 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008835 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
8836 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00008837
Hanno Becker4a810fb2017-05-24 16:27:30 +01008838 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
8839 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008840 }
8841
8842 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008843 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00008844 {
8845 /*
8846 * OpenSSL sends empty messages to randomize the IV
8847 */
Hanno Becker327c93b2018-08-15 13:56:18 +01008848 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008850 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00008851 return( 0 );
8852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008853 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008854 return( ret );
8855 }
8856 }
8857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008858 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00008859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008861
Hanno Becker4a810fb2017-05-24 16:27:30 +01008862 /*
8863 * - For client-side, expect SERVER_HELLO_REQUEST.
8864 * - For server-side, expect CLIENT_HELLO.
8865 * - Fail (TLS) or silently drop record (DTLS) in other cases.
8866 */
8867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008868#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008869 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008870 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01008871 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00008872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008874
8875 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008876#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008877 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008878 {
8879 continue;
8880 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008881#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008882 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008883 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008884#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008885
Hanno Becker4a810fb2017-05-24 16:27:30 +01008886#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008887 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008888 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008891
8892 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008893#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008894 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01008895 {
8896 continue;
8897 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02008898#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008899 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00008900 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01008901#endif /* MBEDTLS_SSL_SRV_C */
8902
Hanno Becker21df7f92017-10-17 11:03:26 +01008903#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01008904 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008905 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
8906 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
8907 ssl->conf->allow_legacy_renegotiation ==
8908 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
8909 {
8910 /*
8911 * Accept renegotiation request
8912 */
Paul Bakker48916f92012-09-16 19:57:18 +00008913
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01008914 /* DTLS clients need to know renego is server-initiated */
8915#if defined(MBEDTLS_SSL_PROTO_DTLS)
8916 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8917 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8918 {
8919 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
8920 }
8921#endif
8922 ret = ssl_start_renegotiation( ssl );
8923 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
8924 ret != 0 )
8925 {
8926 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
8927 return( ret );
8928 }
8929 }
8930 else
Hanno Becker21df7f92017-10-17 11:03:26 +01008931#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008932 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01008933 /*
8934 * Refuse renegotiation
8935 */
8936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008937 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008939#if defined(MBEDTLS_SSL_PROTO_SSL3)
8940 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00008941 {
Gilles Peskine92e44262017-05-10 17:27:49 +02008942 /* SSLv3 does not have a "no_renegotiation" warning, so
8943 we send a fatal alert and abort the connection. */
8944 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8945 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
8946 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008947 }
8948 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008949#endif /* MBEDTLS_SSL_PROTO_SSL3 */
8950#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8951 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8952 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008954 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8955 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8956 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00008957 {
8958 return( ret );
8959 }
Paul Bakker48916f92012-09-16 19:57:18 +00008960 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02008961 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008962#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
8963 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02008964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
8966 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02008967 }
Paul Bakker48916f92012-09-16 19:57:18 +00008968 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02008969
Hanno Becker90333da2017-10-10 11:27:13 +01008970 /* At this point, we don't know whether the renegotiation has been
8971 * completed or not. The cases to consider are the following:
8972 * 1) The renegotiation is complete. In this case, no new record
8973 * has been read yet.
8974 * 2) The renegotiation is incomplete because the client received
8975 * an application data record while awaiting the ServerHello.
8976 * 3) The renegotiation is incomplete because the client received
8977 * a non-handshake, non-application data message while awaiting
8978 * the ServerHello.
8979 * In each of these case, looping will be the proper action:
8980 * - For 1), the next iteration will read a new record and check
8981 * if it's application data.
8982 * - For 2), the loop condition isn't satisfied as application data
8983 * is present, hence continue is the same as break
8984 * - For 3), the loop condition is satisfied and read_record
8985 * will re-deliver the message that was held back by the client
8986 * when expecting the ServerHello.
8987 */
8988 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00008989 }
Hanno Becker21df7f92017-10-17 11:03:26 +01008990#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008991 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01008992 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008993 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008994 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008995 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02008998 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008999 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009000 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009001 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009002 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009003#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009005 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9006 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009009 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009010 }
9011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009012 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9015 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009016 }
9017
9018 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009019
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009020 /* We're going to return something now, cancel timer,
9021 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009022 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009023 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009024
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009025#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009026 /* If we requested renego but received AppData, resend HelloRequest.
9027 * Do it now, after setting in_offt, to avoid taking this branch
9028 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009029#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009030 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009031 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009032 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009033 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009035 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009036 return( ret );
9037 }
9038 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009039#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009040#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009041 }
9042
9043 n = ( len < ssl->in_msglen )
9044 ? len : ssl->in_msglen;
9045
9046 memcpy( buf, ssl->in_offt, n );
9047 ssl->in_msglen -= n;
9048
9049 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009050 {
9051 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009052 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009053 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009054 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009055 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009056 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009057 /* more data available */
9058 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009059 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009062
Paul Bakker23986e52011-04-24 08:57:21 +00009063 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009064}
9065
9066/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009067 * Send application data to be encrypted by the SSL layer, taking care of max
9068 * fragment length and buffer size.
9069 *
9070 * According to RFC 5246 Section 6.2.1:
9071 *
9072 * Zero-length fragments of Application data MAY be sent as they are
9073 * potentially useful as a traffic analysis countermeasure.
9074 *
9075 * Therefore, it is possible that the input message length is 0 and the
9076 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009077 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009078static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009079 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009080{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009081 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9082 const size_t max_len = (size_t) ret;
9083
9084 if( ret < 0 )
9085 {
9086 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9087 return( ret );
9088 }
9089
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009090 if( len > max_len )
9091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009092#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009093 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009096 "maximum fragment length: %d > %d",
9097 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009099 }
9100 else
9101#endif
9102 len = max_len;
9103 }
Paul Bakker887bd502011-06-08 13:10:54 +00009104
Paul Bakker5121ce52009-01-03 21:22:43 +00009105 if( ssl->out_left != 0 )
9106 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009107 /*
9108 * The user has previously tried to send the data and
9109 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9110 * written. In this case, we expect the high-level write function
9111 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009113 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009116 return( ret );
9117 }
9118 }
Paul Bakker887bd502011-06-08 13:10:54 +00009119 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009120 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009121 /*
9122 * The user is trying to send a message the first time, so we need to
9123 * copy the data into the internal buffers and setup the data structure
9124 * to keep track of partial writes
9125 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009126 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009127 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009128 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009129
Hanno Becker67bc7c32018-08-06 11:33:50 +01009130 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009133 return( ret );
9134 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009135 }
9136
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009137 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009138}
9139
9140/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009141 * Write application data, doing 1/n-1 splitting if necessary.
9142 *
9143 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009144 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009145 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009147#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009148static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009149 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009150{
9151 int ret;
9152
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009153 if( ssl->conf->cbc_record_splitting ==
9154 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009155 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009156 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9157 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9158 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009159 {
9160 return( ssl_write_real( ssl, buf, len ) );
9161 }
9162
9163 if( ssl->split_done == 0 )
9164 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009165 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009166 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009167 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009168 }
9169
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009170 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9171 return( ret );
9172 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009173
9174 return( ret + 1 );
9175}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009176#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009177
9178/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009179 * Write application data (public-facing wrapper)
9180 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009181int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009182{
9183 int ret;
9184
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009186
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009187 if( ssl == NULL || ssl->conf == NULL )
9188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9189
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009190#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009191 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9192 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009193 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009194 return( ret );
9195 }
9196#endif
9197
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009198 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009199 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009200 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009201 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009202 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009203 return( ret );
9204 }
9205 }
9206
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009207#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009208 ret = ssl_write_split( ssl, buf, len );
9209#else
9210 ret = ssl_write_real( ssl, buf, len );
9211#endif
9212
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009214
9215 return( ret );
9216}
9217
9218/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009219 * Notify the peer that the connection is being closed
9220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009221int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009222{
9223 int ret;
9224
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009225 if( ssl == NULL || ssl->conf == NULL )
9226 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009229
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009230 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009231 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009233 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009235 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9236 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9237 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009239 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009240 return( ret );
9241 }
9242 }
9243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009245
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009246 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009247}
9248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009249void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009250{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009251 if( transform == NULL )
9252 return;
9253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009254#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009255 deflateEnd( &transform->ctx_deflate );
9256 inflateEnd( &transform->ctx_inflate );
9257#endif
9258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009259 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9260 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009262 mbedtls_md_free( &transform->md_ctx_enc );
9263 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02009264
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009265 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009266}
9267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009268#if defined(MBEDTLS_X509_CRT_PARSE_C)
9269static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009270{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009271 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009272
9273 while( cur != NULL )
9274 {
9275 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009276 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009277 cur = next;
9278 }
9279}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009280#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009281
Hanno Becker0271f962018-08-16 13:23:47 +01009282#if defined(MBEDTLS_SSL_PROTO_DTLS)
9283
9284static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9285{
9286 unsigned offset;
9287 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9288
9289 if( hs == NULL )
9290 return;
9291
Hanno Becker283f5ef2018-08-24 09:34:47 +01009292 ssl_free_buffered_record( ssl );
9293
Hanno Becker0271f962018-08-16 13:23:47 +01009294 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009295 ssl_buffering_free_slot( ssl, offset );
9296}
9297
9298static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9299 uint8_t slot )
9300{
9301 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9302 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009303
9304 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9305 return;
9306
Hanno Beckere605b192018-08-21 15:59:07 +01009307 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009308 {
Hanno Beckere605b192018-08-21 15:59:07 +01009309 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009310 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009311 mbedtls_free( hs_buf->data );
9312 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009313 }
9314}
9315
9316#endif /* MBEDTLS_SSL_PROTO_DTLS */
9317
Gilles Peskine9b562d52018-04-25 20:32:43 +02009318void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009319{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009320 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9321
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009322 if( handshake == NULL )
9323 return;
9324
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009325#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9326 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9327 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009328 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009329 handshake->async_in_progress = 0;
9330 }
9331#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9332
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009333#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9334 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9335 mbedtls_md5_free( &handshake->fin_md5 );
9336 mbedtls_sha1_free( &handshake->fin_sha1 );
9337#endif
9338#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9339#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009340#if defined(MBEDTLS_USE_PSA_CRYPTO)
9341 psa_hash_abort( &handshake->fin_sha256_psa );
9342#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009343 mbedtls_sha256_free( &handshake->fin_sha256 );
9344#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009345#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009346#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009347#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009348 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009349#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009350 mbedtls_sha512_free( &handshake->fin_sha512 );
9351#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009352#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009353#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355#if defined(MBEDTLS_DHM_C)
9356 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009357#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009358#if defined(MBEDTLS_ECDH_C)
9359 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009360#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009361#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009362 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009363#if defined(MBEDTLS_SSL_CLI_C)
9364 mbedtls_free( handshake->ecjpake_cache );
9365 handshake->ecjpake_cache = NULL;
9366 handshake->ecjpake_cache_len = 0;
9367#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009368#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009369
Janos Follath4ae5c292016-02-10 11:27:43 +00009370#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9371 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009372 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009373 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009374#endif
9375
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009376#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9377 if( handshake->psk != NULL )
9378 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009379 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009380 mbedtls_free( handshake->psk );
9381 }
9382#endif
9383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009384#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9385 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009386 /*
9387 * Free only the linked list wrapper, not the keys themselves
9388 * since the belong to the SNI callback
9389 */
9390 if( handshake->sni_key_cert != NULL )
9391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009392 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009393
9394 while( cur != NULL )
9395 {
9396 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009397 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009398 cur = next;
9399 }
9400 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009401#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009402
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009403#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009404 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009405#endif
9406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009407#if defined(MBEDTLS_SSL_PROTO_DTLS)
9408 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02009409 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +01009410 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02009411#endif
9412
Hanno Becker4a63ed42019-01-08 11:39:35 +00009413#if defined(MBEDTLS_ECDH_C) && \
9414 defined(MBEDTLS_USE_PSA_CRYPTO)
9415 psa_destroy_key( handshake->ecdh_psa_privkey );
9416#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
9417
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009418 mbedtls_platform_zeroize( handshake,
9419 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009420}
9421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009422void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00009423{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009424 if( session == NULL )
9425 return;
9426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009427#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +00009428 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +02009429#endif
Paul Bakker0a597072012-09-25 21:55:46 +00009430
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009431#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009432 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02009433#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02009434
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009435 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009436}
9437
Paul Bakker5121ce52009-01-03 21:22:43 +00009438/*
9439 * Free an SSL context
9440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009441void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009442{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009443 if( ssl == NULL )
9444 return;
9445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009447
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009448 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009449 {
Angus Grattond8213d02016-05-25 20:56:48 +10009450 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009451 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009452 }
9453
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01009454 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009455 {
Angus Grattond8213d02016-05-25 20:56:48 +10009456 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009457 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00009458 }
9459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009460#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02009461 if( ssl->compress_buf != NULL )
9462 {
Angus Grattond8213d02016-05-25 20:56:48 +10009463 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009464 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02009465 }
9466#endif
9467
Paul Bakker48916f92012-09-16 19:57:18 +00009468 if( ssl->transform )
9469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009470 mbedtls_ssl_transform_free( ssl->transform );
9471 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00009472 }
9473
9474 if( ssl->handshake )
9475 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02009476 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477 mbedtls_ssl_transform_free( ssl->transform_negotiate );
9478 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009480 mbedtls_free( ssl->handshake );
9481 mbedtls_free( ssl->transform_negotiate );
9482 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00009483 }
9484
Paul Bakkerc0463502013-02-14 11:19:38 +01009485 if( ssl->session )
9486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009487 mbedtls_ssl_session_free( ssl->session );
9488 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01009489 }
9490
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02009491#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02009492 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009493 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009494 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009495 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00009496 }
Paul Bakker0be444a2013-08-27 21:55:01 +02009497#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009499#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
9500 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00009501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
9503 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00009504 }
9505#endif
9506
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009507#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009508 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02009509#endif
9510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00009512
Paul Bakker86f04f42013-02-14 11:20:09 +01009513 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009514 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009515}
9516
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009517/*
9518 * Initialze mbedtls_ssl_config
9519 */
9520void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
9521{
9522 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
9523}
9524
Simon Butcherc97b6972015-12-27 23:48:17 +00009525#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009526static int ssl_preset_default_hashes[] = {
9527#if defined(MBEDTLS_SHA512_C)
9528 MBEDTLS_MD_SHA512,
9529 MBEDTLS_MD_SHA384,
9530#endif
9531#if defined(MBEDTLS_SHA256_C)
9532 MBEDTLS_MD_SHA256,
9533 MBEDTLS_MD_SHA224,
9534#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02009535#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009536 MBEDTLS_MD_SHA1,
9537#endif
9538 MBEDTLS_MD_NONE
9539};
Simon Butcherc97b6972015-12-27 23:48:17 +00009540#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009541
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009542static int ssl_preset_suiteb_ciphersuites[] = {
9543 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
9544 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
9545 0
9546};
9547
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009548#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009549static int ssl_preset_suiteb_hashes[] = {
9550 MBEDTLS_MD_SHA256,
9551 MBEDTLS_MD_SHA384,
9552 MBEDTLS_MD_NONE
9553};
9554#endif
9555
9556#if defined(MBEDTLS_ECP_C)
9557static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
9558 MBEDTLS_ECP_DP_SECP256R1,
9559 MBEDTLS_ECP_DP_SECP384R1,
9560 MBEDTLS_ECP_DP_NONE
9561};
9562#endif
9563
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009564/*
Tillmann Karras588ad502015-09-25 04:27:22 +02009565 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009566 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009567int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009568 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009569{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009570#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009571 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02009572#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009573
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02009574 /* Use the functions here so that they are covered in tests,
9575 * but otherwise access member directly for efficiency */
9576 mbedtls_ssl_conf_endpoint( conf, endpoint );
9577 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009578
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009579 /*
9580 * Things that are common to all presets
9581 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02009582#if defined(MBEDTLS_SSL_CLI_C)
9583 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
9584 {
9585 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
9586#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9587 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
9588#endif
9589 }
9590#endif
9591
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009592#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009593 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009594#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009595
9596#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9597 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
9598#endif
9599
9600#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
9601 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
9602#endif
9603
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009604#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
9605 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
9606#endif
9607
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02009608#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009609 conf->f_cookie_write = ssl_cookie_write_dummy;
9610 conf->f_cookie_check = ssl_cookie_check_dummy;
9611#endif
9612
9613#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
9614 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
9615#endif
9616
Janos Follath088ce432017-04-10 12:42:31 +01009617#if defined(MBEDTLS_SSL_SRV_C)
9618 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
9619#endif
9620
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009621#if defined(MBEDTLS_SSL_PROTO_DTLS)
9622 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
9623 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
9624#endif
9625
9626#if defined(MBEDTLS_SSL_RENEGOTIATION)
9627 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00009628 memset( conf->renego_period, 0x00, 2 );
9629 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009630#endif
9631
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009632#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
9633 if( endpoint == MBEDTLS_SSL_IS_SERVER )
9634 {
Hanno Becker00d0a682017-10-04 13:14:29 +01009635 const unsigned char dhm_p[] =
9636 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
9637 const unsigned char dhm_g[] =
9638 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
9639
Hanno Beckera90658f2017-10-04 15:29:08 +01009640 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
9641 dhm_p, sizeof( dhm_p ),
9642 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009643 {
9644 return( ret );
9645 }
9646 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009647#endif
9648
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009649 /*
9650 * Preset-specific defaults
9651 */
9652 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009653 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009654 /*
9655 * NSA Suite B
9656 */
9657 case MBEDTLS_SSL_PRESET_SUITEB:
9658 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
9659 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
9660 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9661 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9662
9663 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9664 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9665 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9666 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9667 ssl_preset_suiteb_ciphersuites;
9668
9669#if defined(MBEDTLS_X509_CRT_PARSE_C)
9670 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009671#endif
9672
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009673#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009674 conf->sig_hashes = ssl_preset_suiteb_hashes;
9675#endif
9676
9677#if defined(MBEDTLS_ECP_C)
9678 conf->curve_list = ssl_preset_suiteb_curves;
9679#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02009680 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009681
9682 /*
9683 * Default
9684 */
9685 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03009686 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
9687 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
9688 MBEDTLS_SSL_MIN_MAJOR_VERSION :
9689 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
9690 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
9691 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
9692 MBEDTLS_SSL_MIN_MINOR_VERSION :
9693 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009694 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
9695 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
9696
9697#if defined(MBEDTLS_SSL_PROTO_DTLS)
9698 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
9699 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
9700#endif
9701
9702 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
9703 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
9704 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
9705 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
9706 mbedtls_ssl_list_ciphersuites();
9707
9708#if defined(MBEDTLS_X509_CRT_PARSE_C)
9709 conf->cert_profile = &mbedtls_x509_crt_profile_default;
9710#endif
9711
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009712#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01009713 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02009714#endif
9715
9716#if defined(MBEDTLS_ECP_C)
9717 conf->curve_list = mbedtls_ecp_grp_id_list();
9718#endif
9719
9720#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9721 conf->dhm_min_bitlen = 1024;
9722#endif
9723 }
9724
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009725 return( 0 );
9726}
9727
9728/*
9729 * Free mbedtls_ssl_config
9730 */
9731void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
9732{
9733#if defined(MBEDTLS_DHM_C)
9734 mbedtls_mpi_free( &conf->dhm_P );
9735 mbedtls_mpi_free( &conf->dhm_G );
9736#endif
9737
9738#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9739 if( conf->psk != NULL )
9740 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009741 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009742 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00009743 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009744 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09009745 }
9746
9747 if( conf->psk_identity != NULL )
9748 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009749 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09009750 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00009751 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009752 conf->psk_identity_len = 0;
9753 }
9754#endif
9755
9756#if defined(MBEDTLS_X509_CRT_PARSE_C)
9757 ssl_key_cert_free( conf->key_cert );
9758#endif
9759
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009760 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02009761}
9762
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009763#if defined(MBEDTLS_PK_C) && \
9764 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009765/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009766 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009768unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009769{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770#if defined(MBEDTLS_RSA_C)
9771 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
9772 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009773#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774#if defined(MBEDTLS_ECDSA_C)
9775 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
9776 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009777#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009778 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02009779}
9780
Hanno Becker7e5437a2017-04-28 17:15:26 +01009781unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
9782{
9783 switch( type ) {
9784 case MBEDTLS_PK_RSA:
9785 return( MBEDTLS_SSL_SIG_RSA );
9786 case MBEDTLS_PK_ECDSA:
9787 case MBEDTLS_PK_ECKEY:
9788 return( MBEDTLS_SSL_SIG_ECDSA );
9789 default:
9790 return( MBEDTLS_SSL_SIG_ANON );
9791 }
9792}
9793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009794mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009795{
9796 switch( sig )
9797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009798#if defined(MBEDTLS_RSA_C)
9799 case MBEDTLS_SSL_SIG_RSA:
9800 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009801#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802#if defined(MBEDTLS_ECDSA_C)
9803 case MBEDTLS_SSL_SIG_ECDSA:
9804 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009805#endif
9806 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009808 }
9809}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02009810#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009811
Hanno Becker7e5437a2017-04-28 17:15:26 +01009812#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9813 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
9814
9815/* Find an entry in a signature-hash set matching a given hash algorithm. */
9816mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
9817 mbedtls_pk_type_t sig_alg )
9818{
9819 switch( sig_alg )
9820 {
9821 case MBEDTLS_PK_RSA:
9822 return( set->rsa );
9823 case MBEDTLS_PK_ECDSA:
9824 return( set->ecdsa );
9825 default:
9826 return( MBEDTLS_MD_NONE );
9827 }
9828}
9829
9830/* Add a signature-hash-pair to a signature-hash set */
9831void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
9832 mbedtls_pk_type_t sig_alg,
9833 mbedtls_md_type_t md_alg )
9834{
9835 switch( sig_alg )
9836 {
9837 case MBEDTLS_PK_RSA:
9838 if( set->rsa == MBEDTLS_MD_NONE )
9839 set->rsa = md_alg;
9840 break;
9841
9842 case MBEDTLS_PK_ECDSA:
9843 if( set->ecdsa == MBEDTLS_MD_NONE )
9844 set->ecdsa = md_alg;
9845 break;
9846
9847 default:
9848 break;
9849 }
9850}
9851
9852/* Allow exactly one hash algorithm for each signature. */
9853void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
9854 mbedtls_md_type_t md_alg )
9855{
9856 set->rsa = md_alg;
9857 set->ecdsa = md_alg;
9858}
9859
9860#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
9861 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
9862
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009863/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009864 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02009865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009866mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009867{
9868 switch( hash )
9869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009870#if defined(MBEDTLS_MD5_C)
9871 case MBEDTLS_SSL_HASH_MD5:
9872 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009873#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009874#if defined(MBEDTLS_SHA1_C)
9875 case MBEDTLS_SSL_HASH_SHA1:
9876 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009877#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009878#if defined(MBEDTLS_SHA256_C)
9879 case MBEDTLS_SSL_HASH_SHA224:
9880 return( MBEDTLS_MD_SHA224 );
9881 case MBEDTLS_SSL_HASH_SHA256:
9882 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009883#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009884#if defined(MBEDTLS_SHA512_C)
9885 case MBEDTLS_SSL_HASH_SHA384:
9886 return( MBEDTLS_MD_SHA384 );
9887 case MBEDTLS_SSL_HASH_SHA512:
9888 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009889#endif
9890 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009891 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02009892 }
9893}
9894
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009895/*
9896 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
9897 */
9898unsigned char mbedtls_ssl_hash_from_md_alg( int md )
9899{
9900 switch( md )
9901 {
9902#if defined(MBEDTLS_MD5_C)
9903 case MBEDTLS_MD_MD5:
9904 return( MBEDTLS_SSL_HASH_MD5 );
9905#endif
9906#if defined(MBEDTLS_SHA1_C)
9907 case MBEDTLS_MD_SHA1:
9908 return( MBEDTLS_SSL_HASH_SHA1 );
9909#endif
9910#if defined(MBEDTLS_SHA256_C)
9911 case MBEDTLS_MD_SHA224:
9912 return( MBEDTLS_SSL_HASH_SHA224 );
9913 case MBEDTLS_MD_SHA256:
9914 return( MBEDTLS_SSL_HASH_SHA256 );
9915#endif
9916#if defined(MBEDTLS_SHA512_C)
9917 case MBEDTLS_MD_SHA384:
9918 return( MBEDTLS_SSL_HASH_SHA384 );
9919 case MBEDTLS_MD_SHA512:
9920 return( MBEDTLS_SSL_HASH_SHA512 );
9921#endif
9922 default:
9923 return( MBEDTLS_SSL_HASH_NONE );
9924 }
9925}
9926
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009927#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009928/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009929 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009930 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009931 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009932int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009933{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009934 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009935
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009936 if( ssl->conf->curve_list == NULL )
9937 return( -1 );
9938
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009939 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009940 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009941 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009942
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02009943 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01009944}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009945#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009946
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009947#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009948/*
9949 * Check if a hash proposed by the peer is in our list.
9950 * Return 0 if we're willing to use it, -1 otherwise.
9951 */
9952int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
9953 mbedtls_md_type_t md )
9954{
9955 const int *cur;
9956
9957 if( ssl->conf->sig_hashes == NULL )
9958 return( -1 );
9959
9960 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
9961 if( *cur == (int) md )
9962 return( 0 );
9963
9964 return( -1 );
9965}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009966#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02009967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009968#if defined(MBEDTLS_X509_CRT_PARSE_C)
9969int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
9970 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009971 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009972 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009973{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009974 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009975#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009976 int usage = 0;
9977#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009978#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009979 const char *ext_oid;
9980 size_t ext_len;
9981#endif
9982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009983#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
9984 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009985 ((void) cert);
9986 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01009987 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02009988#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
9991 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009992 {
9993 /* Server part of the key exchange */
9994 switch( ciphersuite->key_exchange )
9995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009996 case MBEDTLS_KEY_EXCHANGE_RSA:
9997 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01009998 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02009999 break;
10000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010001 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10002 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10003 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10004 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010005 break;
10006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010007 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10008 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010009 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010010 break;
10011
10012 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010013 case MBEDTLS_KEY_EXCHANGE_NONE:
10014 case MBEDTLS_KEY_EXCHANGE_PSK:
10015 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10016 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010017 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010018 usage = 0;
10019 }
10020 }
10021 else
10022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010023 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10024 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010025 }
10026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010027 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010028 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010029 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010030 ret = -1;
10031 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010032#else
10033 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010034#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010036#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10037 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010039 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10040 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010041 }
10042 else
10043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010044 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10045 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010046 }
10047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010048 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010049 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010050 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010051 ret = -1;
10052 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010053#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010054
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010055 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010056}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010057#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010058
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010059/*
10060 * Convert version numbers to/from wire format
10061 * and, for DTLS, to/from TLS equivalent.
10062 *
10063 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010064 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010065 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10066 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010068void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010069 unsigned char ver[2] )
10070{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010071#if defined(MBEDTLS_SSL_PROTO_DTLS)
10072 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010074 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010075 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10076
10077 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10078 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10079 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010080 else
10081#else
10082 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010083#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010084 {
10085 ver[0] = (unsigned char) major;
10086 ver[1] = (unsigned char) minor;
10087 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010088}
10089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010090void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010091 const unsigned char ver[2] )
10092{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010093#if defined(MBEDTLS_SSL_PROTO_DTLS)
10094 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010095 {
10096 *major = 255 - ver[0] + 2;
10097 *minor = 255 - ver[1] + 1;
10098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010100 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10101 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010102 else
10103#else
10104 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010105#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010106 {
10107 *major = ver[0];
10108 *minor = ver[1];
10109 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010110}
10111
Simon Butcher99000142016-10-13 17:21:01 +010010112int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10113{
10114#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10115 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10116 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10117
10118 switch( md )
10119 {
10120#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10121#if defined(MBEDTLS_MD5_C)
10122 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010123 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010124#endif
10125#if defined(MBEDTLS_SHA1_C)
10126 case MBEDTLS_SSL_HASH_SHA1:
10127 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10128 break;
10129#endif
10130#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10131#if defined(MBEDTLS_SHA512_C)
10132 case MBEDTLS_SSL_HASH_SHA384:
10133 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10134 break;
10135#endif
10136#if defined(MBEDTLS_SHA256_C)
10137 case MBEDTLS_SSL_HASH_SHA256:
10138 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10139 break;
10140#endif
10141 default:
10142 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10143 }
10144
10145 return 0;
10146#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10147 (void) ssl;
10148 (void) md;
10149
10150 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10151#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10152}
10153
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010154#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10155 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10156int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10157 unsigned char *output,
10158 unsigned char *data, size_t data_len )
10159{
10160 int ret = 0;
10161 mbedtls_md5_context mbedtls_md5;
10162 mbedtls_sha1_context mbedtls_sha1;
10163
10164 mbedtls_md5_init( &mbedtls_md5 );
10165 mbedtls_sha1_init( &mbedtls_sha1 );
10166
10167 /*
10168 * digitally-signed struct {
10169 * opaque md5_hash[16];
10170 * opaque sha_hash[20];
10171 * };
10172 *
10173 * md5_hash
10174 * MD5(ClientHello.random + ServerHello.random
10175 * + ServerParams);
10176 * sha_hash
10177 * SHA(ClientHello.random + ServerHello.random
10178 * + ServerParams);
10179 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010180 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010181 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010182 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010183 goto exit;
10184 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010185 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010186 ssl->handshake->randbytes, 64 ) ) != 0 )
10187 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010189 goto exit;
10190 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010191 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010192 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010193 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010194 goto exit;
10195 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010196 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010197 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010198 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010199 goto exit;
10200 }
10201
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010202 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010203 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010204 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010205 goto exit;
10206 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010207 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010208 ssl->handshake->randbytes, 64 ) ) != 0 )
10209 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010210 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010211 goto exit;
10212 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010213 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010214 data_len ) ) != 0 )
10215 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010216 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010217 goto exit;
10218 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010219 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010220 output + 16 ) ) != 0 )
10221 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010222 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010223 goto exit;
10224 }
10225
10226exit:
10227 mbedtls_md5_free( &mbedtls_md5 );
10228 mbedtls_sha1_free( &mbedtls_sha1 );
10229
10230 if( ret != 0 )
10231 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10232 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10233
10234 return( ret );
10235
10236}
10237#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10238 MBEDTLS_SSL_PROTO_TLS1_1 */
10239
10240#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10241 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010242
10243#if defined(MBEDTLS_USE_PSA_CRYPTO)
10244int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10245 unsigned char *hash, size_t *hashlen,
10246 unsigned char *data, size_t data_len,
10247 mbedtls_md_type_t md_alg )
10248{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010249 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010250 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010251 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10252
Andrzej Kurek5615dab2019-01-16 05:26:25 -050010253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010254
10255 if( ( status = psa_hash_setup( &hash_operation,
10256 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010257 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010258 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010259 goto exit;
10260 }
10261
Andrzej Kurek814feff2019-01-14 04:35:19 -050010262 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10263 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010264 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010265 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010266 goto exit;
10267 }
10268
Andrzej Kurek814feff2019-01-14 04:35:19 -050010269 if( ( status = psa_hash_update( &hash_operation,
10270 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010271 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010272 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010273 goto exit;
10274 }
10275
Andrzej Kurek814feff2019-01-14 04:35:19 -050010276 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10277 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010278 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010279 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010280 goto exit;
10281 }
10282
10283exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010284 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010285 {
10286 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10287 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010288 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010289 {
10290 case PSA_ERROR_NOT_SUPPORTED:
10291 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010292 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010293 case PSA_ERROR_BUFFER_TOO_SMALL:
10294 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10295 case PSA_ERROR_INSUFFICIENT_MEMORY:
10296 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10297 default:
10298 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10299 }
10300 }
10301 return( 0 );
10302}
10303
10304#else
10305
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010306int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010307 unsigned char *hash, size_t *hashlen,
10308 unsigned char *data, size_t data_len,
10309 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010310{
10311 int ret = 0;
10312 mbedtls_md_context_t ctx;
10313 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010314 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010315
Andrzej Kurek5615dab2019-01-16 05:26:25 -050010316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010317
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010318 mbedtls_md_init( &ctx );
10319
10320 /*
10321 * digitally-signed struct {
10322 * opaque client_random[32];
10323 * opaque server_random[32];
10324 * ServerDHParams params;
10325 * };
10326 */
10327 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10328 {
10329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10330 goto exit;
10331 }
10332 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10333 {
10334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10335 goto exit;
10336 }
10337 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10338 {
10339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10340 goto exit;
10341 }
10342 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10343 {
10344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10345 goto exit;
10346 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010347 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010348 {
10349 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10350 goto exit;
10351 }
10352
10353exit:
10354 mbedtls_md_free( &ctx );
10355
10356 if( ret != 0 )
10357 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10358 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10359
10360 return( ret );
10361}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010362#endif /* MBEDTLS_USE_PSA_CRYPTO */
10363
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010364#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10365 MBEDTLS_SSL_PROTO_TLS1_2 */
10366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010367#endif /* MBEDTLS_SSL_TLS_C */