blob: aabe8c5f8be3456b191d13b4f1e363e94ff7e147 [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 Becker35c36a62019-04-23 12:31:42 +0100121#if defined(MBEDTLS_SSL_CID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100122/* Top-level Connection ID API */
123
Hanno Beckerca092242019-04-25 16:01:49 +0100124/* WARNING: The CID feature isn't fully implemented yet
125 * and will not be used. */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100126int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
127 int enable,
128 unsigned char const *own_cid,
129 size_t own_cid_len )
130{
Hanno Beckerca092242019-04-25 16:01:49 +0100131 ssl->negotiate_cid = enable;
132 if( enable == MBEDTLS_SSL_CID_DISABLED )
133 {
134 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
135 return( 0 );
136 }
137 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
138
139 if( own_cid_len > MBEDTLS_SSL_CID_IN_LEN_MAX )
140 {
141 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID too large: Maximum %u, actual %u",
142 (unsigned) MBEDTLS_SSL_CID_IN_LEN_MAX,
143 (unsigned) own_cid_len ) );
144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
145 }
146
147 memcpy( ssl->own_cid, own_cid, own_cid_len );
148 ssl->own_cid_len = own_cid_len;
149
150 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100151 return( 0 );
152}
153
Hanno Beckerf1f9a822019-04-23 12:01:20 +0100154/* WARNING: This implementation is a stub and doesn't do anything!
155 * It is included solely to allow review and coding against
156 * the new Connection CID API. */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100157int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
158 int *enabled,
159 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
160 size_t *peer_cid_len )
161{
162 ((void) ssl);
163 ((void) peer_cid);
164 ((void) peer_cid_len);
165
166 *enabled = MBEDTLS_SSL_CID_DISABLED;
167 return( 0 );
168}
Hanno Becker35c36a62019-04-23 12:31:42 +0100169#endif /* MBEDTLS_SSL_CID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100170
Hanno Beckerd5847772018-08-28 10:09:23 +0100171/* Forward declarations for functions related to message buffering. */
172static void ssl_buffering_free( mbedtls_ssl_context *ssl );
173static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
174 uint8_t slot );
175static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
176static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
177static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
178static int ssl_buffer_message( mbedtls_ssl_context *ssl );
179static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100180static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100181
Hanno Beckera67dee22018-08-22 10:05:20 +0100182static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100183static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100184{
Hanno Becker11682cc2018-08-22 14:41:02 +0100185 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100186
187 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100188 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100189
190 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
191}
192
Hanno Becker67bc7c32018-08-06 11:33:50 +0100193static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
194{
Hanno Becker11682cc2018-08-22 14:41:02 +0100195 size_t const bytes_written = ssl->out_left;
196 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100197
198 /* Double-check that the write-index hasn't gone
199 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100200 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100201 {
202 /* Should never happen... */
203 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
204 }
205
206 return( (int) ( mtu - bytes_written ) );
207}
208
209static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
210{
211 int ret;
212 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400213 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100214
215#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
216 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
217
218 if( max_len > mfl )
219 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100220
221 /* By the standard (RFC 6066 Sect. 4), the MFL extension
222 * only limits the maximum record payload size, so in theory
223 * we would be allowed to pack multiple records of payload size
224 * MFL into a single datagram. However, this would mean that there's
225 * no way to explicitly communicate MTU restrictions to the peer.
226 *
227 * The following reduction of max_len makes sure that we never
228 * write datagrams larger than MFL + Record Expansion Overhead.
229 */
230 if( max_len <= ssl->out_left )
231 return( 0 );
232
233 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100234#endif
235
236 ret = ssl_get_remaining_space_in_datagram( ssl );
237 if( ret < 0 )
238 return( ret );
239 remaining = (size_t) ret;
240
241 ret = mbedtls_ssl_get_record_expansion( ssl );
242 if( ret < 0 )
243 return( ret );
244 expansion = (size_t) ret;
245
246 if( remaining <= expansion )
247 return( 0 );
248
249 remaining -= expansion;
250 if( remaining >= max_len )
251 remaining = max_len;
252
253 return( (int) remaining );
254}
255
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200256/*
257 * Double the retransmit timeout value, within the allowed range,
258 * returning -1 if the maximum value has already been reached.
259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200261{
262 uint32_t new_timeout;
263
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200264 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200265 return( -1 );
266
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200267 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
268 * in the following way: after the initial transmission and a first
269 * retransmission, back off to a temporary estimated MTU of 508 bytes.
270 * This value is guaranteed to be deliverable (if not guaranteed to be
271 * delivered) of any compliant IPv4 (and IPv6) network, and should work
272 * on most non-IP stacks too. */
273 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400274 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200275 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
277 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200278
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200279 new_timeout = 2 * ssl->handshake->retransmit_timeout;
280
281 /* Avoid arithmetic overflow and range overflow */
282 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200283 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200284 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200285 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200286 }
287
288 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200290 ssl->handshake->retransmit_timeout ) );
291
292 return( 0 );
293}
294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200296{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200297 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200299 ssl->handshake->retransmit_timeout ) );
300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200304/*
305 * Convert max_fragment_length codes to length.
306 * RFC 6066 says:
307 * enum{
308 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
309 * } MaxFragmentLength;
310 * and we add 0 -> extension unused
311 */
Angus Grattond8213d02016-05-25 20:56:48 +1000312static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200313{
Angus Grattond8213d02016-05-25 20:56:48 +1000314 switch( mfl )
315 {
316 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
317 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
318 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
319 return 512;
320 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
321 return 1024;
322 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
323 return 2048;
324 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
325 return 4096;
326 default:
327 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
328 }
329}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200331
Hanno Becker52055ae2019-02-06 14:30:46 +0000332int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
333 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200334{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_ssl_session_free( dst );
336 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000339
340#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200341 if( src->peer_cert != NULL )
342 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200343 int ret;
344
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200345 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200346 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200347 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200352 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200355 dst->peer_cert = NULL;
356 return( ret );
357 }
358 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000359#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000360 if( src->peer_cert_digest != NULL )
361 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000362 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000363 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000364 if( dst->peer_cert_digest == NULL )
365 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
366
367 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
368 src->peer_cert_digest_len );
369 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000370 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000371 }
372#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200375
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200376#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200377 if( src->ticket != NULL )
378 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200379 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200380 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200381 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200382
383 memcpy( dst->ticket, src->ticket, src->ticket_len );
384 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200385#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200386
387 return( 0 );
388}
389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
391int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200392 const unsigned char *key_enc, const unsigned char *key_dec,
393 size_t keylen,
394 const unsigned char *iv_enc, const unsigned char *iv_dec,
395 size_t ivlen,
396 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200397 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
399int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
400int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
401int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
402int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
403#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000404
Paul Bakker5121ce52009-01-03 21:22:43 +0000405/*
406 * Key material generation
407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200409static int ssl3_prf( const unsigned char *secret, size_t slen,
410 const char *label,
411 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000412 unsigned char *dstbuf, size_t dlen )
413{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100414 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000415 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200416 mbedtls_md5_context md5;
417 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000418 unsigned char padding[16];
419 unsigned char sha1sum[20];
420 ((void)label);
421
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200422 mbedtls_md5_init( &md5 );
423 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200424
Paul Bakker5f70b252012-09-13 14:23:06 +0000425 /*
426 * SSLv3:
427 * block =
428 * MD5( secret + SHA1( 'A' + secret + random ) ) +
429 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
430 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
431 * ...
432 */
433 for( i = 0; i < dlen / 16; i++ )
434 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200435 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000436
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100437 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100438 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100439 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100440 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100441 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100442 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100443 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100444 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100445 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100446 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000447
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100448 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100449 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100450 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100451 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100452 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100453 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100454 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100455 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000456 }
457
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100458exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200459 mbedtls_md5_free( &md5 );
460 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000461
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500462 mbedtls_platform_zeroize( padding, sizeof( padding ) );
463 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000464
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100465 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000466}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200470static int tls1_prf( const unsigned char *secret, size_t slen,
471 const char *label,
472 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000473 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000474{
Paul Bakker23986e52011-04-24 08:57:21 +0000475 size_t nb, hs;
476 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200477 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300478 unsigned char *tmp;
479 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 const mbedtls_md_info_t *md_info;
482 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100483 int ret;
484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
Ron Eldor3b350852019-05-07 18:31:49 +0300487 tmp_len = 20 + strlen( label ) + rlen;
488 tmp = mbedtls_calloc( 1, tmp_len );
489 if( tmp == NULL )
490 {
491 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
492 goto exit;
493 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
495 hs = ( slen + 1 ) / 2;
496 S1 = secret;
497 S2 = secret + slen - hs;
498
499 nb = strlen( label );
500 memcpy( tmp + 20, label, nb );
501 memcpy( tmp + 20 + nb, random, rlen );
502 nb += rlen;
503
504 /*
505 * First compute P_md5(secret,label+random)[0..dlen]
506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300508 {
509 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
510 goto exit;
511 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300514 {
515 goto exit;
516 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
519 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
520 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
522 for( i = 0; i < dlen; i += 16 )
523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_md_hmac_reset ( &md_ctx );
525 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
526 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_md_hmac_reset ( &md_ctx );
529 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
530 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
532 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
533
534 for( j = 0; j < k; j++ )
535 dstbuf[i + j] = h_i[j];
536 }
537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100539
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 /*
541 * XOR out with P_sha1(secret,label+random)[0..dlen]
542 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300544 {
545 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
546 goto exit;
547 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300550 {
551 goto exit;
552 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
555 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
556 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
558 for( i = 0; i < dlen; i += 20 )
559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_md_hmac_reset ( &md_ctx );
561 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
562 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 mbedtls_md_hmac_reset ( &md_ctx );
565 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
566 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
568 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
569
570 for( j = 0; j < k; j++ )
571 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
572 }
573
Ron Eldor3b350852019-05-07 18:31:49 +0300574exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100576
Ron Eldor3b350852019-05-07 18:31:49 +0300577 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500578 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
Ron Eldor3b350852019-05-07 18:31:49 +0300580 mbedtls_free( tmp );
581 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000582}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500586#if defined(MBEDTLS_USE_PSA_CRYPTO)
587static int tls_prf_generic( mbedtls_md_type_t md_type,
588 const unsigned char *secret, size_t slen,
589 const char *label,
590 const unsigned char *random, size_t rlen,
591 unsigned char *dstbuf, size_t dlen )
592{
593 psa_status_t status;
594 psa_algorithm_t alg;
595 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500596 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500597 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
598
Andrzej Kurek2f760752019-01-28 08:08:15 -0500599 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500600 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500601
Andrzej Kurekc929a822019-01-14 03:51:11 -0500602 if( md_type == MBEDTLS_MD_SHA384 )
603 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
604 else
605 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
606
Andrzej Kurek2f760752019-01-28 08:08:15 -0500607 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500608 psa_key_policy_set_usage( &policy,
609 PSA_KEY_USAGE_DERIVE,
610 alg );
611 status = psa_set_key_policy( master_slot, &policy );
612 if( status != PSA_SUCCESS )
613 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
614
615 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
616 if( status != PSA_SUCCESS )
617 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
618
619 status = psa_key_derivation( &generator,
620 master_slot, alg,
621 random, rlen,
622 (unsigned char const *) label,
623 (size_t) strlen( label ),
624 dlen );
625 if( status != PSA_SUCCESS )
626 {
627 psa_generator_abort( &generator );
628 psa_destroy_key( master_slot );
629 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
630 }
631
632 status = psa_generator_read( &generator, dstbuf, dlen );
633 if( status != PSA_SUCCESS )
634 {
635 psa_generator_abort( &generator );
636 psa_destroy_key( master_slot );
637 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
638 }
639
640 status = psa_generator_abort( &generator );
641 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500642 {
643 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500644 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500645 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500646
647 status = psa_destroy_key( master_slot );
648 if( status != PSA_SUCCESS )
649 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
650
Andrzej Kurek33171262019-01-15 03:25:18 -0500651 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500652}
653
654#else /* MBEDTLS_USE_PSA_CRYPTO */
655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100657 const unsigned char *secret, size_t slen,
658 const char *label,
659 const unsigned char *random, size_t rlen,
660 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000661{
662 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100663 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300664 unsigned char *tmp;
665 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
667 const mbedtls_md_info_t *md_info;
668 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100669 int ret;
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
674 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100677
Ron Eldor3b350852019-05-07 18:31:49 +0300678 tmp_len = md_len + strlen( label ) + rlen;
679 tmp = mbedtls_calloc( 1, tmp_len );
680 if( tmp == NULL )
681 {
682 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
683 goto exit;
684 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000685
686 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100687 memcpy( tmp + md_len, label, nb );
688 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000689 nb += rlen;
690
691 /*
692 * Compute P_<hash>(secret, label + random)[0..dlen]
693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300695 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
698 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
699 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100700
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100701 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_md_hmac_reset ( &md_ctx );
704 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
705 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_md_hmac_reset ( &md_ctx );
708 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
709 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000710
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100711 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000712
713 for( j = 0; j < k; j++ )
714 dstbuf[i + j] = h_i[j];
715 }
716
Ron Eldor3b350852019-05-07 18:31:49 +0300717exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100719
Ron Eldor3b350852019-05-07 18:31:49 +0300720 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500721 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000722
Ron Eldor3b350852019-05-07 18:31:49 +0300723 mbedtls_free( tmp );
724
725 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000726}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500727#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100729static int tls_prf_sha256( const unsigned char *secret, size_t slen,
730 const char *label,
731 const unsigned char *random, size_t rlen,
732 unsigned char *dstbuf, size_t dlen )
733{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100735 label, random, rlen, dstbuf, dlen ) );
736}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200740static int tls_prf_sha384( const unsigned char *secret, size_t slen,
741 const char *label,
742 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000743 unsigned char *dstbuf, size_t dlen )
744{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100746 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000747}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748#endif /* MBEDTLS_SHA512_C */
749#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
754 defined(MBEDTLS_SSL_PROTO_TLS1_1)
755static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200756#endif
Paul Bakker380da532012-04-18 16:10:25 +0000757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#if defined(MBEDTLS_SSL_PROTO_SSL3)
759static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
760static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200761#endif
762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
764static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
765static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200766#endif
767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
769#if defined(MBEDTLS_SHA256_C)
770static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
771static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
772static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200773#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775#if defined(MBEDTLS_SHA512_C)
776static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
777static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
778static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100779#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000781
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100782#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100783 defined(MBEDTLS_USE_PSA_CRYPTO)
784static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
785{
786 if( ssl->conf->f_psk != NULL )
787 {
788 /* If we've used a callback to select the PSK,
789 * the static configuration is irrelevant. */
790 if( ssl->handshake->psk_opaque != 0 )
791 return( 1 );
792
793 return( 0 );
794 }
795
796 if( ssl->conf->psk_opaque != 0 )
797 return( 1 );
798
799 return( 0 );
800}
801#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100802 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100803
Ron Eldorcf280092019-05-14 20:19:13 +0300804#if defined(MBEDTLS_SSL_EXPORT_KEYS)
805static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
806{
807#if defined(MBEDTLS_SSL_PROTO_SSL3)
808 if( tls_prf == ssl3_prf )
809 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300810 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300811 }
812 else
813#endif
814#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
815 if( tls_prf == tls1_prf )
816 {
817 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
818 }
819 else
820#endif
821#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
822#if defined(MBEDTLS_SHA512_C)
823 if( tls_prf == tls_prf_sha384 )
824 {
825 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
826 }
827 else
828#endif
829#if defined(MBEDTLS_SHA256_C)
830 if( tls_prf == tls_prf_sha256 )
831 {
832 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
833 }
834 else
835#endif
836#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
837 return( MBEDTLS_SSL_TLS_PRF_NONE );
838}
839#endif /* MBEDTLS_SSL_EXPORT_KEYS */
840
Ron Eldor51d3ab52019-05-12 14:54:30 +0300841int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
842 const unsigned char *secret, size_t slen,
843 const char *label,
844 const unsigned char *random, size_t rlen,
845 unsigned char *dstbuf, size_t dlen )
846{
847 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
848
849 switch( prf )
850 {
851#if defined(MBEDTLS_SSL_PROTO_SSL3)
852 case MBEDTLS_SSL_TLS_PRF_SSL3:
853 tls_prf = ssl3_prf;
854 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300855#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300856#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
857 case MBEDTLS_SSL_TLS_PRF_TLS1:
858 tls_prf = tls1_prf;
859 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300860#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
861
862#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300863#if defined(MBEDTLS_SHA512_C)
864 case MBEDTLS_SSL_TLS_PRF_SHA384:
865 tls_prf = tls_prf_sha384;
866 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300867#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300868#if defined(MBEDTLS_SHA256_C)
869 case MBEDTLS_SSL_TLS_PRF_SHA256:
870 tls_prf = tls_prf_sha256;
871 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300872#endif /* MBEDTLS_SHA256_C */
873#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300874 default:
875 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
876 }
877
878 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
879}
880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000882{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200883 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000884#if defined(MBEDTLS_USE_PSA_CRYPTO)
885 int psa_fallthrough;
886#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000887 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000888 unsigned char keyblk[256];
889 unsigned char *key1;
890 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100891 unsigned char *mac_enc;
892 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000893 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200894 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000895 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000896 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 const mbedtls_cipher_info_t *cipher_info;
898 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100899
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000900 /* cf. RFC 5246, Section 8.1:
901 * "The master secret is always exactly 48 bytes in length." */
902 size_t const master_secret_len = 48;
903
Hanno Becker35b23c72018-10-23 12:10:41 +0100904#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
905 unsigned char session_hash[48];
906#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 mbedtls_ssl_session *session = ssl->session_negotiate;
909 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
910 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000914#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
915 transform->encrypt_then_mac = session->encrypt_then_mac;
916#endif
917 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000918
919 ciphersuite_info = handshake->ciphersuite_info;
920 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100921 if( cipher_info == NULL )
922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000924 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100926 }
927
Hanno Beckere694c3e2017-12-27 21:34:08 +0000928 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100929 if( md_info == NULL )
930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000932 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100934 }
935
Paul Bakker5121ce52009-01-03 21:22:43 +0000936 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000937 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000938 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939#if defined(MBEDTLS_SSL_PROTO_SSL3)
940 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000941 {
Paul Bakker48916f92012-09-16 19:57:18 +0000942 handshake->tls_prf = ssl3_prf;
943 handshake->calc_verify = ssl_calc_verify_ssl;
944 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000945 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200946 else
947#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
949 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000950 {
Paul Bakker48916f92012-09-16 19:57:18 +0000951 handshake->tls_prf = tls1_prf;
952 handshake->calc_verify = ssl_calc_verify_tls;
953 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000954 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200955 else
956#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
958#if defined(MBEDTLS_SHA512_C)
959 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +0000960 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000961 {
Paul Bakker48916f92012-09-16 19:57:18 +0000962 handshake->tls_prf = tls_prf_sha384;
963 handshake->calc_verify = ssl_calc_verify_tls_sha384;
964 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000965 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000966 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200967#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968#if defined(MBEDTLS_SHA256_C)
969 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000970 {
Paul Bakker48916f92012-09-16 19:57:18 +0000971 handshake->tls_prf = tls_prf_sha256;
972 handshake->calc_verify = ssl_calc_verify_tls_sha256;
973 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000974 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200975 else
976#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
980 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200981 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000982
983 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000984 * SSLv3:
985 * master =
986 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
987 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
988 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200989 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200990 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000991 * master = PRF( premaster, "master secret", randbytes )[0..47]
992 */
Hanno Becker35b23c72018-10-23 12:10:41 +0100993 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 {
Hanno Becker35b23c72018-10-23 12:10:41 +0100995 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
996 }
997 else
998 {
999 /* The label for the KDF used for key expansion.
1000 * This is either "master secret" or "extended master secret"
1001 * depending on whether the Extended Master Secret extension
1002 * is used. */
1003 char const *lbl = "master secret";
1004
1005 /* The salt for the KDF used for key expansion.
1006 * - If the Extended Master Secret extension is not used,
1007 * this is ClientHello.Random + ServerHello.Random
1008 * (see Sect. 8.1 in RFC 5246).
1009 * - If the Extended Master Secret extension is used,
1010 * this is the transcript of the handshake so far.
1011 * (see Sect. 4 in RFC 7627). */
1012 unsigned char const *salt = handshake->randbytes;
1013 size_t salt_len = 64;
1014
1015#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001019
Hanno Becker35b23c72018-10-23 12:10:41 +01001020 lbl = "extended master secret";
1021 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001022 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1024 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001027 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1028 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001029 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001030 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001031 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001032#endif /* MBEDTLS_SHA512_C */
1033 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001034 }
1035 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001037 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001038
Hanno Becker35b23c72018-10-23 12:10:41 +01001039 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001040 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001041#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1042
Hanno Becker7d0a5692018-10-23 15:26:22 +01001043#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1044 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1045 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1046 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1047 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001048 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001049 /* Perform PSK-to-MS expansion in a single step. */
1050 psa_status_t status;
1051 psa_algorithm_t alg;
1052 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001053 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001054
1055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1056
1057 psk = ssl->conf->psk_opaque;
1058 if( ssl->handshake->psk_opaque != 0 )
1059 psk = ssl->handshake->psk_opaque;
1060
Hanno Becker22bf1452019-04-05 11:21:08 +01001061 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001062 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1063 else
1064 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1065
1066 status = psa_key_derivation( &generator, psk, alg,
1067 salt, salt_len,
1068 (unsigned char const *) lbl,
1069 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001070 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001071 if( status != PSA_SUCCESS )
1072 {
1073 psa_generator_abort( &generator );
1074 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1075 }
1076
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001077 status = psa_generator_read( &generator, session->master,
1078 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001079 if( status != PSA_SUCCESS )
1080 {
1081 psa_generator_abort( &generator );
1082 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1083 }
1084
1085 status = psa_generator_abort( &generator );
1086 if( status != PSA_SUCCESS )
1087 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001088 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001089 else
1090#endif
1091 {
1092 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1093 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001094 session->master,
1095 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001096 if( ret != 0 )
1097 {
1098 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1099 return( ret );
1100 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001101
Hanno Becker7d0a5692018-10-23 15:26:22 +01001102 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1103 handshake->premaster,
1104 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001105
Hanno Becker7d0a5692018-10-23 15:26:22 +01001106 mbedtls_platform_zeroize( handshake->premaster,
1107 sizeof(handshake->premaster) );
1108 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001109 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001110
1111 /*
1112 * Swap the client and server random values.
1113 */
Paul Bakker48916f92012-09-16 19:57:18 +00001114 memcpy( tmp, handshake->randbytes, 64 );
1115 memcpy( handshake->randbytes, tmp + 32, 32 );
1116 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001117 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
1119 /*
1120 * SSLv3:
1121 * key block =
1122 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1123 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1124 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1125 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1126 * ...
1127 *
1128 * TLSv1:
1129 * key block = PRF( master, "key expansion", randbytes )
1130 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001131 ret = handshake->tls_prf( session->master, 48, "key expansion",
1132 handshake->randbytes, 64, keyblk, 256 );
1133 if( ret != 0 )
1134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001136 return( ret );
1137 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1140 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1141 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1142 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1143 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001144
Paul Bakker5121ce52009-01-03 21:22:43 +00001145 /*
1146 * Determine the appropriate key, IV and MAC length.
1147 */
Paul Bakker68884e32013-01-07 18:20:04 +01001148
Hanno Becker88aaf652017-12-27 08:17:40 +00001149 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001150
Hanno Becker8031d062018-01-03 15:32:31 +00001151#if defined(MBEDTLS_GCM_C) || \
1152 defined(MBEDTLS_CCM_C) || \
1153 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001155 cipher_info->mode == MBEDTLS_MODE_CCM ||
1156 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001157 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001158 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001159
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001160 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001161 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001162 transform->taglen =
1163 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001164
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001165 /* All modes haves 96-bit IVs;
1166 * GCM and CCM has 4 implicit and 8 explicit bytes
1167 * ChachaPoly has all 12 bytes implicit
1168 */
Paul Bakker68884e32013-01-07 18:20:04 +01001169 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001170 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1171 transform->fixed_ivlen = 12;
1172 else
1173 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001174
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001175 /* Minimum length of encrypted record */
1176 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001177 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001178 }
1179 else
Hanno Becker8031d062018-01-03 15:32:31 +00001180#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1181#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1182 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1183 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001184 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001185 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1187 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001190 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001191 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001192
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001193 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001194 mac_key_len = mbedtls_md_get_size( md_info );
1195 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001198 /*
1199 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1200 * (rfc 6066 page 13 or rfc 2104 section 4),
1201 * so we only need to adjust the length here.
1202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001206
1207#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1208 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001209 * HMAC implementation which also truncates the key
1210 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001211 mac_key_len = transform->maclen;
1212#endif
1213 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001215
1216 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001217 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001218
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001219 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001221 transform->minlen = transform->maclen;
1222 else
Paul Bakker68884e32013-01-07 18:20:04 +01001223 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001224 /*
1225 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001226 * 1. if EtM is in use: one block plus MAC
1227 * otherwise: * first multiple of blocklen greater than maclen
1228 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1231 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001232 {
1233 transform->minlen = transform->maclen
1234 + cipher_info->block_size;
1235 }
1236 else
1237#endif
1238 {
1239 transform->minlen = transform->maclen
1240 + cipher_info->block_size
1241 - transform->maclen % cipher_info->block_size;
1242 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1245 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1246 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001247 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001248 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001249#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1251 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1252 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001253 {
1254 transform->minlen += transform->ivlen;
1255 }
1256 else
1257#endif
1258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001260 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1261 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001262 }
Paul Bakker68884e32013-01-07 18:20:04 +01001263 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001264 }
Hanno Becker8031d062018-01-03 15:32:31 +00001265 else
1266#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1267 {
1268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1269 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1270 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001271
Hanno Becker88aaf652017-12-27 08:17:40 +00001272 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1273 (unsigned) keylen,
1274 (unsigned) transform->minlen,
1275 (unsigned) transform->ivlen,
1276 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001277
1278 /*
1279 * Finally setup the cipher contexts, IVs and MAC secrets.
1280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001282 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001284 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001285 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001286
Paul Bakker68884e32013-01-07 18:20:04 +01001287 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001288 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001289
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001290 /*
1291 * This is not used in TLS v1.1.
1292 */
Paul Bakker48916f92012-09-16 19:57:18 +00001293 iv_copy_len = ( transform->fixed_ivlen ) ?
1294 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001295 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1296 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001297 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 }
1299 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#endif /* MBEDTLS_SSL_CLI_C */
1301#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001302 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001303 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001304 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001305 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001306
Hanno Becker81c7b182017-11-09 18:39:33 +00001307 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001308 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001309
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001310 /*
1311 * This is not used in TLS v1.1.
1312 */
Paul Bakker48916f92012-09-16 19:57:18 +00001313 iv_copy_len = ( transform->fixed_ivlen ) ?
1314 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001315 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1316 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001317 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001319 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001323 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1324 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001325 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
Hanno Beckerd56ed242018-01-03 15:32:51 +00001327#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#if defined(MBEDTLS_SSL_PROTO_SSL3)
1329 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001330 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001331 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001334 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1335 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001336 }
1337
Hanno Becker81c7b182017-11-09 18:39:33 +00001338 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1339 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001340 }
1341 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1343#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1344 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1345 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001346 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001347 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1348 For AEAD-based ciphersuites, there is nothing to do here. */
1349 if( mac_key_len != 0 )
1350 {
1351 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1352 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1353 }
Paul Bakker68884e32013-01-07 18:20:04 +01001354 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001355 else
1356#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001359 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1360 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001361 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001362#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1365 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001366 {
1367 int ret = 0;
1368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001370
Hanno Becker88aaf652017-12-27 08:17:40 +00001371 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001372 transform->iv_enc, transform->iv_dec,
1373 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001374 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001375 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001378 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1379 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001380 }
1381 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001382#else
1383 ((void) mac_dec);
1384 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001386
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001387#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1388 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001389 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001390 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1391 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001392 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001393 iv_copy_len );
1394 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001395
1396 if( ssl->conf->f_export_keys_ext != NULL )
1397 {
1398 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1399 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001400 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001401 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001402 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001403 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001404 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001405 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001406#endif
1407
Hanno Beckerf704bef2018-11-16 15:21:18 +00001408#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001409
1410 /* Only use PSA-based ciphers for TLS-1.2.
1411 * That's relevant at least for TLS-1.0, where
1412 * we assume that mbedtls_cipher_crypt() updates
1413 * the structure field for the IV, which the PSA-based
1414 * implementation currently doesn't. */
1415#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1416 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001417 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001418 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001419 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001420 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1421 {
1422 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001423 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001424 }
1425
1426 if( ret == 0 )
1427 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001428 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001429 psa_fallthrough = 0;
1430 }
1431 else
1432 {
1433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1434 psa_fallthrough = 1;
1435 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001436 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001437 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001438 psa_fallthrough = 1;
1439#else
1440 psa_fallthrough = 1;
1441#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001442
Hanno Beckercb1cc802018-11-17 22:27:38 +00001443 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001444#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001445 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001446 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001449 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001450 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001451
Hanno Beckerf704bef2018-11-16 15:21:18 +00001452#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001453 /* Only use PSA-based ciphers for TLS-1.2.
1454 * That's relevant at least for TLS-1.0, where
1455 * we assume that mbedtls_cipher_crypt() updates
1456 * the structure field for the IV, which the PSA-based
1457 * implementation currently doesn't. */
1458#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1459 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001460 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001461 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001462 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001463 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1464 {
1465 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001466 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001467 }
1468
1469 if( ret == 0 )
1470 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001471 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001472 psa_fallthrough = 0;
1473 }
1474 else
1475 {
1476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1477 psa_fallthrough = 1;
1478 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001479 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001480 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001481 psa_fallthrough = 1;
1482#else
1483 psa_fallthrough = 1;
1484#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001485
Hanno Beckercb1cc802018-11-17 22:27:38 +00001486 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001487#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001488 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001489 cipher_info ) ) != 0 )
1490 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001491 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001492 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001493 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001496 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001500 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001501 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001504 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001508 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001509 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#if defined(MBEDTLS_CIPHER_MODE_CBC)
1512 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1515 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001518 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001519 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1522 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001525 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001526 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001527 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001529
Paul Bakker5121ce52009-01-03 21:22:43 +00001530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001532 // Initialize compression
1533 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001535 {
Paul Bakker16770332013-10-11 09:59:44 +02001536 if( ssl->compress_buf == NULL )
1537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001539 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001540 if( ssl->compress_buf == NULL )
1541 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001543 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001544 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1545 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001546 }
1547 }
1548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001550
Paul Bakker48916f92012-09-16 19:57:18 +00001551 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1552 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001553
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001554 if( deflateInit( &transform->ctx_deflate,
1555 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001556 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001559 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1560 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001561 }
1562 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001566end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001567 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1568 mbedtls_platform_zeroize( handshake->randbytes,
1569 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001570 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001571}
1572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#if defined(MBEDTLS_SSL_PROTO_SSL3)
1574void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001575{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001576 mbedtls_md5_context md5;
1577 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001578 unsigned char pad_1[48];
1579 unsigned char pad_2[48];
1580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001582
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001583 mbedtls_md5_init( &md5 );
1584 mbedtls_sha1_init( &sha1 );
1585
1586 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1587 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001588
Paul Bakker380da532012-04-18 16:10:25 +00001589 memset( pad_1, 0x36, 48 );
1590 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001591
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001592 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1593 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1594 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001595
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001596 mbedtls_md5_starts_ret( &md5 );
1597 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1598 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1599 mbedtls_md5_update_ret( &md5, hash, 16 );
1600 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001601
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001602 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1603 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1604 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001605
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001606 mbedtls_sha1_starts_ret( &sha1 );
1607 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1608 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1609 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1610 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001614
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001615 mbedtls_md5_free( &md5 );
1616 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001617
Paul Bakker380da532012-04-18 16:10:25 +00001618 return;
1619}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1623void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001624{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001625 mbedtls_md5_context md5;
1626 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001629
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001630 mbedtls_md5_init( &md5 );
1631 mbedtls_sha1_init( &sha1 );
1632
1633 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1634 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001635
Andrzej Kurekeb342242019-01-29 09:14:33 -05001636 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001637 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001641
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001642 mbedtls_md5_free( &md5 );
1643 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001644
Paul Bakker380da532012-04-18 16:10:25 +00001645 return;
1646}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1650#if defined(MBEDTLS_SHA256_C)
1651void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001652{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001653#if defined(MBEDTLS_USE_PSA_CRYPTO)
1654 size_t hash_size;
1655 psa_status_t status;
1656 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1657
1658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1659 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1660 if( status != PSA_SUCCESS )
1661 {
1662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1663 return;
1664 }
1665
1666 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1667 if( status != PSA_SUCCESS )
1668 {
1669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1670 return;
1671 }
1672 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1674#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001675 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001676
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001677 mbedtls_sha256_init( &sha256 );
1678
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001680
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001681 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001682 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001686
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001687 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001688#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001689 return;
1690}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693#if defined(MBEDTLS_SHA512_C)
1694void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001695{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001696#if defined(MBEDTLS_USE_PSA_CRYPTO)
1697 size_t hash_size;
1698 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001699 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001700
1701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001702 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001703 if( status != PSA_SUCCESS )
1704 {
1705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1706 return;
1707 }
1708
Andrzej Kurek972fba52019-01-30 03:29:12 -05001709 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001710 if( status != PSA_SUCCESS )
1711 {
1712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1713 return;
1714 }
1715 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1717#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001718 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001719
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001720 mbedtls_sha512_init( &sha512 );
1721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001723
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001724 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001725 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001729
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001730 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001731#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001732 return;
1733}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734#endif /* MBEDTLS_SHA512_C */
1735#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1738int 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 +02001739{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001740 unsigned char *p = ssl->handshake->premaster;
1741 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001742 const unsigned char *psk = ssl->conf->psk;
1743 size_t psk_len = ssl->conf->psk_len;
1744
1745 /* If the psk callback was called, use its result */
1746 if( ssl->handshake->psk != NULL )
1747 {
1748 psk = ssl->handshake->psk;
1749 psk_len = ssl->handshake->psk_len;
1750 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001751
1752 /*
1753 * PMS = struct {
1754 * opaque other_secret<0..2^16-1>;
1755 * opaque psk<0..2^16-1>;
1756 * };
1757 * with "other_secret" depending on the particular key exchange
1758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1760 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001761 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001762 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001764
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001765 *(p++) = (unsigned char)( psk_len >> 8 );
1766 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001767
1768 if( end < p || (size_t)( end - p ) < psk_len )
1769 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1770
1771 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001772 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001773 }
1774 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1776#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1777 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001778 {
1779 /*
1780 * other_secret already set by the ClientKeyExchange message,
1781 * and is 48 bytes long
1782 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001783 if( end - p < 2 )
1784 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1785
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001786 *p++ = 0;
1787 *p++ = 48;
1788 p += 48;
1789 }
1790 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1792#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1793 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001794 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001795 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001796 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001797
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001798 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001800 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001801 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001804 return( ret );
1805 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001806 *(p++) = (unsigned char)( len >> 8 );
1807 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001808 p += len;
1809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001811 }
1812 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1814#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1815 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001816 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001817 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001818 size_t zlen;
1819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001821 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001822 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001825 return( ret );
1826 }
1827
1828 *(p++) = (unsigned char)( zlen >> 8 );
1829 *(p++) = (unsigned char)( zlen );
1830 p += zlen;
1831
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001832 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1833 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001834 }
1835 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1839 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001840 }
1841
1842 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001843 if( end - p < 2 )
1844 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001845
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001846 *(p++) = (unsigned char)( psk_len >> 8 );
1847 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001848
1849 if( end < p || (size_t)( end - p ) < psk_len )
1850 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1851
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001852 memcpy( p, psk, psk_len );
1853 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001854
1855 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1856
1857 return( 0 );
1858}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001862/*
1863 * SSLv3.0 MAC functions
1864 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001865#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001866static void ssl_mac( mbedtls_md_context_t *md_ctx,
1867 const unsigned char *secret,
1868 const unsigned char *buf, size_t len,
1869 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001870 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001871{
1872 unsigned char header[11];
1873 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001874 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1876 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001877
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001878 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001880 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001881 else
Paul Bakker68884e32013-01-07 18:20:04 +01001882 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001883
1884 memcpy( header, ctr, 8 );
1885 header[ 8] = (unsigned char) type;
1886 header[ 9] = (unsigned char)( len >> 8 );
1887 header[10] = (unsigned char)( len );
1888
Paul Bakker68884e32013-01-07 18:20:04 +01001889 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 mbedtls_md_starts( md_ctx );
1891 mbedtls_md_update( md_ctx, secret, md_size );
1892 mbedtls_md_update( md_ctx, padding, padlen );
1893 mbedtls_md_update( md_ctx, header, 11 );
1894 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001895 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001896
Paul Bakker68884e32013-01-07 18:20:04 +01001897 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 mbedtls_md_starts( md_ctx );
1899 mbedtls_md_update( md_ctx, secret, md_size );
1900 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001901 mbedtls_md_update( md_ctx, out, md_size );
1902 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001903}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001905
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001906/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001907 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001908#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001909 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1910 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1911 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1912/* This function makes sure every byte in the memory region is accessed
1913 * (in ascending addresses order) */
1914static void ssl_read_memory( unsigned char *p, size_t len )
1915{
1916 unsigned char acc = 0;
1917 volatile unsigned char force;
1918
1919 for( ; len != 0; p++, len-- )
1920 acc ^= *p;
1921
1922 force = acc;
1923 (void) force;
1924}
1925#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1926
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001927/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001929 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001930
1931static void ssl_extract_add_data_from_record( unsigned char* add_data,
1932 mbedtls_record *rec )
1933{
1934 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1935 add_data[8] = rec->type;
1936 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
1937 add_data[11] = ( rec->data_len >> 8 ) & 0xFF;
1938 add_data[12] = rec->data_len & 0xFF;
1939}
1940
Hanno Beckera18d1322018-01-03 14:27:32 +00001941int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1942 mbedtls_ssl_transform *transform,
1943 mbedtls_record *rec,
1944 int (*f_rng)(void *, unsigned char *, size_t),
1945 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001946{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001948 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001949 unsigned char * data;
1950 unsigned char add_data[13];
1951 size_t post_avail;
1952
1953 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00001954#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001955 ((void) ssl);
1956#endif
1957
1958 /* The PRNG is used for dynamic IV generation that's used
1959 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1960#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1961 ( defined(MBEDTLS_AES_C) || \
1962 defined(MBEDTLS_ARIA_C) || \
1963 defined(MBEDTLS_CAMELLIA_C) ) && \
1964 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1965 ((void) f_rng);
1966 ((void) p_rng);
1967#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001970
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001971 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001972 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1974 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1975 }
1976 if( rec == NULL ||
1977 rec->buf == NULL ||
1978 rec->buf_len < rec->data_offset ||
1979 rec->buf_len - rec->data_offset < rec->data_len )
1980 {
1981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001983 }
1984
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001985 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1986 data = rec->buf + rec->data_offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001988 data, rec->data_len );
1989
1990 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1991
1992 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1993 {
1994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1995 (unsigned) rec->data_len,
1996 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1997 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1998 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001999
Paul Bakker5121ce52009-01-03 21:22:43 +00002000 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002001 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002002 */
Hanno Becker52344c22018-01-03 15:24:20 +00002003#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 if( mode == MBEDTLS_MODE_STREAM ||
2005 ( mode == MBEDTLS_MODE_CBC
2006#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002007 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002008#endif
2009 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002010 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002011 if( post_avail < transform->maclen )
2012 {
2013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2014 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2015 }
2016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002018 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002019 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002020 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002021 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2022 data, rec->data_len, rec->ctr, rec->type, mac );
2023 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002024 }
2025 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002026#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2028 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002029 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002030 {
Hanno Becker992b6872017-11-09 18:57:39 +00002031 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2032
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002033 ssl_extract_add_data_from_record( add_data, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002034
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002035 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2036 sizeof( add_data ) );
2037 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2038 data, rec->data_len );
2039 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2040 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2041
2042 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002043 }
2044 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002045#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002049 }
2050
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002051 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2052 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002053
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002054 rec->data_len += transform->maclen;
2055 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002056 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002057 }
Hanno Becker52344c22018-01-03 15:24:20 +00002058#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002059
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002060 /*
2061 * Encrypt
2062 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2064 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002066 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002067 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002069 "including %d bytes of padding",
2070 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002071
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002072 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2073 transform->iv_enc, transform->ivlen,
2074 data, rec->data_len,
2075 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002078 return( ret );
2079 }
2080
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002081 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2084 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002085 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 }
Paul Bakker68884e32013-01-07 18:20:04 +01002087 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002089
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002090#if defined(MBEDTLS_GCM_C) || \
2091 defined(MBEDTLS_CCM_C) || \
2092 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002094 mode == MBEDTLS_MODE_CCM ||
2095 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002096 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002097 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002098 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002099 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002100
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002101 /* Check that there's space for both the authentication tag
2102 * and the explicit IV before and after the record content. */
2103 if( post_avail < transform->taglen ||
2104 rec->data_offset < explicit_iv_len )
2105 {
2106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2107 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2108 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002109
Paul Bakker68884e32013-01-07 18:20:04 +01002110 /*
2111 * Generate IV
2112 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002113 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2114 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002115 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002116 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002117 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2118 explicit_iv_len );
2119 /* Prefix record content with explicit IV. */
2120 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002121 }
2122 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2123 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002124 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002125 unsigned char i;
2126
2127 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2128
2129 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002130 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002131 }
2132 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002133 {
2134 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002137 }
2138
Hanno Becker1f10d762019-04-26 13:34:37 +01002139 ssl_extract_add_data_from_record( add_data, rec );
2140
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002141 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2142 iv, transform->ivlen );
2143 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002144 data - explicit_iv_len, explicit_iv_len );
2145 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2146 add_data, 13 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002148 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002149 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002150
Paul Bakker68884e32013-01-07 18:20:04 +01002151 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002152 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002153 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002154
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002155 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002156 iv, transform->ivlen,
2157 add_data, 13, /* add data */
2158 data, rec->data_len, /* source */
2159 data, &rec->data_len, /* destination */
2160 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002163 return( ret );
2164 }
2165
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002166 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2167 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002168
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002169 rec->data_len += transform->taglen + explicit_iv_len;
2170 rec->data_offset -= explicit_iv_len;
2171 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002172 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002173 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002174 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2176#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002177 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002180 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002181 size_t padlen, i;
2182 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002183
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002184 /* Currently we're always using minimal padding
2185 * (up to 255 bytes would be allowed). */
2186 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2187 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002188 padlen = 0;
2189
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002190 /* Check there's enough space in the buffer for the padding. */
2191 if( post_avail < padlen + 1 )
2192 {
2193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2194 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2195 }
2196
Paul Bakker5121ce52009-01-03 21:22:43 +00002197 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002198 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002199
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002200 rec->data_len += padlen + 1;
2201 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002204 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002205 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2206 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002207 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002208 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002209 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002210 if( f_rng == NULL )
2211 {
2212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2213 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2214 }
2215
2216 if( rec->data_offset < transform->ivlen )
2217 {
2218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2219 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2220 }
2221
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002222 /*
2223 * Generate IV
2224 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002225 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002226 if( ret != 0 )
2227 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002228
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002229 memcpy( data - transform->ivlen, transform->iv_enc,
2230 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002231
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002232 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002236 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002237 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002238 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002239
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002240 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2241 transform->iv_enc,
2242 transform->ivlen,
2243 data, rec->data_len,
2244 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002247 return( ret );
2248 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002249
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002250 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2253 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002254 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002257 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002258 {
2259 /*
2260 * Save IV in SSL3 and TLS1
2261 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002262 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2263 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002264 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002265 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002266#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002267 {
2268 data -= transform->ivlen;
2269 rec->data_offset -= transform->ivlen;
2270 rec->data_len += transform->ivlen;
2271 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002274 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002275 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002276 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2277
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002278 /*
2279 * MAC(MAC_write_key, seq_num +
2280 * TLSCipherText.type +
2281 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002282 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002283 * IV + // except for TLS 1.0
2284 * ENC(content + padding + padding_length));
2285 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002286
2287 if( post_avail < transform->maclen)
2288 {
2289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2290 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2291 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002292
Hanno Becker1f10d762019-04-26 13:34:37 +01002293 ssl_extract_add_data_from_record( add_data, rec );
2294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002296 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2297 sizeof( add_data ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002298
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002299 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2300 sizeof( add_data ) );
2301 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2302 data, rec->data_len );
2303 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2304 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002305
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002306 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002307
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002308 rec->data_len += transform->maclen;
2309 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002310 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002311 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002313 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002314 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002316 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2319 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002320 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002321
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002322 /* Make extra sure authentication was performed, exactly once */
2323 if( auth_done != 1 )
2324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2326 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002327 }
2328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002330
2331 return( 0 );
2332}
2333
Hanno Beckera18d1322018-01-03 14:27:32 +00002334int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2335 mbedtls_ssl_transform *transform,
2336 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002337{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002338 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002340 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002341#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002342 size_t padlen = 0, correct = 1;
2343#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002344 unsigned char* data;
2345 unsigned char add_data[13];
2346
Hanno Beckera18d1322018-01-03 14:27:32 +00002347#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002348 ((void) ssl);
2349#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002352 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002353 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2355 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2356 }
2357 if( rec == NULL ||
2358 rec->buf == NULL ||
2359 rec->buf_len < rec->data_offset ||
2360 rec->buf_len - rec->data_offset < rec->data_len )
2361 {
2362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002364 }
2365
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002366 data = rec->buf + rec->data_offset;
2367 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2370 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002371 {
2372 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002373 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2374 transform->iv_dec,
2375 transform->ivlen,
2376 data, rec->data_len,
2377 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002380 return( ret );
2381 }
2382
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002383 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2386 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002388 }
Paul Bakker68884e32013-01-07 18:20:04 +01002389 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002391#if defined(MBEDTLS_GCM_C) || \
2392 defined(MBEDTLS_CCM_C) || \
2393 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002395 mode == MBEDTLS_MODE_CCM ||
2396 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002397 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002398 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002399 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002400
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002401 /*
2402 * Compute and update sizes
2403 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002404 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002407 "+ taglen (%d)", rec->data_len,
2408 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002410 }
Paul Bakker68884e32013-01-07 18:20:04 +01002411
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002412 /*
2413 * Prepare IV
2414 */
2415 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2416 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002417 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002418 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002419 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002420
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002421 }
2422 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2423 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002424 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002425 unsigned char i;
2426
2427 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2428
2429 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002430 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002431 }
2432 else
2433 {
2434 /* Reminder if we ever add an AEAD mode with a different size */
2435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2436 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2437 }
2438
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002439 data += explicit_iv_len;
2440 rec->data_offset += explicit_iv_len;
2441 rec->data_len -= explicit_iv_len + transform->taglen;
2442
2443 ssl_extract_add_data_from_record( add_data, rec );
2444 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2445 add_data, 13 );
2446
2447 memcpy( transform->iv_dec + transform->fixed_ivlen,
2448 data - explicit_iv_len, explicit_iv_len );
2449
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002450 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002451 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002452 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002453
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002454
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002455 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002456 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002457 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002458 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2459 iv, transform->ivlen,
2460 add_data, 13,
2461 data, rec->data_len,
2462 data, &olen,
2463 data + rec->data_len,
2464 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2469 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002470
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002471 return( ret );
2472 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002473 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002474
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002475 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2478 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002479 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002480 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2483#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002484 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002487 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002488
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 /*
Paul Bakker45829992013-01-03 14:52:21 +01002490 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002493 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2494 {
2495 /* The ciphertext is prefixed with the CBC IV. */
2496 minlen += transform->ivlen;
2497 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002498#endif
Paul Bakker45829992013-01-03 14:52:21 +01002499
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002500 /* Size considerations:
2501 *
2502 * - The CBC cipher text must not be empty and hence
2503 * at least of size transform->ivlen.
2504 *
2505 * Together with the potential IV-prefix, this explains
2506 * the first of the two checks below.
2507 *
2508 * - The record must contain a MAC, either in plain or
2509 * encrypted, depending on whether Encrypt-then-MAC
2510 * is used or not.
2511 * - If it is, the message contains the IV-prefix,
2512 * the CBC ciphertext, and the MAC.
2513 * - If it is not, the padded plaintext, and hence
2514 * the CBC ciphertext, has at least length maclen + 1
2515 * because there is at least the padding length byte.
2516 *
2517 * As the CBC ciphertext is not empty, both cases give the
2518 * lower bound minlen + maclen + 1 on the record size, which
2519 * we test for in the second check below.
2520 */
2521 if( rec->data_len < minlen + transform->ivlen ||
2522 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002525 "+ 1 ) ( + expl IV )", rec->data_len,
2526 transform->ivlen,
2527 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002529 }
2530
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002531 /*
2532 * Authenticate before decrypt if enabled
2533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002535 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002536 {
Hanno Becker992b6872017-11-09 18:57:39 +00002537 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002540
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002541 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2542 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002543
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002544 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002545
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002546 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, 13 );
2547 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2548 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2549 data, rec->data_len );
2550 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2551 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002552
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002553 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2554 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002555 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002556 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002557
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002558 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2559 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002563 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002564 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002565 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002567
2568 /*
2569 * Check length sanity
2570 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002571 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002574 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002576 }
2577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002579 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002580 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002581 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002582 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002583 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002584 /* This is safe because data_len >= minlen + maclen + 1 initially,
2585 * and at this point we have at most subtracted maclen (note that
2586 * minlen == transform->ivlen here). */
2587 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002588
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002589 data += transform->ivlen;
2590 rec->data_offset += transform->ivlen;
2591 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002592 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002594
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002595 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2596 transform->iv_dec, transform->ivlen,
2597 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002600 return( ret );
2601 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002602
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002603 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2606 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002607 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002610 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002611 {
2612 /*
2613 * Save IV in SSL3 and TLS1
2614 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002615 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2616 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002617 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002618#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002620 /* Safe since data_len >= minlen + maclen + 1, so after having
2621 * subtracted at most minlen and maclen up to this point,
2622 * data_len > 0. */
2623 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002624
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002625 if( auth_done == 1 )
2626 {
2627 correct *= ( rec->data_len >= padlen + 1 );
2628 padlen *= ( rec->data_len >= padlen + 1 );
2629 }
2630 else
Paul Bakker45829992013-01-03 14:52:21 +01002631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002633 if( rec->data_len < transform->maclen + padlen + 1 )
2634 {
2635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2636 rec->data_len,
2637 transform->maclen,
2638 padlen + 1 ) );
2639 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002640#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002641
2642 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2643 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002644 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002646 padlen++;
2647
2648 /* Regardless of the validity of the padding,
2649 * we have data_len >= padlen here. */
2650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002652 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002654 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656#if defined(MBEDTLS_SSL_DEBUG_ALL)
2657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002658 "should be no more than %d",
2659 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002660#endif
Paul Bakker45829992013-01-03 14:52:21 +01002661 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 }
2663 }
2664 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2666#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2667 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002668 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002669 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002670 /* The padding check involves a series of up to 256
2671 * consecutive memory reads at the end of the record
2672 * plaintext buffer. In order to hide the length and
2673 * validity of the padding, always perform exactly
2674 * `min(256,plaintext_len)` reads (but take into account
2675 * only the last `padlen` bytes for the padding check). */
2676 size_t pad_count = 0;
2677 size_t real_count = 0;
2678 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002679
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002680 /* Index of first padding byte; it has been ensured above
2681 * that the subtraction is safe. */
2682 size_t const padding_idx = rec->data_len - padlen;
2683 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2684 size_t const start_idx = rec->data_len - num_checks;
2685 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002686
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002687 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002688 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002689 real_count |= ( idx >= padding_idx );
2690 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002691 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002692 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002695 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002697#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002698 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002700 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2702 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2705 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002706 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002707
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002708 /* If the padding was found to be invalid, padlen == 0
2709 * and the subtraction is safe. If the padding was found valid,
2710 * padlen hasn't been changed and the previous assertion
2711 * data_len >= padlen still holds. */
2712 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002714 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002716 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2719 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002720 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002721
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002722#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002724 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002725#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002726
2727 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002728 * Authenticate if not done yet.
2729 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 */
Hanno Becker52344c22018-01-03 15:24:20 +00002731#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002732 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002733 {
Hanno Becker992b6872017-11-09 18:57:39 +00002734 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002735
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002736 /* If the initial value of padlen was such that
2737 * data_len < maclen + padlen + 1, then padlen
2738 * got reset to 1, and the initial check
2739 * data_len >= minlen + maclen + 1
2740 * guarantees that at this point we still
2741 * have at least data_len >= maclen.
2742 *
2743 * If the initial value of padlen was such that
2744 * data_len >= maclen + padlen + 1, then we have
2745 * subtracted either padlen + 1 (if the padding was correct)
2746 * or 0 (if the padding was incorrect) since then,
2747 * hence data_len >= maclen in any case.
2748 */
2749 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002750
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002751 ssl_extract_add_data_from_record( add_data, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002754 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002755 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002756 ssl_mac( &transform->md_ctx_dec,
2757 transform->mac_dec,
2758 data, rec->data_len,
2759 rec->ctr, rec->type,
2760 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002761 }
2762 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2764#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2765 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002766 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002767 {
2768 /*
2769 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002770 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002771 *
2772 * Known timing attacks:
2773 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2774 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002775 * To compensate for different timings for the MAC calculation
2776 * depending on how much padding was removed (which is determined
2777 * by padlen), process extra_run more blocks through the hash
2778 * function.
2779 *
2780 * The formula in the paper is
2781 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2782 * where L1 is the size of the header plus the decrypted message
2783 * plus CBC padding and L2 is the size of the header plus the
2784 * decrypted message. This is for an underlying hash function
2785 * with 64-byte blocks.
2786 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2787 * correctly. We round down instead of up, so -56 is the correct
2788 * value for our calculations instead of -55.
2789 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002790 * Repeat the formula rather than defining a block_size variable.
2791 * This avoids requiring division by a variable at runtime
2792 * (which would be marginally less efficient and would require
2793 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002794 */
2795 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002796 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002797
2798 /*
2799 * The next two sizes are the minimum and maximum values of
2800 * in_msglen over all padlen values.
2801 *
2802 * They're independent of padlen, since we previously did
2803 * in_msglen -= padlen.
2804 *
2805 * Note that max_len + maclen is never more than the buffer
2806 * length, as we previously did in_msglen -= maclen too.
2807 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002808 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002809 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2810
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002811 memset( tmp, 0, sizeof( tmp ) );
2812
2813 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002814 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002815#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2816 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002817 case MBEDTLS_MD_MD5:
2818 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002819 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002820 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002821 extra_run = ( 13 + rec->data_len + padlen + 8 ) / 64 -
2822 ( 13 + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002823 break;
2824#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002825#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002826 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002827 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002828 extra_run = ( 13 + rec->data_len + padlen + 16 ) / 128 -
2829 ( 13 + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002830 break;
2831#endif
2832 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002834 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2835 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002836
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002837 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002838
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002839 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2840 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2841 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002842 /* Make sure we access everything even when padlen > 0. This
2843 * makes the synchronisation requirements for just-in-time
2844 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002845 ssl_read_memory( data + rec->data_len, padlen );
2846 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002847
2848 /* Call mbedtls_md_process at least once due to cache attacks
2849 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002850 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002851 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002852
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002853 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002854
2855 /* Make sure we access all the memory that could contain the MAC,
2856 * before we check it in the next code block. This makes the
2857 * synchronisation requirements for just-in-time Prime+Probe
2858 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002859 ssl_read_memory( data + min_len,
2860 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002861 }
2862 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2864 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002865 {
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é-Gonnard71096242013-10-25 19:31:25 +02002868 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002869
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002870#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002871 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2872 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002873#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002874
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002875 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2876 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878#if defined(MBEDTLS_SSL_DEBUG_ALL)
2879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002880#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002881 correct = 0;
2882 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002883 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002884 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002885
2886 /*
2887 * Finally check the correct flag
2888 */
2889 if( correct == 0 )
2890 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00002891#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002892
2893 /* Make extra sure authentication was performed, exactly once */
2894 if( auth_done != 1 )
2895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2897 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002898 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002901
2902 return( 0 );
2903}
2904
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002905#undef MAC_NONE
2906#undef MAC_PLAINTEXT
2907#undef MAC_CIPHERTEXT
2908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002910/*
2911 * Compression/decompression functions
2912 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002914{
2915 int ret;
2916 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002917 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002918 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002919 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002922
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002923 if( len_pre == 0 )
2924 return( 0 );
2925
Paul Bakker2770fbd2012-07-03 13:30:23 +00002926 memcpy( msg_pre, ssl->out_msg, len_pre );
2927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002929 ssl->out_msglen ) );
2930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002932 ssl->out_msg, ssl->out_msglen );
2933
Paul Bakker48916f92012-09-16 19:57:18 +00002934 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2935 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2936 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002937 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002938
Paul Bakker48916f92012-09-16 19:57:18 +00002939 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002940 if( ret != Z_OK )
2941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2943 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002944 }
2945
Angus Grattond8213d02016-05-25 20:56:48 +10002946 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002947 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002950 ssl->out_msglen ) );
2951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002953 ssl->out_msg, ssl->out_msglen );
2954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002956
2957 return( 0 );
2958}
2959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002961{
2962 int ret;
2963 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002964 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002965 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002966 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002969
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002970 if( len_pre == 0 )
2971 return( 0 );
2972
Paul Bakker2770fbd2012-07-03 13:30:23 +00002973 memcpy( msg_pre, ssl->in_msg, len_pre );
2974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002976 ssl->in_msglen ) );
2977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002979 ssl->in_msg, ssl->in_msglen );
2980
Paul Bakker48916f92012-09-16 19:57:18 +00002981 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2982 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2983 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002984 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002985 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002986
Paul Bakker48916f92012-09-16 19:57:18 +00002987 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002988 if( ret != Z_OK )
2989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2991 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002992 }
2993
Angus Grattond8213d02016-05-25 20:56:48 +10002994 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002995 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002998 ssl->in_msglen ) );
2999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003001 ssl->in_msg, ssl->in_msglen );
3002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003004
3005 return( 0 );
3006}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3010static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012#if defined(MBEDTLS_SSL_PROTO_DTLS)
3013static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003014{
3015 /* If renegotiation is not enforced, retransmit until we would reach max
3016 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003017 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003018 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003019 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003020 unsigned char doublings = 1;
3021
3022 while( ratio != 0 )
3023 {
3024 ++doublings;
3025 ratio >>= 1;
3026 }
3027
3028 if( ++ssl->renego_records_seen > doublings )
3029 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003031 return( 0 );
3032 }
3033 }
3034
3035 return( ssl_write_hello_request( ssl ) );
3036}
3037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003039
Paul Bakker5121ce52009-01-03 21:22:43 +00003040/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003041 * Fill the input message buffer by appending data to it.
3042 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003043 *
3044 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3045 * available (from this read and/or a previous one). Otherwise, an error code
3046 * is returned (possibly EOF or WANT_READ).
3047 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003048 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3049 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3050 * since we always read a whole datagram at once.
3051 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003052 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003053 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003056{
Paul Bakker23986e52011-04-24 08:57:21 +00003057 int ret;
3058 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003061
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003062 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003065 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003067 }
3068
Angus Grattond8213d02016-05-25 20:56:48 +10003069 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3072 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003073 }
3074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003076 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003077 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003078 uint32_t timeout;
3079
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003080 /* Just to be sure */
3081 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3082 {
3083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3084 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3085 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3086 }
3087
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003088 /*
3089 * The point is, we need to always read a full datagram at once, so we
3090 * sometimes read more then requested, and handle the additional data.
3091 * It could be the rest of the current record (while fetching the
3092 * header) and/or some other records in the same datagram.
3093 */
3094
3095 /*
3096 * Move to the next record in the already read datagram if applicable
3097 */
3098 if( ssl->next_record_offset != 0 )
3099 {
3100 if( ssl->in_left < ssl->next_record_offset )
3101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3103 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003104 }
3105
3106 ssl->in_left -= ssl->next_record_offset;
3107
3108 if( ssl->in_left != 0 )
3109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003111 ssl->next_record_offset ) );
3112 memmove( ssl->in_hdr,
3113 ssl->in_hdr + ssl->next_record_offset,
3114 ssl->in_left );
3115 }
3116
3117 ssl->next_record_offset = 0;
3118 }
3119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003121 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003122
3123 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003124 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003125 */
3126 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003129 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003130 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003131
3132 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003133 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003134 * are not at the beginning of a new record, the caller did something
3135 * wrong.
3136 */
3137 if( ssl->in_left != 0 )
3138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3140 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003141 }
3142
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003143 /*
3144 * Don't even try to read if time's out already.
3145 * This avoids by-passing the timer when repeatedly receiving messages
3146 * that will end up being dropped.
3147 */
3148 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003149 {
3150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003151 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003152 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003153 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003154 {
Angus Grattond8213d02016-05-25 20:56:48 +10003155 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003158 timeout = ssl->handshake->retransmit_timeout;
3159 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003160 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003163
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003164 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003165 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3166 timeout );
3167 else
3168 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003171
3172 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003174 }
3175
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003176 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003179 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003182 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003183 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003186 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003187 }
3188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003192 return( ret );
3193 }
3194
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003195 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003196 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003198 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003200 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003201 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003204 return( ret );
3205 }
3206
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003207 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003208 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003210 }
3211
Paul Bakker5121ce52009-01-03 21:22:43 +00003212 if( ret < 0 )
3213 return( ret );
3214
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003215 ssl->in_left = ret;
3216 }
3217 else
3218#endif
3219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003221 ssl->in_left, nb_want ) );
3222
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003223 while( ssl->in_left < nb_want )
3224 {
3225 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003226
3227 if( ssl_check_timer( ssl ) != 0 )
3228 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3229 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003230 {
3231 if( ssl->f_recv_timeout != NULL )
3232 {
3233 ret = ssl->f_recv_timeout( ssl->p_bio,
3234 ssl->in_hdr + ssl->in_left, len,
3235 ssl->conf->read_timeout );
3236 }
3237 else
3238 {
3239 ret = ssl->f_recv( ssl->p_bio,
3240 ssl->in_hdr + ssl->in_left, len );
3241 }
3242 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003245 ssl->in_left, nb_want ) );
3246 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003247
3248 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003250
3251 if( ret < 0 )
3252 return( ret );
3253
mohammad160352aecb92018-03-28 23:41:40 -07003254 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003255 {
Darryl Green11999bb2018-03-13 15:22:58 +00003256 MBEDTLS_SSL_DEBUG_MSG( 1,
3257 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003258 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003259 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3260 }
3261
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003262 ssl->in_left += ret;
3263 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003264 }
3265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003267
3268 return( 0 );
3269}
3270
3271/*
3272 * Flush any data not yet written
3273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003274int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003275{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003276 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003277 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003280
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003281 if( ssl->f_send == NULL )
3282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003284 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003285 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003286 }
3287
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003288 /* Avoid incrementing counter if data is flushed */
3289 if( ssl->out_left == 0 )
3290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003292 return( 0 );
3293 }
3294
Paul Bakker5121ce52009-01-03 21:22:43 +00003295 while( ssl->out_left > 0 )
3296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3298 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003299
Hanno Becker2b1e3542018-08-06 11:19:13 +01003300 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003301 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003304
3305 if( ret <= 0 )
3306 return( ret );
3307
mohammad160352aecb92018-03-28 23:41:40 -07003308 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003309 {
Darryl Green11999bb2018-03-13 15:22:58 +00003310 MBEDTLS_SSL_DEBUG_MSG( 1,
3311 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003312 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003313 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3314 }
3315
Paul Bakker5121ce52009-01-03 21:22:43 +00003316 ssl->out_left -= ret;
3317 }
3318
Hanno Becker2b1e3542018-08-06 11:19:13 +01003319#if defined(MBEDTLS_SSL_PROTO_DTLS)
3320 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003321 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003322 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003323 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003324 else
3325#endif
3326 {
3327 ssl->out_hdr = ssl->out_buf + 8;
3328 }
3329 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003332
3333 return( 0 );
3334}
3335
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003336/*
3337 * Functions to handle the DTLS retransmission state machine
3338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003340/*
3341 * Append current handshake message to current outgoing flight
3342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003344{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3347 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3348 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003349
3350 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003351 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003352 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003355 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003356 }
3357
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003358 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003359 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003362 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003363 }
3364
3365 /* Copy current handshake message with headers */
3366 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3367 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003368 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003369 msg->next = NULL;
3370
3371 /* Append to the current flight */
3372 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003373 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003374 else
3375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003377 while( cur->next != NULL )
3378 cur = cur->next;
3379 cur->next = msg;
3380 }
3381
Hanno Becker3b235902018-08-06 09:54:53 +01003382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003383 return( 0 );
3384}
3385
3386/*
3387 * Free the current flight of handshake messages
3388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003390{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 mbedtls_ssl_flight_item *cur = flight;
3392 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003393
3394 while( cur != NULL )
3395 {
3396 next = cur->next;
3397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 mbedtls_free( cur->p );
3399 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003400
3401 cur = next;
3402 }
3403}
3404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3406static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003407#endif
3408
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003409/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003410 * Swap transform_out and out_ctr with the alternative ones
3411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003413{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003415 unsigned char tmp_out_ctr[8];
3416
3417 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003420 return;
3421 }
3422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003424
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003425 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003426 tmp_transform = ssl->transform_out;
3427 ssl->transform_out = ssl->handshake->alt_transform_out;
3428 ssl->handshake->alt_transform_out = tmp_transform;
3429
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003430 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003431 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3432 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003433 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003434
3435 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003436 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3439 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3444 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003445 }
3446 }
3447#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003448}
3449
3450/*
3451 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003452 */
3453int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3454{
3455 int ret = 0;
3456
3457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3458
3459 ret = mbedtls_ssl_flight_transmit( ssl );
3460
3461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3462
3463 return( ret );
3464}
3465
3466/*
3467 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003468 *
3469 * Need to remember the current message in case flush_output returns
3470 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003471 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003472 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003473int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003474{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003475 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003479 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003481
3482 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003483 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003484 ssl_swap_epochs( ssl );
3485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003487 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003488
3489 while( ssl->handshake->cur_msg != NULL )
3490 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003491 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003492 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003493
Hanno Beckere1dcb032018-08-17 16:47:58 +01003494 int const is_finished =
3495 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3496 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3497
Hanno Becker04da1892018-08-14 13:22:10 +01003498 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3499 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3500
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003501 /* Swap epochs before sending Finished: we can't do it after
3502 * sending ChangeCipherSpec, in case write returns WANT_READ.
3503 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003504 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003505 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003507 ssl_swap_epochs( ssl );
3508 }
3509
Hanno Becker67bc7c32018-08-06 11:33:50 +01003510 ret = ssl_get_remaining_payload_in_datagram( ssl );
3511 if( ret < 0 )
3512 return( ret );
3513 max_frag_len = (size_t) ret;
3514
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003515 /* CCS is copied as is, while HS messages may need fragmentation */
3516 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3517 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003518 if( max_frag_len == 0 )
3519 {
3520 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3521 return( ret );
3522
3523 continue;
3524 }
3525
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003526 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003527 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003528 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003529
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003530 /* Update position inside current message */
3531 ssl->handshake->cur_msg_p += cur->len;
3532 }
3533 else
3534 {
3535 const unsigned char * const p = ssl->handshake->cur_msg_p;
3536 const size_t hs_len = cur->len - 12;
3537 const size_t frag_off = p - ( cur->p + 12 );
3538 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003539 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003540
Hanno Beckere1dcb032018-08-17 16:47:58 +01003541 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003542 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003543 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003544 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003545
Hanno Becker67bc7c32018-08-06 11:33:50 +01003546 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3547 return( ret );
3548
3549 continue;
3550 }
3551 max_hs_frag_len = max_frag_len - 12;
3552
3553 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3554 max_hs_frag_len : rem_len;
3555
3556 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003557 {
3558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003559 (unsigned) cur_hs_frag_len,
3560 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003561 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003562
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003563 /* Messages are stored with handshake headers as if not fragmented,
3564 * copy beginning of headers then fill fragmentation fields.
3565 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3566 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003567
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003568 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3569 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3570 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3571
Hanno Becker67bc7c32018-08-06 11:33:50 +01003572 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3573 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3574 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003575
3576 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3577
Hanno Becker3f7b9732018-08-28 09:53:25 +01003578 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003579 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3580 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003581 ssl->out_msgtype = cur->type;
3582
3583 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003584 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003585 }
3586
3587 /* If done with the current message move to the next one if any */
3588 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3589 {
3590 if( cur->next != NULL )
3591 {
3592 ssl->handshake->cur_msg = cur->next;
3593 ssl->handshake->cur_msg_p = cur->next->p + 12;
3594 }
3595 else
3596 {
3597 ssl->handshake->cur_msg = NULL;
3598 ssl->handshake->cur_msg_p = NULL;
3599 }
3600 }
3601
3602 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003603 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003606 return( ret );
3607 }
3608 }
3609
Hanno Becker67bc7c32018-08-06 11:33:50 +01003610 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3611 return( ret );
3612
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003613 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3615 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003616 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003619 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3620 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003621
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003623
3624 return( 0 );
3625}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003626
3627/*
3628 * To be called when the last message of an incoming flight is received.
3629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003631{
3632 /* We won't need to resend that one any more */
3633 ssl_flight_free( ssl->handshake->flight );
3634 ssl->handshake->flight = NULL;
3635 ssl->handshake->cur_msg = NULL;
3636
3637 /* The next incoming flight will start with this msg_seq */
3638 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3639
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003640 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003641 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003642
Hanno Becker0271f962018-08-16 13:23:47 +01003643 /* Clear future message buffering structure. */
3644 ssl_buffering_free( ssl );
3645
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003646 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003647 ssl_set_timer( ssl, 0 );
3648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3650 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003653 }
3654 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003656}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003657
3658/*
3659 * To be called when the last message of an outgoing flight is send.
3660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003662{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003663 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003664 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3667 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003670 }
3671 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003673}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003675
Paul Bakker5121ce52009-01-03 21:22:43 +00003676/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003677 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003678 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003679
3680/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003681 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003682 *
3683 * - fill in handshake headers
3684 * - update handshake checksum
3685 * - DTLS: save message for resending
3686 * - then pass to the record layer
3687 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003688 * DTLS: except for HelloRequest, messages are only queued, and will only be
3689 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003690 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003691 * Inputs:
3692 * - ssl->out_msglen: 4 + actual handshake message len
3693 * (4 is the size of handshake headers for TLS)
3694 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3695 * - ssl->out_msg + 4: the handshake message body
3696 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003697 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003698 * - ssl->out_msglen: the length of the record contents
3699 * (including handshake headers but excluding record headers)
3700 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003701 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003702int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003703{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003704 int ret;
3705 const size_t hs_len = ssl->out_msglen - 4;
3706 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003707
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3709
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003710 /*
3711 * Sanity checks
3712 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003713 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003714 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3715 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003716 /* In SSLv3, the client might send a NoCertificate alert. */
3717#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3718 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3719 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3720 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3721#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3722 {
3723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3724 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3725 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003726 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003727
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003728 /* Whenever we send anything different from a
3729 * HelloRequest we should be in a handshake - double check. */
3730 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3731 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003732 ssl->handshake == NULL )
3733 {
3734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3735 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3736 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003739 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003740 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003742 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3744 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003745 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003746#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003747
Hanno Beckerb50a2532018-08-06 11:52:54 +01003748 /* Double-check that we did not exceed the bounds
3749 * of the outgoing record buffer.
3750 * This should never fail as the various message
3751 * writing functions must obey the bounds of the
3752 * outgoing record buffer, but better be safe.
3753 *
3754 * Note: We deliberately do not check for the MTU or MFL here.
3755 */
3756 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3757 {
3758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3759 "size %u, maximum %u",
3760 (unsigned) ssl->out_msglen,
3761 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3762 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3763 }
3764
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003765 /*
3766 * Fill handshake headers
3767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003768 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003769 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003770 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3771 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3772 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003773
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003774 /*
3775 * DTLS has additional fields in the Handshake layer,
3776 * between the length field and the actual payload:
3777 * uint16 message_seq;
3778 * uint24 fragment_offset;
3779 * uint24 fragment_length;
3780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003781#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003782 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003783 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003784 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003785 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003786 {
3787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3788 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003789 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003790 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003791 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3792 }
3793
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003794 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003795 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003796
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003797 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003798 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003799 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003800 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3801 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3802 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003803 }
3804 else
3805 {
3806 ssl->out_msg[4] = 0;
3807 ssl->out_msg[5] = 0;
3808 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003809
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003810 /* Handshake hashes are computed without fragmentation,
3811 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003812 memset( ssl->out_msg + 6, 0x00, 3 );
3813 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003814 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003815#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003816
Hanno Becker0207e532018-08-28 10:28:28 +01003817 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003818 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3819 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003820 }
3821
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003822 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003823#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003824 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003825 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3826 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003827 {
3828 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003831 return( ret );
3832 }
3833 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003834 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003835#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003836 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003837 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003838 {
3839 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3840 return( ret );
3841 }
3842 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003843
3844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3845
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003846 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003847}
3848
3849/*
3850 * Record layer functions
3851 */
3852
3853/*
3854 * Write current record.
3855 *
3856 * Uses:
3857 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3858 * - ssl->out_msglen: length of the record content (excl headers)
3859 * - ssl->out_msg: record content
3860 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003861int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003862{
3863 int ret, done = 0;
3864 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003865 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003866
3867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003869#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003870 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003871 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003872 {
3873 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003875 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003876 return( ret );
3877 }
3878
3879 len = ssl->out_msglen;
3880 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3884 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003888 ret = mbedtls_ssl_hw_record_write( ssl );
3889 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003891 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3892 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003893 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003894
3895 if( ret == 0 )
3896 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003897 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003899 if( !done )
3900 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003901 unsigned i;
3902 size_t protected_record_size;
3903
Paul Bakker05ef8352012-05-08 09:17:57 +00003904 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003906 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003907
Hanno Becker19859472018-08-06 09:40:20 +01003908 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003909 ssl->out_len[0] = (unsigned char)( len >> 8 );
3910 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003911
Paul Bakker48916f92012-09-16 19:57:18 +00003912 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003913 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003914 mbedtls_record rec;
3915
3916 rec.buf = ssl->out_iv;
3917 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3918 ( ssl->out_iv - ssl->out_buf );
3919 rec.data_len = ssl->out_msglen;
3920 rec.data_offset = ssl->out_msg - rec.buf;
3921
3922 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3923 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3924 ssl->conf->transport, rec.ver );
3925 rec.type = ssl->out_msgtype;
3926
Hanno Beckera18d1322018-01-03 14:27:32 +00003927 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003928 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003930 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003931 return( ret );
3932 }
3933
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003934 if( rec.data_offset != 0 )
3935 {
3936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3937 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3938 }
3939
Hanno Becker78f839d2019-03-14 12:56:23 +00003940 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003941 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3942 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003943 }
3944
Hanno Becker2b1e3542018-08-06 11:19:13 +01003945 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3946
3947#if defined(MBEDTLS_SSL_PROTO_DTLS)
3948 /* In case of DTLS, double-check that we don't exceed
3949 * the remaining space in the datagram. */
3950 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3951 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003952 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003953 if( ret < 0 )
3954 return( ret );
3955
3956 if( protected_record_size > (size_t) ret )
3957 {
3958 /* Should never happen */
3959 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3960 }
3961 }
3962#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003965 "version = [%d:%d], msglen = %d",
3966 ssl->out_hdr[0], ssl->out_hdr[1],
3967 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003970 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003971
3972 ssl->out_left += protected_record_size;
3973 ssl->out_hdr += protected_record_size;
3974 ssl_update_out_pointers( ssl, ssl->transform_out );
3975
Hanno Becker04484622018-08-06 09:49:38 +01003976 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3977 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3978 break;
3979
3980 /* The loop goes to its end iff the counter is wrapping */
3981 if( i == ssl_ep_len( ssl ) )
3982 {
3983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3984 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3985 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003986 }
3987
Hanno Becker67bc7c32018-08-06 11:33:50 +01003988#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01003989 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3990 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003991 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003992 size_t remaining;
3993 ret = ssl_get_remaining_payload_in_datagram( ssl );
3994 if( ret < 0 )
3995 {
3996 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3997 ret );
3998 return( ret );
3999 }
4000
4001 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004002 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004003 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004004 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004005 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004006 else
4007 {
Hanno Becker513815a2018-08-20 11:56:09 +01004008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004009 }
4010 }
4011#endif /* MBEDTLS_SSL_PROTO_DTLS */
4012
4013 if( ( flush == SSL_FORCE_FLUSH ) &&
4014 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004017 return( ret );
4018 }
4019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004021
4022 return( 0 );
4023}
4024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004025#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004026
4027static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4028{
4029 if( ssl->in_msglen < ssl->in_hslen ||
4030 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4031 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4032 {
4033 return( 1 );
4034 }
4035 return( 0 );
4036}
Hanno Becker44650b72018-08-16 12:51:11 +01004037
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004038static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004039{
4040 return( ( ssl->in_msg[9] << 16 ) |
4041 ( ssl->in_msg[10] << 8 ) |
4042 ssl->in_msg[11] );
4043}
4044
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004045static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004046{
4047 return( ( ssl->in_msg[6] << 16 ) |
4048 ( ssl->in_msg[7] << 8 ) |
4049 ssl->in_msg[8] );
4050}
4051
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004052static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004053{
4054 uint32_t msg_len, frag_off, frag_len;
4055
4056 msg_len = ssl_get_hs_total_len( ssl );
4057 frag_off = ssl_get_hs_frag_off( ssl );
4058 frag_len = ssl_get_hs_frag_len( ssl );
4059
4060 if( frag_off > msg_len )
4061 return( -1 );
4062
4063 if( frag_len > msg_len - frag_off )
4064 return( -1 );
4065
4066 if( frag_len + 12 > ssl->in_msglen )
4067 return( -1 );
4068
4069 return( 0 );
4070}
4071
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004072/*
4073 * Mark bits in bitmask (used for DTLS HS reassembly)
4074 */
4075static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4076{
4077 unsigned int start_bits, end_bits;
4078
4079 start_bits = 8 - ( offset % 8 );
4080 if( start_bits != 8 )
4081 {
4082 size_t first_byte_idx = offset / 8;
4083
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004084 /* Special case */
4085 if( len <= start_bits )
4086 {
4087 for( ; len != 0; len-- )
4088 mask[first_byte_idx] |= 1 << ( start_bits - len );
4089
4090 /* Avoid potential issues with offset or len becoming invalid */
4091 return;
4092 }
4093
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004094 offset += start_bits; /* Now offset % 8 == 0 */
4095 len -= start_bits;
4096
4097 for( ; start_bits != 0; start_bits-- )
4098 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4099 }
4100
4101 end_bits = len % 8;
4102 if( end_bits != 0 )
4103 {
4104 size_t last_byte_idx = ( offset + len ) / 8;
4105
4106 len -= end_bits; /* Now len % 8 == 0 */
4107
4108 for( ; end_bits != 0; end_bits-- )
4109 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4110 }
4111
4112 memset( mask + offset / 8, 0xFF, len / 8 );
4113}
4114
4115/*
4116 * Check that bitmask is full
4117 */
4118static int ssl_bitmask_check( unsigned char *mask, size_t len )
4119{
4120 size_t i;
4121
4122 for( i = 0; i < len / 8; i++ )
4123 if( mask[i] != 0xFF )
4124 return( -1 );
4125
4126 for( i = 0; i < len % 8; i++ )
4127 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4128 return( -1 );
4129
4130 return( 0 );
4131}
4132
Hanno Becker56e205e2018-08-16 09:06:12 +01004133/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004134static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004135 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004136{
Hanno Becker56e205e2018-08-16 09:06:12 +01004137 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004138
Hanno Becker56e205e2018-08-16 09:06:12 +01004139 alloc_len = 12; /* Handshake header */
4140 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004141
Hanno Beckerd07df862018-08-16 09:14:58 +01004142 if( add_bitmap )
4143 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004144
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004145 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004146}
Hanno Becker56e205e2018-08-16 09:06:12 +01004147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004148#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004149
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004150static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004151{
4152 return( ( ssl->in_msg[1] << 16 ) |
4153 ( ssl->in_msg[2] << 8 ) |
4154 ssl->in_msg[3] );
4155}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004156
Simon Butcher99000142016-10-13 17:21:01 +01004157int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004158{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004159 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004162 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004163 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004164 }
4165
Hanno Becker12555c62018-08-16 12:47:53 +01004166 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004168 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004169 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004170 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004172#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004173 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004174 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004175 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004176 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004177
Hanno Becker44650b72018-08-16 12:51:11 +01004178 if( ssl_check_hs_header( ssl ) != 0 )
4179 {
4180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4181 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4182 }
4183
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004184 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004185 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4186 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4187 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4188 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004189 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004190 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4191 {
4192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4193 recv_msg_seq,
4194 ssl->handshake->in_msg_seq ) );
4195 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4196 }
4197
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004198 /* Retransmit only on last message from previous flight, to avoid
4199 * too many retransmissions.
4200 * Besides, No sane server ever retransmits HelloVerifyRequest */
4201 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004202 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004205 "message_seq = %d, start_of_flight = %d",
4206 recv_msg_seq,
4207 ssl->handshake->in_flight_start_seq ) );
4208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004209 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004211 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004212 return( ret );
4213 }
4214 }
4215 else
4216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004218 "message_seq = %d, expected = %d",
4219 recv_msg_seq,
4220 ssl->handshake->in_msg_seq ) );
4221 }
4222
Hanno Becker90333da2017-10-10 11:27:13 +01004223 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004224 }
4225 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004226
Hanno Becker6d97ef52018-08-16 13:09:04 +01004227 /* Message reassembly is handled alongside buffering of future
4228 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004229 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004230 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004231 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004234 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004235 }
4236 }
4237 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004238#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004239 /* With TLS we don't handle fragmentation (for now) */
4240 if( ssl->in_msglen < ssl->in_hslen )
4241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4243 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004244 }
4245
Simon Butcher99000142016-10-13 17:21:01 +01004246 return( 0 );
4247}
4248
4249void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4250{
Hanno Becker0271f962018-08-16 13:23:47 +01004251 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004252
Hanno Becker0271f962018-08-16 13:23:47 +01004253 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004254 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004255 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004256 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004257
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004258 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004260 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004261 ssl->handshake != NULL )
4262 {
Hanno Becker0271f962018-08-16 13:23:47 +01004263 unsigned offset;
4264 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004265
Hanno Becker0271f962018-08-16 13:23:47 +01004266 /* Increment handshake sequence number */
4267 hs->in_msg_seq++;
4268
4269 /*
4270 * Clear up handshake buffering and reassembly structure.
4271 */
4272
4273 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004274 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004275
4276 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004277 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4278 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004279 offset++, hs_buf++ )
4280 {
4281 *hs_buf = *(hs_buf + 1);
4282 }
4283
4284 /* Create a fresh last entry */
4285 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004286 }
4287#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004288}
4289
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004290/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004291 * DTLS anti-replay: RFC 6347 4.1.2.6
4292 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004293 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4294 * Bit n is set iff record number in_window_top - n has been seen.
4295 *
4296 * Usually, in_window_top is the last record number seen and the lsb of
4297 * in_window is set. The only exception is the initial state (record number 0
4298 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004300#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4301static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004302{
4303 ssl->in_window_top = 0;
4304 ssl->in_window = 0;
4305}
4306
4307static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4308{
4309 return( ( (uint64_t) buf[0] << 40 ) |
4310 ( (uint64_t) buf[1] << 32 ) |
4311 ( (uint64_t) buf[2] << 24 ) |
4312 ( (uint64_t) buf[3] << 16 ) |
4313 ( (uint64_t) buf[4] << 8 ) |
4314 ( (uint64_t) buf[5] ) );
4315}
4316
4317/*
4318 * Return 0 if sequence number is acceptable, -1 otherwise
4319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004320int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004321{
4322 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4323 uint64_t bit;
4324
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004325 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004326 return( 0 );
4327
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004328 if( rec_seqnum > ssl->in_window_top )
4329 return( 0 );
4330
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004331 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004332
4333 if( bit >= 64 )
4334 return( -1 );
4335
4336 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4337 return( -1 );
4338
4339 return( 0 );
4340}
4341
4342/*
4343 * Update replay window on new validated record
4344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004345void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004346{
4347 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4348
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004349 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004350 return;
4351
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004352 if( rec_seqnum > ssl->in_window_top )
4353 {
4354 /* Update window_top and the contents of the window */
4355 uint64_t shift = rec_seqnum - ssl->in_window_top;
4356
4357 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004358 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004359 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004360 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004361 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004362 ssl->in_window |= 1;
4363 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004364
4365 ssl->in_window_top = rec_seqnum;
4366 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004367 else
4368 {
4369 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004370 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004371
4372 if( bit < 64 ) /* Always true, but be extra sure */
4373 ssl->in_window |= (uint64_t) 1 << bit;
4374 }
4375}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004376#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004377
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004378#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004379/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004380static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4381
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004382/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004383 * Without any SSL context, check if a datagram looks like a ClientHello with
4384 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004385 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004386 *
4387 * - if cookie is valid, return 0
4388 * - if ClientHello looks superficially valid but cookie is not,
4389 * fill obuf and set olen, then
4390 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4391 * - otherwise return a specific error code
4392 */
4393static int ssl_check_dtls_clihlo_cookie(
4394 mbedtls_ssl_cookie_write_t *f_cookie_write,
4395 mbedtls_ssl_cookie_check_t *f_cookie_check,
4396 void *p_cookie,
4397 const unsigned char *cli_id, size_t cli_id_len,
4398 const unsigned char *in, size_t in_len,
4399 unsigned char *obuf, size_t buf_len, size_t *olen )
4400{
4401 size_t sid_len, cookie_len;
4402 unsigned char *p;
4403
4404 if( f_cookie_write == NULL || f_cookie_check == NULL )
4405 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4406
4407 /*
4408 * Structure of ClientHello with record and handshake headers,
4409 * and expected values. We don't need to check a lot, more checks will be
4410 * done when actually parsing the ClientHello - skipping those checks
4411 * avoids code duplication and does not make cookie forging any easier.
4412 *
4413 * 0-0 ContentType type; copied, must be handshake
4414 * 1-2 ProtocolVersion version; copied
4415 * 3-4 uint16 epoch; copied, must be 0
4416 * 5-10 uint48 sequence_number; copied
4417 * 11-12 uint16 length; (ignored)
4418 *
4419 * 13-13 HandshakeType msg_type; (ignored)
4420 * 14-16 uint24 length; (ignored)
4421 * 17-18 uint16 message_seq; copied
4422 * 19-21 uint24 fragment_offset; copied, must be 0
4423 * 22-24 uint24 fragment_length; (ignored)
4424 *
4425 * 25-26 ProtocolVersion client_version; (ignored)
4426 * 27-58 Random random; (ignored)
4427 * 59-xx SessionID session_id; 1 byte len + sid_len content
4428 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4429 * ...
4430 *
4431 * Minimum length is 61 bytes.
4432 */
4433 if( in_len < 61 ||
4434 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4435 in[3] != 0 || in[4] != 0 ||
4436 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4437 {
4438 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4439 }
4440
4441 sid_len = in[59];
4442 if( sid_len > in_len - 61 )
4443 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4444
4445 cookie_len = in[60 + sid_len];
4446 if( cookie_len > in_len - 60 )
4447 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4448
4449 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4450 cli_id, cli_id_len ) == 0 )
4451 {
4452 /* Valid cookie */
4453 return( 0 );
4454 }
4455
4456 /*
4457 * If we get here, we've got an invalid cookie, let's prepare HVR.
4458 *
4459 * 0-0 ContentType type; copied
4460 * 1-2 ProtocolVersion version; copied
4461 * 3-4 uint16 epoch; copied
4462 * 5-10 uint48 sequence_number; copied
4463 * 11-12 uint16 length; olen - 13
4464 *
4465 * 13-13 HandshakeType msg_type; hello_verify_request
4466 * 14-16 uint24 length; olen - 25
4467 * 17-18 uint16 message_seq; copied
4468 * 19-21 uint24 fragment_offset; copied
4469 * 22-24 uint24 fragment_length; olen - 25
4470 *
4471 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4472 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4473 *
4474 * Minimum length is 28.
4475 */
4476 if( buf_len < 28 )
4477 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4478
4479 /* Copy most fields and adapt others */
4480 memcpy( obuf, in, 25 );
4481 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4482 obuf[25] = 0xfe;
4483 obuf[26] = 0xff;
4484
4485 /* Generate and write actual cookie */
4486 p = obuf + 28;
4487 if( f_cookie_write( p_cookie,
4488 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4489 {
4490 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4491 }
4492
4493 *olen = p - obuf;
4494
4495 /* Go back and fill length fields */
4496 obuf[27] = (unsigned char)( *olen - 28 );
4497
4498 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4499 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4500 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4501
4502 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4503 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4504
4505 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4506}
4507
4508/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004509 * Handle possible client reconnect with the same UDP quadruplet
4510 * (RFC 6347 Section 4.2.8).
4511 *
4512 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4513 * that looks like a ClientHello.
4514 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004515 * - if the input looks like a ClientHello without cookies,
4516 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004517 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004518 * - if the input looks like a ClientHello with a valid cookie,
4519 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004520 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004521 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004522 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004523 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004524 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4525 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004526 */
4527static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4528{
4529 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004530 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004531
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004532 ret = ssl_check_dtls_clihlo_cookie(
4533 ssl->conf->f_cookie_write,
4534 ssl->conf->f_cookie_check,
4535 ssl->conf->p_cookie,
4536 ssl->cli_id, ssl->cli_id_len,
4537 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004538 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004539
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004540 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4541
4542 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004543 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004544 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004545 * If the error is permanent we'll catch it later,
4546 * if it's not, then hopefully it'll work next time. */
4547 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4548
4549 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004550 }
4551
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004552 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004553 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004554 /* Got a valid cookie, partially reset context */
4555 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4556 {
4557 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4558 return( ret );
4559 }
4560
4561 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004562 }
4563
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004564 return( ret );
4565}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004566#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004567
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004568/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004569 * ContentType type;
4570 * ProtocolVersion version;
4571 * uint16 epoch; // DTLS only
4572 * uint48 sequence_number; // DTLS only
4573 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004574 *
4575 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004576 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004577 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4578 *
4579 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004580 * 1. proceed with the record if this function returns 0
4581 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4582 * 3. return CLIENT_RECONNECT if this function return that value
4583 * 4. drop the whole datagram if this function returns anything else.
4584 * Point 2 is needed when the peer is resending, and we have already received
4585 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004587static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004588{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004589 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004591 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 +02004592
Paul Bakker5121ce52009-01-03 21:22:43 +00004593 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004594 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004595 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004597 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004598 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004599 ssl->in_msgtype,
4600 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004601
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004602 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004603 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4604 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4605 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4606 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004609
4610#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004611 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4612 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004613 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4614#endif /* MBEDTLS_SSL_PROTO_DTLS */
4615 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4616 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004618 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004619 }
4620
4621 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004622 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4625 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004626 }
4627
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004628 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4631 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004632 }
4633
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004634 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004635 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004636 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4639 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004640 }
4641
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004642 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004643 * DTLS-related tests.
4644 * Check epoch before checking length constraint because
4645 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4646 * message gets duplicated before the corresponding Finished message,
4647 * the second ChangeCipherSpec should be discarded because it belongs
4648 * to an old epoch, but not because its length is shorter than
4649 * the minimum record length for packets using the new record transform.
4650 * Note that these two kinds of failures are handled differently,
4651 * as an unexpected record is silently skipped but an invalid
4652 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004653 */
4654#if defined(MBEDTLS_SSL_PROTO_DTLS)
4655 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4656 {
4657 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4658
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004659 /* Check epoch (and sequence number) with DTLS */
4660 if( rec_epoch != ssl->in_epoch )
4661 {
4662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4663 "expected %d, received %d",
4664 ssl->in_epoch, rec_epoch ) );
4665
4666#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4667 /*
4668 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4669 * access the first byte of record content (handshake type), as we
4670 * have an active transform (possibly iv_len != 0), so use the
4671 * fact that the record header len is 13 instead.
4672 */
4673 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4674 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4675 rec_epoch == 0 &&
4676 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4677 ssl->in_left > 13 &&
4678 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4679 {
4680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4681 "from the same port" ) );
4682 return( ssl_handle_possible_reconnect( ssl ) );
4683 }
4684 else
4685#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004686 {
4687 /* Consider buffering the record. */
4688 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4689 {
4690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4691 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4692 }
4693
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004694 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004695 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004696 }
4697
4698#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4699 /* Replay detection only works for the current epoch */
4700 if( rec_epoch == ssl->in_epoch &&
4701 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4702 {
4703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4704 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4705 }
4706#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004707
Hanno Becker52c6dc62017-05-26 16:07:36 +01004708 /* Drop unexpected ApplicationData records,
4709 * except at the beginning of renegotiations */
4710 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4711 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4712#if defined(MBEDTLS_SSL_RENEGOTIATION)
4713 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4714 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4715#endif
4716 )
4717 {
4718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4719 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4720 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004721 }
4722#endif /* MBEDTLS_SSL_PROTO_DTLS */
4723
Hanno Becker52c6dc62017-05-26 16:07:36 +01004724
4725 /* Check length against bounds of the current transform and version */
4726 if( ssl->transform_in == NULL )
4727 {
4728 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004729 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004730 {
4731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4732 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4733 }
4734 }
4735 else
4736 {
4737 if( ssl->in_msglen < ssl->transform_in->minlen )
4738 {
4739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4740 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4741 }
4742
4743#if defined(MBEDTLS_SSL_PROTO_SSL3)
4744 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004745 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004746 {
4747 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4748 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4749 }
4750#endif
4751#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4752 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4753 /*
4754 * TLS encrypted messages can have up to 256 bytes of padding
4755 */
4756 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4757 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004758 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004759 {
4760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4761 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4762 }
4763#endif
4764 }
4765
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004766 return( 0 );
4767}
Paul Bakker5121ce52009-01-03 21:22:43 +00004768
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004769/*
4770 * If applicable, decrypt (and decompress) record content
4771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004772static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004773{
4774 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004776 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4777 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004779#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4780 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004784 ret = mbedtls_ssl_hw_record_read( ssl );
4785 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004787 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4788 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004789 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004790
4791 if( ret == 0 )
4792 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004793 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004794#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004795 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004796 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004797 mbedtls_record rec;
4798
4799 rec.buf = ssl->in_iv;
4800 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4801 - ( ssl->in_iv - ssl->in_buf );
4802 rec.data_len = ssl->in_msglen;
4803 rec.data_offset = 0;
4804
4805 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4806 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4807 ssl->conf->transport, rec.ver );
4808 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00004809 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4810 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004812 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004813 return( ret );
4814 }
4815
Hanno Becker29800d22018-08-07 14:30:18 +01004816 if( ssl->in_iv + rec.data_offset != ssl->in_msg )
4817 {
4818 /* Should never happen */
4819 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4820 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004821
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004822 ssl->in_msglen = rec.data_len;
4823 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4824 ssl->in_len[1] = (unsigned char)( rec.data_len );
4825
Hanno Becker1c0c37f2018-08-07 14:29:29 +01004826 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4827 ssl->in_msg, ssl->in_msglen );
4828
Angus Grattond8213d02016-05-25 20:56:48 +10004829 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4832 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004833 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004834 else if( ssl->in_msglen == 0 )
4835 {
4836#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4837 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4838 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4839 {
4840 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4842 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4843 }
4844#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4845
4846 ssl->nb_zero++;
4847
4848 /*
4849 * Three or more empty messages may be a DoS attack
4850 * (excessive CPU consumption).
4851 */
4852 if( ssl->nb_zero > 3 )
4853 {
4854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
4855 "messages, possible DoS attack" ) );
4856 /* Q: Is that the right error code? */
4857 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4858 }
4859 }
4860 else
4861 ssl->nb_zero = 0;
4862
4863#if defined(MBEDTLS_SSL_PROTO_DTLS)
4864 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4865 {
4866 ; /* in_ctr read from peer, not maintained internally */
4867 }
4868 else
4869#endif
4870 {
4871 unsigned i;
4872 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4873 if( ++ssl->in_ctr[i - 1] != 0 )
4874 break;
4875
4876 /* The loop goes to its end iff the counter is wrapping */
4877 if( i == ssl_ep_len( ssl ) )
4878 {
4879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4880 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4881 }
4882 }
4883
Paul Bakker5121ce52009-01-03 21:22:43 +00004884 }
4885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004886#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004887 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004888 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004889 {
4890 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004892 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004893 return( ret );
4894 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004895 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004896#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004898#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004899 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004901 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004902 }
4903#endif
4904
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004905 return( 0 );
4906}
4907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004908static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004909
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004910/*
4911 * Read a record.
4912 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004913 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4914 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4915 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004916 */
Hanno Becker1097b342018-08-15 14:09:41 +01004917
4918/* Helper functions for mbedtls_ssl_read_record(). */
4919static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004920static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4921static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004922
Hanno Becker327c93b2018-08-15 13:56:18 +01004923int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004924 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004925{
4926 int ret;
4927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004929
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004930 if( ssl->keep_current_message == 0 )
4931 {
4932 do {
Simon Butcher99000142016-10-13 17:21:01 +01004933
Hanno Becker26994592018-08-15 14:14:59 +01004934 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004935 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004936 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004937
Hanno Beckere74d5562018-08-15 14:26:08 +01004938 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004939 {
Hanno Becker40f50842018-08-15 14:48:01 +01004940#if defined(MBEDTLS_SSL_PROTO_DTLS)
4941 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004942
Hanno Becker40f50842018-08-15 14:48:01 +01004943 /* We only check for buffered messages if the
4944 * current datagram is fully consumed. */
4945 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004946 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004947 {
Hanno Becker40f50842018-08-15 14:48:01 +01004948 if( ssl_load_buffered_message( ssl ) == 0 )
4949 have_buffered = 1;
4950 }
4951
4952 if( have_buffered == 0 )
4953#endif /* MBEDTLS_SSL_PROTO_DTLS */
4954 {
4955 ret = ssl_get_next_record( ssl );
4956 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4957 continue;
4958
4959 if( ret != 0 )
4960 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004961 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004962 return( ret );
4963 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004964 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004965 }
4966
4967 ret = mbedtls_ssl_handle_message_type( ssl );
4968
Hanno Becker40f50842018-08-15 14:48:01 +01004969#if defined(MBEDTLS_SSL_PROTO_DTLS)
4970 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4971 {
4972 /* Buffer future message */
4973 ret = ssl_buffer_message( ssl );
4974 if( ret != 0 )
4975 return( ret );
4976
4977 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4978 }
4979#endif /* MBEDTLS_SSL_PROTO_DTLS */
4980
Hanno Becker90333da2017-10-10 11:27:13 +01004981 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4982 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004983
4984 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004985 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004986 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004987 return( ret );
4988 }
4989
Hanno Becker327c93b2018-08-15 13:56:18 +01004990 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004991 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004992 {
4993 mbedtls_ssl_update_handshake_status( ssl );
4994 }
Simon Butcher99000142016-10-13 17:21:01 +01004995 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004996 else
Simon Butcher99000142016-10-13 17:21:01 +01004997 {
Hanno Becker02f59072018-08-15 14:00:24 +01004998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004999 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005000 }
5001
5002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5003
5004 return( 0 );
5005}
5006
Hanno Becker40f50842018-08-15 14:48:01 +01005007#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005008static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005009{
Hanno Becker40f50842018-08-15 14:48:01 +01005010 if( ssl->in_left > ssl->next_record_offset )
5011 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005012
Hanno Becker40f50842018-08-15 14:48:01 +01005013 return( 0 );
5014}
5015
5016static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5017{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005018 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005019 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005020 int ret = 0;
5021
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005022 if( hs == NULL )
5023 return( -1 );
5024
Hanno Beckere00ae372018-08-20 09:39:42 +01005025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5026
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005027 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5028 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5029 {
5030 /* Check if we have seen a ChangeCipherSpec before.
5031 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005032 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005033 {
5034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5035 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005036 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005037 }
5038
Hanno Becker39b8bc92018-08-28 17:17:13 +01005039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005040 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5041 ssl->in_msglen = 1;
5042 ssl->in_msg[0] = 1;
5043
5044 /* As long as they are equal, the exact value doesn't matter. */
5045 ssl->in_left = 0;
5046 ssl->next_record_offset = 0;
5047
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005048 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005049 goto exit;
5050 }
Hanno Becker37f95322018-08-16 13:55:32 +01005051
Hanno Beckerb8f50142018-08-28 10:01:34 +01005052#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005053 /* Debug only */
5054 {
5055 unsigned offset;
5056 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5057 {
5058 hs_buf = &hs->buffering.hs[offset];
5059 if( hs_buf->is_valid == 1 )
5060 {
5061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5062 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005063 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005064 }
5065 }
5066 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005067#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005068
5069 /* Check if we have buffered and/or fully reassembled the
5070 * next handshake message. */
5071 hs_buf = &hs->buffering.hs[0];
5072 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5073 {
5074 /* Synthesize a record containing the buffered HS message. */
5075 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5076 ( hs_buf->data[2] << 8 ) |
5077 hs_buf->data[3];
5078
5079 /* Double-check that we haven't accidentally buffered
5080 * a message that doesn't fit into the input buffer. */
5081 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5082 {
5083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5084 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5085 }
5086
5087 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5088 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5089 hs_buf->data, msg_len + 12 );
5090
5091 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5092 ssl->in_hslen = msg_len + 12;
5093 ssl->in_msglen = msg_len + 12;
5094 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5095
5096 ret = 0;
5097 goto exit;
5098 }
5099 else
5100 {
5101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5102 hs->in_msg_seq ) );
5103 }
5104
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005105 ret = -1;
5106
5107exit:
5108
5109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5110 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005111}
5112
Hanno Beckera02b0b42018-08-21 17:20:27 +01005113static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5114 size_t desired )
5115{
5116 int offset;
5117 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5119 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005120
Hanno Becker01315ea2018-08-21 17:22:17 +01005121 /* Get rid of future records epoch first, if such exist. */
5122 ssl_free_buffered_record( ssl );
5123
5124 /* Check if we have enough space available now. */
5125 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5126 hs->buffering.total_bytes_buffered ) )
5127 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005129 return( 0 );
5130 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005131
Hanno Becker4f432ad2018-08-28 10:02:32 +01005132 /* We don't have enough space to buffer the next expected handshake
5133 * message. Remove buffers used for future messages to gain space,
5134 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005135 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5136 offset >= 0; offset-- )
5137 {
5138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5139 offset ) );
5140
Hanno Beckerb309b922018-08-23 13:18:05 +01005141 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005142
5143 /* Check if we have enough space available now. */
5144 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5145 hs->buffering.total_bytes_buffered ) )
5146 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005148 return( 0 );
5149 }
5150 }
5151
5152 return( -1 );
5153}
5154
Hanno Becker40f50842018-08-15 14:48:01 +01005155static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5156{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005157 int ret = 0;
5158 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5159
5160 if( hs == NULL )
5161 return( 0 );
5162
5163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5164
5165 switch( ssl->in_msgtype )
5166 {
5167 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005169
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005170 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005171 break;
5172
5173 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005174 {
5175 unsigned recv_msg_seq_offset;
5176 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5177 mbedtls_ssl_hs_buffer *hs_buf;
5178 size_t msg_len = ssl->in_hslen - 12;
5179
5180 /* We should never receive an old handshake
5181 * message - double-check nonetheless. */
5182 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5183 {
5184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5185 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5186 }
5187
5188 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5189 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5190 {
5191 /* Silently ignore -- message too far in the future */
5192 MBEDTLS_SSL_DEBUG_MSG( 2,
5193 ( "Ignore future HS message with sequence number %u, "
5194 "buffering window %u - %u",
5195 recv_msg_seq, ssl->handshake->in_msg_seq,
5196 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5197
5198 goto exit;
5199 }
5200
5201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5202 recv_msg_seq, recv_msg_seq_offset ) );
5203
5204 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5205
5206 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005207 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005208 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005209 size_t reassembly_buf_sz;
5210
Hanno Becker37f95322018-08-16 13:55:32 +01005211 hs_buf->is_fragmented =
5212 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5213
5214 /* We copy the message back into the input buffer
5215 * after reassembly, so check that it's not too large.
5216 * This is an implementation-specific limitation
5217 * and not one from the standard, hence it is not
5218 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005219 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005220 {
5221 /* Ignore message */
5222 goto exit;
5223 }
5224
Hanno Beckere0b150f2018-08-21 15:51:03 +01005225 /* Check if we have enough space to buffer the message. */
5226 if( hs->buffering.total_bytes_buffered >
5227 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5228 {
5229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5230 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5231 }
5232
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005233 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5234 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005235
5236 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5237 hs->buffering.total_bytes_buffered ) )
5238 {
5239 if( recv_msg_seq_offset > 0 )
5240 {
5241 /* If we can't buffer a future message because
5242 * of space limitations -- ignore. */
5243 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",
5244 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5245 (unsigned) hs->buffering.total_bytes_buffered ) );
5246 goto exit;
5247 }
Hanno Beckere1801392018-08-21 16:51:05 +01005248 else
5249 {
5250 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",
5251 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5252 (unsigned) hs->buffering.total_bytes_buffered ) );
5253 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005254
Hanno Beckera02b0b42018-08-21 17:20:27 +01005255 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005256 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005257 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",
5258 (unsigned) msg_len,
5259 (unsigned) reassembly_buf_sz,
5260 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005261 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005262 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5263 goto exit;
5264 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005265 }
5266
5267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5268 msg_len ) );
5269
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005270 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5271 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005272 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005273 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005274 goto exit;
5275 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005276 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005277
5278 /* Prepare final header: copy msg_type, length and message_seq,
5279 * then add standardised fragment_offset and fragment_length */
5280 memcpy( hs_buf->data, ssl->in_msg, 6 );
5281 memset( hs_buf->data + 6, 0, 3 );
5282 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5283
5284 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005285
5286 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005287 }
5288 else
5289 {
5290 /* Make sure msg_type and length are consistent */
5291 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5292 {
5293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5294 /* Ignore */
5295 goto exit;
5296 }
5297 }
5298
Hanno Becker4422bbb2018-08-20 09:40:19 +01005299 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005300 {
5301 size_t frag_len, frag_off;
5302 unsigned char * const msg = hs_buf->data + 12;
5303
5304 /*
5305 * Check and copy current fragment
5306 */
5307
5308 /* Validation of header fields already done in
5309 * mbedtls_ssl_prepare_handshake_record(). */
5310 frag_off = ssl_get_hs_frag_off( ssl );
5311 frag_len = ssl_get_hs_frag_len( ssl );
5312
5313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5314 frag_off, frag_len ) );
5315 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5316
5317 if( hs_buf->is_fragmented )
5318 {
5319 unsigned char * const bitmask = msg + msg_len;
5320 ssl_bitmask_set( bitmask, frag_off, frag_len );
5321 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5322 msg_len ) == 0 );
5323 }
5324 else
5325 {
5326 hs_buf->is_complete = 1;
5327 }
5328
5329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5330 hs_buf->is_complete ? "" : "not yet " ) );
5331 }
5332
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005333 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005334 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005335
5336 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005337 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005338 break;
5339 }
5340
5341exit:
5342
5343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5344 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005345}
5346#endif /* MBEDTLS_SSL_PROTO_DTLS */
5347
Hanno Becker1097b342018-08-15 14:09:41 +01005348static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005349{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005350 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005351 * Consume last content-layer message and potentially
5352 * update in_msglen which keeps track of the contents'
5353 * consumption state.
5354 *
5355 * (1) Handshake messages:
5356 * Remove last handshake message, move content
5357 * and adapt in_msglen.
5358 *
5359 * (2) Alert messages:
5360 * Consume whole record content, in_msglen = 0.
5361 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005362 * (3) Change cipher spec:
5363 * Consume whole record content, in_msglen = 0.
5364 *
5365 * (4) Application data:
5366 * Don't do anything - the record layer provides
5367 * the application data as a stream transport
5368 * and consumes through mbedtls_ssl_read only.
5369 *
5370 */
5371
5372 /* Case (1): Handshake messages */
5373 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005374 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005375 /* Hard assertion to be sure that no application data
5376 * is in flight, as corrupting ssl->in_msglen during
5377 * ssl->in_offt != NULL is fatal. */
5378 if( ssl->in_offt != NULL )
5379 {
5380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5381 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5382 }
5383
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005384 /*
5385 * Get next Handshake message in the current record
5386 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005387
Hanno Becker4a810fb2017-05-24 16:27:30 +01005388 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005389 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005390 * current handshake content: If DTLS handshake
5391 * fragmentation is used, that's the fragment
5392 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005393 * size here is faulty and should be changed at
5394 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005395 * (2) While it doesn't seem to cause problems, one
5396 * has to be very careful not to assume that in_hslen
5397 * is always <= in_msglen in a sensible communication.
5398 * Again, it's wrong for DTLS handshake fragmentation.
5399 * The following check is therefore mandatory, and
5400 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005401 * Additionally, ssl->in_hslen might be arbitrarily out of
5402 * bounds after handling a DTLS message with an unexpected
5403 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005404 */
5405 if( ssl->in_hslen < ssl->in_msglen )
5406 {
5407 ssl->in_msglen -= ssl->in_hslen;
5408 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5409 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005410
Hanno Becker4a810fb2017-05-24 16:27:30 +01005411 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5412 ssl->in_msg, ssl->in_msglen );
5413 }
5414 else
5415 {
5416 ssl->in_msglen = 0;
5417 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005418
Hanno Becker4a810fb2017-05-24 16:27:30 +01005419 ssl->in_hslen = 0;
5420 }
5421 /* Case (4): Application data */
5422 else if( ssl->in_offt != NULL )
5423 {
5424 return( 0 );
5425 }
5426 /* Everything else (CCS & Alerts) */
5427 else
5428 {
5429 ssl->in_msglen = 0;
5430 }
5431
Hanno Becker1097b342018-08-15 14:09:41 +01005432 return( 0 );
5433}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005434
Hanno Beckere74d5562018-08-15 14:26:08 +01005435static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5436{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005437 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005438 return( 1 );
5439
5440 return( 0 );
5441}
5442
Hanno Becker5f066e72018-08-16 14:56:31 +01005443#if defined(MBEDTLS_SSL_PROTO_DTLS)
5444
5445static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5446{
5447 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5448 if( hs == NULL )
5449 return;
5450
Hanno Becker01315ea2018-08-21 17:22:17 +01005451 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005452 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005453 hs->buffering.total_bytes_buffered -=
5454 hs->buffering.future_record.len;
5455
5456 mbedtls_free( hs->buffering.future_record.data );
5457 hs->buffering.future_record.data = NULL;
5458 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005459}
5460
5461static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5462{
5463 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5464 unsigned char * rec;
5465 size_t rec_len;
5466 unsigned rec_epoch;
5467
5468 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5469 return( 0 );
5470
5471 if( hs == NULL )
5472 return( 0 );
5473
Hanno Becker5f066e72018-08-16 14:56:31 +01005474 rec = hs->buffering.future_record.data;
5475 rec_len = hs->buffering.future_record.len;
5476 rec_epoch = hs->buffering.future_record.epoch;
5477
5478 if( rec == NULL )
5479 return( 0 );
5480
Hanno Becker4cb782d2018-08-20 11:19:05 +01005481 /* Only consider loading future records if the
5482 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005483 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005484 return( 0 );
5485
Hanno Becker5f066e72018-08-16 14:56:31 +01005486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5487
5488 if( rec_epoch != ssl->in_epoch )
5489 {
5490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5491 goto exit;
5492 }
5493
5494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5495
5496 /* Double-check that the record is not too large */
5497 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5498 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5499 {
5500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5501 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5502 }
5503
5504 memcpy( ssl->in_hdr, rec, rec_len );
5505 ssl->in_left = rec_len;
5506 ssl->next_record_offset = 0;
5507
5508 ssl_free_buffered_record( ssl );
5509
5510exit:
5511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5512 return( 0 );
5513}
5514
5515static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5516{
5517 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5518 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005519 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005520
5521 /* Don't buffer future records outside handshakes. */
5522 if( hs == NULL )
5523 return( 0 );
5524
5525 /* Only buffer handshake records (we are only interested
5526 * in Finished messages). */
5527 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5528 return( 0 );
5529
5530 /* Don't buffer more than one future epoch record. */
5531 if( hs->buffering.future_record.data != NULL )
5532 return( 0 );
5533
Hanno Becker01315ea2018-08-21 17:22:17 +01005534 /* Don't buffer record if there's not enough buffering space remaining. */
5535 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5536 hs->buffering.total_bytes_buffered ) )
5537 {
5538 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",
5539 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5540 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005541 return( 0 );
5542 }
5543
Hanno Becker5f066e72018-08-16 14:56:31 +01005544 /* Buffer record */
5545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5546 ssl->in_epoch + 1 ) );
5547 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5548 rec_hdr_len + ssl->in_msglen );
5549
5550 /* ssl_parse_record_header() only considers records
5551 * of the next epoch as candidates for buffering. */
5552 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005553 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005554
5555 hs->buffering.future_record.data =
5556 mbedtls_calloc( 1, hs->buffering.future_record.len );
5557 if( hs->buffering.future_record.data == NULL )
5558 {
5559 /* If we run out of RAM trying to buffer a
5560 * record from the next epoch, just ignore. */
5561 return( 0 );
5562 }
5563
Hanno Becker01315ea2018-08-21 17:22:17 +01005564 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005565
Hanno Becker01315ea2018-08-21 17:22:17 +01005566 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005567 return( 0 );
5568}
5569
5570#endif /* MBEDTLS_SSL_PROTO_DTLS */
5571
Hanno Beckere74d5562018-08-15 14:26:08 +01005572static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005573{
5574 int ret;
5575
Hanno Becker5f066e72018-08-16 14:56:31 +01005576#if defined(MBEDTLS_SSL_PROTO_DTLS)
5577 /* We might have buffered a future record; if so,
5578 * and if the epoch matches now, load it.
5579 * On success, this call will set ssl->in_left to
5580 * the length of the buffered record, so that
5581 * the calls to ssl_fetch_input() below will
5582 * essentially be no-ops. */
5583 ret = ssl_load_buffered_record( ssl );
5584 if( ret != 0 )
5585 return( ret );
5586#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005588 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005590 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005591 return( ret );
5592 }
5593
5594 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005596#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005597 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5598 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005599 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005600 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5601 {
5602 ret = ssl_buffer_future_record( ssl );
5603 if( ret != 0 )
5604 return( ret );
5605
5606 /* Fall through to handling of unexpected records */
5607 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5608 }
5609
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005610 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5611 {
5612 /* Skip unexpected record (but not whole datagram) */
5613 ssl->next_record_offset = ssl->in_msglen
5614 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005615
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5617 "(header)" ) );
5618 }
5619 else
5620 {
5621 /* Skip invalid record and the rest of the datagram */
5622 ssl->next_record_offset = 0;
5623 ssl->in_left = 0;
5624
5625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5626 "(header)" ) );
5627 }
5628
5629 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005630 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005631 }
5632#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005633 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005634 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005635
5636 /*
5637 * Read and optionally decrypt the message contents
5638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005639 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5640 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005642 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005643 return( ret );
5644 }
5645
5646 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005647#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005648 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005650 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005651 if( ssl->next_record_offset < ssl->in_left )
5652 {
5653 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5654 }
5655 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005656 else
5657#endif
5658 ssl->in_left = 0;
5659
5660 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005662#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005663 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005664 {
5665 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005666 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5667 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005668 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005669 /* Except when waiting for Finished as a bad mac here
5670 * probably means something went wrong in the handshake
5671 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5672 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5673 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5674 {
5675#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5676 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5677 {
5678 mbedtls_ssl_send_alert_message( ssl,
5679 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5680 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5681 }
5682#endif
5683 return( ret );
5684 }
5685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005686#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005687 if( ssl->conf->badmac_limit != 0 &&
5688 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5691 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005692 }
5693#endif
5694
Hanno Becker4a810fb2017-05-24 16:27:30 +01005695 /* As above, invalid records cause
5696 * dismissal of the whole datagram. */
5697
5698 ssl->next_record_offset = 0;
5699 ssl->in_left = 0;
5700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005702 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005703 }
5704
5705 return( ret );
5706 }
5707 else
5708#endif
5709 {
5710 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005711#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5712 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005714 mbedtls_ssl_send_alert_message( ssl,
5715 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5716 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005717 }
5718#endif
5719 return( ret );
5720 }
5721 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005722
Simon Butcher99000142016-10-13 17:21:01 +01005723 return( 0 );
5724}
5725
5726int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5727{
5728 int ret;
5729
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005730 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005731 * Handle particular types of records
5732 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005733 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005734 {
Simon Butcher99000142016-10-13 17:21:01 +01005735 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5736 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005737 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005738 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005739 }
5740
Hanno Beckere678eaa2018-08-21 14:57:46 +01005741 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005742 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005743 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005744 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5746 ssl->in_msglen ) );
5747 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005748 }
5749
Hanno Beckere678eaa2018-08-21 14:57:46 +01005750 if( ssl->in_msg[0] != 1 )
5751 {
5752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5753 ssl->in_msg[0] ) );
5754 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5755 }
5756
5757#if defined(MBEDTLS_SSL_PROTO_DTLS)
5758 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5759 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5760 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5761 {
5762 if( ssl->handshake == NULL )
5763 {
5764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5765 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5766 }
5767
5768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5769 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5770 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005771#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005772 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005774 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005775 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005776 if( ssl->in_msglen != 2 )
5777 {
5778 /* Note: Standard allows for more than one 2 byte alert
5779 to be packed in a single message, but Mbed TLS doesn't
5780 currently support this. */
5781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5782 ssl->in_msglen ) );
5783 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5784 }
5785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005787 ssl->in_msg[0], ssl->in_msg[1] ) );
5788
5789 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005790 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005791 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005792 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005795 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005797 }
5798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005799 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5800 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5803 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005804 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005805
5806#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5807 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5808 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5809 {
Hanno Becker90333da2017-10-10 11:27:13 +01005810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005811 /* Will be handled when trying to parse ServerHello */
5812 return( 0 );
5813 }
5814#endif
5815
5816#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5817 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5818 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5819 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5820 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5821 {
5822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5823 /* Will be handled in mbedtls_ssl_parse_certificate() */
5824 return( 0 );
5825 }
5826#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5827
5828 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005829 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005830 }
5831
Hanno Beckerc76c6192017-06-06 10:03:17 +01005832#if defined(MBEDTLS_SSL_PROTO_DTLS)
5833 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5834 ssl->handshake != NULL &&
5835 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5836 {
5837 ssl_handshake_wrapup_free_hs_transform( ssl );
5838 }
5839#endif
5840
Paul Bakker5121ce52009-01-03 21:22:43 +00005841 return( 0 );
5842}
5843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005845{
5846 int ret;
5847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005848 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5849 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5850 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005851 {
5852 return( ret );
5853 }
5854
5855 return( 0 );
5856}
5857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005858int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005859 unsigned char level,
5860 unsigned char message )
5861{
5862 int ret;
5863
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005864 if( ssl == NULL || ssl->conf == NULL )
5865 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005868 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005870 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005871 ssl->out_msglen = 2;
5872 ssl->out_msg[0] = level;
5873 ssl->out_msg[1] = message;
5874
Hanno Becker67bc7c32018-08-06 11:33:50 +01005875 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005878 return( ret );
5879 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005881
5882 return( 0 );
5883}
5884
Hanno Beckerb9d44792019-02-08 07:19:04 +00005885#if defined(MBEDTLS_X509_CRT_PARSE_C)
5886static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5887{
5888#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5889 if( session->peer_cert != NULL )
5890 {
5891 mbedtls_x509_crt_free( session->peer_cert );
5892 mbedtls_free( session->peer_cert );
5893 session->peer_cert = NULL;
5894 }
5895#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5896 if( session->peer_cert_digest != NULL )
5897 {
5898 /* Zeroization is not necessary. */
5899 mbedtls_free( session->peer_cert_digest );
5900 session->peer_cert_digest = NULL;
5901 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5902 session->peer_cert_digest_len = 0;
5903 }
5904#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5905}
5906#endif /* MBEDTLS_X509_CRT_PARSE_C */
5907
Paul Bakker5121ce52009-01-03 21:22:43 +00005908/*
5909 * Handshake functions
5910 */
Hanno Becker21489932019-02-05 13:20:55 +00005911#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005912/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005913int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005914{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005915 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5916 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005919
Hanno Becker7177a882019-02-05 13:36:46 +00005920 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005923 ssl->state++;
5924 return( 0 );
5925 }
5926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5928 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005929}
5930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005931int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005932{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005933 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5934 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005937
Hanno Becker7177a882019-02-05 13:36:46 +00005938 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005941 ssl->state++;
5942 return( 0 );
5943 }
5944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5946 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005947}
Gilles Peskinef9828522017-05-03 12:28:43 +02005948
Hanno Becker21489932019-02-05 13:20:55 +00005949#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005950/* Some certificate support -> implement write and parse */
5951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005952int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005953{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005954 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005955 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00005957 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5958 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005961
Hanno Becker7177a882019-02-05 13:36:46 +00005962 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005965 ssl->state++;
5966 return( 0 );
5967 }
5968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005969#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005970 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005971 {
5972 if( ssl->client_auth == 0 )
5973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005975 ssl->state++;
5976 return( 0 );
5977 }
5978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005979#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005980 /*
5981 * If using SSLv3 and got no cert, send an Alert message
5982 * (otherwise an empty Certificate message will be sent).
5983 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005984 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
5985 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005986 {
5987 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
5989 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
5990 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00005991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005993 goto write_msg;
5994 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005996 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005997#endif /* MBEDTLS_SSL_CLI_C */
5998#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005999 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006001 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6004 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006005 }
6006 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006007#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006010
6011 /*
6012 * 0 . 0 handshake type
6013 * 1 . 3 handshake length
6014 * 4 . 6 length of all certs
6015 * 7 . 9 length of cert. 1
6016 * 10 . n-1 peer certificate
6017 * n . n+2 length of cert. 2
6018 * n+3 . ... upper level cert, etc.
6019 */
6020 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006022
Paul Bakker29087132010-03-21 21:03:34 +00006023 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006024 {
6025 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006026 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006029 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006031 }
6032
6033 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6034 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6035 ssl->out_msg[i + 2] = (unsigned char)( n );
6036
6037 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6038 i += n; crt = crt->next;
6039 }
6040
6041 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6042 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6043 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6044
6045 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6047 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006048
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006049#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006050write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006051#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006052
6053 ssl->state++;
6054
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006055 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006056 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006057 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006058 return( ret );
6059 }
6060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006062
Paul Bakkered27a042013-04-18 22:46:23 +02006063 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006064}
6065
Hanno Becker84879e32019-01-31 07:44:03 +00006066#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006067
6068#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006069static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6070 unsigned char *crt_buf,
6071 size_t crt_buf_len )
6072{
6073 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6074
6075 if( peer_crt == NULL )
6076 return( -1 );
6077
6078 if( peer_crt->raw.len != crt_buf_len )
6079 return( -1 );
6080
Hanno Becker46f34d02019-02-08 14:00:04 +00006081 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006082}
Hanno Becker177475a2019-02-05 17:02:46 +00006083#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6084static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6085 unsigned char *crt_buf,
6086 size_t crt_buf_len )
6087{
6088 int ret;
6089 unsigned char const * const peer_cert_digest =
6090 ssl->session->peer_cert_digest;
6091 mbedtls_md_type_t const peer_cert_digest_type =
6092 ssl->session->peer_cert_digest_type;
6093 mbedtls_md_info_t const * const digest_info =
6094 mbedtls_md_info_from_type( peer_cert_digest_type );
6095 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6096 size_t digest_len;
6097
6098 if( peer_cert_digest == NULL || digest_info == NULL )
6099 return( -1 );
6100
6101 digest_len = mbedtls_md_get_size( digest_info );
6102 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6103 return( -1 );
6104
6105 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6106 if( ret != 0 )
6107 return( -1 );
6108
6109 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6110}
6111#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006112#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006113
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006114/*
6115 * Once the certificate message is read, parse it into a cert chain and
6116 * perform basic checks, but leave actual verification to the caller
6117 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006118static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6119 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006120{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006121 int ret;
6122#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6123 int crt_cnt=0;
6124#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006125 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006126 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006128 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006131 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6132 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006133 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006134 }
6135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006136 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6137 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006140 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6141 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006143 }
6144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006145 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006146
Paul Bakker5121ce52009-01-03 21:22:43 +00006147 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006148 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006149 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006150 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006151
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006152 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006153 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006156 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6157 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006158 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006159 }
6160
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006161 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6162 i += 3;
6163
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006164 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006165 while( i < ssl->in_hslen )
6166 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006167 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006168 if ( i + 3 > ssl->in_hslen ) {
6169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006170 mbedtls_ssl_send_alert_message( ssl,
6171 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6172 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006173 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6174 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006175 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6176 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006177 if( ssl->in_msg[i] != 0 )
6178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006180 mbedtls_ssl_send_alert_message( ssl,
6181 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6182 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006183 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006184 }
6185
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006186 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006187 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6188 | (unsigned int) ssl->in_msg[i + 2];
6189 i += 3;
6190
6191 if( n < 128 || i + n > ssl->in_hslen )
6192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006194 mbedtls_ssl_send_alert_message( ssl,
6195 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6196 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006197 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006198 }
6199
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006200 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006201#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6202 if( crt_cnt++ == 0 &&
6203 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6204 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006205 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006206 /* During client-side renegotiation, check that the server's
6207 * end-CRTs hasn't changed compared to the initial handshake,
6208 * mitigating the triple handshake attack. On success, reuse
6209 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006210 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6211 if( ssl_check_peer_crt_unchanged( ssl,
6212 &ssl->in_msg[i],
6213 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006214 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6216 mbedtls_ssl_send_alert_message( ssl,
6217 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6218 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6219 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006220 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006221
6222 /* Now we can safely free the original chain. */
6223 ssl_clear_peer_cert( ssl->session );
6224 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006225#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6226
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006227 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006228#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006229 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006230#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006231 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006232 * it in-place from the input buffer instead of making a copy. */
6233 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6234#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006235 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006236 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006237 case 0: /*ok*/
6238 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6239 /* Ignore certificate with an unknown algorithm: maybe a
6240 prior certificate was already trusted. */
6241 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006242
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006243 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6244 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6245 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006246
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006247 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6248 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6249 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006250
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006251 default:
6252 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6253 crt_parse_der_failed:
6254 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6255 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6256 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006257 }
6258
6259 i += n;
6260 }
6261
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006262 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006263 return( 0 );
6264}
6265
Hanno Becker4a55f632019-02-05 12:49:06 +00006266#if defined(MBEDTLS_SSL_SRV_C)
6267static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6268{
6269 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6270 return( -1 );
6271
6272#if defined(MBEDTLS_SSL_PROTO_SSL3)
6273 /*
6274 * Check if the client sent an empty certificate
6275 */
6276 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6277 {
6278 if( ssl->in_msglen == 2 &&
6279 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6280 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6281 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6282 {
6283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6284 return( 0 );
6285 }
6286
6287 return( -1 );
6288 }
6289#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6290
6291#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6292 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6293 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6294 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6295 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6296 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6297 {
6298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6299 return( 0 );
6300 }
6301
6302 return( -1 );
6303#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6304 MBEDTLS_SSL_PROTO_TLS1_2 */
6305}
6306#endif /* MBEDTLS_SSL_SRV_C */
6307
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006308/* Check if a certificate message is expected.
6309 * Return either
6310 * - SSL_CERTIFICATE_EXPECTED, or
6311 * - SSL_CERTIFICATE_SKIP
6312 * indicating whether a Certificate message is expected or not.
6313 */
6314#define SSL_CERTIFICATE_EXPECTED 0
6315#define SSL_CERTIFICATE_SKIP 1
6316static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6317 int authmode )
6318{
6319 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006320 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006321
6322 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6323 return( SSL_CERTIFICATE_SKIP );
6324
6325#if defined(MBEDTLS_SSL_SRV_C)
6326 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6327 {
6328 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6329 return( SSL_CERTIFICATE_SKIP );
6330
6331 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6332 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006333 ssl->session_negotiate->verify_result =
6334 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6335 return( SSL_CERTIFICATE_SKIP );
6336 }
6337 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006338#else
6339 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006340#endif /* MBEDTLS_SSL_SRV_C */
6341
6342 return( SSL_CERTIFICATE_EXPECTED );
6343}
6344
Hanno Becker68636192019-02-05 14:36:34 +00006345static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6346 int authmode,
6347 mbedtls_x509_crt *chain,
6348 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006349{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006350 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006351 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006352 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006353 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006354
Hanno Becker8927c832019-04-03 12:52:50 +01006355 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6356 void *p_vrfy;
6357
Hanno Becker68636192019-02-05 14:36:34 +00006358 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6359 return( 0 );
6360
Hanno Becker8927c832019-04-03 12:52:50 +01006361 if( ssl->f_vrfy != NULL )
6362 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006363 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006364 f_vrfy = ssl->f_vrfy;
6365 p_vrfy = ssl->p_vrfy;
6366 }
6367 else
6368 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006369 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006370 f_vrfy = ssl->conf->f_vrfy;
6371 p_vrfy = ssl->conf->p_vrfy;
6372 }
6373
Hanno Becker68636192019-02-05 14:36:34 +00006374 /*
6375 * Main check: verify certificate
6376 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006377#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6378 if( ssl->conf->f_ca_cb != NULL )
6379 {
6380 ((void) rs_ctx);
6381 have_ca_chain = 1;
6382
6383 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006384 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006385 chain,
6386 ssl->conf->f_ca_cb,
6387 ssl->conf->p_ca_cb,
6388 ssl->conf->cert_profile,
6389 ssl->hostname,
6390 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006391 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006392 }
6393 else
6394#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6395 {
6396 mbedtls_x509_crt *ca_chain;
6397 mbedtls_x509_crl *ca_crl;
6398
6399#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6400 if( ssl->handshake->sni_ca_chain != NULL )
6401 {
6402 ca_chain = ssl->handshake->sni_ca_chain;
6403 ca_crl = ssl->handshake->sni_ca_crl;
6404 }
6405 else
6406#endif
6407 {
6408 ca_chain = ssl->conf->ca_chain;
6409 ca_crl = ssl->conf->ca_crl;
6410 }
6411
6412 if( ca_chain != NULL )
6413 have_ca_chain = 1;
6414
6415 ret = mbedtls_x509_crt_verify_restartable(
6416 chain,
6417 ca_chain, ca_crl,
6418 ssl->conf->cert_profile,
6419 ssl->hostname,
6420 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006421 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006422 }
Hanno Becker68636192019-02-05 14:36:34 +00006423
6424 if( ret != 0 )
6425 {
6426 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6427 }
6428
6429#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6430 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6431 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6432#endif
6433
6434 /*
6435 * Secondary checks: always done, but change 'ret' only if it was 0
6436 */
6437
6438#if defined(MBEDTLS_ECP_C)
6439 {
6440 const mbedtls_pk_context *pk = &chain->pk;
6441
6442 /* If certificate uses an EC key, make sure the curve is OK */
6443 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6444 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6445 {
6446 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6447
6448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6449 if( ret == 0 )
6450 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6451 }
6452 }
6453#endif /* MBEDTLS_ECP_C */
6454
6455 if( mbedtls_ssl_check_cert_usage( chain,
6456 ciphersuite_info,
6457 ! ssl->conf->endpoint,
6458 &ssl->session_negotiate->verify_result ) != 0 )
6459 {
6460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6461 if( ret == 0 )
6462 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6463 }
6464
6465 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6466 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6467 * with details encoded in the verification flags. All other kinds
6468 * of error codes, including those from the user provided f_vrfy
6469 * functions, are treated as fatal and lead to a failure of
6470 * ssl_parse_certificate even if verification was optional. */
6471 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6472 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6473 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6474 {
6475 ret = 0;
6476 }
6477
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006478 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006479 {
6480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6481 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6482 }
6483
6484 if( ret != 0 )
6485 {
6486 uint8_t alert;
6487
6488 /* The certificate may have been rejected for several reasons.
6489 Pick one and send the corresponding alert. Which alert to send
6490 may be a subject of debate in some cases. */
6491 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6492 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6493 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6494 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6495 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6496 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6497 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6498 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6499 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6500 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6501 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6502 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6503 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6504 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6505 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6506 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6507 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6508 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6509 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6510 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6511 else
6512 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6513 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6514 alert );
6515 }
6516
6517#if defined(MBEDTLS_DEBUG_C)
6518 if( ssl->session_negotiate->verify_result != 0 )
6519 {
6520 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6521 ssl->session_negotiate->verify_result ) );
6522 }
6523 else
6524 {
6525 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6526 }
6527#endif /* MBEDTLS_DEBUG_C */
6528
6529 return( ret );
6530}
6531
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006532#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6533static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6534 unsigned char *start, size_t len )
6535{
6536 int ret;
6537 /* Remember digest of the peer's end-CRT. */
6538 ssl->session_negotiate->peer_cert_digest =
6539 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6540 if( ssl->session_negotiate->peer_cert_digest == NULL )
6541 {
6542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6543 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6544 mbedtls_ssl_send_alert_message( ssl,
6545 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6546 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6547
6548 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6549 }
6550
6551 ret = mbedtls_md( mbedtls_md_info_from_type(
6552 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6553 start, len,
6554 ssl->session_negotiate->peer_cert_digest );
6555
6556 ssl->session_negotiate->peer_cert_digest_type =
6557 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6558 ssl->session_negotiate->peer_cert_digest_len =
6559 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6560
6561 return( ret );
6562}
6563
6564static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6565 unsigned char *start, size_t len )
6566{
6567 unsigned char *end = start + len;
6568 int ret;
6569
6570 /* Make a copy of the peer's raw public key. */
6571 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6572 ret = mbedtls_pk_parse_subpubkey( &start, end,
6573 &ssl->handshake->peer_pubkey );
6574 if( ret != 0 )
6575 {
6576 /* We should have parsed the public key before. */
6577 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6578 }
6579
6580 return( 0 );
6581}
6582#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6583
Hanno Becker68636192019-02-05 14:36:34 +00006584int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6585{
6586 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006587 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006588#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6589 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6590 ? ssl->handshake->sni_authmode
6591 : ssl->conf->authmode;
6592#else
6593 const int authmode = ssl->conf->authmode;
6594#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006595 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006596 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006597
6598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6599
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006600 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6601 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006602 {
6603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006604 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006605 }
6606
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006607#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6608 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006609 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006610 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006611 chain = ssl->handshake->ecrs_peer_cert;
6612 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006613 goto crt_verify;
6614 }
6615#endif
6616
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006617 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006618 {
6619 /* mbedtls_ssl_read_record may have sent an alert already. We
6620 let it decide whether to alert. */
6621 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006622 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006623 }
6624
Hanno Becker4a55f632019-02-05 12:49:06 +00006625#if defined(MBEDTLS_SSL_SRV_C)
6626 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6627 {
6628 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006629
6630 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006631 ret = 0;
6632 else
6633 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006634
Hanno Becker6bdfab22019-02-05 13:11:17 +00006635 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006636 }
6637#endif /* MBEDTLS_SSL_SRV_C */
6638
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006639 /* Clear existing peer CRT structure in case we tried to
6640 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006641 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006642
6643 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6644 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006645 {
6646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6647 sizeof( mbedtls_x509_crt ) ) );
6648 mbedtls_ssl_send_alert_message( ssl,
6649 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6650 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006651
Hanno Becker3dad3112019-02-05 17:19:52 +00006652 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6653 goto exit;
6654 }
6655 mbedtls_x509_crt_init( chain );
6656
6657 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006658 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006659 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006660
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006661#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6662 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006663 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006664
6665crt_verify:
6666 if( ssl->handshake->ecrs_enabled)
6667 rs_ctx = &ssl->handshake->ecrs_ctx;
6668#endif
6669
Hanno Becker68636192019-02-05 14:36:34 +00006670 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006671 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006672 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006673 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006674
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006675#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006676 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006677 unsigned char *crt_start, *pk_start;
6678 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006679
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006680 /* We parse the CRT chain without copying, so
6681 * these pointers point into the input buffer,
6682 * and are hence still valid after freeing the
6683 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006684
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006685 crt_start = chain->raw.p;
6686 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006687
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006688 pk_start = chain->pk_raw.p;
6689 pk_len = chain->pk_raw.len;
6690
6691 /* Free the CRT structures before computing
6692 * digest and copying the peer's public key. */
6693 mbedtls_x509_crt_free( chain );
6694 mbedtls_free( chain );
6695 chain = NULL;
6696
6697 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006698 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006699 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006700
6701 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6702 if( ret != 0 )
6703 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006704 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006705#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6706 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006707 ssl->session_negotiate->peer_cert = chain;
6708 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006709#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006712
Hanno Becker6bdfab22019-02-05 13:11:17 +00006713exit:
6714
Hanno Becker3dad3112019-02-05 17:19:52 +00006715 if( ret == 0 )
6716 ssl->state++;
6717
6718#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6719 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6720 {
6721 ssl->handshake->ecrs_peer_cert = chain;
6722 chain = NULL;
6723 }
6724#endif
6725
6726 if( chain != NULL )
6727 {
6728 mbedtls_x509_crt_free( chain );
6729 mbedtls_free( chain );
6730 }
6731
Paul Bakker5121ce52009-01-03 21:22:43 +00006732 return( ret );
6733}
Hanno Becker21489932019-02-05 13:20:55 +00006734#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006736int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006737{
6738 int ret;
6739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006742 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006743 ssl->out_msglen = 1;
6744 ssl->out_msg[0] = 1;
6745
Paul Bakker5121ce52009-01-03 21:22:43 +00006746 ssl->state++;
6747
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006748 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006749 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006750 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006751 return( ret );
6752 }
6753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006755
6756 return( 0 );
6757}
6758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006760{
6761 int ret;
6762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006764
Hanno Becker327c93b2018-08-15 13:56:18 +01006765 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006767 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006768 return( ret );
6769 }
6770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006771 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006774 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6775 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006777 }
6778
Hanno Beckere678eaa2018-08-21 14:57:46 +01006779 /* CCS records are only accepted if they have length 1 and content '1',
6780 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006781
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006782 /*
6783 * Switch to our negotiated transform and session parameters for inbound
6784 * data.
6785 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006787 ssl->transform_in = ssl->transform_negotiate;
6788 ssl->session_in = ssl->session_negotiate;
6789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006791 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006793#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006794 ssl_dtls_replay_reset( ssl );
6795#endif
6796
6797 /* Increment epoch */
6798 if( ++ssl->in_epoch == 0 )
6799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006801 /* This is highly unlikely to happen for legitimate reasons, so
6802 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006804 }
6805 }
6806 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006808 memset( ssl->in_ctr, 0, 8 );
6809
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006810 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6813 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006815 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006818 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6819 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006820 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006821 }
6822 }
6823#endif
6824
Paul Bakker5121ce52009-01-03 21:22:43 +00006825 ssl->state++;
6826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006828
6829 return( 0 );
6830}
6831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006832void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6833 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006834{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006835 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6838 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6839 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006840 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006841 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006842#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6844#if defined(MBEDTLS_SHA512_C)
6845 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006846 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6847 else
6848#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006849#if defined(MBEDTLS_SHA256_C)
6850 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006851 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006852 else
6853#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006854#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006857 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006858 }
Paul Bakker380da532012-04-18 16:10:25 +00006859}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006862{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6864 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006865 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6866 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006867#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006868#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6869#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006870#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006871 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006872 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6873#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006874 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006875#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006876#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006877#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006878#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006879 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006880 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006881#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006882 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006883#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006884#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006886}
6887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006888static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006889 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006890{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006891#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6892 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006893 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6894 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006895#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6897#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006898#if defined(MBEDTLS_USE_PSA_CRYPTO)
6899 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6900#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006901 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006902#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006903#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006905#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006906 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006907#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006908 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006909#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006910#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006911#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006912}
6913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6915 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6916static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006917 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006918{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006919 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6920 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006921}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006922#endif
Paul Bakker380da532012-04-18 16:10:25 +00006923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6925#if defined(MBEDTLS_SHA256_C)
6926static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006927 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006928{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006929#if defined(MBEDTLS_USE_PSA_CRYPTO)
6930 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6931#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006932 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006933#endif
Paul Bakker380da532012-04-18 16:10:25 +00006934}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006935#endif
Paul Bakker380da532012-04-18 16:10:25 +00006936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937#if defined(MBEDTLS_SHA512_C)
6938static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006939 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006940{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006941#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006942 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006943#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006944 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006945#endif
Paul Bakker380da532012-04-18 16:10:25 +00006946}
Paul Bakker769075d2012-11-24 11:26:46 +01006947#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006951static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006953{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006954 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006955 mbedtls_md5_context md5;
6956 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006957
Paul Bakker5121ce52009-01-03 21:22:43 +00006958 unsigned char padbuf[48];
6959 unsigned char md5sum[16];
6960 unsigned char sha1sum[20];
6961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006963 if( !session )
6964 session = ssl->session;
6965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006967
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006968 mbedtls_md5_init( &md5 );
6969 mbedtls_sha1_init( &sha1 );
6970
6971 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6972 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006973
6974 /*
6975 * SSLv3:
6976 * hash =
6977 * MD5( master + pad2 +
6978 * MD5( handshake + sender + master + pad1 ) )
6979 * + SHA1( master + pad2 +
6980 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006981 */
6982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006984 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6985 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006986#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006989 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6990 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006991#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006994 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006995
Paul Bakker1ef83d62012-04-11 12:09:53 +00006996 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006997
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006998 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6999 mbedtls_md5_update_ret( &md5, session->master, 48 );
7000 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7001 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007002
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007003 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7004 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7005 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7006 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007007
Paul Bakker1ef83d62012-04-11 12:09:53 +00007008 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007009
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007010 mbedtls_md5_starts_ret( &md5 );
7011 mbedtls_md5_update_ret( &md5, session->master, 48 );
7012 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7013 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7014 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007015
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007016 mbedtls_sha1_starts_ret( &sha1 );
7017 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7018 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7019 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7020 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007022 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007023
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007024 mbedtls_md5_free( &md5 );
7025 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007026
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007027 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7028 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7029 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007032}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007033#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007036static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007037 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007038{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007039 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007040 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007041 mbedtls_md5_context md5;
7042 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007043 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007045 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007046 if( !session )
7047 session = ssl->session;
7048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007050
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007051 mbedtls_md5_init( &md5 );
7052 mbedtls_sha1_init( &sha1 );
7053
7054 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7055 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007056
Paul Bakker1ef83d62012-04-11 12:09:53 +00007057 /*
7058 * TLSv1:
7059 * hash = PRF( master, finished_label,
7060 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7061 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007063#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007064 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7065 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007066#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007069 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7070 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007071#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007073 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007074 ? "client finished"
7075 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007076
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007077 mbedtls_md5_finish_ret( &md5, padbuf );
7078 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007079
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007080 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007081 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007084
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007085 mbedtls_md5_free( &md5 );
7086 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007087
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007088 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007091}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007092#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7095#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007096static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007098{
7099 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007100 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007101 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007102#if defined(MBEDTLS_USE_PSA_CRYPTO)
7103 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007104 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007105 psa_status_t status;
7106#else
7107 mbedtls_sha256_context sha256;
7108#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007111 if( !session )
7112 session = ssl->session;
7113
Andrzej Kurekeb342242019-01-29 09:14:33 -05007114 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7115 ? "client finished"
7116 : "server finished";
7117
7118#if defined(MBEDTLS_USE_PSA_CRYPTO)
7119 sha256_psa = psa_hash_operation_init();
7120
7121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7122
7123 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7124 if( status != PSA_SUCCESS )
7125 {
7126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7127 return;
7128 }
7129
7130 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7131 if( status != PSA_SUCCESS )
7132 {
7133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7134 return;
7135 }
7136 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7137#else
7138
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007139 mbedtls_sha256_init( &sha256 );
7140
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007142
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007143 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007144
7145 /*
7146 * TLSv1.2:
7147 * hash = PRF( master, finished_label,
7148 * Hash( handshake ) )[0.11]
7149 */
7150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151#if !defined(MBEDTLS_SHA256_ALT)
7152 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007153 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007154#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007155
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007156 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007157 mbedtls_sha256_free( &sha256 );
7158#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007159
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007160 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007161 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007164
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007165 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007168}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007171#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007172static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007174{
7175 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007176 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007177 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007178#if defined(MBEDTLS_USE_PSA_CRYPTO)
7179 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007180 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007181 psa_status_t status;
7182#else
7183 mbedtls_sha512_context sha512;
7184#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007187 if( !session )
7188 session = ssl->session;
7189
Andrzej Kurekeb342242019-01-29 09:14:33 -05007190 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7191 ? "client finished"
7192 : "server finished";
7193
7194#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007195 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007196
7197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7198
Andrzej Kurek972fba52019-01-30 03:29:12 -05007199 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007200 if( status != PSA_SUCCESS )
7201 {
7202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7203 return;
7204 }
7205
Andrzej Kurek972fba52019-01-30 03:29:12 -05007206 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007207 if( status != PSA_SUCCESS )
7208 {
7209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7210 return;
7211 }
7212 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7213#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007214 mbedtls_sha512_init( &sha512 );
7215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007217
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007218 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007219
7220 /*
7221 * TLSv1.2:
7222 * hash = PRF( master, finished_label,
7223 * Hash( handshake ) )[0.11]
7224 */
7225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007226#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007227 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7228 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007229#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007230
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007231 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007232 mbedtls_sha512_free( &sha512 );
7233#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007234
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007235 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007236 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007239
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007240 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007243}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244#endif /* MBEDTLS_SHA512_C */
7245#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007248{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007250
7251 /*
7252 * Free our handshake params
7253 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007254 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007256 ssl->handshake = NULL;
7257
7258 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007259 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007260 */
7261 if( ssl->transform )
7262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263 mbedtls_ssl_transform_free( ssl->transform );
7264 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007265 }
7266 ssl->transform = ssl->transform_negotiate;
7267 ssl->transform_negotiate = NULL;
7268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007269 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007270}
7271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007273{
7274 int resume = ssl->handshake->resume;
7275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278#if defined(MBEDTLS_SSL_RENEGOTIATION)
7279 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007281 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007282 ssl->renego_records_seen = 0;
7283 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007284#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007285
7286 /*
7287 * Free the previous session and switch in the current one
7288 */
Paul Bakker0a597072012-09-25 21:55:46 +00007289 if( ssl->session )
7290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007292 /* RFC 7366 3.1: keep the EtM state */
7293 ssl->session_negotiate->encrypt_then_mac =
7294 ssl->session->encrypt_then_mac;
7295#endif
7296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297 mbedtls_ssl_session_free( ssl->session );
7298 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007299 }
7300 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007301 ssl->session_negotiate = NULL;
7302
Paul Bakker0a597072012-09-25 21:55:46 +00007303 /*
7304 * Add cache entry
7305 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007306 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007307 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007308 resume == 0 )
7309 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007310 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007312 }
Paul Bakker0a597072012-09-25 21:55:46 +00007313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007315 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007316 ssl->handshake->flight != NULL )
7317 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007318 /* Cancel handshake timer */
7319 ssl_set_timer( ssl, 0 );
7320
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007321 /* Keep last flight around in case we need to resend it:
7322 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007323 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007324 }
7325 else
7326#endif
7327 ssl_handshake_wrapup_free_hs_transform( ssl );
7328
Paul Bakker48916f92012-09-16 19:57:18 +00007329 ssl->state++;
7330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007332}
7333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007334int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007335{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007336 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007339
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007340 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007341
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007342 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007343
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007344 /*
7345 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7346 * may define some other value. Currently (early 2016), no defined
7347 * ciphersuite does this (and this is unlikely to change as activity has
7348 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007352#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007353 ssl->verify_data_len = hash_len;
7354 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007355#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007356
Paul Bakker5121ce52009-01-03 21:22:43 +00007357 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007358 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7359 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007360
7361 /*
7362 * In case of session resuming, invert the client and server
7363 * ChangeCipherSpec messages order.
7364 */
Paul Bakker0a597072012-09-25 21:55:46 +00007365 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007368 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007370#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007372 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007373 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007374#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007375 }
7376 else
7377 ssl->state++;
7378
Paul Bakker48916f92012-09-16 19:57:18 +00007379 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007380 * Switch to our negotiated transform and session parameters for outbound
7381 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007385#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007386 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007387 {
7388 unsigned char i;
7389
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007390 /* Remember current epoch settings for resending */
7391 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007392 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007393
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007394 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007395 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007396
7397 /* Increment epoch */
7398 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007399 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007400 break;
7401
7402 /* The loop goes to its end iff the counter is wrapping */
7403 if( i == 0 )
7404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7406 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007407 }
7408 }
7409 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007410#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007411 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007412
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007413 ssl->transform_out = ssl->transform_negotiate;
7414 ssl->session_out = ssl->session_negotiate;
7415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7417 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007419 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007421 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7422 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007423 }
7424 }
7425#endif
7426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007428 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007429 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007430#endif
7431
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007432 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007433 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007435 return( ret );
7436 }
7437
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007438#if defined(MBEDTLS_SSL_PROTO_DTLS)
7439 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7440 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7441 {
7442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7443 return( ret );
7444 }
7445#endif
7446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007448
7449 return( 0 );
7450}
7451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007453#define SSL_MAX_HASH_LEN 36
7454#else
7455#define SSL_MAX_HASH_LEN 12
7456#endif
7457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007459{
Paul Bakker23986e52011-04-24 08:57:21 +00007460 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007461 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007462 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007465
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007466 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007467
Hanno Becker327c93b2018-08-15 13:56:18 +01007468 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007471 return( ret );
7472 }
7473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007477 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7478 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007479 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007480 }
7481
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007482 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483#if defined(MBEDTLS_SSL_PROTO_SSL3)
7484 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007485 hash_len = 36;
7486 else
7487#endif
7488 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007490 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7491 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007494 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7495 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007496 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007497 }
7498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007500 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007503 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7504 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007506 }
7507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007509 ssl->verify_data_len = hash_len;
7510 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007511#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007512
Paul Bakker0a597072012-09-25 21:55:46 +00007513 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007516 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007518#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007519#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007520 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007522#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007523 }
7524 else
7525 ssl->state++;
7526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007528 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007530#endif
7531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007533
7534 return( 0 );
7535}
7536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007537static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007538{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007539 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007541#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7542 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7543 mbedtls_md5_init( &handshake->fin_md5 );
7544 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007545 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7546 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007547#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007548#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7549#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007550#if defined(MBEDTLS_USE_PSA_CRYPTO)
7551 handshake->fin_sha256_psa = psa_hash_operation_init();
7552 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7553#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007555 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007556#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007557#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007559#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007560 handshake->fin_sha384_psa = psa_hash_operation_init();
7561 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007562#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007564 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007565#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007566#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007567#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007568
7569 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007570
7571#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7572 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7573 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7574#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007576#if defined(MBEDTLS_DHM_C)
7577 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007578#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579#if defined(MBEDTLS_ECDH_C)
7580 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007581#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007582#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007583 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007584#if defined(MBEDTLS_SSL_CLI_C)
7585 handshake->ecjpake_cache = NULL;
7586 handshake->ecjpake_cache_len = 0;
7587#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007588#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007589
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007590#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007591 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007592#endif
7593
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007594#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7595 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7596#endif
Hanno Becker75173122019-02-06 16:18:31 +00007597
7598#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7599 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7600 mbedtls_pk_init( &handshake->peer_pubkey );
7601#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007602}
7603
Hanno Beckera18d1322018-01-03 14:27:32 +00007604void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007605{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7609 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007610
Hanno Beckerd56ed242018-01-03 15:32:51 +00007611#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612 mbedtls_md_init( &transform->md_ctx_enc );
7613 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007614#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007615}
7616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007618{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007620}
7621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007623{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007624 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007625 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007627 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007629 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007630 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007631
7632 /*
7633 * Either the pointers are now NULL or cleared properly and can be freed.
7634 * Now allocate missing structures.
7635 */
7636 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007637 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007638 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007639 }
Paul Bakker48916f92012-09-16 19:57:18 +00007640
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007641 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007642 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007643 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007644 }
Paul Bakker48916f92012-09-16 19:57:18 +00007645
Paul Bakker82788fb2014-10-20 13:59:19 +02007646 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007647 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007648 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007649 }
Paul Bakker48916f92012-09-16 19:57:18 +00007650
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007651 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007652 if( ssl->handshake == NULL ||
7653 ssl->transform_negotiate == NULL ||
7654 ssl->session_negotiate == NULL )
7655 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007658 mbedtls_free( ssl->handshake );
7659 mbedtls_free( ssl->transform_negotiate );
7660 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007661
7662 ssl->handshake = NULL;
7663 ssl->transform_negotiate = NULL;
7664 ssl->session_negotiate = NULL;
7665
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007666 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007667 }
7668
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007669 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007670 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007671 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007672 ssl_handshake_params_init( ssl->handshake );
7673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007674#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007675 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7676 {
7677 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007678
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007679 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7680 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7681 else
7682 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007683
7684 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007685 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007686#endif
7687
Paul Bakker48916f92012-09-16 19:57:18 +00007688 return( 0 );
7689}
7690
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007691#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007692/* Dummy cookie callbacks for defaults */
7693static int ssl_cookie_write_dummy( void *ctx,
7694 unsigned char **p, unsigned char *end,
7695 const unsigned char *cli_id, size_t cli_id_len )
7696{
7697 ((void) ctx);
7698 ((void) p);
7699 ((void) end);
7700 ((void) cli_id);
7701 ((void) cli_id_len);
7702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007703 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007704}
7705
7706static int ssl_cookie_check_dummy( void *ctx,
7707 const unsigned char *cookie, size_t cookie_len,
7708 const unsigned char *cli_id, size_t cli_id_len )
7709{
7710 ((void) ctx);
7711 ((void) cookie);
7712 ((void) cookie_len);
7713 ((void) cli_id);
7714 ((void) cli_id_len);
7715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007717}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007718#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007719
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007720/* Once ssl->out_hdr as the address of the beginning of the
7721 * next outgoing record is set, deduce the other pointers.
7722 *
7723 * Note: For TLS, we save the implicit record sequence number
7724 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7725 * and the caller has to make sure there's space for this.
7726 */
7727
7728static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7729 mbedtls_ssl_transform *transform )
7730{
7731#if defined(MBEDTLS_SSL_PROTO_DTLS)
7732 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7733 {
7734 ssl->out_ctr = ssl->out_hdr + 3;
7735 ssl->out_len = ssl->out_hdr + 11;
7736 ssl->out_iv = ssl->out_hdr + 13;
7737 }
7738 else
7739#endif
7740 {
7741 ssl->out_ctr = ssl->out_hdr - 8;
7742 ssl->out_len = ssl->out_hdr + 3;
7743 ssl->out_iv = ssl->out_hdr + 5;
7744 }
7745
7746 /* Adjust out_msg to make space for explicit IV, if used. */
7747 if( transform != NULL &&
7748 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7749 {
7750 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7751 }
7752 else
7753 ssl->out_msg = ssl->out_iv;
7754}
7755
7756/* Once ssl->in_hdr as the address of the beginning of the
7757 * next incoming record is set, deduce the other pointers.
7758 *
7759 * Note: For TLS, we save the implicit record sequence number
7760 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7761 * and the caller has to make sure there's space for this.
7762 */
7763
7764static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7765 mbedtls_ssl_transform *transform )
7766{
7767#if defined(MBEDTLS_SSL_PROTO_DTLS)
7768 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7769 {
7770 ssl->in_ctr = ssl->in_hdr + 3;
7771 ssl->in_len = ssl->in_hdr + 11;
7772 ssl->in_iv = ssl->in_hdr + 13;
7773 }
7774 else
7775#endif
7776 {
7777 ssl->in_ctr = ssl->in_hdr - 8;
7778 ssl->in_len = ssl->in_hdr + 3;
7779 ssl->in_iv = ssl->in_hdr + 5;
7780 }
7781
7782 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7783 if( transform != NULL &&
7784 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7785 {
7786 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7787 }
7788 else
7789 ssl->in_msg = ssl->in_iv;
7790}
7791
Paul Bakker5121ce52009-01-03 21:22:43 +00007792/*
7793 * Initialize an SSL context
7794 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007795void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7796{
7797 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7798}
7799
7800/*
7801 * Setup an SSL context
7802 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007803
7804static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7805{
7806 /* Set the incoming and outgoing record pointers. */
7807#if defined(MBEDTLS_SSL_PROTO_DTLS)
7808 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7809 {
7810 ssl->out_hdr = ssl->out_buf;
7811 ssl->in_hdr = ssl->in_buf;
7812 }
7813 else
7814#endif /* MBEDTLS_SSL_PROTO_DTLS */
7815 {
7816 ssl->out_hdr = ssl->out_buf + 8;
7817 ssl->in_hdr = ssl->in_buf + 8;
7818 }
7819
7820 /* Derive other internal pointers. */
7821 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7822 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7823}
7824
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007825int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007826 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007827{
Paul Bakker48916f92012-09-16 19:57:18 +00007828 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007829
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007830 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007831
7832 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007833 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007834 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007835
7836 /* Set to NULL in case of an error condition */
7837 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007838
Angus Grattond8213d02016-05-25 20:56:48 +10007839 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7840 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007841 {
Angus Grattond8213d02016-05-25 20:56:48 +10007842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007843 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007844 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007845 }
7846
7847 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7848 if( ssl->out_buf == NULL )
7849 {
7850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007851 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007852 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007853 }
7854
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007855 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007856
Paul Bakker48916f92012-09-16 19:57:18 +00007857 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007858 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007859
7860 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007861
7862error:
7863 mbedtls_free( ssl->in_buf );
7864 mbedtls_free( ssl->out_buf );
7865
7866 ssl->conf = NULL;
7867
7868 ssl->in_buf = NULL;
7869 ssl->out_buf = NULL;
7870
7871 ssl->in_hdr = NULL;
7872 ssl->in_ctr = NULL;
7873 ssl->in_len = NULL;
7874 ssl->in_iv = NULL;
7875 ssl->in_msg = NULL;
7876
7877 ssl->out_hdr = NULL;
7878 ssl->out_ctr = NULL;
7879 ssl->out_len = NULL;
7880 ssl->out_iv = NULL;
7881 ssl->out_msg = NULL;
7882
k-stachowiak9f7798e2018-07-31 16:52:32 +02007883 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007884}
7885
7886/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007887 * Reset an initialized and used SSL context for re-use while retaining
7888 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007889 *
7890 * If partial is non-zero, keep data in the input buffer and client ID.
7891 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007892 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007893static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007894{
Paul Bakker48916f92012-09-16 19:57:18 +00007895 int ret;
7896
Hanno Becker7e772132018-08-10 12:38:21 +01007897#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7898 !defined(MBEDTLS_SSL_SRV_C)
7899 ((void) partial);
7900#endif
7901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007902 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007903
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007904 /* Cancel any possibly running timer */
7905 ssl_set_timer( ssl, 0 );
7906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907#if defined(MBEDTLS_SSL_RENEGOTIATION)
7908 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007909 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007910
7911 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7913 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007914#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007916
Paul Bakker7eb013f2011-10-06 12:37:39 +00007917 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007918 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007919
7920 ssl->in_msgtype = 0;
7921 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007923 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007924 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007925#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007927 ssl_dtls_replay_reset( ssl );
7928#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007929
7930 ssl->in_hslen = 0;
7931 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007932
7933 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007934
7935 ssl->out_msgtype = 0;
7936 ssl->out_msglen = 0;
7937 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7939 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007940 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007941#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007942
Hanno Becker19859472018-08-06 09:40:20 +01007943 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7944
Paul Bakker48916f92012-09-16 19:57:18 +00007945 ssl->transform_in = NULL;
7946 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007947
Hanno Becker78640902018-08-13 16:35:15 +01007948 ssl->session_in = NULL;
7949 ssl->session_out = NULL;
7950
Angus Grattond8213d02016-05-25 20:56:48 +10007951 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007952
7953#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007954 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007955#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7956 {
7957 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007958 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007959 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7962 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7965 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7968 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007969 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007970 }
7971#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007972
Paul Bakker48916f92012-09-16 19:57:18 +00007973 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975 mbedtls_ssl_transform_free( ssl->transform );
7976 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007977 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007978 }
Paul Bakker48916f92012-09-16 19:57:18 +00007979
Paul Bakkerc0463502013-02-14 11:19:38 +01007980 if( ssl->session )
7981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982 mbedtls_ssl_session_free( ssl->session );
7983 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007984 ssl->session = NULL;
7985 }
7986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007988 ssl->alpn_chosen = NULL;
7989#endif
7990
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007991#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007992#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007993 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007994#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007995 {
7996 mbedtls_free( ssl->cli_id );
7997 ssl->cli_id = NULL;
7998 ssl->cli_id_len = 0;
7999 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008000#endif
8001
Paul Bakker48916f92012-09-16 19:57:18 +00008002 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8003 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008004
8005 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008006}
8007
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008008/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008009 * Reset an initialized and used SSL context for re-use while retaining
8010 * all application-set variables, function pointers and data.
8011 */
8012int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8013{
8014 return( ssl_session_reset_int( ssl, 0 ) );
8015}
8016
8017/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008018 * SSL set accessors
8019 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008020void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008021{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008022 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008023}
8024
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008025void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008026{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008027 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008028}
8029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008030#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008031void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008032{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008033 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008034}
8035#endif
8036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008038void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008039{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008040 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008041}
8042#endif
8043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008044#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008045
Hanno Becker1841b0a2018-08-24 11:13:57 +01008046void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8047 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008048{
8049 ssl->disable_datagram_packing = !allow_packing;
8050}
8051
8052void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8053 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008054{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008055 conf->hs_timeout_min = min;
8056 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008057}
8058#endif
8059
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008060void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008061{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008062 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008063}
8064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008065#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008066void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008067 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008068 void *p_vrfy )
8069{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008070 conf->f_vrfy = f_vrfy;
8071 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008072}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008073#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008074
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008075void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008076 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008077 void *p_rng )
8078{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008079 conf->f_rng = f_rng;
8080 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008081}
8082
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008083void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008084 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008085 void *p_dbg )
8086{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008087 conf->f_dbg = f_dbg;
8088 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008089}
8090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008091void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008092 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008093 mbedtls_ssl_send_t *f_send,
8094 mbedtls_ssl_recv_t *f_recv,
8095 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008096{
8097 ssl->p_bio = p_bio;
8098 ssl->f_send = f_send;
8099 ssl->f_recv = f_recv;
8100 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008101}
8102
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008103#if defined(MBEDTLS_SSL_PROTO_DTLS)
8104void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8105{
8106 ssl->mtu = mtu;
8107}
8108#endif
8109
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008110void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008111{
8112 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008113}
8114
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008115void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8116 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008117 mbedtls_ssl_set_timer_t *f_set_timer,
8118 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008119{
8120 ssl->p_timer = p_timer;
8121 ssl->f_set_timer = f_set_timer;
8122 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008123
8124 /* Make sure we start with no timer running */
8125 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008126}
8127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008128#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008129void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008130 void *p_cache,
8131 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8132 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008133{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008134 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008135 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008136 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008137}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140#if defined(MBEDTLS_SSL_CLI_C)
8141int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008142{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008143 int ret;
8144
8145 if( ssl == NULL ||
8146 session == NULL ||
8147 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008148 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008150 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008151 }
8152
Hanno Becker52055ae2019-02-06 14:30:46 +00008153 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8154 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008155 return( ret );
8156
Paul Bakker0a597072012-09-25 21:55:46 +00008157 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008158
8159 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008160}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008162
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008163void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008164 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008165{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008166 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8167 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8168 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8169 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008170}
8171
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008172void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008173 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008174 int major, int minor )
8175{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008177 return;
8178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008179 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008180 return;
8181
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008182 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008183}
8184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008186void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008187 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008188{
8189 conf->cert_profile = profile;
8190}
8191
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008192/* Append a new keycert entry to a (possibly empty) list */
8193static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8194 mbedtls_x509_crt *cert,
8195 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008196{
niisato8ee24222018-06-25 19:05:48 +09008197 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008198
niisato8ee24222018-06-25 19:05:48 +09008199 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8200 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008201 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008202
niisato8ee24222018-06-25 19:05:48 +09008203 new_cert->cert = cert;
8204 new_cert->key = key;
8205 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008206
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008207 /* Update head is the list was null, else add to the end */
8208 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008209 {
niisato8ee24222018-06-25 19:05:48 +09008210 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008211 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008212 else
8213 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008214 mbedtls_ssl_key_cert *cur = *head;
8215 while( cur->next != NULL )
8216 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008217 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008218 }
8219
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008220 return( 0 );
8221}
8222
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008223int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008224 mbedtls_x509_crt *own_cert,
8225 mbedtls_pk_context *pk_key )
8226{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008227 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008228}
8229
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008230void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008231 mbedtls_x509_crt *ca_chain,
8232 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008233{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008234 conf->ca_chain = ca_chain;
8235 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008236
8237#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8238 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8239 * cannot be used together. */
8240 conf->f_ca_cb = NULL;
8241 conf->p_ca_cb = NULL;
8242#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008243}
Hanno Becker5adaad92019-03-27 16:54:37 +00008244
8245#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8246void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008247 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008248 void *p_ca_cb )
8249{
8250 conf->f_ca_cb = f_ca_cb;
8251 conf->p_ca_cb = p_ca_cb;
8252
8253 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8254 * cannot be used together. */
8255 conf->ca_chain = NULL;
8256 conf->ca_crl = NULL;
8257}
8258#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008259#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008260
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008261#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8262int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8263 mbedtls_x509_crt *own_cert,
8264 mbedtls_pk_context *pk_key )
8265{
8266 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8267 own_cert, pk_key ) );
8268}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008269
8270void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8271 mbedtls_x509_crt *ca_chain,
8272 mbedtls_x509_crl *ca_crl )
8273{
8274 ssl->handshake->sni_ca_chain = ca_chain;
8275 ssl->handshake->sni_ca_crl = ca_crl;
8276}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008277
8278void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8279 int authmode )
8280{
8281 ssl->handshake->sni_authmode = authmode;
8282}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008283#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8284
Hanno Becker8927c832019-04-03 12:52:50 +01008285#if defined(MBEDTLS_X509_CRT_PARSE_C)
8286void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8287 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8288 void *p_vrfy )
8289{
8290 ssl->f_vrfy = f_vrfy;
8291 ssl->p_vrfy = p_vrfy;
8292}
8293#endif
8294
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008295#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008296/*
8297 * Set EC J-PAKE password for current handshake
8298 */
8299int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8300 const unsigned char *pw,
8301 size_t pw_len )
8302{
8303 mbedtls_ecjpake_role role;
8304
Janos Follath8eb64132016-06-03 15:40:57 +01008305 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008306 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8307
8308 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8309 role = MBEDTLS_ECJPAKE_SERVER;
8310 else
8311 role = MBEDTLS_ECJPAKE_CLIENT;
8312
8313 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8314 role,
8315 MBEDTLS_MD_SHA256,
8316 MBEDTLS_ECP_DP_SECP256R1,
8317 pw, pw_len ) );
8318}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008319#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008321#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008322
8323static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8324{
8325 /* Remove reference to existing PSK, if any. */
8326#if defined(MBEDTLS_USE_PSA_CRYPTO)
8327 if( conf->psk_opaque != 0 )
8328 {
8329 /* The maintenance of the PSK key slot is the
8330 * user's responsibility. */
8331 conf->psk_opaque = 0;
8332 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008333 /* This and the following branch should never
8334 * be taken simultaenously as we maintain the
8335 * invariant that raw and opaque PSKs are never
8336 * configured simultaneously. As a safeguard,
8337 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008338#endif /* MBEDTLS_USE_PSA_CRYPTO */
8339 if( conf->psk != NULL )
8340 {
8341 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8342
8343 mbedtls_free( conf->psk );
8344 conf->psk = NULL;
8345 conf->psk_len = 0;
8346 }
8347
8348 /* Remove reference to PSK identity, if any. */
8349 if( conf->psk_identity != NULL )
8350 {
8351 mbedtls_free( conf->psk_identity );
8352 conf->psk_identity = NULL;
8353 conf->psk_identity_len = 0;
8354 }
8355}
8356
Hanno Becker7390c712018-11-15 13:33:04 +00008357/* This function assumes that PSK identity in the SSL config is unset.
8358 * It checks that the provided identity is well-formed and attempts
8359 * to make a copy of it in the SSL config.
8360 * On failure, the PSK identity in the config remains unset. */
8361static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8362 unsigned char const *psk_identity,
8363 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008364{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008365 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008366 if( psk_identity == NULL ||
8367 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008368 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008369 {
8370 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8371 }
8372
Hanno Becker7390c712018-11-15 13:33:04 +00008373 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8374 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008375 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008376
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008377 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008378 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008379
8380 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008381}
8382
Hanno Becker7390c712018-11-15 13:33:04 +00008383int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8384 const unsigned char *psk, size_t psk_len,
8385 const unsigned char *psk_identity, size_t psk_identity_len )
8386{
8387 int ret;
8388 /* Remove opaque/raw PSK + PSK Identity */
8389 ssl_conf_remove_psk( conf );
8390
8391 /* Check and set raw PSK */
8392 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8393 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8394 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8395 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8396 conf->psk_len = psk_len;
8397 memcpy( conf->psk, psk, conf->psk_len );
8398
8399 /* Check and set PSK Identity */
8400 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8401 if( ret != 0 )
8402 ssl_conf_remove_psk( conf );
8403
8404 return( ret );
8405}
8406
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008407static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8408{
8409#if defined(MBEDTLS_USE_PSA_CRYPTO)
8410 if( ssl->handshake->psk_opaque != 0 )
8411 {
8412 ssl->handshake->psk_opaque = 0;
8413 }
8414 else
8415#endif /* MBEDTLS_USE_PSA_CRYPTO */
8416 if( ssl->handshake->psk != NULL )
8417 {
8418 mbedtls_platform_zeroize( ssl->handshake->psk,
8419 ssl->handshake->psk_len );
8420 mbedtls_free( ssl->handshake->psk );
8421 ssl->handshake->psk_len = 0;
8422 }
8423}
8424
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008425int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8426 const unsigned char *psk, size_t psk_len )
8427{
8428 if( psk == NULL || ssl->handshake == NULL )
8429 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8430
8431 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8432 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8433
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008434 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008435
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008436 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008437 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008438
8439 ssl->handshake->psk_len = psk_len;
8440 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8441
8442 return( 0 );
8443}
8444
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008445#if defined(MBEDTLS_USE_PSA_CRYPTO)
8446int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008447 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008448 const unsigned char *psk_identity,
8449 size_t psk_identity_len )
8450{
Hanno Becker7390c712018-11-15 13:33:04 +00008451 int ret;
8452 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008453 ssl_conf_remove_psk( conf );
8454
Hanno Becker7390c712018-11-15 13:33:04 +00008455 /* Check and set opaque PSK */
8456 if( psk_slot == 0 )
8457 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008458 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008459
8460 /* Check and set PSK Identity */
8461 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8462 psk_identity_len );
8463 if( ret != 0 )
8464 ssl_conf_remove_psk( conf );
8465
8466 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008467}
8468
8469int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008470 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008471{
8472 if( psk_slot == 0 || ssl->handshake == NULL )
8473 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8474
8475 ssl_remove_psk( ssl );
8476 ssl->handshake->psk_opaque = psk_slot;
8477 return( 0 );
8478}
8479#endif /* MBEDTLS_USE_PSA_CRYPTO */
8480
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008481void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008482 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008483 size_t),
8484 void *p_psk )
8485{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008486 conf->f_psk = f_psk;
8487 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008488}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008489#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008490
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008491#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008492
8493#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008494int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008495{
8496 int ret;
8497
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008498 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8499 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8500 {
8501 mbedtls_mpi_free( &conf->dhm_P );
8502 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008503 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008504 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008505
8506 return( 0 );
8507}
Hanno Becker470a8c42017-10-04 15:28:46 +01008508#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008509
Hanno Beckera90658f2017-10-04 15:29:08 +01008510int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8511 const unsigned char *dhm_P, size_t P_len,
8512 const unsigned char *dhm_G, size_t G_len )
8513{
8514 int ret;
8515
8516 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8517 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8518 {
8519 mbedtls_mpi_free( &conf->dhm_P );
8520 mbedtls_mpi_free( &conf->dhm_G );
8521 return( ret );
8522 }
8523
8524 return( 0 );
8525}
Paul Bakker5121ce52009-01-03 21:22:43 +00008526
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008527int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008528{
8529 int ret;
8530
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008531 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8532 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8533 {
8534 mbedtls_mpi_free( &conf->dhm_P );
8535 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008536 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008537 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008538
8539 return( 0 );
8540}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008541#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008542
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008543#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8544/*
8545 * Set the minimum length for Diffie-Hellman parameters
8546 */
8547void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8548 unsigned int bitlen )
8549{
8550 conf->dhm_min_bitlen = bitlen;
8551}
8552#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8553
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008554#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008555/*
8556 * Set allowed/preferred hashes for handshake signatures
8557 */
8558void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8559 const int *hashes )
8560{
8561 conf->sig_hashes = hashes;
8562}
Hanno Becker947194e2017-04-07 13:25:49 +01008563#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008564
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008565#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008566/*
8567 * Set the allowed elliptic curves
8568 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008569void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008570 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008571{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008572 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008573}
Hanno Becker947194e2017-04-07 13:25:49 +01008574#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008575
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008576#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008577int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008578{
Hanno Becker947194e2017-04-07 13:25:49 +01008579 /* Initialize to suppress unnecessary compiler warning */
8580 size_t hostname_len = 0;
8581
8582 /* Check if new hostname is valid before
8583 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008584 if( hostname != NULL )
8585 {
8586 hostname_len = strlen( hostname );
8587
8588 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8589 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8590 }
8591
8592 /* Now it's clear that we will overwrite the old hostname,
8593 * so we can free it safely */
8594
8595 if( ssl->hostname != NULL )
8596 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008597 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008598 mbedtls_free( ssl->hostname );
8599 }
8600
8601 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008602
Paul Bakker5121ce52009-01-03 21:22:43 +00008603 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008604 {
8605 ssl->hostname = NULL;
8606 }
8607 else
8608 {
8609 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008610 if( ssl->hostname == NULL )
8611 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008612
Hanno Becker947194e2017-04-07 13:25:49 +01008613 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008614
Hanno Becker947194e2017-04-07 13:25:49 +01008615 ssl->hostname[hostname_len] = '\0';
8616 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008617
8618 return( 0 );
8619}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008620#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008621
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008622#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008623void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008624 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008625 const unsigned char *, size_t),
8626 void *p_sni )
8627{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008628 conf->f_sni = f_sni;
8629 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008630}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008631#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008633#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008634int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008635{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008636 size_t cur_len, tot_len;
8637 const char **p;
8638
8639 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008640 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8641 * MUST NOT be truncated."
8642 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008643 */
8644 tot_len = 0;
8645 for( p = protos; *p != NULL; p++ )
8646 {
8647 cur_len = strlen( *p );
8648 tot_len += cur_len;
8649
8650 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008652 }
8653
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008654 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008655
8656 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008657}
8658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008659const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008660{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008661 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008662}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008663#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008664
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008665void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008666{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008667 conf->max_major_ver = major;
8668 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008669}
8670
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008671void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008672{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008673 conf->min_major_ver = major;
8674 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008675}
8676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008677#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008678void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008679{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008680 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008681}
8682#endif
8683
Janos Follath088ce432017-04-10 12:42:31 +01008684#if defined(MBEDTLS_SSL_SRV_C)
8685void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8686 char cert_req_ca_list )
8687{
8688 conf->cert_req_ca_list = cert_req_ca_list;
8689}
8690#endif
8691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008692#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008693void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008694{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008695 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008696}
8697#endif
8698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008699#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008700void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008701{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008702 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008703}
8704#endif
8705
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008706#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008707void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008708{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008709 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008710}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008711#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008713#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008714int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008715{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008716 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008717 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008719 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008720 }
8721
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008722 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008723
8724 return( 0 );
8725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008726#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008728#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008729void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008730{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008731 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008732}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008733#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008735#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008736void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008737{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008738 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008739}
8740#endif
8741
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008742void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008743{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008744 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008745}
8746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008747#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008748void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008749{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008750 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008751}
8752
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008753void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008754{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008755 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008756}
8757
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008758void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008759 const unsigned char period[8] )
8760{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008761 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008762}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008763#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008765#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008766#if defined(MBEDTLS_SSL_CLI_C)
8767void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008768{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008769 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008770}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008771#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008772
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008773#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008774void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8775 mbedtls_ssl_ticket_write_t *f_ticket_write,
8776 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8777 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008778{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008779 conf->f_ticket_write = f_ticket_write;
8780 conf->f_ticket_parse = f_ticket_parse;
8781 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008782}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008783#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008784#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008785
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008786#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8787void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8788 mbedtls_ssl_export_keys_t *f_export_keys,
8789 void *p_export_keys )
8790{
8791 conf->f_export_keys = f_export_keys;
8792 conf->p_export_keys = p_export_keys;
8793}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03008794
8795void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
8796 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
8797 void *p_export_keys )
8798{
8799 conf->f_export_keys_ext = f_export_keys_ext;
8800 conf->p_export_keys = p_export_keys;
8801}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008802#endif
8803
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008804#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008805void mbedtls_ssl_conf_async_private_cb(
8806 mbedtls_ssl_config *conf,
8807 mbedtls_ssl_async_sign_t *f_async_sign,
8808 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8809 mbedtls_ssl_async_resume_t *f_async_resume,
8810 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008811 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008812{
8813 conf->f_async_sign_start = f_async_sign;
8814 conf->f_async_decrypt_start = f_async_decrypt;
8815 conf->f_async_resume = f_async_resume;
8816 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008817 conf->p_async_config_data = async_config_data;
8818}
8819
Gilles Peskine8f97af72018-04-26 11:46:10 +02008820void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8821{
8822 return( conf->p_async_config_data );
8823}
8824
Gilles Peskine1febfef2018-04-30 11:54:39 +02008825void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008826{
8827 if( ssl->handshake == NULL )
8828 return( NULL );
8829 else
8830 return( ssl->handshake->user_async_ctx );
8831}
8832
Gilles Peskine1febfef2018-04-30 11:54:39 +02008833void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008834 void *ctx )
8835{
8836 if( ssl->handshake != NULL )
8837 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008838}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008839#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008840
Paul Bakker5121ce52009-01-03 21:22:43 +00008841/*
8842 * SSL get accessors
8843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008844size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008845{
8846 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8847}
8848
Hanno Becker8b170a02017-10-10 11:51:19 +01008849int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8850{
8851 /*
8852 * Case A: We're currently holding back
8853 * a message for further processing.
8854 */
8855
8856 if( ssl->keep_current_message == 1 )
8857 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008858 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008859 return( 1 );
8860 }
8861
8862 /*
8863 * Case B: Further records are pending in the current datagram.
8864 */
8865
8866#if defined(MBEDTLS_SSL_PROTO_DTLS)
8867 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8868 ssl->in_left > ssl->next_record_offset )
8869 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008870 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008871 return( 1 );
8872 }
8873#endif /* MBEDTLS_SSL_PROTO_DTLS */
8874
8875 /*
8876 * Case C: A handshake message is being processed.
8877 */
8878
Hanno Becker8b170a02017-10-10 11:51:19 +01008879 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8880 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008881 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008882 return( 1 );
8883 }
8884
8885 /*
8886 * Case D: An application data message is being processed
8887 */
8888 if( ssl->in_offt != NULL )
8889 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008890 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008891 return( 1 );
8892 }
8893
8894 /*
8895 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008896 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008897 * we implement support for multiple alerts in single records.
8898 */
8899
8900 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8901 return( 0 );
8902}
8903
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008904uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008905{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008906 if( ssl->session != NULL )
8907 return( ssl->session->verify_result );
8908
8909 if( ssl->session_negotiate != NULL )
8910 return( ssl->session_negotiate->verify_result );
8911
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008912 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008913}
8914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008915const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008916{
Paul Bakker926c8e42013-03-06 10:23:34 +01008917 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008918 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008920 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008921}
8922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008923const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008924{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008925#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008926 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008927 {
8928 switch( ssl->minor_ver )
8929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008930 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008931 return( "DTLSv1.0" );
8932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008933 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008934 return( "DTLSv1.2" );
8935
8936 default:
8937 return( "unknown (DTLS)" );
8938 }
8939 }
8940#endif
8941
Paul Bakker43ca69c2011-01-15 17:35:19 +00008942 switch( ssl->minor_ver )
8943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008944 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008945 return( "SSLv3.0" );
8946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008947 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008948 return( "TLSv1.0" );
8949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008950 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008951 return( "TLSv1.1" );
8952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008953 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008954 return( "TLSv1.2" );
8955
Paul Bakker43ca69c2011-01-15 17:35:19 +00008956 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008957 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008958 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008959}
8960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008961int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008962{
Hanno Becker3136ede2018-08-17 15:28:19 +01008963 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008964 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008965 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008966
Hanno Becker78640902018-08-13 16:35:15 +01008967 if( transform == NULL )
8968 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008970#if defined(MBEDTLS_ZLIB_SUPPORT)
8971 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8972 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008973#endif
8974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008975 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008977 case MBEDTLS_MODE_GCM:
8978 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008979 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008980 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008981 transform_expansion = transform->minlen;
8982 break;
8983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008984 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008985
8986 block_size = mbedtls_cipher_get_block_size(
8987 &transform->cipher_ctx_enc );
8988
Hanno Becker3136ede2018-08-17 15:28:19 +01008989 /* Expansion due to the addition of the MAC. */
8990 transform_expansion += transform->maclen;
8991
8992 /* Expansion due to the addition of CBC padding;
8993 * Theoretically up to 256 bytes, but we never use
8994 * more than the block size of the underlying cipher. */
8995 transform_expansion += block_size;
8996
8997 /* For TLS 1.1 or higher, an explicit IV is added
8998 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008999#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9000 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009001 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009002#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009003
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009004 break;
9005
9006 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009008 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009009 }
9010
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02009011 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009012}
9013
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009014#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9015size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9016{
9017 size_t max_len;
9018
9019 /*
9020 * Assume mfl_code is correct since it was checked when set
9021 */
Angus Grattond8213d02016-05-25 20:56:48 +10009022 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009023
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009024 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009025 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009026 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009027 {
Angus Grattond8213d02016-05-25 20:56:48 +10009028 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009029 }
9030
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009031 /* During a handshake, use the value being negotiated */
9032 if( ssl->session_negotiate != NULL &&
9033 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9034 {
9035 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9036 }
9037
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009038 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009039}
9040#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9041
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009042#if defined(MBEDTLS_SSL_PROTO_DTLS)
9043static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9044{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009045 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9046 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9047 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9048 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9049 return ( 0 );
9050
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009051 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9052 return( ssl->mtu );
9053
9054 if( ssl->mtu == 0 )
9055 return( ssl->handshake->mtu );
9056
9057 return( ssl->mtu < ssl->handshake->mtu ?
9058 ssl->mtu : ssl->handshake->mtu );
9059}
9060#endif /* MBEDTLS_SSL_PROTO_DTLS */
9061
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009062int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9063{
9064 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9065
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009066#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9067 !defined(MBEDTLS_SSL_PROTO_DTLS)
9068 (void) ssl;
9069#endif
9070
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009071#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9072 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9073
9074 if( max_len > mfl )
9075 max_len = mfl;
9076#endif
9077
9078#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009079 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009080 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009081 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009082 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9083 const size_t overhead = (size_t) ret;
9084
9085 if( ret < 0 )
9086 return( ret );
9087
9088 if( mtu <= overhead )
9089 {
9090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9091 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9092 }
9093
9094 if( max_len > mtu - overhead )
9095 max_len = mtu - overhead;
9096 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009097#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009098
Hanno Becker0defedb2018-08-10 12:35:02 +01009099#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9100 !defined(MBEDTLS_SSL_PROTO_DTLS)
9101 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009102#endif
9103
9104 return( (int) max_len );
9105}
9106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009107#if defined(MBEDTLS_X509_CRT_PARSE_C)
9108const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009109{
9110 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009111 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009112
Hanno Beckere6824572019-02-07 13:18:46 +00009113#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009114 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009115#else
9116 return( NULL );
9117#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009118}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009119#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009121#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009122int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9123 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009124{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009125 if( ssl == NULL ||
9126 dst == NULL ||
9127 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009128 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009131 }
9132
Hanno Becker52055ae2019-02-06 14:30:46 +00009133 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009135#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009136
Paul Bakker5121ce52009-01-03 21:22:43 +00009137/*
Paul Bakker1961b702013-01-25 14:49:24 +01009138 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009140int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009141{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009142 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009143
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009144 if( ssl == NULL || ssl->conf == NULL )
9145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009147#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009148 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009149 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009150#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009151#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009152 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009153 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009154#endif
9155
Paul Bakker1961b702013-01-25 14:49:24 +01009156 return( ret );
9157}
9158
9159/*
9160 * Perform the SSL handshake
9161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009162int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009163{
9164 int ret = 0;
9165
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009166 if( ssl == NULL || ssl->conf == NULL )
9167 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009171 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009173 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009174
9175 if( ret != 0 )
9176 break;
9177 }
9178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009180
9181 return( ret );
9182}
9183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009184#if defined(MBEDTLS_SSL_RENEGOTIATION)
9185#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009186/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009187 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009189static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009190{
9191 int ret;
9192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009194
9195 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009196 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9197 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009198
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009199 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009200 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009202 return( ret );
9203 }
9204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009206
9207 return( 0 );
9208}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009209#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009210
9211/*
9212 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009213 * - any side: calling mbedtls_ssl_renegotiate(),
9214 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9215 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009216 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009217 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009218 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009220static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009221{
9222 int ret;
9223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009225
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009226 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9227 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009228
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009229 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9230 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009231#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009232 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009233 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009234 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009235 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009236 ssl->handshake->out_msg_seq = 1;
9237 else
9238 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009239 }
9240#endif
9241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009242 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9243 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009245 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009248 return( ret );
9249 }
9250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009252
9253 return( 0 );
9254}
9255
9256/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009257 * Renegotiate current connection on client,
9258 * or request renegotiation on server
9259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009260int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009261{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009262 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009263
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009264 if( ssl == NULL || ssl->conf == NULL )
9265 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009267#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009268 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009269 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009271 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9272 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009274 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009275
9276 /* Did we already try/start sending HelloRequest? */
9277 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009278 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009279
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009280 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009281 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009282#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009284#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009285 /*
9286 * On client, either start the renegotiation process or,
9287 * if already in progress, continue the handshake
9288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009289 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009291 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9292 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009293
9294 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009297 return( ret );
9298 }
9299 }
9300 else
9301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009302 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009305 return( ret );
9306 }
9307 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009309
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009310 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009311}
9312
9313/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009314 * Check record counters and renegotiate if they're above the limit.
9315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009316static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009317{
Andres AG2196c7f2016-12-15 17:01:16 +00009318 size_t ep_len = ssl_ep_len( ssl );
9319 int in_ctr_cmp;
9320 int out_ctr_cmp;
9321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009322 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9323 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009324 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009325 {
9326 return( 0 );
9327 }
9328
Andres AG2196c7f2016-12-15 17:01:16 +00009329 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9330 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009331 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009332 ssl->conf->renego_period + ep_len, 8 - ep_len );
9333
9334 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009335 {
9336 return( 0 );
9337 }
9338
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009340 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009341}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009342#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009343
9344/*
9345 * Receive application data decrypted from the SSL layer
9346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009347int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009348{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009349 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009350 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009351
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009352 if( ssl == NULL || ssl->conf == NULL )
9353 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009358 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009360 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009361 return( ret );
9362
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009363 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009364 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009365 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009366 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009367 return( ret );
9368 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009369 }
9370#endif
9371
Hanno Becker4a810fb2017-05-24 16:27:30 +01009372 /*
9373 * Check if renegotiation is necessary and/or handshake is
9374 * in process. If yes, perform/continue, and fall through
9375 * if an unexpected packet is received while the client
9376 * is waiting for the ServerHello.
9377 *
9378 * (There is no equivalent to the last condition on
9379 * the server-side as it is not treated as within
9380 * a handshake while waiting for the ClientHello
9381 * after a renegotiation request.)
9382 */
9383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009384#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009385 ret = ssl_check_ctr_renegotiate( ssl );
9386 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9387 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009389 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009390 return( ret );
9391 }
9392#endif
9393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009394 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009396 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009397 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9398 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009400 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009401 return( ret );
9402 }
9403 }
9404
Hanno Beckere41158b2017-10-23 13:30:32 +01009405 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009406 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009407 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009408 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009409 if( ssl->f_get_timer != NULL &&
9410 ssl->f_get_timer( ssl->p_timer ) == -1 )
9411 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009412 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009413 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009414
Hanno Becker327c93b2018-08-15 13:56:18 +01009415 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009416 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009417 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9418 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009419
Hanno Becker4a810fb2017-05-24 16:27:30 +01009420 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9421 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009422 }
9423
9424 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009425 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009426 {
9427 /*
9428 * OpenSSL sends empty messages to randomize the IV
9429 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009430 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009432 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009433 return( 0 );
9434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009436 return( ret );
9437 }
9438 }
9439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009440 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009443
Hanno Becker4a810fb2017-05-24 16:27:30 +01009444 /*
9445 * - For client-side, expect SERVER_HELLO_REQUEST.
9446 * - For server-side, expect CLIENT_HELLO.
9447 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9448 */
9449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009450#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009451 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009452 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009453 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009456
9457 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009458#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009459 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009460 {
9461 continue;
9462 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009463#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009464 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009465 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009466#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009467
Hanno Becker4a810fb2017-05-24 16:27:30 +01009468#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009469 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009470 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009473
9474 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009475#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009476 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009477 {
9478 continue;
9479 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009480#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009481 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009482 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009483#endif /* MBEDTLS_SSL_SRV_C */
9484
Hanno Becker21df7f92017-10-17 11:03:26 +01009485#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009486 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009487 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9488 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9489 ssl->conf->allow_legacy_renegotiation ==
9490 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9491 {
9492 /*
9493 * Accept renegotiation request
9494 */
Paul Bakker48916f92012-09-16 19:57:18 +00009495
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009496 /* DTLS clients need to know renego is server-initiated */
9497#if defined(MBEDTLS_SSL_PROTO_DTLS)
9498 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9499 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9500 {
9501 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9502 }
9503#endif
9504 ret = ssl_start_renegotiation( ssl );
9505 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9506 ret != 0 )
9507 {
9508 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9509 return( ret );
9510 }
9511 }
9512 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009513#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009514 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009515 /*
9516 * Refuse renegotiation
9517 */
9518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009519 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009521#if defined(MBEDTLS_SSL_PROTO_SSL3)
9522 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009523 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009524 /* SSLv3 does not have a "no_renegotiation" warning, so
9525 we send a fatal alert and abort the connection. */
9526 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9527 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9528 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009529 }
9530 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9532#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9533 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9534 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009536 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9537 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9538 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009539 {
9540 return( ret );
9541 }
Paul Bakker48916f92012-09-16 19:57:18 +00009542 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009543 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009544#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9545 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009549 }
Paul Bakker48916f92012-09-16 19:57:18 +00009550 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009551
Hanno Becker90333da2017-10-10 11:27:13 +01009552 /* At this point, we don't know whether the renegotiation has been
9553 * completed or not. The cases to consider are the following:
9554 * 1) The renegotiation is complete. In this case, no new record
9555 * has been read yet.
9556 * 2) The renegotiation is incomplete because the client received
9557 * an application data record while awaiting the ServerHello.
9558 * 3) The renegotiation is incomplete because the client received
9559 * a non-handshake, non-application data message while awaiting
9560 * the ServerHello.
9561 * In each of these case, looping will be the proper action:
9562 * - For 1), the next iteration will read a new record and check
9563 * if it's application data.
9564 * - For 2), the loop condition isn't satisfied as application data
9565 * is present, hence continue is the same as break
9566 * - For 3), the loop condition is satisfied and read_record
9567 * will re-deliver the message that was held back by the client
9568 * when expecting the ServerHello.
9569 */
9570 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009571 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009572#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009573 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009574 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009575 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009576 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009577 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009580 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009581 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009582 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009583 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009584 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009585#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009587 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9588 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009591 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009592 }
9593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009594 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9597 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009598 }
9599
9600 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009601
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009602 /* We're going to return something now, cancel timer,
9603 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009605 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009606
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009607#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009608 /* If we requested renego but received AppData, resend HelloRequest.
9609 * Do it now, after setting in_offt, to avoid taking this branch
9610 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009611#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009612 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009613 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009614 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009615 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009617 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009618 return( ret );
9619 }
9620 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009621#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009622#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009623 }
9624
9625 n = ( len < ssl->in_msglen )
9626 ? len : ssl->in_msglen;
9627
9628 memcpy( buf, ssl->in_offt, n );
9629 ssl->in_msglen -= n;
9630
9631 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009632 {
9633 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009634 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009635 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009636 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009637 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009638 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009639 /* more data available */
9640 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009641 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009644
Paul Bakker23986e52011-04-24 08:57:21 +00009645 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009646}
9647
9648/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009649 * Send application data to be encrypted by the SSL layer, taking care of max
9650 * fragment length and buffer size.
9651 *
9652 * According to RFC 5246 Section 6.2.1:
9653 *
9654 * Zero-length fragments of Application data MAY be sent as they are
9655 * potentially useful as a traffic analysis countermeasure.
9656 *
9657 * Therefore, it is possible that the input message length is 0 and the
9658 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009659 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009660static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009661 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009662{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009663 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9664 const size_t max_len = (size_t) ret;
9665
9666 if( ret < 0 )
9667 {
9668 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9669 return( ret );
9670 }
9671
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009672 if( len > max_len )
9673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009675 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009678 "maximum fragment length: %d > %d",
9679 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009680 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009681 }
9682 else
9683#endif
9684 len = max_len;
9685 }
Paul Bakker887bd502011-06-08 13:10:54 +00009686
Paul Bakker5121ce52009-01-03 21:22:43 +00009687 if( ssl->out_left != 0 )
9688 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009689 /*
9690 * The user has previously tried to send the data and
9691 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9692 * written. In this case, we expect the high-level write function
9693 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009695 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009697 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009698 return( ret );
9699 }
9700 }
Paul Bakker887bd502011-06-08 13:10:54 +00009701 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009702 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009703 /*
9704 * The user is trying to send a message the first time, so we need to
9705 * copy the data into the internal buffers and setup the data structure
9706 * to keep track of partial writes
9707 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009708 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009709 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009710 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009711
Hanno Becker67bc7c32018-08-06 11:33:50 +01009712 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009714 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009715 return( ret );
9716 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009717 }
9718
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009719 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009720}
9721
9722/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009723 * Write application data, doing 1/n-1 splitting if necessary.
9724 *
9725 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009726 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009727 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009729#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009730static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009731 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009732{
9733 int ret;
9734
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009735 if( ssl->conf->cbc_record_splitting ==
9736 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009737 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009738 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9739 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9740 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009741 {
9742 return( ssl_write_real( ssl, buf, len ) );
9743 }
9744
9745 if( ssl->split_done == 0 )
9746 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009747 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009748 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009749 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009750 }
9751
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009752 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9753 return( ret );
9754 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009755
9756 return( ret + 1 );
9757}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009758#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009759
9760/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009761 * Write application data (public-facing wrapper)
9762 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009763int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009764{
9765 int ret;
9766
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009768
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009769 if( ssl == NULL || ssl->conf == NULL )
9770 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9771
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009772#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009773 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9774 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009775 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009776 return( ret );
9777 }
9778#endif
9779
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009780 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009781 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009782 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009783 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009784 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009785 return( ret );
9786 }
9787 }
9788
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009789#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009790 ret = ssl_write_split( ssl, buf, len );
9791#else
9792 ret = ssl_write_real( ssl, buf, len );
9793#endif
9794
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009796
9797 return( ret );
9798}
9799
9800/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009801 * Notify the peer that the connection is being closed
9802 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009803int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009804{
9805 int ret;
9806
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009807 if( ssl == NULL || ssl->conf == NULL )
9808 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009811
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009812 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009813 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009815 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009817 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9818 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9819 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009822 return( ret );
9823 }
9824 }
9825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009827
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009828 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009829}
9830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009832{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009833 if( transform == NULL )
9834 return;
9835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009836#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009837 deflateEnd( &transform->ctx_deflate );
9838 inflateEnd( &transform->ctx_inflate );
9839#endif
9840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9842 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009843
Hanno Beckerd56ed242018-01-03 15:32:51 +00009844#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845 mbedtls_md_free( &transform->md_ctx_enc );
9846 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00009847#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009848
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009849 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009850}
9851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852#if defined(MBEDTLS_X509_CRT_PARSE_C)
9853static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009854{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009856
9857 while( cur != NULL )
9858 {
9859 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009860 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009861 cur = next;
9862 }
9863}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009864#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009865
Hanno Becker0271f962018-08-16 13:23:47 +01009866#if defined(MBEDTLS_SSL_PROTO_DTLS)
9867
9868static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9869{
9870 unsigned offset;
9871 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9872
9873 if( hs == NULL )
9874 return;
9875
Hanno Becker283f5ef2018-08-24 09:34:47 +01009876 ssl_free_buffered_record( ssl );
9877
Hanno Becker0271f962018-08-16 13:23:47 +01009878 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009879 ssl_buffering_free_slot( ssl, offset );
9880}
9881
9882static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9883 uint8_t slot )
9884{
9885 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9886 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009887
9888 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9889 return;
9890
Hanno Beckere605b192018-08-21 15:59:07 +01009891 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009892 {
Hanno Beckere605b192018-08-21 15:59:07 +01009893 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009894 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009895 mbedtls_free( hs_buf->data );
9896 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009897 }
9898}
9899
9900#endif /* MBEDTLS_SSL_PROTO_DTLS */
9901
Gilles Peskine9b562d52018-04-25 20:32:43 +02009902void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009903{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009904 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9905
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009906 if( handshake == NULL )
9907 return;
9908
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009909#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9910 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9911 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009912 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009913 handshake->async_in_progress = 0;
9914 }
9915#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9916
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009917#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9918 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9919 mbedtls_md5_free( &handshake->fin_md5 );
9920 mbedtls_sha1_free( &handshake->fin_sha1 );
9921#endif
9922#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9923#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009924#if defined(MBEDTLS_USE_PSA_CRYPTO)
9925 psa_hash_abort( &handshake->fin_sha256_psa );
9926#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009927 mbedtls_sha256_free( &handshake->fin_sha256 );
9928#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009929#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009930#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009931#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009932 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009933#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009934 mbedtls_sha512_free( &handshake->fin_sha512 );
9935#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009936#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009937#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009939#if defined(MBEDTLS_DHM_C)
9940 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009941#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009942#if defined(MBEDTLS_ECDH_C)
9943 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009944#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009945#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009946 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009947#if defined(MBEDTLS_SSL_CLI_C)
9948 mbedtls_free( handshake->ecjpake_cache );
9949 handshake->ecjpake_cache = NULL;
9950 handshake->ecjpake_cache_len = 0;
9951#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009952#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009953
Janos Follath4ae5c292016-02-10 11:27:43 +00009954#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9955 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009956 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009957 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009958#endif
9959
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009960#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9961 if( handshake->psk != NULL )
9962 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009963 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009964 mbedtls_free( handshake->psk );
9965 }
9966#endif
9967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009968#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9969 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009970 /*
9971 * Free only the linked list wrapper, not the keys themselves
9972 * since the belong to the SNI callback
9973 */
9974 if( handshake->sni_key_cert != NULL )
9975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009976 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009977
9978 while( cur != NULL )
9979 {
9980 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009981 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009982 cur = next;
9983 }
9984 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009985#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009986
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009987#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02009988 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +00009989 if( handshake->ecrs_peer_cert != NULL )
9990 {
9991 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
9992 mbedtls_free( handshake->ecrs_peer_cert );
9993 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02009994#endif
9995
Hanno Becker75173122019-02-06 16:18:31 +00009996#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9997 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9998 mbedtls_pk_free( &handshake->peer_pubkey );
9999#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010001#if defined(MBEDTLS_SSL_PROTO_DTLS)
10002 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010003 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010004 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010005#endif
10006
Hanno Becker4a63ed42019-01-08 11:39:35 +000010007#if defined(MBEDTLS_ECDH_C) && \
10008 defined(MBEDTLS_USE_PSA_CRYPTO)
10009 psa_destroy_key( handshake->ecdh_psa_privkey );
10010#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10011
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010012 mbedtls_platform_zeroize( handshake,
10013 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010014}
10015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010016void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010017{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010018 if( session == NULL )
10019 return;
10020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010021#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010022 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010023#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010024
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010025#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010026 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010027#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010028
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010029 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010030}
10031
Paul Bakker5121ce52009-01-03 21:22:43 +000010032/*
10033 * Free an SSL context
10034 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010035void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010036{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010037 if( ssl == NULL )
10038 return;
10039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010041
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010042 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010043 {
Angus Grattond8213d02016-05-25 20:56:48 +100010044 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010045 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010046 }
10047
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010048 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010049 {
Angus Grattond8213d02016-05-25 20:56:48 +100010050 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010051 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010052 }
10053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010054#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010055 if( ssl->compress_buf != NULL )
10056 {
Angus Grattond8213d02016-05-25 20:56:48 +100010057 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010058 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010059 }
10060#endif
10061
Paul Bakker48916f92012-09-16 19:57:18 +000010062 if( ssl->transform )
10063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010064 mbedtls_ssl_transform_free( ssl->transform );
10065 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010066 }
10067
10068 if( ssl->handshake )
10069 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010070 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010071 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10072 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010074 mbedtls_free( ssl->handshake );
10075 mbedtls_free( ssl->transform_negotiate );
10076 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010077 }
10078
Paul Bakkerc0463502013-02-14 11:19:38 +010010079 if( ssl->session )
10080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010081 mbedtls_ssl_session_free( ssl->session );
10082 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010083 }
10084
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010085#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010086 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010087 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010088 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010089 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010090 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010091#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010093#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10094 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10097 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010098 }
10099#endif
10100
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010101#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010102 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010103#endif
10104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010106
Paul Bakker86f04f42013-02-14 11:20:09 +010010107 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010108 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010109}
10110
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010111/*
10112 * Initialze mbedtls_ssl_config
10113 */
10114void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10115{
10116 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10117}
10118
Simon Butcherc97b6972015-12-27 23:48:17 +000010119#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010120static int ssl_preset_default_hashes[] = {
10121#if defined(MBEDTLS_SHA512_C)
10122 MBEDTLS_MD_SHA512,
10123 MBEDTLS_MD_SHA384,
10124#endif
10125#if defined(MBEDTLS_SHA256_C)
10126 MBEDTLS_MD_SHA256,
10127 MBEDTLS_MD_SHA224,
10128#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010129#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010130 MBEDTLS_MD_SHA1,
10131#endif
10132 MBEDTLS_MD_NONE
10133};
Simon Butcherc97b6972015-12-27 23:48:17 +000010134#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010135
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010136static int ssl_preset_suiteb_ciphersuites[] = {
10137 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10138 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10139 0
10140};
10141
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010142#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010143static int ssl_preset_suiteb_hashes[] = {
10144 MBEDTLS_MD_SHA256,
10145 MBEDTLS_MD_SHA384,
10146 MBEDTLS_MD_NONE
10147};
10148#endif
10149
10150#if defined(MBEDTLS_ECP_C)
10151static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10152 MBEDTLS_ECP_DP_SECP256R1,
10153 MBEDTLS_ECP_DP_SECP384R1,
10154 MBEDTLS_ECP_DP_NONE
10155};
10156#endif
10157
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010158/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010159 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010160 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010161int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010162 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010163{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010164#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010165 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010166#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010167
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010168 /* Use the functions here so that they are covered in tests,
10169 * but otherwise access member directly for efficiency */
10170 mbedtls_ssl_conf_endpoint( conf, endpoint );
10171 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010172
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010173 /*
10174 * Things that are common to all presets
10175 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010176#if defined(MBEDTLS_SSL_CLI_C)
10177 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10178 {
10179 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10180#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10181 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10182#endif
10183 }
10184#endif
10185
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010186#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010187 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010188#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010189
10190#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10191 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10192#endif
10193
10194#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10195 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10196#endif
10197
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010198#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10199 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10200#endif
10201
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010202#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010203 conf->f_cookie_write = ssl_cookie_write_dummy;
10204 conf->f_cookie_check = ssl_cookie_check_dummy;
10205#endif
10206
10207#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10208 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10209#endif
10210
Janos Follath088ce432017-04-10 12:42:31 +010010211#if defined(MBEDTLS_SSL_SRV_C)
10212 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10213#endif
10214
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010215#if defined(MBEDTLS_SSL_PROTO_DTLS)
10216 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10217 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10218#endif
10219
10220#if defined(MBEDTLS_SSL_RENEGOTIATION)
10221 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010222 memset( conf->renego_period, 0x00, 2 );
10223 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010224#endif
10225
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010226#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10227 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10228 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010229 const unsigned char dhm_p[] =
10230 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10231 const unsigned char dhm_g[] =
10232 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10233
Hanno Beckera90658f2017-10-04 15:29:08 +010010234 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10235 dhm_p, sizeof( dhm_p ),
10236 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010237 {
10238 return( ret );
10239 }
10240 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010241#endif
10242
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010243 /*
10244 * Preset-specific defaults
10245 */
10246 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010247 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010248 /*
10249 * NSA Suite B
10250 */
10251 case MBEDTLS_SSL_PRESET_SUITEB:
10252 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10253 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10254 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10255 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10256
10257 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10258 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10259 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10260 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10261 ssl_preset_suiteb_ciphersuites;
10262
10263#if defined(MBEDTLS_X509_CRT_PARSE_C)
10264 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010265#endif
10266
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010267#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010268 conf->sig_hashes = ssl_preset_suiteb_hashes;
10269#endif
10270
10271#if defined(MBEDTLS_ECP_C)
10272 conf->curve_list = ssl_preset_suiteb_curves;
10273#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010274 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010275
10276 /*
10277 * Default
10278 */
10279 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010280 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10281 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10282 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10283 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10284 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10285 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10286 MBEDTLS_SSL_MIN_MINOR_VERSION :
10287 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010288 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10289 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10290
10291#if defined(MBEDTLS_SSL_PROTO_DTLS)
10292 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10293 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10294#endif
10295
10296 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10297 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10298 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10299 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10300 mbedtls_ssl_list_ciphersuites();
10301
10302#if defined(MBEDTLS_X509_CRT_PARSE_C)
10303 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10304#endif
10305
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010306#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010307 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010308#endif
10309
10310#if defined(MBEDTLS_ECP_C)
10311 conf->curve_list = mbedtls_ecp_grp_id_list();
10312#endif
10313
10314#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10315 conf->dhm_min_bitlen = 1024;
10316#endif
10317 }
10318
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010319 return( 0 );
10320}
10321
10322/*
10323 * Free mbedtls_ssl_config
10324 */
10325void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10326{
10327#if defined(MBEDTLS_DHM_C)
10328 mbedtls_mpi_free( &conf->dhm_P );
10329 mbedtls_mpi_free( &conf->dhm_G );
10330#endif
10331
10332#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10333 if( conf->psk != NULL )
10334 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010335 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010336 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010337 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010338 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010339 }
10340
10341 if( conf->psk_identity != NULL )
10342 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010343 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010344 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010345 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010346 conf->psk_identity_len = 0;
10347 }
10348#endif
10349
10350#if defined(MBEDTLS_X509_CRT_PARSE_C)
10351 ssl_key_cert_free( conf->key_cert );
10352#endif
10353
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010354 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010355}
10356
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010357#if defined(MBEDTLS_PK_C) && \
10358 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010359/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010360 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010362unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010363{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010364#if defined(MBEDTLS_RSA_C)
10365 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10366 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010367#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010368#if defined(MBEDTLS_ECDSA_C)
10369 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10370 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010371#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010372 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010373}
10374
Hanno Becker7e5437a2017-04-28 17:15:26 +010010375unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10376{
10377 switch( type ) {
10378 case MBEDTLS_PK_RSA:
10379 return( MBEDTLS_SSL_SIG_RSA );
10380 case MBEDTLS_PK_ECDSA:
10381 case MBEDTLS_PK_ECKEY:
10382 return( MBEDTLS_SSL_SIG_ECDSA );
10383 default:
10384 return( MBEDTLS_SSL_SIG_ANON );
10385 }
10386}
10387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010388mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010389{
10390 switch( sig )
10391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010392#if defined(MBEDTLS_RSA_C)
10393 case MBEDTLS_SSL_SIG_RSA:
10394 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010395#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010396#if defined(MBEDTLS_ECDSA_C)
10397 case MBEDTLS_SSL_SIG_ECDSA:
10398 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010399#endif
10400 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010401 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010402 }
10403}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010404#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010405
Hanno Becker7e5437a2017-04-28 17:15:26 +010010406#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10407 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10408
10409/* Find an entry in a signature-hash set matching a given hash algorithm. */
10410mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10411 mbedtls_pk_type_t sig_alg )
10412{
10413 switch( sig_alg )
10414 {
10415 case MBEDTLS_PK_RSA:
10416 return( set->rsa );
10417 case MBEDTLS_PK_ECDSA:
10418 return( set->ecdsa );
10419 default:
10420 return( MBEDTLS_MD_NONE );
10421 }
10422}
10423
10424/* Add a signature-hash-pair to a signature-hash set */
10425void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10426 mbedtls_pk_type_t sig_alg,
10427 mbedtls_md_type_t md_alg )
10428{
10429 switch( sig_alg )
10430 {
10431 case MBEDTLS_PK_RSA:
10432 if( set->rsa == MBEDTLS_MD_NONE )
10433 set->rsa = md_alg;
10434 break;
10435
10436 case MBEDTLS_PK_ECDSA:
10437 if( set->ecdsa == MBEDTLS_MD_NONE )
10438 set->ecdsa = md_alg;
10439 break;
10440
10441 default:
10442 break;
10443 }
10444}
10445
10446/* Allow exactly one hash algorithm for each signature. */
10447void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10448 mbedtls_md_type_t md_alg )
10449{
10450 set->rsa = md_alg;
10451 set->ecdsa = md_alg;
10452}
10453
10454#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10455 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10456
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010457/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010458 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010460mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010461{
10462 switch( hash )
10463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010464#if defined(MBEDTLS_MD5_C)
10465 case MBEDTLS_SSL_HASH_MD5:
10466 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010467#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010468#if defined(MBEDTLS_SHA1_C)
10469 case MBEDTLS_SSL_HASH_SHA1:
10470 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010471#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010472#if defined(MBEDTLS_SHA256_C)
10473 case MBEDTLS_SSL_HASH_SHA224:
10474 return( MBEDTLS_MD_SHA224 );
10475 case MBEDTLS_SSL_HASH_SHA256:
10476 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010477#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010478#if defined(MBEDTLS_SHA512_C)
10479 case MBEDTLS_SSL_HASH_SHA384:
10480 return( MBEDTLS_MD_SHA384 );
10481 case MBEDTLS_SSL_HASH_SHA512:
10482 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010483#endif
10484 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010485 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010486 }
10487}
10488
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010489/*
10490 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10491 */
10492unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10493{
10494 switch( md )
10495 {
10496#if defined(MBEDTLS_MD5_C)
10497 case MBEDTLS_MD_MD5:
10498 return( MBEDTLS_SSL_HASH_MD5 );
10499#endif
10500#if defined(MBEDTLS_SHA1_C)
10501 case MBEDTLS_MD_SHA1:
10502 return( MBEDTLS_SSL_HASH_SHA1 );
10503#endif
10504#if defined(MBEDTLS_SHA256_C)
10505 case MBEDTLS_MD_SHA224:
10506 return( MBEDTLS_SSL_HASH_SHA224 );
10507 case MBEDTLS_MD_SHA256:
10508 return( MBEDTLS_SSL_HASH_SHA256 );
10509#endif
10510#if defined(MBEDTLS_SHA512_C)
10511 case MBEDTLS_MD_SHA384:
10512 return( MBEDTLS_SSL_HASH_SHA384 );
10513 case MBEDTLS_MD_SHA512:
10514 return( MBEDTLS_SSL_HASH_SHA512 );
10515#endif
10516 default:
10517 return( MBEDTLS_SSL_HASH_NONE );
10518 }
10519}
10520
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010521#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010522/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010523 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010524 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010525 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010526int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010527{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010528 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010529
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010530 if( ssl->conf->curve_list == NULL )
10531 return( -1 );
10532
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010533 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010534 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010535 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010536
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010537 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010538}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010539#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010540
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010541#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010542/*
10543 * Check if a hash proposed by the peer is in our list.
10544 * Return 0 if we're willing to use it, -1 otherwise.
10545 */
10546int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10547 mbedtls_md_type_t md )
10548{
10549 const int *cur;
10550
10551 if( ssl->conf->sig_hashes == NULL )
10552 return( -1 );
10553
10554 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10555 if( *cur == (int) md )
10556 return( 0 );
10557
10558 return( -1 );
10559}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010560#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010562#if defined(MBEDTLS_X509_CRT_PARSE_C)
10563int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10564 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010565 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010566 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010567{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010568 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010569#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010570 int usage = 0;
10571#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010572#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010573 const char *ext_oid;
10574 size_t ext_len;
10575#endif
10576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010577#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10578 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010579 ((void) cert);
10580 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010581 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010582#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010584#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10585 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010586 {
10587 /* Server part of the key exchange */
10588 switch( ciphersuite->key_exchange )
10589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010590 case MBEDTLS_KEY_EXCHANGE_RSA:
10591 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010592 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010593 break;
10594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010595 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10596 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10597 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10598 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010599 break;
10600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010601 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10602 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010603 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010604 break;
10605
10606 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010607 case MBEDTLS_KEY_EXCHANGE_NONE:
10608 case MBEDTLS_KEY_EXCHANGE_PSK:
10609 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10610 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010611 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010612 usage = 0;
10613 }
10614 }
10615 else
10616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010617 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10618 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010619 }
10620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010621 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010622 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010623 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010624 ret = -1;
10625 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010626#else
10627 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010628#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010630#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10631 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010633 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10634 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010635 }
10636 else
10637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010638 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10639 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010640 }
10641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010642 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010643 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010644 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010645 ret = -1;
10646 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010647#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010648
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010649 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010650}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010651#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010652
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010653/*
10654 * Convert version numbers to/from wire format
10655 * and, for DTLS, to/from TLS equivalent.
10656 *
10657 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010658 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010659 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10660 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10661 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010662void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010663 unsigned char ver[2] )
10664{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010665#if defined(MBEDTLS_SSL_PROTO_DTLS)
10666 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010668 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010669 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10670
10671 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10672 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10673 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010674 else
10675#else
10676 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010677#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010678 {
10679 ver[0] = (unsigned char) major;
10680 ver[1] = (unsigned char) minor;
10681 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010682}
10683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010684void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010685 const unsigned char ver[2] )
10686{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010687#if defined(MBEDTLS_SSL_PROTO_DTLS)
10688 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010689 {
10690 *major = 255 - ver[0] + 2;
10691 *minor = 255 - ver[1] + 1;
10692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010693 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010694 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10695 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010696 else
10697#else
10698 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010699#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010700 {
10701 *major = ver[0];
10702 *minor = ver[1];
10703 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010704}
10705
Simon Butcher99000142016-10-13 17:21:01 +010010706int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10707{
10708#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10709 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10710 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10711
10712 switch( md )
10713 {
10714#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10715#if defined(MBEDTLS_MD5_C)
10716 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010717 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010718#endif
10719#if defined(MBEDTLS_SHA1_C)
10720 case MBEDTLS_SSL_HASH_SHA1:
10721 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10722 break;
10723#endif
10724#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10725#if defined(MBEDTLS_SHA512_C)
10726 case MBEDTLS_SSL_HASH_SHA384:
10727 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10728 break;
10729#endif
10730#if defined(MBEDTLS_SHA256_C)
10731 case MBEDTLS_SSL_HASH_SHA256:
10732 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10733 break;
10734#endif
10735 default:
10736 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10737 }
10738
10739 return 0;
10740#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10741 (void) ssl;
10742 (void) md;
10743
10744 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10745#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10746}
10747
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010748#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10749 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10750int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10751 unsigned char *output,
10752 unsigned char *data, size_t data_len )
10753{
10754 int ret = 0;
10755 mbedtls_md5_context mbedtls_md5;
10756 mbedtls_sha1_context mbedtls_sha1;
10757
10758 mbedtls_md5_init( &mbedtls_md5 );
10759 mbedtls_sha1_init( &mbedtls_sha1 );
10760
10761 /*
10762 * digitally-signed struct {
10763 * opaque md5_hash[16];
10764 * opaque sha_hash[20];
10765 * };
10766 *
10767 * md5_hash
10768 * MD5(ClientHello.random + ServerHello.random
10769 * + ServerParams);
10770 * sha_hash
10771 * SHA(ClientHello.random + ServerHello.random
10772 * + ServerParams);
10773 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010774 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010775 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010776 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010777 goto exit;
10778 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010779 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010780 ssl->handshake->randbytes, 64 ) ) != 0 )
10781 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010782 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010783 goto exit;
10784 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010785 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010786 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010787 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010788 goto exit;
10789 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010790 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010791 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010792 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010793 goto exit;
10794 }
10795
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010796 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010797 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010798 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010799 goto exit;
10800 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010801 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010802 ssl->handshake->randbytes, 64 ) ) != 0 )
10803 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010804 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010805 goto exit;
10806 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010807 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010808 data_len ) ) != 0 )
10809 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010810 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010811 goto exit;
10812 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010813 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010814 output + 16 ) ) != 0 )
10815 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010817 goto exit;
10818 }
10819
10820exit:
10821 mbedtls_md5_free( &mbedtls_md5 );
10822 mbedtls_sha1_free( &mbedtls_sha1 );
10823
10824 if( ret != 0 )
10825 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10826 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10827
10828 return( ret );
10829
10830}
10831#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10832 MBEDTLS_SSL_PROTO_TLS1_1 */
10833
10834#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10835 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010836
10837#if defined(MBEDTLS_USE_PSA_CRYPTO)
10838int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10839 unsigned char *hash, size_t *hashlen,
10840 unsigned char *data, size_t data_len,
10841 mbedtls_md_type_t md_alg )
10842{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010843 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010844 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010845 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10846
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010847 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010848
10849 if( ( status = psa_hash_setup( &hash_operation,
10850 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010851 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010852 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010853 goto exit;
10854 }
10855
Andrzej Kurek814feff2019-01-14 04:35:19 -050010856 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10857 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010858 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010859 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010860 goto exit;
10861 }
10862
Andrzej Kurek814feff2019-01-14 04:35:19 -050010863 if( ( status = psa_hash_update( &hash_operation,
10864 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010865 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010866 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010867 goto exit;
10868 }
10869
Andrzej Kurek814feff2019-01-14 04:35:19 -050010870 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10871 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010872 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010873 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010874 goto exit;
10875 }
10876
10877exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010878 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010879 {
10880 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10881 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010882 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010883 {
10884 case PSA_ERROR_NOT_SUPPORTED:
10885 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010886 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010887 case PSA_ERROR_BUFFER_TOO_SMALL:
10888 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10889 case PSA_ERROR_INSUFFICIENT_MEMORY:
10890 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10891 default:
10892 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10893 }
10894 }
10895 return( 0 );
10896}
10897
10898#else
10899
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010900int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010901 unsigned char *hash, size_t *hashlen,
10902 unsigned char *data, size_t data_len,
10903 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010904{
10905 int ret = 0;
10906 mbedtls_md_context_t ctx;
10907 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010908 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010909
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010910 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010911
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010912 mbedtls_md_init( &ctx );
10913
10914 /*
10915 * digitally-signed struct {
10916 * opaque client_random[32];
10917 * opaque server_random[32];
10918 * ServerDHParams params;
10919 * };
10920 */
10921 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10922 {
10923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10924 goto exit;
10925 }
10926 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10927 {
10928 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10929 goto exit;
10930 }
10931 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10932 {
10933 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10934 goto exit;
10935 }
10936 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10937 {
10938 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10939 goto exit;
10940 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010941 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010942 {
10943 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10944 goto exit;
10945 }
10946
10947exit:
10948 mbedtls_md_free( &ctx );
10949
10950 if( ret != 0 )
10951 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10952 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10953
10954 return( ret );
10955}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010956#endif /* MBEDTLS_USE_PSA_CRYPTO */
10957
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010958#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10959 MBEDTLS_SSL_PROTO_TLS1_2 */
10960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010961#endif /* MBEDTLS_SSL_TLS_C */