blob: 2750b4e51f091f6dd95bb80903a28b6bb6b0b149 [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
Hanno Becker4bf74652019-04-26 16:22:27 +0100936#if defined(MBEDTLS_SSL_CID)
937 /* Copy own and peer's CID if the use of the CID
938 * extension has been negotiated. */
939 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
940 {
941 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
942 transform->in_cid_len = ssl->own_cid_len;
943 transform->out_cid_len = ssl->handshake->peer_cid_len;
944 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
945 memcpy( transform->out_cid, ssl->handshake->peer_cid,
946 ssl->handshake->peer_cid_len );
947
948 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
949 transform->out_cid_len );
950 MBEDTLS_SSL_DEBUG_BUF( 3, "Ingoing CID", transform->in_cid,
951 transform->in_cid_len );
952 }
953#endif /* MBEDTLS_SSL_CID */
954
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000956 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000957 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958#if defined(MBEDTLS_SSL_PROTO_SSL3)
959 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000960 {
Paul Bakker48916f92012-09-16 19:57:18 +0000961 handshake->tls_prf = ssl3_prf;
962 handshake->calc_verify = ssl_calc_verify_ssl;
963 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000964 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200965 else
966#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
968 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000969 {
Paul Bakker48916f92012-09-16 19:57:18 +0000970 handshake->tls_prf = tls1_prf;
971 handshake->calc_verify = ssl_calc_verify_tls;
972 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000973 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200974 else
975#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
977#if defined(MBEDTLS_SHA512_C)
978 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +0000979 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000980 {
Paul Bakker48916f92012-09-16 19:57:18 +0000981 handshake->tls_prf = tls_prf_sha384;
982 handshake->calc_verify = ssl_calc_verify_tls_sha384;
983 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000984 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000985 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200986#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987#if defined(MBEDTLS_SHA256_C)
988 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000989 {
Paul Bakker48916f92012-09-16 19:57:18 +0000990 handshake->tls_prf = tls_prf_sha256;
991 handshake->calc_verify = ssl_calc_verify_tls_sha256;
992 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000993 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200994 else
995#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
999 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001000 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001001
1002 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 * SSLv3:
1004 * master =
1005 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1006 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1007 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001008 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001009 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001010 * master = PRF( premaster, "master secret", randbytes )[0..47]
1011 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001012 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001013 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001014 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1015 }
1016 else
1017 {
1018 /* The label for the KDF used for key expansion.
1019 * This is either "master secret" or "extended master secret"
1020 * depending on whether the Extended Master Secret extension
1021 * is used. */
1022 char const *lbl = "master secret";
1023
1024 /* The salt for the KDF used for key expansion.
1025 * - If the Extended Master Secret extension is not used,
1026 * this is ClientHello.Random + ServerHello.Random
1027 * (see Sect. 8.1 in RFC 5246).
1028 * - If the Extended Master Secret extension is used,
1029 * this is the transcript of the handshake so far.
1030 * (see Sect. 4 in RFC 7627). */
1031 unsigned char const *salt = handshake->randbytes;
1032 size_t salt_len = 64;
1033
1034#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001038
Hanno Becker35b23c72018-10-23 12:10:41 +01001039 lbl = "extended master secret";
1040 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001041 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1043 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001046 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1047 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001048 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001049 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001050 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001051#endif /* MBEDTLS_SHA512_C */
1052 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001053 }
1054 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001056 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001057
Hanno Becker35b23c72018-10-23 12:10:41 +01001058 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001059 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001060#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1061
Hanno Becker7d0a5692018-10-23 15:26:22 +01001062#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1063 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1064 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1065 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1066 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001067 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001068 /* Perform PSK-to-MS expansion in a single step. */
1069 psa_status_t status;
1070 psa_algorithm_t alg;
1071 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001072 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001073
1074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1075
1076 psk = ssl->conf->psk_opaque;
1077 if( ssl->handshake->psk_opaque != 0 )
1078 psk = ssl->handshake->psk_opaque;
1079
Hanno Becker22bf1452019-04-05 11:21:08 +01001080 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001081 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1082 else
1083 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1084
1085 status = psa_key_derivation( &generator, psk, alg,
1086 salt, salt_len,
1087 (unsigned char const *) lbl,
1088 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001089 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001090 if( status != PSA_SUCCESS )
1091 {
1092 psa_generator_abort( &generator );
1093 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1094 }
1095
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001096 status = psa_generator_read( &generator, session->master,
1097 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001098 if( status != PSA_SUCCESS )
1099 {
1100 psa_generator_abort( &generator );
1101 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1102 }
1103
1104 status = psa_generator_abort( &generator );
1105 if( status != PSA_SUCCESS )
1106 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001107 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001108 else
1109#endif
1110 {
1111 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1112 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001113 session->master,
1114 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001115 if( ret != 0 )
1116 {
1117 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1118 return( ret );
1119 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001120
Hanno Becker7d0a5692018-10-23 15:26:22 +01001121 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1122 handshake->premaster,
1123 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001124
Hanno Becker7d0a5692018-10-23 15:26:22 +01001125 mbedtls_platform_zeroize( handshake->premaster,
1126 sizeof(handshake->premaster) );
1127 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001128 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001129
1130 /*
1131 * Swap the client and server random values.
1132 */
Paul Bakker48916f92012-09-16 19:57:18 +00001133 memcpy( tmp, handshake->randbytes, 64 );
1134 memcpy( handshake->randbytes, tmp + 32, 32 );
1135 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001136 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
1138 /*
1139 * SSLv3:
1140 * key block =
1141 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1142 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1143 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1144 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1145 * ...
1146 *
1147 * TLSv1:
1148 * key block = PRF( master, "key expansion", randbytes )
1149 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001150 ret = handshake->tls_prf( session->master, 48, "key expansion",
1151 handshake->randbytes, 64, keyblk, 256 );
1152 if( ret != 0 )
1153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001155 return( ret );
1156 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1159 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1160 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1161 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1162 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001163
Paul Bakker5121ce52009-01-03 21:22:43 +00001164 /*
1165 * Determine the appropriate key, IV and MAC length.
1166 */
Paul Bakker68884e32013-01-07 18:20:04 +01001167
Hanno Becker88aaf652017-12-27 08:17:40 +00001168 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001169
Hanno Becker8031d062018-01-03 15:32:31 +00001170#if defined(MBEDTLS_GCM_C) || \
1171 defined(MBEDTLS_CCM_C) || \
1172 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001174 cipher_info->mode == MBEDTLS_MODE_CCM ||
1175 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001176 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001177 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001178
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001179 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001180 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001181 transform->taglen =
1182 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001183
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001184 /* All modes haves 96-bit IVs;
1185 * GCM and CCM has 4 implicit and 8 explicit bytes
1186 * ChachaPoly has all 12 bytes implicit
1187 */
Paul Bakker68884e32013-01-07 18:20:04 +01001188 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001189 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1190 transform->fixed_ivlen = 12;
1191 else
1192 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001193
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001194 /* Minimum length of encrypted record */
1195 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001196 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001197 }
1198 else
Hanno Becker8031d062018-01-03 15:32:31 +00001199#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1200#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1201 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1202 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001203 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001204 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1206 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001209 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001210 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001211
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001212 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001213 mac_key_len = mbedtls_md_get_size( md_info );
1214 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001217 /*
1218 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1219 * (rfc 6066 page 13 or rfc 2104 section 4),
1220 * so we only need to adjust the length here.
1221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001225
1226#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1227 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001228 * HMAC implementation which also truncates the key
1229 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001230 mac_key_len = transform->maclen;
1231#endif
1232 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001234
1235 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001236 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001237
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001238 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001240 transform->minlen = transform->maclen;
1241 else
Paul Bakker68884e32013-01-07 18:20:04 +01001242 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001243 /*
1244 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001245 * 1. if EtM is in use: one block plus MAC
1246 * otherwise: * first multiple of blocklen greater than maclen
1247 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1250 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001251 {
1252 transform->minlen = transform->maclen
1253 + cipher_info->block_size;
1254 }
1255 else
1256#endif
1257 {
1258 transform->minlen = transform->maclen
1259 + cipher_info->block_size
1260 - transform->maclen % cipher_info->block_size;
1261 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1264 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1265 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001266 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001267 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001268#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1270 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1271 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001272 {
1273 transform->minlen += transform->ivlen;
1274 }
1275 else
1276#endif
1277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001279 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1280 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001281 }
Paul Bakker68884e32013-01-07 18:20:04 +01001282 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 }
Hanno Becker8031d062018-01-03 15:32:31 +00001284 else
1285#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1286 {
1287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1288 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1289 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001290
Hanno Becker88aaf652017-12-27 08:17:40 +00001291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1292 (unsigned) keylen,
1293 (unsigned) transform->minlen,
1294 (unsigned) transform->ivlen,
1295 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001296
1297 /*
1298 * Finally setup the cipher contexts, IVs and MAC secrets.
1299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001301 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001302 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001303 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001304 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001305
Paul Bakker68884e32013-01-07 18:20:04 +01001306 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001307 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001308
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001309 /*
1310 * This is not used in TLS v1.1.
1311 */
Paul Bakker48916f92012-09-16 19:57:18 +00001312 iv_copy_len = ( transform->fixed_ivlen ) ?
1313 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001314 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1315 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001316 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001317 }
1318 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319#endif /* MBEDTLS_SSL_CLI_C */
1320#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001321 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001323 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001324 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001325
Hanno Becker81c7b182017-11-09 18:39:33 +00001326 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001327 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001329 /*
1330 * This is not used in TLS v1.1.
1331 */
Paul Bakker48916f92012-09-16 19:57:18 +00001332 iv_copy_len = ( transform->fixed_ivlen ) ?
1333 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001334 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1335 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001336 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001338 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001342 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1343 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001344 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001345
Hanno Beckerd56ed242018-01-03 15:32:51 +00001346#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347#if defined(MBEDTLS_SSL_PROTO_SSL3)
1348 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001349 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001350 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001353 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1354 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001355 }
1356
Hanno Becker81c7b182017-11-09 18:39:33 +00001357 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1358 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001359 }
1360 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1362#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1363 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1364 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001365 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001366 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1367 For AEAD-based ciphersuites, there is nothing to do here. */
1368 if( mac_key_len != 0 )
1369 {
1370 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1371 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1372 }
Paul Bakker68884e32013-01-07 18:20:04 +01001373 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001374 else
1375#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001378 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1379 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001380 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001381#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1384 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001385 {
1386 int ret = 0;
1387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001389
Hanno Becker88aaf652017-12-27 08:17:40 +00001390 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001391 transform->iv_enc, transform->iv_dec,
1392 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001393 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001394 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001397 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1398 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001399 }
1400 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001401#else
1402 ((void) mac_dec);
1403 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001405
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001406#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1407 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001408 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001409 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1410 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001411 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001412 iv_copy_len );
1413 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001414
1415 if( ssl->conf->f_export_keys_ext != NULL )
1416 {
1417 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1418 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001419 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001420 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001421 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001422 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001423 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001424 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001425#endif
1426
Hanno Beckerf704bef2018-11-16 15:21:18 +00001427#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001428
1429 /* Only use PSA-based ciphers for TLS-1.2.
1430 * That's relevant at least for TLS-1.0, where
1431 * we assume that mbedtls_cipher_crypt() updates
1432 * the structure field for the IV, which the PSA-based
1433 * implementation currently doesn't. */
1434#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1435 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001436 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001437 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001438 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001439 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1440 {
1441 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001442 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001443 }
1444
1445 if( ret == 0 )
1446 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001447 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001448 psa_fallthrough = 0;
1449 }
1450 else
1451 {
1452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1453 psa_fallthrough = 1;
1454 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001455 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001456 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001457 psa_fallthrough = 1;
1458#else
1459 psa_fallthrough = 1;
1460#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001461
Hanno Beckercb1cc802018-11-17 22:27:38 +00001462 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001463#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001464 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001465 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001466 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001468 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001469 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001470
Hanno Beckerf704bef2018-11-16 15:21:18 +00001471#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001472 /* Only use PSA-based ciphers for TLS-1.2.
1473 * That's relevant at least for TLS-1.0, where
1474 * we assume that mbedtls_cipher_crypt() updates
1475 * the structure field for the IV, which the PSA-based
1476 * implementation currently doesn't. */
1477#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1478 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001479 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001480 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001481 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001482 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1483 {
1484 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001485 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001486 }
1487
1488 if( ret == 0 )
1489 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001491 psa_fallthrough = 0;
1492 }
1493 else
1494 {
1495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1496 psa_fallthrough = 1;
1497 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001498 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001499 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001500 psa_fallthrough = 1;
1501#else
1502 psa_fallthrough = 1;
1503#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001504
Hanno Beckercb1cc802018-11-17 22:27:38 +00001505 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001506#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001507 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001508 cipher_info ) ) != 0 )
1509 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001510 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001511 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001512 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001515 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001519 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001520 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001523 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001527 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001528 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#if defined(MBEDTLS_CIPHER_MODE_CBC)
1531 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1534 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001537 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001538 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1541 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001544 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001545 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001546 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001548
Paul Bakker5121ce52009-01-03 21:22:43 +00001549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001551 // Initialize compression
1552 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001554 {
Paul Bakker16770332013-10-11 09:59:44 +02001555 if( ssl->compress_buf == NULL )
1556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001558 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001559 if( ssl->compress_buf == NULL )
1560 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001562 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001563 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1564 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001565 }
1566 }
1567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001569
Paul Bakker48916f92012-09-16 19:57:18 +00001570 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1571 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001572
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001573 if( deflateInit( &transform->ctx_deflate,
1574 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001575 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001578 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1579 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001580 }
1581 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001585end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001586 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1587 mbedtls_platform_zeroize( handshake->randbytes,
1588 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001589 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001590}
1591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592#if defined(MBEDTLS_SSL_PROTO_SSL3)
1593void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001594{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001595 mbedtls_md5_context md5;
1596 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001597 unsigned char pad_1[48];
1598 unsigned char pad_2[48];
1599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001601
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001602 mbedtls_md5_init( &md5 );
1603 mbedtls_sha1_init( &sha1 );
1604
1605 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1606 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001607
Paul Bakker380da532012-04-18 16:10:25 +00001608 memset( pad_1, 0x36, 48 );
1609 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001610
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001611 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1612 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1613 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001614
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001615 mbedtls_md5_starts_ret( &md5 );
1616 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1617 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1618 mbedtls_md5_update_ret( &md5, hash, 16 );
1619 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001620
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001621 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1622 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1623 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001624
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001625 mbedtls_sha1_starts_ret( &sha1 );
1626 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1627 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1628 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1629 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001633
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001634 mbedtls_md5_free( &md5 );
1635 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001636
Paul Bakker380da532012-04-18 16:10:25 +00001637 return;
1638}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1642void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001643{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001644 mbedtls_md5_context md5;
1645 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001648
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001649 mbedtls_md5_init( &md5 );
1650 mbedtls_sha1_init( &sha1 );
1651
1652 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1653 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001654
Andrzej Kurekeb342242019-01-29 09:14:33 -05001655 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001656 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001660
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001661 mbedtls_md5_free( &md5 );
1662 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001663
Paul Bakker380da532012-04-18 16:10:25 +00001664 return;
1665}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1669#if defined(MBEDTLS_SHA256_C)
1670void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001671{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001672#if defined(MBEDTLS_USE_PSA_CRYPTO)
1673 size_t hash_size;
1674 psa_status_t status;
1675 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1676
1677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1678 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1679 if( status != PSA_SUCCESS )
1680 {
1681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1682 return;
1683 }
1684
1685 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1686 if( status != PSA_SUCCESS )
1687 {
1688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1689 return;
1690 }
1691 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1693#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001694 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001695
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001696 mbedtls_sha256_init( &sha256 );
1697
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001699
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001700 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001701 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001705
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001706 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001707#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001708 return;
1709}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712#if defined(MBEDTLS_SHA512_C)
1713void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001714{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001715#if defined(MBEDTLS_USE_PSA_CRYPTO)
1716 size_t hash_size;
1717 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001718 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001719
1720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001721 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001722 if( status != PSA_SUCCESS )
1723 {
1724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1725 return;
1726 }
1727
Andrzej Kurek972fba52019-01-30 03:29:12 -05001728 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001729 if( status != PSA_SUCCESS )
1730 {
1731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1732 return;
1733 }
1734 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1736#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001737 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001738
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001739 mbedtls_sha512_init( &sha512 );
1740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001742
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001743 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001744 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001748
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001749 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001750#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001751 return;
1752}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753#endif /* MBEDTLS_SHA512_C */
1754#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1757int 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 +02001758{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001759 unsigned char *p = ssl->handshake->premaster;
1760 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001761 const unsigned char *psk = ssl->conf->psk;
1762 size_t psk_len = ssl->conf->psk_len;
1763
1764 /* If the psk callback was called, use its result */
1765 if( ssl->handshake->psk != NULL )
1766 {
1767 psk = ssl->handshake->psk;
1768 psk_len = ssl->handshake->psk_len;
1769 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001770
1771 /*
1772 * PMS = struct {
1773 * opaque other_secret<0..2^16-1>;
1774 * opaque psk<0..2^16-1>;
1775 * };
1776 * with "other_secret" depending on the particular key exchange
1777 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1779 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001780 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001781 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001783
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001784 *(p++) = (unsigned char)( psk_len >> 8 );
1785 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001786
1787 if( end < p || (size_t)( end - p ) < psk_len )
1788 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1789
1790 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001791 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001792 }
1793 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1795#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1796 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001797 {
1798 /*
1799 * other_secret already set by the ClientKeyExchange message,
1800 * and is 48 bytes long
1801 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001802 if( end - p < 2 )
1803 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1804
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001805 *p++ = 0;
1806 *p++ = 48;
1807 p += 48;
1808 }
1809 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1811#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1812 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001813 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001814 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001815 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001816
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001817 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001819 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001820 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001823 return( ret );
1824 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001825 *(p++) = (unsigned char)( len >> 8 );
1826 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001827 p += len;
1828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001830 }
1831 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1833#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1834 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001835 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001836 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001837 size_t zlen;
1838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001840 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001841 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001844 return( ret );
1845 }
1846
1847 *(p++) = (unsigned char)( zlen >> 8 );
1848 *(p++) = (unsigned char)( zlen );
1849 p += zlen;
1850
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001851 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1852 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001853 }
1854 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1858 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001859 }
1860
1861 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001862 if( end - p < 2 )
1863 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001864
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001865 *(p++) = (unsigned char)( psk_len >> 8 );
1866 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001867
1868 if( end < p || (size_t)( end - p ) < psk_len )
1869 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1870
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001871 memcpy( p, psk, psk_len );
1872 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001873
1874 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1875
1876 return( 0 );
1877}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001881/*
1882 * SSLv3.0 MAC functions
1883 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001884#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001885static void ssl_mac( mbedtls_md_context_t *md_ctx,
1886 const unsigned char *secret,
1887 const unsigned char *buf, size_t len,
1888 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001889 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001890{
1891 unsigned char header[11];
1892 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001893 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1895 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001896
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001897 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001899 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001900 else
Paul Bakker68884e32013-01-07 18:20:04 +01001901 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001902
1903 memcpy( header, ctr, 8 );
1904 header[ 8] = (unsigned char) type;
1905 header[ 9] = (unsigned char)( len >> 8 );
1906 header[10] = (unsigned char)( len );
1907
Paul Bakker68884e32013-01-07 18:20:04 +01001908 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 mbedtls_md_starts( md_ctx );
1910 mbedtls_md_update( md_ctx, secret, md_size );
1911 mbedtls_md_update( md_ctx, padding, padlen );
1912 mbedtls_md_update( md_ctx, header, 11 );
1913 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001914 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001915
Paul Bakker68884e32013-01-07 18:20:04 +01001916 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 mbedtls_md_starts( md_ctx );
1918 mbedtls_md_update( md_ctx, secret, md_size );
1919 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001920 mbedtls_md_update( md_ctx, out, md_size );
1921 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001922}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001924
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001925/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001926 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001927#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001928 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1929 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1930 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1931/* This function makes sure every byte in the memory region is accessed
1932 * (in ascending addresses order) */
1933static void ssl_read_memory( unsigned char *p, size_t len )
1934{
1935 unsigned char acc = 0;
1936 volatile unsigned char force;
1937
1938 for( ; len != 0; p++, len-- )
1939 acc ^= *p;
1940
1941 force = acc;
1942 (void) force;
1943}
1944#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1945
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001946/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001948 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001949
1950static void ssl_extract_add_data_from_record( unsigned char* add_data,
1951 mbedtls_record *rec )
1952{
1953 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1954 add_data[8] = rec->type;
1955 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
1956 add_data[11] = ( rec->data_len >> 8 ) & 0xFF;
1957 add_data[12] = rec->data_len & 0xFF;
1958}
1959
Hanno Beckera18d1322018-01-03 14:27:32 +00001960int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1961 mbedtls_ssl_transform *transform,
1962 mbedtls_record *rec,
1963 int (*f_rng)(void *, unsigned char *, size_t),
1964 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001965{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001967 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001968 unsigned char * data;
1969 unsigned char add_data[13];
1970 size_t post_avail;
1971
1972 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00001973#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001974 ((void) ssl);
1975#endif
1976
1977 /* The PRNG is used for dynamic IV generation that's used
1978 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1979#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1980 ( defined(MBEDTLS_AES_C) || \
1981 defined(MBEDTLS_ARIA_C) || \
1982 defined(MBEDTLS_CAMELLIA_C) ) && \
1983 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1984 ((void) f_rng);
1985 ((void) p_rng);
1986#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001989
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001990 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001991 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1993 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1994 }
1995 if( rec == NULL ||
1996 rec->buf == NULL ||
1997 rec->buf_len < rec->data_offset ||
1998 rec->buf_len - rec->data_offset < rec->data_len )
1999 {
2000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002002 }
2003
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002004 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2005 data = rec->buf + rec->data_offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002007 data, rec->data_len );
2008
2009 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2010
2011 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2012 {
2013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2014 (unsigned) rec->data_len,
2015 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2016 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2017 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002018
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002020 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002021 */
Hanno Becker52344c22018-01-03 15:24:20 +00002022#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 if( mode == MBEDTLS_MODE_STREAM ||
2024 ( mode == MBEDTLS_MODE_CBC
2025#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002026 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002027#endif
2028 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002030 if( post_avail < transform->maclen )
2031 {
2032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2033 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2034 }
2035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002037 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002038 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002039 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002040 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2041 data, rec->data_len, rec->ctr, rec->type, mac );
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2047 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002048 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002049 {
Hanno Becker992b6872017-11-09 18:57:39 +00002050 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2051
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002052 ssl_extract_add_data_from_record( add_data, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002053
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002054 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2055 sizeof( add_data ) );
2056 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2057 data, rec->data_len );
2058 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2059 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2060
2061 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002062 }
2063 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002064#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2067 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002068 }
2069
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002070 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2071 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002072
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002073 rec->data_len += transform->maclen;
2074 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002075 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002076 }
Hanno Becker52344c22018-01-03 15:24:20 +00002077#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002078
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002079 /*
2080 * Encrypt
2081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2083 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002085 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002086 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002088 "including %d bytes of padding",
2089 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002090
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002091 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2092 transform->iv_enc, transform->ivlen,
2093 data, rec->data_len,
2094 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002097 return( ret );
2098 }
2099
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002100 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2103 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002104 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 }
Paul Bakker68884e32013-01-07 18:20:04 +01002106 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002108
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002109#if defined(MBEDTLS_GCM_C) || \
2110 defined(MBEDTLS_CCM_C) || \
2111 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002113 mode == MBEDTLS_MODE_CCM ||
2114 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002115 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002116 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002117 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002118 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002119
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002120 /* Check that there's space for both the authentication tag
2121 * and the explicit IV before and after the record content. */
2122 if( post_avail < transform->taglen ||
2123 rec->data_offset < explicit_iv_len )
2124 {
2125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2126 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2127 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002128
Paul Bakker68884e32013-01-07 18:20:04 +01002129 /*
2130 * Generate IV
2131 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002132 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2133 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002134 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002135 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002136 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2137 explicit_iv_len );
2138 /* Prefix record content with explicit IV. */
2139 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002140 }
2141 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2142 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002143 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002144 unsigned char i;
2145
2146 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2147
2148 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002149 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002150 }
2151 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002152 {
2153 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2155 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002156 }
2157
Hanno Becker1f10d762019-04-26 13:34:37 +01002158 ssl_extract_add_data_from_record( add_data, rec );
2159
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002160 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2161 iv, transform->ivlen );
2162 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002163 data - explicit_iv_len, explicit_iv_len );
2164 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2165 add_data, 13 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002167 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002168 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002169
Paul Bakker68884e32013-01-07 18:20:04 +01002170 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002171 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002172 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002173
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002174 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002175 iv, transform->ivlen,
2176 add_data, 13, /* add data */
2177 data, rec->data_len, /* source */
2178 data, &rec->data_len, /* destination */
2179 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002182 return( ret );
2183 }
2184
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002185 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2186 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002187
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002188 rec->data_len += transform->taglen + explicit_iv_len;
2189 rec->data_offset -= explicit_iv_len;
2190 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002191 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002192 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2195#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002196 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002199 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002200 size_t padlen, i;
2201 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002202
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002203 /* Currently we're always using minimal padding
2204 * (up to 255 bytes would be allowed). */
2205 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2206 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002207 padlen = 0;
2208
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002209 /* Check there's enough space in the buffer for the padding. */
2210 if( post_avail < padlen + 1 )
2211 {
2212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2213 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2214 }
2215
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002217 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002218
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002219 rec->data_len += padlen + 1;
2220 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002223 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002224 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2225 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002226 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002227 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002228 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002229 if( f_rng == NULL )
2230 {
2231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2232 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2233 }
2234
2235 if( rec->data_offset < transform->ivlen )
2236 {
2237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2238 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2239 }
2240
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002241 /*
2242 * Generate IV
2243 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002244 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002245 if( ret != 0 )
2246 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002247
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002248 memcpy( data - transform->ivlen, transform->iv_enc,
2249 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002250
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002251 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002255 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002256 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002257 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002258
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002259 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2260 transform->iv_enc,
2261 transform->ivlen,
2262 data, rec->data_len,
2263 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002266 return( ret );
2267 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002268
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002269 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2272 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002273 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002276 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002277 {
2278 /*
2279 * Save IV in SSL3 and TLS1
2280 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002281 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2282 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002283 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002284 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002285#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002286 {
2287 data -= transform->ivlen;
2288 rec->data_offset -= transform->ivlen;
2289 rec->data_len += transform->ivlen;
2290 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002293 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002294 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002295 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2296
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002297 /*
2298 * MAC(MAC_write_key, seq_num +
2299 * TLSCipherText.type +
2300 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002301 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002302 * IV + // except for TLS 1.0
2303 * ENC(content + padding + padding_length));
2304 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002305
2306 if( post_avail < transform->maclen)
2307 {
2308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2309 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2310 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002311
Hanno Becker1f10d762019-04-26 13:34:37 +01002312 ssl_extract_add_data_from_record( add_data, rec );
2313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002315 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2316 sizeof( add_data ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002317
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002318 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2319 sizeof( add_data ) );
2320 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2321 data, rec->data_len );
2322 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2323 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002324
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002325 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002326
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002327 rec->data_len += transform->maclen;
2328 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002329 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002330 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002333 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002335 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2338 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002339 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002340
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002341 /* Make extra sure authentication was performed, exactly once */
2342 if( auth_done != 1 )
2343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2345 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002346 }
2347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002349
2350 return( 0 );
2351}
2352
Hanno Beckera18d1322018-01-03 14:27:32 +00002353int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2354 mbedtls_ssl_transform *transform,
2355 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002356{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002357 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002359 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002360#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002361 size_t padlen = 0, correct = 1;
2362#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002363 unsigned char* data;
2364 unsigned char add_data[13];
2365
Hanno Beckera18d1322018-01-03 14:27:32 +00002366#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002367 ((void) ssl);
2368#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002371 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002372 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2374 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2375 }
2376 if( rec == NULL ||
2377 rec->buf == NULL ||
2378 rec->buf_len < rec->data_offset ||
2379 rec->buf_len - rec->data_offset < rec->data_len )
2380 {
2381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002383 }
2384
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002385 data = rec->buf + rec->data_offset;
2386 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2389 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002390 {
2391 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002392 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2393 transform->iv_dec,
2394 transform->ivlen,
2395 data, rec->data_len,
2396 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002399 return( ret );
2400 }
2401
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002402 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2405 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002406 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 }
Paul Bakker68884e32013-01-07 18:20:04 +01002408 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002410#if defined(MBEDTLS_GCM_C) || \
2411 defined(MBEDTLS_CCM_C) || \
2412 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002414 mode == MBEDTLS_MODE_CCM ||
2415 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002416 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002417 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002418 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002419
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002420 /*
2421 * Compute and update sizes
2422 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002423 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002426 "+ taglen (%d)", rec->data_len,
2427 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002429 }
Paul Bakker68884e32013-01-07 18:20:04 +01002430
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002431 /*
2432 * Prepare IV
2433 */
2434 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2435 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002436 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002437 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002438 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002439
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002440 }
2441 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2442 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002443 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002444 unsigned char i;
2445
2446 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2447
2448 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002449 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002450 }
2451 else
2452 {
2453 /* Reminder if we ever add an AEAD mode with a different size */
2454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2455 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2456 }
2457
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002458 data += explicit_iv_len;
2459 rec->data_offset += explicit_iv_len;
2460 rec->data_len -= explicit_iv_len + transform->taglen;
2461
2462 ssl_extract_add_data_from_record( add_data, rec );
2463 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2464 add_data, 13 );
2465
2466 memcpy( transform->iv_dec + transform->fixed_ivlen,
2467 data - explicit_iv_len, explicit_iv_len );
2468
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002469 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002470 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002471 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002472
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002473
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002474 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002475 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002476 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002477 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2478 iv, transform->ivlen,
2479 add_data, 13,
2480 data, rec->data_len,
2481 data, &olen,
2482 data + rec->data_len,
2483 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2488 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002489
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002490 return( ret );
2491 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002492 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002493
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002494 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2497 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002498 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002499 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2502#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002503 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002506 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002507
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 /*
Paul Bakker45829992013-01-03 14:52:21 +01002509 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002512 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2513 {
2514 /* The ciphertext is prefixed with the CBC IV. */
2515 minlen += transform->ivlen;
2516 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002517#endif
Paul Bakker45829992013-01-03 14:52:21 +01002518
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002519 /* Size considerations:
2520 *
2521 * - The CBC cipher text must not be empty and hence
2522 * at least of size transform->ivlen.
2523 *
2524 * Together with the potential IV-prefix, this explains
2525 * the first of the two checks below.
2526 *
2527 * - The record must contain a MAC, either in plain or
2528 * encrypted, depending on whether Encrypt-then-MAC
2529 * is used or not.
2530 * - If it is, the message contains the IV-prefix,
2531 * the CBC ciphertext, and the MAC.
2532 * - If it is not, the padded plaintext, and hence
2533 * the CBC ciphertext, has at least length maclen + 1
2534 * because there is at least the padding length byte.
2535 *
2536 * As the CBC ciphertext is not empty, both cases give the
2537 * lower bound minlen + maclen + 1 on the record size, which
2538 * we test for in the second check below.
2539 */
2540 if( rec->data_len < minlen + transform->ivlen ||
2541 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002544 "+ 1 ) ( + expl IV )", rec->data_len,
2545 transform->ivlen,
2546 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002548 }
2549
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002550 /*
2551 * Authenticate before decrypt if enabled
2552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002554 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002555 {
Hanno Becker992b6872017-11-09 18:57:39 +00002556 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002559
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002560 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2561 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002562
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002563 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002564
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002565 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, 13 );
2566 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2567 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2568 data, rec->data_len );
2569 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2570 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002571
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002572 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2573 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002574 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002575 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002576
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002577 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2578 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002582 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002583 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002584 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002586
2587 /*
2588 * Check length sanity
2589 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002590 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002593 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002595 }
2596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002598 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002599 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002600 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002601 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002602 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002603 /* This is safe because data_len >= minlen + maclen + 1 initially,
2604 * and at this point we have at most subtracted maclen (note that
2605 * minlen == transform->ivlen here). */
2606 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002607
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002608 data += transform->ivlen;
2609 rec->data_offset += transform->ivlen;
2610 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002611 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002613
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002614 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2615 transform->iv_dec, transform->ivlen,
2616 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002619 return( ret );
2620 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002621
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002622 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2625 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002626 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002629 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002630 {
2631 /*
2632 * Save IV in SSL3 and TLS1
2633 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002634 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2635 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002637#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002639 /* Safe since data_len >= minlen + maclen + 1, so after having
2640 * subtracted at most minlen and maclen up to this point,
2641 * data_len > 0. */
2642 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002643
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002644 if( auth_done == 1 )
2645 {
2646 correct *= ( rec->data_len >= padlen + 1 );
2647 padlen *= ( rec->data_len >= padlen + 1 );
2648 }
2649 else
Paul Bakker45829992013-01-03 14:52:21 +01002650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002652 if( rec->data_len < transform->maclen + padlen + 1 )
2653 {
2654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2655 rec->data_len,
2656 transform->maclen,
2657 padlen + 1 ) );
2658 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002659#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002660
2661 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2662 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002663 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002664
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002665 padlen++;
2666
2667 /* Regardless of the validity of the padding,
2668 * we have data_len >= padlen here. */
2669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002671 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002672 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002673 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675#if defined(MBEDTLS_SSL_DEBUG_ALL)
2676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002677 "should be no more than %d",
2678 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002679#endif
Paul Bakker45829992013-01-03 14:52:21 +01002680 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002681 }
2682 }
2683 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2685#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2686 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002687 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002689 /* The padding check involves a series of up to 256
2690 * consecutive memory reads at the end of the record
2691 * plaintext buffer. In order to hide the length and
2692 * validity of the padding, always perform exactly
2693 * `min(256,plaintext_len)` reads (but take into account
2694 * only the last `padlen` bytes for the padding check). */
2695 size_t pad_count = 0;
2696 size_t real_count = 0;
2697 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002698
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002699 /* Index of first padding byte; it has been ensured above
2700 * that the subtraction is safe. */
2701 size_t const padding_idx = rec->data_len - padlen;
2702 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2703 size_t const start_idx = rec->data_len - num_checks;
2704 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002705
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002706 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002707 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002708 real_count |= ( idx >= padding_idx );
2709 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002710 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002711 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002714 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002716#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002717 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002718 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002719 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2721 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2724 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002725 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002726
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002727 /* If the padding was found to be invalid, padlen == 0
2728 * and the subtraction is safe. If the padding was found valid,
2729 * padlen hasn't been changed and the previous assertion
2730 * data_len >= padlen still holds. */
2731 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002732 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002733 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002735 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2738 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002739 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002741#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002743 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002744#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002745
2746 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002747 * Authenticate if not done yet.
2748 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 */
Hanno Becker52344c22018-01-03 15:24:20 +00002750#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002751 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002752 {
Hanno Becker992b6872017-11-09 18:57:39 +00002753 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002754
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002755 /* If the initial value of padlen was such that
2756 * data_len < maclen + padlen + 1, then padlen
2757 * got reset to 1, and the initial check
2758 * data_len >= minlen + maclen + 1
2759 * guarantees that at this point we still
2760 * have at least data_len >= maclen.
2761 *
2762 * If the initial value of padlen was such that
2763 * data_len >= maclen + padlen + 1, then we have
2764 * subtracted either padlen + 1 (if the padding was correct)
2765 * or 0 (if the padding was incorrect) since then,
2766 * hence data_len >= maclen in any case.
2767 */
2768 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002769
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002770 ssl_extract_add_data_from_record( add_data, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002773 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002774 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002775 ssl_mac( &transform->md_ctx_dec,
2776 transform->mac_dec,
2777 data, rec->data_len,
2778 rec->ctr, rec->type,
2779 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002780 }
2781 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2783#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2784 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002785 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002786 {
2787 /*
2788 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002789 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002790 *
2791 * Known timing attacks:
2792 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2793 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002794 * To compensate for different timings for the MAC calculation
2795 * depending on how much padding was removed (which is determined
2796 * by padlen), process extra_run more blocks through the hash
2797 * function.
2798 *
2799 * The formula in the paper is
2800 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2801 * where L1 is the size of the header plus the decrypted message
2802 * plus CBC padding and L2 is the size of the header plus the
2803 * decrypted message. This is for an underlying hash function
2804 * with 64-byte blocks.
2805 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2806 * correctly. We round down instead of up, so -56 is the correct
2807 * value for our calculations instead of -55.
2808 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002809 * Repeat the formula rather than defining a block_size variable.
2810 * This avoids requiring division by a variable at runtime
2811 * (which would be marginally less efficient and would require
2812 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002813 */
2814 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002815 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002816
2817 /*
2818 * The next two sizes are the minimum and maximum values of
2819 * in_msglen over all padlen values.
2820 *
2821 * They're independent of padlen, since we previously did
2822 * in_msglen -= padlen.
2823 *
2824 * Note that max_len + maclen is never more than the buffer
2825 * length, as we previously did in_msglen -= maclen too.
2826 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002827 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002828 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2829
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002830 memset( tmp, 0, sizeof( tmp ) );
2831
2832 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002833 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002834#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2835 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002836 case MBEDTLS_MD_MD5:
2837 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002838 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002839 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002840 extra_run = ( 13 + rec->data_len + padlen + 8 ) / 64 -
2841 ( 13 + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002842 break;
2843#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002844#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002845 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002846 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002847 extra_run = ( 13 + rec->data_len + padlen + 16 ) / 128 -
2848 ( 13 + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002849 break;
2850#endif
2851 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002853 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2854 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002855
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002856 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002857
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002858 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2859 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2860 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002861 /* Make sure we access everything even when padlen > 0. This
2862 * makes the synchronisation requirements for just-in-time
2863 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002864 ssl_read_memory( data + rec->data_len, padlen );
2865 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002866
2867 /* Call mbedtls_md_process at least once due to cache attacks
2868 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002869 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002870 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002871
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002872 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002873
2874 /* Make sure we access all the memory that could contain the MAC,
2875 * before we check it in the next code block. This makes the
2876 * synchronisation requirements for just-in-time Prime+Probe
2877 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002878 ssl_read_memory( data + min_len,
2879 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002880 }
2881 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2883 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2886 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002887 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002888
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002889#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002890 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2891 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002892#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002893
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002894 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2895 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897#if defined(MBEDTLS_SSL_DEBUG_ALL)
2898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002899#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002900 correct = 0;
2901 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002902 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002903 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002904
2905 /*
2906 * Finally check the correct flag
2907 */
2908 if( correct == 0 )
2909 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00002910#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002911
2912 /* Make extra sure authentication was performed, exactly once */
2913 if( auth_done != 1 )
2914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2916 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002917 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002920
2921 return( 0 );
2922}
2923
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002924#undef MAC_NONE
2925#undef MAC_PLAINTEXT
2926#undef MAC_CIPHERTEXT
2927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002929/*
2930 * Compression/decompression functions
2931 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002933{
2934 int ret;
2935 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002936 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002937 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002938 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002941
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002942 if( len_pre == 0 )
2943 return( 0 );
2944
Paul Bakker2770fbd2012-07-03 13:30:23 +00002945 memcpy( msg_pre, ssl->out_msg, len_pre );
2946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002948 ssl->out_msglen ) );
2949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002951 ssl->out_msg, ssl->out_msglen );
2952
Paul Bakker48916f92012-09-16 19:57:18 +00002953 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2954 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2955 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002956 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002957
Paul Bakker48916f92012-09-16 19:57:18 +00002958 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002959 if( ret != Z_OK )
2960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2962 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002963 }
2964
Angus Grattond8213d02016-05-25 20:56:48 +10002965 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002966 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002969 ssl->out_msglen ) );
2970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002972 ssl->out_msg, ssl->out_msglen );
2973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002975
2976 return( 0 );
2977}
2978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002980{
2981 int ret;
2982 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002983 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002984 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002985 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002988
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002989 if( len_pre == 0 )
2990 return( 0 );
2991
Paul Bakker2770fbd2012-07-03 13:30:23 +00002992 memcpy( msg_pre, ssl->in_msg, len_pre );
2993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002995 ssl->in_msglen ) );
2996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002998 ssl->in_msg, ssl->in_msglen );
2999
Paul Bakker48916f92012-09-16 19:57:18 +00003000 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3001 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3002 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003003 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003004 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003005
Paul Bakker48916f92012-09-16 19:57:18 +00003006 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003007 if( ret != Z_OK )
3008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3010 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003011 }
3012
Angus Grattond8213d02016-05-25 20:56:48 +10003013 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003014 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003017 ssl->in_msglen ) );
3018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003020 ssl->in_msg, ssl->in_msglen );
3021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003023
3024 return( 0 );
3025}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3029static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031#if defined(MBEDTLS_SSL_PROTO_DTLS)
3032static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003033{
3034 /* If renegotiation is not enforced, retransmit until we would reach max
3035 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003036 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003037 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003038 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003039 unsigned char doublings = 1;
3040
3041 while( ratio != 0 )
3042 {
3043 ++doublings;
3044 ratio >>= 1;
3045 }
3046
3047 if( ++ssl->renego_records_seen > doublings )
3048 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003050 return( 0 );
3051 }
3052 }
3053
3054 return( ssl_write_hello_request( ssl ) );
3055}
3056#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003058
Paul Bakker5121ce52009-01-03 21:22:43 +00003059/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003060 * Fill the input message buffer by appending data to it.
3061 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003062 *
3063 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3064 * available (from this read and/or a previous one). Otherwise, an error code
3065 * is returned (possibly EOF or WANT_READ).
3066 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003067 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3068 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3069 * since we always read a whole datagram at once.
3070 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003071 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003072 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003073 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003075{
Paul Bakker23986e52011-04-24 08:57:21 +00003076 int ret;
3077 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003080
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003081 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003084 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003086 }
3087
Angus Grattond8213d02016-05-25 20:56:48 +10003088 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003092 }
3093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003095 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003096 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003097 uint32_t timeout;
3098
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003099 /* Just to be sure */
3100 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3101 {
3102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3103 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3104 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3105 }
3106
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003107 /*
3108 * The point is, we need to always read a full datagram at once, so we
3109 * sometimes read more then requested, and handle the additional data.
3110 * It could be the rest of the current record (while fetching the
3111 * header) and/or some other records in the same datagram.
3112 */
3113
3114 /*
3115 * Move to the next record in the already read datagram if applicable
3116 */
3117 if( ssl->next_record_offset != 0 )
3118 {
3119 if( ssl->in_left < ssl->next_record_offset )
3120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3122 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003123 }
3124
3125 ssl->in_left -= ssl->next_record_offset;
3126
3127 if( ssl->in_left != 0 )
3128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003130 ssl->next_record_offset ) );
3131 memmove( ssl->in_hdr,
3132 ssl->in_hdr + ssl->next_record_offset,
3133 ssl->in_left );
3134 }
3135
3136 ssl->next_record_offset = 0;
3137 }
3138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003140 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003141
3142 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003143 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003144 */
3145 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003148 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003149 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003150
3151 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003152 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003153 * are not at the beginning of a new record, the caller did something
3154 * wrong.
3155 */
3156 if( ssl->in_left != 0 )
3157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3159 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003160 }
3161
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003162 /*
3163 * Don't even try to read if time's out already.
3164 * This avoids by-passing the timer when repeatedly receiving messages
3165 * that will end up being dropped.
3166 */
3167 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003168 {
3169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003170 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003171 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003172 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003173 {
Angus Grattond8213d02016-05-25 20:56:48 +10003174 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003177 timeout = ssl->handshake->retransmit_timeout;
3178 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003179 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003182
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003183 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003184 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3185 timeout );
3186 else
3187 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003190
3191 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003193 }
3194
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003195 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003198 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003201 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003202 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003205 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003206 }
3207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003211 return( ret );
3212 }
3213
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003214 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003215 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003217 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003219 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003220 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003223 return( ret );
3224 }
3225
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003226 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003227 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003229 }
3230
Paul Bakker5121ce52009-01-03 21:22:43 +00003231 if( ret < 0 )
3232 return( ret );
3233
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003234 ssl->in_left = ret;
3235 }
3236 else
3237#endif
3238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003240 ssl->in_left, nb_want ) );
3241
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003242 while( ssl->in_left < nb_want )
3243 {
3244 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003245
3246 if( ssl_check_timer( ssl ) != 0 )
3247 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3248 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003249 {
3250 if( ssl->f_recv_timeout != NULL )
3251 {
3252 ret = ssl->f_recv_timeout( ssl->p_bio,
3253 ssl->in_hdr + ssl->in_left, len,
3254 ssl->conf->read_timeout );
3255 }
3256 else
3257 {
3258 ret = ssl->f_recv( ssl->p_bio,
3259 ssl->in_hdr + ssl->in_left, len );
3260 }
3261 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003264 ssl->in_left, nb_want ) );
3265 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003266
3267 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003269
3270 if( ret < 0 )
3271 return( ret );
3272
mohammad160352aecb92018-03-28 23:41:40 -07003273 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003274 {
Darryl Green11999bb2018-03-13 15:22:58 +00003275 MBEDTLS_SSL_DEBUG_MSG( 1,
3276 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003277 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003278 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3279 }
3280
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003281 ssl->in_left += ret;
3282 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003283 }
3284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003286
3287 return( 0 );
3288}
3289
3290/*
3291 * Flush any data not yet written
3292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003294{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003295 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003296 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003299
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003300 if( ssl->f_send == NULL )
3301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003303 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003305 }
3306
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003307 /* Avoid incrementing counter if data is flushed */
3308 if( ssl->out_left == 0 )
3309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003311 return( 0 );
3312 }
3313
Paul Bakker5121ce52009-01-03 21:22:43 +00003314 while( ssl->out_left > 0 )
3315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3317 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003318
Hanno Becker2b1e3542018-08-06 11:19:13 +01003319 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003320 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003323
3324 if( ret <= 0 )
3325 return( ret );
3326
mohammad160352aecb92018-03-28 23:41:40 -07003327 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003328 {
Darryl Green11999bb2018-03-13 15:22:58 +00003329 MBEDTLS_SSL_DEBUG_MSG( 1,
3330 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003331 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003332 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3333 }
3334
Paul Bakker5121ce52009-01-03 21:22:43 +00003335 ssl->out_left -= ret;
3336 }
3337
Hanno Becker2b1e3542018-08-06 11:19:13 +01003338#if defined(MBEDTLS_SSL_PROTO_DTLS)
3339 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003340 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003341 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003342 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003343 else
3344#endif
3345 {
3346 ssl->out_hdr = ssl->out_buf + 8;
3347 }
3348 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003351
3352 return( 0 );
3353}
3354
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003355/*
3356 * Functions to handle the DTLS retransmission state machine
3357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003359/*
3360 * Append current handshake message to current outgoing flight
3361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003363{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3366 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3367 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003368
3369 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003370 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003371 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003374 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003375 }
3376
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003377 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003378 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003381 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003382 }
3383
3384 /* Copy current handshake message with headers */
3385 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3386 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003387 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003388 msg->next = NULL;
3389
3390 /* Append to the current flight */
3391 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003392 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003393 else
3394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003396 while( cur->next != NULL )
3397 cur = cur->next;
3398 cur->next = msg;
3399 }
3400
Hanno Becker3b235902018-08-06 09:54:53 +01003401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003402 return( 0 );
3403}
3404
3405/*
3406 * Free the current flight of handshake messages
3407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003409{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410 mbedtls_ssl_flight_item *cur = flight;
3411 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003412
3413 while( cur != NULL )
3414 {
3415 next = cur->next;
3416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 mbedtls_free( cur->p );
3418 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003419
3420 cur = next;
3421 }
3422}
3423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3425static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003426#endif
3427
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003428/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003429 * Swap transform_out and out_ctr with the alternative ones
3430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003432{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003434 unsigned char tmp_out_ctr[8];
3435
3436 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003439 return;
3440 }
3441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003443
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003444 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003445 tmp_transform = ssl->transform_out;
3446 ssl->transform_out = ssl->handshake->alt_transform_out;
3447 ssl->handshake->alt_transform_out = tmp_transform;
3448
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003449 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003450 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3451 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003452 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003453
3454 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003455 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3458 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3463 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003464 }
3465 }
3466#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003467}
3468
3469/*
3470 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003471 */
3472int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3473{
3474 int ret = 0;
3475
3476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3477
3478 ret = mbedtls_ssl_flight_transmit( ssl );
3479
3480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3481
3482 return( ret );
3483}
3484
3485/*
3486 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003487 *
3488 * Need to remember the current message in case flush_output returns
3489 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003490 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003491 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003492int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003493{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003494 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003498 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003500
3501 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003502 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003503 ssl_swap_epochs( ssl );
3504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003506 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003507
3508 while( ssl->handshake->cur_msg != NULL )
3509 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003510 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003511 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003512
Hanno Beckere1dcb032018-08-17 16:47:58 +01003513 int const is_finished =
3514 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3515 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3516
Hanno Becker04da1892018-08-14 13:22:10 +01003517 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3518 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3519
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003520 /* Swap epochs before sending Finished: we can't do it after
3521 * sending ChangeCipherSpec, in case write returns WANT_READ.
3522 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003523 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003524 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003526 ssl_swap_epochs( ssl );
3527 }
3528
Hanno Becker67bc7c32018-08-06 11:33:50 +01003529 ret = ssl_get_remaining_payload_in_datagram( ssl );
3530 if( ret < 0 )
3531 return( ret );
3532 max_frag_len = (size_t) ret;
3533
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003534 /* CCS is copied as is, while HS messages may need fragmentation */
3535 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3536 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003537 if( max_frag_len == 0 )
3538 {
3539 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3540 return( ret );
3541
3542 continue;
3543 }
3544
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003545 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003546 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003547 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003548
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003549 /* Update position inside current message */
3550 ssl->handshake->cur_msg_p += cur->len;
3551 }
3552 else
3553 {
3554 const unsigned char * const p = ssl->handshake->cur_msg_p;
3555 const size_t hs_len = cur->len - 12;
3556 const size_t frag_off = p - ( cur->p + 12 );
3557 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003558 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003559
Hanno Beckere1dcb032018-08-17 16:47:58 +01003560 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003561 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003562 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003563 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003564
Hanno Becker67bc7c32018-08-06 11:33:50 +01003565 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3566 return( ret );
3567
3568 continue;
3569 }
3570 max_hs_frag_len = max_frag_len - 12;
3571
3572 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3573 max_hs_frag_len : rem_len;
3574
3575 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003576 {
3577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003578 (unsigned) cur_hs_frag_len,
3579 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003580 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003581
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003582 /* Messages are stored with handshake headers as if not fragmented,
3583 * copy beginning of headers then fill fragmentation fields.
3584 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3585 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003586
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003587 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3588 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3589 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3590
Hanno Becker67bc7c32018-08-06 11:33:50 +01003591 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3592 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3593 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003594
3595 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3596
Hanno Becker3f7b9732018-08-28 09:53:25 +01003597 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003598 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3599 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003600 ssl->out_msgtype = cur->type;
3601
3602 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003603 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003604 }
3605
3606 /* If done with the current message move to the next one if any */
3607 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3608 {
3609 if( cur->next != NULL )
3610 {
3611 ssl->handshake->cur_msg = cur->next;
3612 ssl->handshake->cur_msg_p = cur->next->p + 12;
3613 }
3614 else
3615 {
3616 ssl->handshake->cur_msg = NULL;
3617 ssl->handshake->cur_msg_p = NULL;
3618 }
3619 }
3620
3621 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003622 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003625 return( ret );
3626 }
3627 }
3628
Hanno Becker67bc7c32018-08-06 11:33:50 +01003629 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3630 return( ret );
3631
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003632 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3634 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003635 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003638 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3639 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003640
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003642
3643 return( 0 );
3644}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003645
3646/*
3647 * To be called when the last message of an incoming flight is received.
3648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003650{
3651 /* We won't need to resend that one any more */
3652 ssl_flight_free( ssl->handshake->flight );
3653 ssl->handshake->flight = NULL;
3654 ssl->handshake->cur_msg = NULL;
3655
3656 /* The next incoming flight will start with this msg_seq */
3657 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3658
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003659 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003660 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003661
Hanno Becker0271f962018-08-16 13:23:47 +01003662 /* Clear future message buffering structure. */
3663 ssl_buffering_free( ssl );
3664
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003665 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003666 ssl_set_timer( ssl, 0 );
3667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3669 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003672 }
3673 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003675}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003676
3677/*
3678 * To be called when the last message of an outgoing flight is send.
3679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003680void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003681{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003682 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003683 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003685 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3686 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003689 }
3690 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003691 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003692}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003693#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003694
Paul Bakker5121ce52009-01-03 21:22:43 +00003695/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003696 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003697 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003698
3699/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003700 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003701 *
3702 * - fill in handshake headers
3703 * - update handshake checksum
3704 * - DTLS: save message for resending
3705 * - then pass to the record layer
3706 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003707 * DTLS: except for HelloRequest, messages are only queued, and will only be
3708 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003709 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003710 * Inputs:
3711 * - ssl->out_msglen: 4 + actual handshake message len
3712 * (4 is the size of handshake headers for TLS)
3713 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3714 * - ssl->out_msg + 4: the handshake message body
3715 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003716 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003717 * - ssl->out_msglen: the length of the record contents
3718 * (including handshake headers but excluding record headers)
3719 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003720 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003721int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003722{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003723 int ret;
3724 const size_t hs_len = ssl->out_msglen - 4;
3725 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003726
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3728
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003729 /*
3730 * Sanity checks
3731 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003732 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003733 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3734 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003735 /* In SSLv3, the client might send a NoCertificate alert. */
3736#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3737 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3738 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3739 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3740#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3741 {
3742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3743 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3744 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003745 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003746
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003747 /* Whenever we send anything different from a
3748 * HelloRequest we should be in a handshake - double check. */
3749 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3750 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003751 ssl->handshake == NULL )
3752 {
3753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3754 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3755 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003757#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003758 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003759 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003761 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3763 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003764 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003765#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003766
Hanno Beckerb50a2532018-08-06 11:52:54 +01003767 /* Double-check that we did not exceed the bounds
3768 * of the outgoing record buffer.
3769 * This should never fail as the various message
3770 * writing functions must obey the bounds of the
3771 * outgoing record buffer, but better be safe.
3772 *
3773 * Note: We deliberately do not check for the MTU or MFL here.
3774 */
3775 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3776 {
3777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3778 "size %u, maximum %u",
3779 (unsigned) ssl->out_msglen,
3780 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3781 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3782 }
3783
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003784 /*
3785 * Fill handshake headers
3786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003788 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003789 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3790 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3791 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003792
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003793 /*
3794 * DTLS has additional fields in the Handshake layer,
3795 * between the length field and the actual payload:
3796 * uint16 message_seq;
3797 * uint24 fragment_offset;
3798 * uint24 fragment_length;
3799 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003800#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003801 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003802 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003803 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003804 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003805 {
3806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3807 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003808 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003809 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003810 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3811 }
3812
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003813 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003814 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003815
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003816 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003817 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003818 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003819 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3820 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3821 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003822 }
3823 else
3824 {
3825 ssl->out_msg[4] = 0;
3826 ssl->out_msg[5] = 0;
3827 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003828
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003829 /* Handshake hashes are computed without fragmentation,
3830 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003831 memset( ssl->out_msg + 6, 0x00, 3 );
3832 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003833 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003834#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003835
Hanno Becker0207e532018-08-28 10:28:28 +01003836 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003837 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3838 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003839 }
3840
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003841 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003843 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003844 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3845 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003846 {
3847 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003850 return( ret );
3851 }
3852 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003853 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003854#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003855 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003856 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003857 {
3858 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3859 return( ret );
3860 }
3861 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003862
3863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3864
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003865 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003866}
3867
3868/*
3869 * Record layer functions
3870 */
3871
3872/*
3873 * Write current record.
3874 *
3875 * Uses:
3876 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3877 * - ssl->out_msglen: length of the record content (excl headers)
3878 * - ssl->out_msg: record content
3879 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003880int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003881{
3882 int ret, done = 0;
3883 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003884 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003885
3886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003888#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003889 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003891 {
3892 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003894 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003895 return( ret );
3896 }
3897
3898 len = ssl->out_msglen;
3899 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003900#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3903 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003907 ret = mbedtls_ssl_hw_record_write( ssl );
3908 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003910 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3911 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003912 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003913
3914 if( ret == 0 )
3915 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003916 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003917#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003918 if( !done )
3919 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003920 unsigned i;
3921 size_t protected_record_size;
3922
Paul Bakker05ef8352012-05-08 09:17:57 +00003923 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003924 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003925 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003926
Hanno Becker19859472018-08-06 09:40:20 +01003927 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003928 ssl->out_len[0] = (unsigned char)( len >> 8 );
3929 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003930
Paul Bakker48916f92012-09-16 19:57:18 +00003931 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003932 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003933 mbedtls_record rec;
3934
3935 rec.buf = ssl->out_iv;
3936 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3937 ( ssl->out_iv - ssl->out_buf );
3938 rec.data_len = ssl->out_msglen;
3939 rec.data_offset = ssl->out_msg - rec.buf;
3940
3941 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3942 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3943 ssl->conf->transport, rec.ver );
3944 rec.type = ssl->out_msgtype;
3945
Hanno Beckera18d1322018-01-03 14:27:32 +00003946 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003947 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003950 return( ret );
3951 }
3952
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003953 if( rec.data_offset != 0 )
3954 {
3955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3956 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3957 }
3958
Hanno Becker78f839d2019-03-14 12:56:23 +00003959 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003960 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3961 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003962 }
3963
Hanno Becker2b1e3542018-08-06 11:19:13 +01003964 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3965
3966#if defined(MBEDTLS_SSL_PROTO_DTLS)
3967 /* In case of DTLS, double-check that we don't exceed
3968 * the remaining space in the datagram. */
3969 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3970 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003971 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003972 if( ret < 0 )
3973 return( ret );
3974
3975 if( protected_record_size > (size_t) ret )
3976 {
3977 /* Should never happen */
3978 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3979 }
3980 }
3981#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003984 "version = [%d:%d], msglen = %d",
3985 ssl->out_hdr[0], ssl->out_hdr[1],
3986 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003988 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003989 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003990
3991 ssl->out_left += protected_record_size;
3992 ssl->out_hdr += protected_record_size;
3993 ssl_update_out_pointers( ssl, ssl->transform_out );
3994
Hanno Becker04484622018-08-06 09:49:38 +01003995 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3996 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3997 break;
3998
3999 /* The loop goes to its end iff the counter is wrapping */
4000 if( i == ssl_ep_len( ssl ) )
4001 {
4002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4003 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4004 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004005 }
4006
Hanno Becker67bc7c32018-08-06 11:33:50 +01004007#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004008 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4009 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004010 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004011 size_t remaining;
4012 ret = ssl_get_remaining_payload_in_datagram( ssl );
4013 if( ret < 0 )
4014 {
4015 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4016 ret );
4017 return( ret );
4018 }
4019
4020 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004021 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004022 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004023 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004024 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004025 else
4026 {
Hanno Becker513815a2018-08-20 11:56:09 +01004027 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004028 }
4029 }
4030#endif /* MBEDTLS_SSL_PROTO_DTLS */
4031
4032 if( ( flush == SSL_FORCE_FLUSH ) &&
4033 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004035 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004036 return( ret );
4037 }
4038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004040
4041 return( 0 );
4042}
4043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004044#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004045
4046static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4047{
4048 if( ssl->in_msglen < ssl->in_hslen ||
4049 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4050 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4051 {
4052 return( 1 );
4053 }
4054 return( 0 );
4055}
Hanno Becker44650b72018-08-16 12:51:11 +01004056
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004057static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004058{
4059 return( ( ssl->in_msg[9] << 16 ) |
4060 ( ssl->in_msg[10] << 8 ) |
4061 ssl->in_msg[11] );
4062}
4063
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004064static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004065{
4066 return( ( ssl->in_msg[6] << 16 ) |
4067 ( ssl->in_msg[7] << 8 ) |
4068 ssl->in_msg[8] );
4069}
4070
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004071static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004072{
4073 uint32_t msg_len, frag_off, frag_len;
4074
4075 msg_len = ssl_get_hs_total_len( ssl );
4076 frag_off = ssl_get_hs_frag_off( ssl );
4077 frag_len = ssl_get_hs_frag_len( ssl );
4078
4079 if( frag_off > msg_len )
4080 return( -1 );
4081
4082 if( frag_len > msg_len - frag_off )
4083 return( -1 );
4084
4085 if( frag_len + 12 > ssl->in_msglen )
4086 return( -1 );
4087
4088 return( 0 );
4089}
4090
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004091/*
4092 * Mark bits in bitmask (used for DTLS HS reassembly)
4093 */
4094static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4095{
4096 unsigned int start_bits, end_bits;
4097
4098 start_bits = 8 - ( offset % 8 );
4099 if( start_bits != 8 )
4100 {
4101 size_t first_byte_idx = offset / 8;
4102
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004103 /* Special case */
4104 if( len <= start_bits )
4105 {
4106 for( ; len != 0; len-- )
4107 mask[first_byte_idx] |= 1 << ( start_bits - len );
4108
4109 /* Avoid potential issues with offset or len becoming invalid */
4110 return;
4111 }
4112
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004113 offset += start_bits; /* Now offset % 8 == 0 */
4114 len -= start_bits;
4115
4116 for( ; start_bits != 0; start_bits-- )
4117 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4118 }
4119
4120 end_bits = len % 8;
4121 if( end_bits != 0 )
4122 {
4123 size_t last_byte_idx = ( offset + len ) / 8;
4124
4125 len -= end_bits; /* Now len % 8 == 0 */
4126
4127 for( ; end_bits != 0; end_bits-- )
4128 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4129 }
4130
4131 memset( mask + offset / 8, 0xFF, len / 8 );
4132}
4133
4134/*
4135 * Check that bitmask is full
4136 */
4137static int ssl_bitmask_check( unsigned char *mask, size_t len )
4138{
4139 size_t i;
4140
4141 for( i = 0; i < len / 8; i++ )
4142 if( mask[i] != 0xFF )
4143 return( -1 );
4144
4145 for( i = 0; i < len % 8; i++ )
4146 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4147 return( -1 );
4148
4149 return( 0 );
4150}
4151
Hanno Becker56e205e2018-08-16 09:06:12 +01004152/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004153static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004154 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004155{
Hanno Becker56e205e2018-08-16 09:06:12 +01004156 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004157
Hanno Becker56e205e2018-08-16 09:06:12 +01004158 alloc_len = 12; /* Handshake header */
4159 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004160
Hanno Beckerd07df862018-08-16 09:14:58 +01004161 if( add_bitmap )
4162 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004163
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004164 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004165}
Hanno Becker56e205e2018-08-16 09:06:12 +01004166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004167#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004168
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004169static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004170{
4171 return( ( ssl->in_msg[1] << 16 ) |
4172 ( ssl->in_msg[2] << 8 ) |
4173 ssl->in_msg[3] );
4174}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004175
Simon Butcher99000142016-10-13 17:21:01 +01004176int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004177{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004178 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004181 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004182 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004183 }
4184
Hanno Becker12555c62018-08-16 12:47:53 +01004185 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004188 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004189 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004192 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004193 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004194 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004195 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004196
Hanno Becker44650b72018-08-16 12:51:11 +01004197 if( ssl_check_hs_header( ssl ) != 0 )
4198 {
4199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4200 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4201 }
4202
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004203 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004204 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4205 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4206 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4207 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004208 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004209 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4210 {
4211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4212 recv_msg_seq,
4213 ssl->handshake->in_msg_seq ) );
4214 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4215 }
4216
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004217 /* Retransmit only on last message from previous flight, to avoid
4218 * too many retransmissions.
4219 * Besides, No sane server ever retransmits HelloVerifyRequest */
4220 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004221 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004224 "message_seq = %d, start_of_flight = %d",
4225 recv_msg_seq,
4226 ssl->handshake->in_flight_start_seq ) );
4227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004230 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004231 return( ret );
4232 }
4233 }
4234 else
4235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004237 "message_seq = %d, expected = %d",
4238 recv_msg_seq,
4239 ssl->handshake->in_msg_seq ) );
4240 }
4241
Hanno Becker90333da2017-10-10 11:27:13 +01004242 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004243 }
4244 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004245
Hanno Becker6d97ef52018-08-16 13:09:04 +01004246 /* Message reassembly is handled alongside buffering of future
4247 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004248 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004249 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004250 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004253 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004254 }
4255 }
4256 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004257#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004258 /* With TLS we don't handle fragmentation (for now) */
4259 if( ssl->in_msglen < ssl->in_hslen )
4260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4262 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004263 }
4264
Simon Butcher99000142016-10-13 17:21:01 +01004265 return( 0 );
4266}
4267
4268void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4269{
Hanno Becker0271f962018-08-16 13:23:47 +01004270 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004271
Hanno Becker0271f962018-08-16 13:23:47 +01004272 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004273 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004274 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004275 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004276
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004277 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004279 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004280 ssl->handshake != NULL )
4281 {
Hanno Becker0271f962018-08-16 13:23:47 +01004282 unsigned offset;
4283 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004284
Hanno Becker0271f962018-08-16 13:23:47 +01004285 /* Increment handshake sequence number */
4286 hs->in_msg_seq++;
4287
4288 /*
4289 * Clear up handshake buffering and reassembly structure.
4290 */
4291
4292 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004293 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004294
4295 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004296 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4297 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004298 offset++, hs_buf++ )
4299 {
4300 *hs_buf = *(hs_buf + 1);
4301 }
4302
4303 /* Create a fresh last entry */
4304 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004305 }
4306#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004307}
4308
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004309/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004310 * DTLS anti-replay: RFC 6347 4.1.2.6
4311 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004312 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4313 * Bit n is set iff record number in_window_top - n has been seen.
4314 *
4315 * Usually, in_window_top is the last record number seen and the lsb of
4316 * in_window is set. The only exception is the initial state (record number 0
4317 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4320static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004321{
4322 ssl->in_window_top = 0;
4323 ssl->in_window = 0;
4324}
4325
4326static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4327{
4328 return( ( (uint64_t) buf[0] << 40 ) |
4329 ( (uint64_t) buf[1] << 32 ) |
4330 ( (uint64_t) buf[2] << 24 ) |
4331 ( (uint64_t) buf[3] << 16 ) |
4332 ( (uint64_t) buf[4] << 8 ) |
4333 ( (uint64_t) buf[5] ) );
4334}
4335
4336/*
4337 * Return 0 if sequence number is acceptable, -1 otherwise
4338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004339int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004340{
4341 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4342 uint64_t bit;
4343
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004344 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004345 return( 0 );
4346
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004347 if( rec_seqnum > ssl->in_window_top )
4348 return( 0 );
4349
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004350 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004351
4352 if( bit >= 64 )
4353 return( -1 );
4354
4355 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4356 return( -1 );
4357
4358 return( 0 );
4359}
4360
4361/*
4362 * Update replay window on new validated record
4363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004365{
4366 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4367
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004368 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004369 return;
4370
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004371 if( rec_seqnum > ssl->in_window_top )
4372 {
4373 /* Update window_top and the contents of the window */
4374 uint64_t shift = rec_seqnum - ssl->in_window_top;
4375
4376 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004377 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004378 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004379 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004380 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004381 ssl->in_window |= 1;
4382 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004383
4384 ssl->in_window_top = rec_seqnum;
4385 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004386 else
4387 {
4388 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004389 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004390
4391 if( bit < 64 ) /* Always true, but be extra sure */
4392 ssl->in_window |= (uint64_t) 1 << bit;
4393 }
4394}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004395#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004396
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004397#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004398/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004399static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4400
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004401/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004402 * Without any SSL context, check if a datagram looks like a ClientHello with
4403 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004404 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004405 *
4406 * - if cookie is valid, return 0
4407 * - if ClientHello looks superficially valid but cookie is not,
4408 * fill obuf and set olen, then
4409 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4410 * - otherwise return a specific error code
4411 */
4412static int ssl_check_dtls_clihlo_cookie(
4413 mbedtls_ssl_cookie_write_t *f_cookie_write,
4414 mbedtls_ssl_cookie_check_t *f_cookie_check,
4415 void *p_cookie,
4416 const unsigned char *cli_id, size_t cli_id_len,
4417 const unsigned char *in, size_t in_len,
4418 unsigned char *obuf, size_t buf_len, size_t *olen )
4419{
4420 size_t sid_len, cookie_len;
4421 unsigned char *p;
4422
4423 if( f_cookie_write == NULL || f_cookie_check == NULL )
4424 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4425
4426 /*
4427 * Structure of ClientHello with record and handshake headers,
4428 * and expected values. We don't need to check a lot, more checks will be
4429 * done when actually parsing the ClientHello - skipping those checks
4430 * avoids code duplication and does not make cookie forging any easier.
4431 *
4432 * 0-0 ContentType type; copied, must be handshake
4433 * 1-2 ProtocolVersion version; copied
4434 * 3-4 uint16 epoch; copied, must be 0
4435 * 5-10 uint48 sequence_number; copied
4436 * 11-12 uint16 length; (ignored)
4437 *
4438 * 13-13 HandshakeType msg_type; (ignored)
4439 * 14-16 uint24 length; (ignored)
4440 * 17-18 uint16 message_seq; copied
4441 * 19-21 uint24 fragment_offset; copied, must be 0
4442 * 22-24 uint24 fragment_length; (ignored)
4443 *
4444 * 25-26 ProtocolVersion client_version; (ignored)
4445 * 27-58 Random random; (ignored)
4446 * 59-xx SessionID session_id; 1 byte len + sid_len content
4447 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4448 * ...
4449 *
4450 * Minimum length is 61 bytes.
4451 */
4452 if( in_len < 61 ||
4453 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4454 in[3] != 0 || in[4] != 0 ||
4455 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4456 {
4457 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4458 }
4459
4460 sid_len = in[59];
4461 if( sid_len > in_len - 61 )
4462 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4463
4464 cookie_len = in[60 + sid_len];
4465 if( cookie_len > in_len - 60 )
4466 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4467
4468 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4469 cli_id, cli_id_len ) == 0 )
4470 {
4471 /* Valid cookie */
4472 return( 0 );
4473 }
4474
4475 /*
4476 * If we get here, we've got an invalid cookie, let's prepare HVR.
4477 *
4478 * 0-0 ContentType type; copied
4479 * 1-2 ProtocolVersion version; copied
4480 * 3-4 uint16 epoch; copied
4481 * 5-10 uint48 sequence_number; copied
4482 * 11-12 uint16 length; olen - 13
4483 *
4484 * 13-13 HandshakeType msg_type; hello_verify_request
4485 * 14-16 uint24 length; olen - 25
4486 * 17-18 uint16 message_seq; copied
4487 * 19-21 uint24 fragment_offset; copied
4488 * 22-24 uint24 fragment_length; olen - 25
4489 *
4490 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4491 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4492 *
4493 * Minimum length is 28.
4494 */
4495 if( buf_len < 28 )
4496 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4497
4498 /* Copy most fields and adapt others */
4499 memcpy( obuf, in, 25 );
4500 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4501 obuf[25] = 0xfe;
4502 obuf[26] = 0xff;
4503
4504 /* Generate and write actual cookie */
4505 p = obuf + 28;
4506 if( f_cookie_write( p_cookie,
4507 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4508 {
4509 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4510 }
4511
4512 *olen = p - obuf;
4513
4514 /* Go back and fill length fields */
4515 obuf[27] = (unsigned char)( *olen - 28 );
4516
4517 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4518 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4519 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4520
4521 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4522 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4523
4524 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4525}
4526
4527/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004528 * Handle possible client reconnect with the same UDP quadruplet
4529 * (RFC 6347 Section 4.2.8).
4530 *
4531 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4532 * that looks like a ClientHello.
4533 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004534 * - if the input looks like a ClientHello without cookies,
4535 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004536 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004537 * - if the input looks like a ClientHello with a valid cookie,
4538 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004539 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004540 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004541 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004542 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004543 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4544 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004545 */
4546static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4547{
4548 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004549 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004550
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004551 ret = ssl_check_dtls_clihlo_cookie(
4552 ssl->conf->f_cookie_write,
4553 ssl->conf->f_cookie_check,
4554 ssl->conf->p_cookie,
4555 ssl->cli_id, ssl->cli_id_len,
4556 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004557 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004558
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004559 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4560
4561 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004562 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004563 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004564 * If the error is permanent we'll catch it later,
4565 * if it's not, then hopefully it'll work next time. */
4566 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4567
4568 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004569 }
4570
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004571 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004572 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004573 /* Got a valid cookie, partially reset context */
4574 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4575 {
4576 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4577 return( ret );
4578 }
4579
4580 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004581 }
4582
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004583 return( ret );
4584}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004585#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004586
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004587/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004588 * ContentType type;
4589 * ProtocolVersion version;
4590 * uint16 epoch; // DTLS only
4591 * uint48 sequence_number; // DTLS only
4592 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004593 *
4594 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004595 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004596 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4597 *
4598 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004599 * 1. proceed with the record if this function returns 0
4600 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4601 * 3. return CLIENT_RECONNECT if this function return that value
4602 * 4. drop the whole datagram if this function returns anything else.
4603 * Point 2 is needed when the peer is resending, and we have already received
4604 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004606static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004607{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004608 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004610 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 +02004611
Paul Bakker5121ce52009-01-03 21:22:43 +00004612 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004613 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004614 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004616 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004617 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004618 ssl->in_msgtype,
4619 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004620
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004621 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004622 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4623 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4624 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4625 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004628
4629#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004630 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4631 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004632 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4633#endif /* MBEDTLS_SSL_PROTO_DTLS */
4634 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4635 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004637 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004638 }
4639
4640 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004641 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4644 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004645 }
4646
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004647 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4650 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004651 }
4652
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004653 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004654 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004655 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4658 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004659 }
4660
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004661 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004662 * DTLS-related tests.
4663 * Check epoch before checking length constraint because
4664 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4665 * message gets duplicated before the corresponding Finished message,
4666 * the second ChangeCipherSpec should be discarded because it belongs
4667 * to an old epoch, but not because its length is shorter than
4668 * the minimum record length for packets using the new record transform.
4669 * Note that these two kinds of failures are handled differently,
4670 * as an unexpected record is silently skipped but an invalid
4671 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004672 */
4673#if defined(MBEDTLS_SSL_PROTO_DTLS)
4674 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4675 {
4676 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4677
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004678 /* Check epoch (and sequence number) with DTLS */
4679 if( rec_epoch != ssl->in_epoch )
4680 {
4681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4682 "expected %d, received %d",
4683 ssl->in_epoch, rec_epoch ) );
4684
4685#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4686 /*
4687 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4688 * access the first byte of record content (handshake type), as we
4689 * have an active transform (possibly iv_len != 0), so use the
4690 * fact that the record header len is 13 instead.
4691 */
4692 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4693 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4694 rec_epoch == 0 &&
4695 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4696 ssl->in_left > 13 &&
4697 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4698 {
4699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4700 "from the same port" ) );
4701 return( ssl_handle_possible_reconnect( ssl ) );
4702 }
4703 else
4704#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004705 {
4706 /* Consider buffering the record. */
4707 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4708 {
4709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4710 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4711 }
4712
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004713 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004714 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004715 }
4716
4717#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4718 /* Replay detection only works for the current epoch */
4719 if( rec_epoch == ssl->in_epoch &&
4720 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4721 {
4722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4723 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4724 }
4725#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004726
Hanno Becker52c6dc62017-05-26 16:07:36 +01004727 /* Drop unexpected ApplicationData records,
4728 * except at the beginning of renegotiations */
4729 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4730 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4731#if defined(MBEDTLS_SSL_RENEGOTIATION)
4732 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4733 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4734#endif
4735 )
4736 {
4737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4738 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4739 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004740 }
4741#endif /* MBEDTLS_SSL_PROTO_DTLS */
4742
Hanno Becker52c6dc62017-05-26 16:07:36 +01004743
4744 /* Check length against bounds of the current transform and version */
4745 if( ssl->transform_in == NULL )
4746 {
4747 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004748 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004749 {
4750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4751 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4752 }
4753 }
4754 else
4755 {
4756 if( ssl->in_msglen < ssl->transform_in->minlen )
4757 {
4758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4759 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4760 }
4761
4762#if defined(MBEDTLS_SSL_PROTO_SSL3)
4763 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004764 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004765 {
4766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4767 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4768 }
4769#endif
4770#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4771 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4772 /*
4773 * TLS encrypted messages can have up to 256 bytes of padding
4774 */
4775 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4776 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004777 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004778 {
4779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4780 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4781 }
4782#endif
4783 }
4784
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004785 return( 0 );
4786}
Paul Bakker5121ce52009-01-03 21:22:43 +00004787
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004788/*
4789 * If applicable, decrypt (and decompress) record content
4790 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004791static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004792{
4793 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004795 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4796 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004798#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4799 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004803 ret = mbedtls_ssl_hw_record_read( ssl );
4804 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004806 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4807 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004808 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004809
4810 if( ret == 0 )
4811 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004812 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004813#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004814 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004815 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004816 mbedtls_record rec;
4817
4818 rec.buf = ssl->in_iv;
4819 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4820 - ( ssl->in_iv - ssl->in_buf );
4821 rec.data_len = ssl->in_msglen;
4822 rec.data_offset = 0;
4823
4824 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4825 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4826 ssl->conf->transport, rec.ver );
4827 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00004828 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4829 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004832 return( ret );
4833 }
4834
Hanno Becker29800d22018-08-07 14:30:18 +01004835 if( ssl->in_iv + rec.data_offset != ssl->in_msg )
4836 {
4837 /* Should never happen */
4838 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4839 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004840
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004841 ssl->in_msglen = rec.data_len;
4842 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4843 ssl->in_len[1] = (unsigned char)( rec.data_len );
4844
Hanno Becker1c0c37f2018-08-07 14:29:29 +01004845 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4846 ssl->in_msg, ssl->in_msglen );
4847
Angus Grattond8213d02016-05-25 20:56:48 +10004848 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4851 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004852 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004853 else if( ssl->in_msglen == 0 )
4854 {
4855#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4856 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4857 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4858 {
4859 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4861 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4862 }
4863#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4864
4865 ssl->nb_zero++;
4866
4867 /*
4868 * Three or more empty messages may be a DoS attack
4869 * (excessive CPU consumption).
4870 */
4871 if( ssl->nb_zero > 3 )
4872 {
4873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
4874 "messages, possible DoS attack" ) );
4875 /* Q: Is that the right error code? */
4876 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4877 }
4878 }
4879 else
4880 ssl->nb_zero = 0;
4881
4882#if defined(MBEDTLS_SSL_PROTO_DTLS)
4883 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4884 {
4885 ; /* in_ctr read from peer, not maintained internally */
4886 }
4887 else
4888#endif
4889 {
4890 unsigned i;
4891 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4892 if( ++ssl->in_ctr[i - 1] != 0 )
4893 break;
4894
4895 /* The loop goes to its end iff the counter is wrapping */
4896 if( i == ssl_ep_len( ssl ) )
4897 {
4898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4899 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4900 }
4901 }
4902
Paul Bakker5121ce52009-01-03 21:22:43 +00004903 }
4904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004905#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004906 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004907 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004908 {
4909 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004911 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004912 return( ret );
4913 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004914 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004915#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004917#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004918 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004920 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004921 }
4922#endif
4923
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004924 return( 0 );
4925}
4926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004927static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004928
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004929/*
4930 * Read a record.
4931 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004932 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4933 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4934 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004935 */
Hanno Becker1097b342018-08-15 14:09:41 +01004936
4937/* Helper functions for mbedtls_ssl_read_record(). */
4938static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004939static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4940static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004941
Hanno Becker327c93b2018-08-15 13:56:18 +01004942int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004943 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004944{
4945 int ret;
4946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004948
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004949 if( ssl->keep_current_message == 0 )
4950 {
4951 do {
Simon Butcher99000142016-10-13 17:21:01 +01004952
Hanno Becker26994592018-08-15 14:14:59 +01004953 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004954 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004955 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004956
Hanno Beckere74d5562018-08-15 14:26:08 +01004957 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004958 {
Hanno Becker40f50842018-08-15 14:48:01 +01004959#if defined(MBEDTLS_SSL_PROTO_DTLS)
4960 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004961
Hanno Becker40f50842018-08-15 14:48:01 +01004962 /* We only check for buffered messages if the
4963 * current datagram is fully consumed. */
4964 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004965 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004966 {
Hanno Becker40f50842018-08-15 14:48:01 +01004967 if( ssl_load_buffered_message( ssl ) == 0 )
4968 have_buffered = 1;
4969 }
4970
4971 if( have_buffered == 0 )
4972#endif /* MBEDTLS_SSL_PROTO_DTLS */
4973 {
4974 ret = ssl_get_next_record( ssl );
4975 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4976 continue;
4977
4978 if( ret != 0 )
4979 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004980 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004981 return( ret );
4982 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004983 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004984 }
4985
4986 ret = mbedtls_ssl_handle_message_type( ssl );
4987
Hanno Becker40f50842018-08-15 14:48:01 +01004988#if defined(MBEDTLS_SSL_PROTO_DTLS)
4989 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4990 {
4991 /* Buffer future message */
4992 ret = ssl_buffer_message( ssl );
4993 if( ret != 0 )
4994 return( ret );
4995
4996 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4997 }
4998#endif /* MBEDTLS_SSL_PROTO_DTLS */
4999
Hanno Becker90333da2017-10-10 11:27:13 +01005000 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5001 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005002
5003 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005004 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005005 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005006 return( ret );
5007 }
5008
Hanno Becker327c93b2018-08-15 13:56:18 +01005009 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005010 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005011 {
5012 mbedtls_ssl_update_handshake_status( ssl );
5013 }
Simon Butcher99000142016-10-13 17:21:01 +01005014 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005015 else
Simon Butcher99000142016-10-13 17:21:01 +01005016 {
Hanno Becker02f59072018-08-15 14:00:24 +01005017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005018 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005019 }
5020
5021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5022
5023 return( 0 );
5024}
5025
Hanno Becker40f50842018-08-15 14:48:01 +01005026#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005027static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005028{
Hanno Becker40f50842018-08-15 14:48:01 +01005029 if( ssl->in_left > ssl->next_record_offset )
5030 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005031
Hanno Becker40f50842018-08-15 14:48:01 +01005032 return( 0 );
5033}
5034
5035static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5036{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005037 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005038 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005039 int ret = 0;
5040
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005041 if( hs == NULL )
5042 return( -1 );
5043
Hanno Beckere00ae372018-08-20 09:39:42 +01005044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5045
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005046 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5047 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5048 {
5049 /* Check if we have seen a ChangeCipherSpec before.
5050 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005051 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005052 {
5053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5054 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005055 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005056 }
5057
Hanno Becker39b8bc92018-08-28 17:17:13 +01005058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005059 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5060 ssl->in_msglen = 1;
5061 ssl->in_msg[0] = 1;
5062
5063 /* As long as they are equal, the exact value doesn't matter. */
5064 ssl->in_left = 0;
5065 ssl->next_record_offset = 0;
5066
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005067 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005068 goto exit;
5069 }
Hanno Becker37f95322018-08-16 13:55:32 +01005070
Hanno Beckerb8f50142018-08-28 10:01:34 +01005071#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005072 /* Debug only */
5073 {
5074 unsigned offset;
5075 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5076 {
5077 hs_buf = &hs->buffering.hs[offset];
5078 if( hs_buf->is_valid == 1 )
5079 {
5080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5081 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005082 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005083 }
5084 }
5085 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005086#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005087
5088 /* Check if we have buffered and/or fully reassembled the
5089 * next handshake message. */
5090 hs_buf = &hs->buffering.hs[0];
5091 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5092 {
5093 /* Synthesize a record containing the buffered HS message. */
5094 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5095 ( hs_buf->data[2] << 8 ) |
5096 hs_buf->data[3];
5097
5098 /* Double-check that we haven't accidentally buffered
5099 * a message that doesn't fit into the input buffer. */
5100 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5101 {
5102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5103 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5104 }
5105
5106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5107 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5108 hs_buf->data, msg_len + 12 );
5109
5110 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5111 ssl->in_hslen = msg_len + 12;
5112 ssl->in_msglen = msg_len + 12;
5113 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5114
5115 ret = 0;
5116 goto exit;
5117 }
5118 else
5119 {
5120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5121 hs->in_msg_seq ) );
5122 }
5123
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005124 ret = -1;
5125
5126exit:
5127
5128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5129 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005130}
5131
Hanno Beckera02b0b42018-08-21 17:20:27 +01005132static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5133 size_t desired )
5134{
5135 int offset;
5136 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5138 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005139
Hanno Becker01315ea2018-08-21 17:22:17 +01005140 /* Get rid of future records epoch first, if such exist. */
5141 ssl_free_buffered_record( ssl );
5142
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 future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005148 return( 0 );
5149 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005150
Hanno Becker4f432ad2018-08-28 10:02:32 +01005151 /* We don't have enough space to buffer the next expected handshake
5152 * message. Remove buffers used for future messages to gain space,
5153 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005154 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5155 offset >= 0; offset-- )
5156 {
5157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5158 offset ) );
5159
Hanno Beckerb309b922018-08-23 13:18:05 +01005160 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005161
5162 /* Check if we have enough space available now. */
5163 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5164 hs->buffering.total_bytes_buffered ) )
5165 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005167 return( 0 );
5168 }
5169 }
5170
5171 return( -1 );
5172}
5173
Hanno Becker40f50842018-08-15 14:48:01 +01005174static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5175{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005176 int ret = 0;
5177 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5178
5179 if( hs == NULL )
5180 return( 0 );
5181
5182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5183
5184 switch( ssl->in_msgtype )
5185 {
5186 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005188
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005189 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005190 break;
5191
5192 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005193 {
5194 unsigned recv_msg_seq_offset;
5195 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5196 mbedtls_ssl_hs_buffer *hs_buf;
5197 size_t msg_len = ssl->in_hslen - 12;
5198
5199 /* We should never receive an old handshake
5200 * message - double-check nonetheless. */
5201 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5202 {
5203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5205 }
5206
5207 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5208 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5209 {
5210 /* Silently ignore -- message too far in the future */
5211 MBEDTLS_SSL_DEBUG_MSG( 2,
5212 ( "Ignore future HS message with sequence number %u, "
5213 "buffering window %u - %u",
5214 recv_msg_seq, ssl->handshake->in_msg_seq,
5215 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5216
5217 goto exit;
5218 }
5219
5220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5221 recv_msg_seq, recv_msg_seq_offset ) );
5222
5223 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5224
5225 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005226 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005227 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005228 size_t reassembly_buf_sz;
5229
Hanno Becker37f95322018-08-16 13:55:32 +01005230 hs_buf->is_fragmented =
5231 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5232
5233 /* We copy the message back into the input buffer
5234 * after reassembly, so check that it's not too large.
5235 * This is an implementation-specific limitation
5236 * and not one from the standard, hence it is not
5237 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005238 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005239 {
5240 /* Ignore message */
5241 goto exit;
5242 }
5243
Hanno Beckere0b150f2018-08-21 15:51:03 +01005244 /* Check if we have enough space to buffer the message. */
5245 if( hs->buffering.total_bytes_buffered >
5246 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5247 {
5248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5249 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5250 }
5251
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005252 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5253 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005254
5255 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5256 hs->buffering.total_bytes_buffered ) )
5257 {
5258 if( recv_msg_seq_offset > 0 )
5259 {
5260 /* If we can't buffer a future message because
5261 * of space limitations -- ignore. */
5262 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",
5263 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5264 (unsigned) hs->buffering.total_bytes_buffered ) );
5265 goto exit;
5266 }
Hanno Beckere1801392018-08-21 16:51:05 +01005267 else
5268 {
5269 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",
5270 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5271 (unsigned) hs->buffering.total_bytes_buffered ) );
5272 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005273
Hanno Beckera02b0b42018-08-21 17:20:27 +01005274 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005275 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005276 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",
5277 (unsigned) msg_len,
5278 (unsigned) reassembly_buf_sz,
5279 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005280 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005281 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5282 goto exit;
5283 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005284 }
5285
5286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5287 msg_len ) );
5288
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005289 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5290 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005291 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005292 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005293 goto exit;
5294 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005295 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005296
5297 /* Prepare final header: copy msg_type, length and message_seq,
5298 * then add standardised fragment_offset and fragment_length */
5299 memcpy( hs_buf->data, ssl->in_msg, 6 );
5300 memset( hs_buf->data + 6, 0, 3 );
5301 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5302
5303 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005304
5305 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005306 }
5307 else
5308 {
5309 /* Make sure msg_type and length are consistent */
5310 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5311 {
5312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5313 /* Ignore */
5314 goto exit;
5315 }
5316 }
5317
Hanno Becker4422bbb2018-08-20 09:40:19 +01005318 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005319 {
5320 size_t frag_len, frag_off;
5321 unsigned char * const msg = hs_buf->data + 12;
5322
5323 /*
5324 * Check and copy current fragment
5325 */
5326
5327 /* Validation of header fields already done in
5328 * mbedtls_ssl_prepare_handshake_record(). */
5329 frag_off = ssl_get_hs_frag_off( ssl );
5330 frag_len = ssl_get_hs_frag_len( ssl );
5331
5332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5333 frag_off, frag_len ) );
5334 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5335
5336 if( hs_buf->is_fragmented )
5337 {
5338 unsigned char * const bitmask = msg + msg_len;
5339 ssl_bitmask_set( bitmask, frag_off, frag_len );
5340 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5341 msg_len ) == 0 );
5342 }
5343 else
5344 {
5345 hs_buf->is_complete = 1;
5346 }
5347
5348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5349 hs_buf->is_complete ? "" : "not yet " ) );
5350 }
5351
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005352 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005353 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005354
5355 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005356 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005357 break;
5358 }
5359
5360exit:
5361
5362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5363 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005364}
5365#endif /* MBEDTLS_SSL_PROTO_DTLS */
5366
Hanno Becker1097b342018-08-15 14:09:41 +01005367static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005368{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005369 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005370 * Consume last content-layer message and potentially
5371 * update in_msglen which keeps track of the contents'
5372 * consumption state.
5373 *
5374 * (1) Handshake messages:
5375 * Remove last handshake message, move content
5376 * and adapt in_msglen.
5377 *
5378 * (2) Alert messages:
5379 * Consume whole record content, in_msglen = 0.
5380 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005381 * (3) Change cipher spec:
5382 * Consume whole record content, in_msglen = 0.
5383 *
5384 * (4) Application data:
5385 * Don't do anything - the record layer provides
5386 * the application data as a stream transport
5387 * and consumes through mbedtls_ssl_read only.
5388 *
5389 */
5390
5391 /* Case (1): Handshake messages */
5392 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005393 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005394 /* Hard assertion to be sure that no application data
5395 * is in flight, as corrupting ssl->in_msglen during
5396 * ssl->in_offt != NULL is fatal. */
5397 if( ssl->in_offt != NULL )
5398 {
5399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5400 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5401 }
5402
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005403 /*
5404 * Get next Handshake message in the current record
5405 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005406
Hanno Becker4a810fb2017-05-24 16:27:30 +01005407 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005408 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005409 * current handshake content: If DTLS handshake
5410 * fragmentation is used, that's the fragment
5411 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005412 * size here is faulty and should be changed at
5413 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005414 * (2) While it doesn't seem to cause problems, one
5415 * has to be very careful not to assume that in_hslen
5416 * is always <= in_msglen in a sensible communication.
5417 * Again, it's wrong for DTLS handshake fragmentation.
5418 * The following check is therefore mandatory, and
5419 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005420 * Additionally, ssl->in_hslen might be arbitrarily out of
5421 * bounds after handling a DTLS message with an unexpected
5422 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005423 */
5424 if( ssl->in_hslen < ssl->in_msglen )
5425 {
5426 ssl->in_msglen -= ssl->in_hslen;
5427 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5428 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005429
Hanno Becker4a810fb2017-05-24 16:27:30 +01005430 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5431 ssl->in_msg, ssl->in_msglen );
5432 }
5433 else
5434 {
5435 ssl->in_msglen = 0;
5436 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005437
Hanno Becker4a810fb2017-05-24 16:27:30 +01005438 ssl->in_hslen = 0;
5439 }
5440 /* Case (4): Application data */
5441 else if( ssl->in_offt != NULL )
5442 {
5443 return( 0 );
5444 }
5445 /* Everything else (CCS & Alerts) */
5446 else
5447 {
5448 ssl->in_msglen = 0;
5449 }
5450
Hanno Becker1097b342018-08-15 14:09:41 +01005451 return( 0 );
5452}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005453
Hanno Beckere74d5562018-08-15 14:26:08 +01005454static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5455{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005456 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005457 return( 1 );
5458
5459 return( 0 );
5460}
5461
Hanno Becker5f066e72018-08-16 14:56:31 +01005462#if defined(MBEDTLS_SSL_PROTO_DTLS)
5463
5464static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5465{
5466 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5467 if( hs == NULL )
5468 return;
5469
Hanno Becker01315ea2018-08-21 17:22:17 +01005470 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005471 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005472 hs->buffering.total_bytes_buffered -=
5473 hs->buffering.future_record.len;
5474
5475 mbedtls_free( hs->buffering.future_record.data );
5476 hs->buffering.future_record.data = NULL;
5477 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005478}
5479
5480static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5481{
5482 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5483 unsigned char * rec;
5484 size_t rec_len;
5485 unsigned rec_epoch;
5486
5487 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5488 return( 0 );
5489
5490 if( hs == NULL )
5491 return( 0 );
5492
Hanno Becker5f066e72018-08-16 14:56:31 +01005493 rec = hs->buffering.future_record.data;
5494 rec_len = hs->buffering.future_record.len;
5495 rec_epoch = hs->buffering.future_record.epoch;
5496
5497 if( rec == NULL )
5498 return( 0 );
5499
Hanno Becker4cb782d2018-08-20 11:19:05 +01005500 /* Only consider loading future records if the
5501 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005502 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005503 return( 0 );
5504
Hanno Becker5f066e72018-08-16 14:56:31 +01005505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5506
5507 if( rec_epoch != ssl->in_epoch )
5508 {
5509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5510 goto exit;
5511 }
5512
5513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5514
5515 /* Double-check that the record is not too large */
5516 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5517 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5518 {
5519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5520 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5521 }
5522
5523 memcpy( ssl->in_hdr, rec, rec_len );
5524 ssl->in_left = rec_len;
5525 ssl->next_record_offset = 0;
5526
5527 ssl_free_buffered_record( ssl );
5528
5529exit:
5530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5531 return( 0 );
5532}
5533
5534static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5535{
5536 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5537 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005538 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005539
5540 /* Don't buffer future records outside handshakes. */
5541 if( hs == NULL )
5542 return( 0 );
5543
5544 /* Only buffer handshake records (we are only interested
5545 * in Finished messages). */
5546 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5547 return( 0 );
5548
5549 /* Don't buffer more than one future epoch record. */
5550 if( hs->buffering.future_record.data != NULL )
5551 return( 0 );
5552
Hanno Becker01315ea2018-08-21 17:22:17 +01005553 /* Don't buffer record if there's not enough buffering space remaining. */
5554 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5555 hs->buffering.total_bytes_buffered ) )
5556 {
5557 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",
5558 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5559 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005560 return( 0 );
5561 }
5562
Hanno Becker5f066e72018-08-16 14:56:31 +01005563 /* Buffer record */
5564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5565 ssl->in_epoch + 1 ) );
5566 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5567 rec_hdr_len + ssl->in_msglen );
5568
5569 /* ssl_parse_record_header() only considers records
5570 * of the next epoch as candidates for buffering. */
5571 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005572 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005573
5574 hs->buffering.future_record.data =
5575 mbedtls_calloc( 1, hs->buffering.future_record.len );
5576 if( hs->buffering.future_record.data == NULL )
5577 {
5578 /* If we run out of RAM trying to buffer a
5579 * record from the next epoch, just ignore. */
5580 return( 0 );
5581 }
5582
Hanno Becker01315ea2018-08-21 17:22:17 +01005583 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005584
Hanno Becker01315ea2018-08-21 17:22:17 +01005585 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005586 return( 0 );
5587}
5588
5589#endif /* MBEDTLS_SSL_PROTO_DTLS */
5590
Hanno Beckere74d5562018-08-15 14:26:08 +01005591static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005592{
5593 int ret;
5594
Hanno Becker5f066e72018-08-16 14:56:31 +01005595#if defined(MBEDTLS_SSL_PROTO_DTLS)
5596 /* We might have buffered a future record; if so,
5597 * and if the epoch matches now, load it.
5598 * On success, this call will set ssl->in_left to
5599 * the length of the buffered record, so that
5600 * the calls to ssl_fetch_input() below will
5601 * essentially be no-ops. */
5602 ret = ssl_load_buffered_record( ssl );
5603 if( ret != 0 )
5604 return( ret );
5605#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005610 return( ret );
5611 }
5612
5613 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005616 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5617 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005618 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005619 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5620 {
5621 ret = ssl_buffer_future_record( ssl );
5622 if( ret != 0 )
5623 return( ret );
5624
5625 /* Fall through to handling of unexpected records */
5626 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5627 }
5628
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005629 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5630 {
5631 /* Skip unexpected record (but not whole datagram) */
5632 ssl->next_record_offset = ssl->in_msglen
5633 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005634
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5636 "(header)" ) );
5637 }
5638 else
5639 {
5640 /* Skip invalid record and the rest of the datagram */
5641 ssl->next_record_offset = 0;
5642 ssl->in_left = 0;
5643
5644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5645 "(header)" ) );
5646 }
5647
5648 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005649 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005650 }
5651#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005652 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005653 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005654
5655 /*
5656 * Read and optionally decrypt the message contents
5657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5659 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005662 return( ret );
5663 }
5664
5665 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005666#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005667 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005669 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005670 if( ssl->next_record_offset < ssl->in_left )
5671 {
5672 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5673 }
5674 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005675 else
5676#endif
5677 ssl->in_left = 0;
5678
5679 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005681#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005682 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005683 {
5684 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005685 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5686 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005687 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005688 /* Except when waiting for Finished as a bad mac here
5689 * probably means something went wrong in the handshake
5690 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5691 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5692 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5693 {
5694#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5695 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5696 {
5697 mbedtls_ssl_send_alert_message( ssl,
5698 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5699 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5700 }
5701#endif
5702 return( ret );
5703 }
5704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005705#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005706 if( ssl->conf->badmac_limit != 0 &&
5707 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5710 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005711 }
5712#endif
5713
Hanno Becker4a810fb2017-05-24 16:27:30 +01005714 /* As above, invalid records cause
5715 * dismissal of the whole datagram. */
5716
5717 ssl->next_record_offset = 0;
5718 ssl->in_left = 0;
5719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005721 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005722 }
5723
5724 return( ret );
5725 }
5726 else
5727#endif
5728 {
5729 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5731 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005733 mbedtls_ssl_send_alert_message( ssl,
5734 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5735 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005736 }
5737#endif
5738 return( ret );
5739 }
5740 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005741
Simon Butcher99000142016-10-13 17:21:01 +01005742 return( 0 );
5743}
5744
5745int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5746{
5747 int ret;
5748
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005749 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005750 * Handle particular types of records
5751 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005752 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005753 {
Simon Butcher99000142016-10-13 17:21:01 +01005754 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5755 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005756 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005757 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005758 }
5759
Hanno Beckere678eaa2018-08-21 14:57:46 +01005760 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005761 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005762 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005763 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5765 ssl->in_msglen ) );
5766 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005767 }
5768
Hanno Beckere678eaa2018-08-21 14:57:46 +01005769 if( ssl->in_msg[0] != 1 )
5770 {
5771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5772 ssl->in_msg[0] ) );
5773 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5774 }
5775
5776#if defined(MBEDTLS_SSL_PROTO_DTLS)
5777 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5778 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5779 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5780 {
5781 if( ssl->handshake == NULL )
5782 {
5783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5784 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5785 }
5786
5787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5788 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5789 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005790#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005791 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005793 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005794 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005795 if( ssl->in_msglen != 2 )
5796 {
5797 /* Note: Standard allows for more than one 2 byte alert
5798 to be packed in a single message, but Mbed TLS doesn't
5799 currently support this. */
5800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5801 ssl->in_msglen ) );
5802 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5803 }
5804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005806 ssl->in_msg[0], ssl->in_msg[1] ) );
5807
5808 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005809 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005810 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005811 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005814 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005815 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005816 }
5817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5819 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5822 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005823 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005824
5825#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5826 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5827 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5828 {
Hanno Becker90333da2017-10-10 11:27:13 +01005829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005830 /* Will be handled when trying to parse ServerHello */
5831 return( 0 );
5832 }
5833#endif
5834
5835#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5836 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5837 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5838 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5839 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5840 {
5841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5842 /* Will be handled in mbedtls_ssl_parse_certificate() */
5843 return( 0 );
5844 }
5845#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5846
5847 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005848 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005849 }
5850
Hanno Beckerc76c6192017-06-06 10:03:17 +01005851#if defined(MBEDTLS_SSL_PROTO_DTLS)
5852 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5853 ssl->handshake != NULL &&
5854 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5855 {
5856 ssl_handshake_wrapup_free_hs_transform( ssl );
5857 }
5858#endif
5859
Paul Bakker5121ce52009-01-03 21:22:43 +00005860 return( 0 );
5861}
5862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005864{
5865 int ret;
5866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005867 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5868 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5869 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005870 {
5871 return( ret );
5872 }
5873
5874 return( 0 );
5875}
5876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005878 unsigned char level,
5879 unsigned char message )
5880{
5881 int ret;
5882
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005883 if( ssl == NULL || ssl->conf == NULL )
5884 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005887 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005889 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005890 ssl->out_msglen = 2;
5891 ssl->out_msg[0] = level;
5892 ssl->out_msg[1] = message;
5893
Hanno Becker67bc7c32018-08-06 11:33:50 +01005894 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005896 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005897 return( ret );
5898 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005900
5901 return( 0 );
5902}
5903
Hanno Beckerb9d44792019-02-08 07:19:04 +00005904#if defined(MBEDTLS_X509_CRT_PARSE_C)
5905static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5906{
5907#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5908 if( session->peer_cert != NULL )
5909 {
5910 mbedtls_x509_crt_free( session->peer_cert );
5911 mbedtls_free( session->peer_cert );
5912 session->peer_cert = NULL;
5913 }
5914#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5915 if( session->peer_cert_digest != NULL )
5916 {
5917 /* Zeroization is not necessary. */
5918 mbedtls_free( session->peer_cert_digest );
5919 session->peer_cert_digest = NULL;
5920 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5921 session->peer_cert_digest_len = 0;
5922 }
5923#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5924}
5925#endif /* MBEDTLS_X509_CRT_PARSE_C */
5926
Paul Bakker5121ce52009-01-03 21:22:43 +00005927/*
5928 * Handshake functions
5929 */
Hanno Becker21489932019-02-05 13:20:55 +00005930#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005931/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005932int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005933{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005934 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5935 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005938
Hanno Becker7177a882019-02-05 13:36:46 +00005939 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005942 ssl->state++;
5943 return( 0 );
5944 }
5945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5947 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005948}
5949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005950int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005951{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005952 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5953 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005956
Hanno Becker7177a882019-02-05 13:36:46 +00005957 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005960 ssl->state++;
5961 return( 0 );
5962 }
5963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5965 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005966}
Gilles Peskinef9828522017-05-03 12:28:43 +02005967
Hanno Becker21489932019-02-05 13:20:55 +00005968#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005969/* Some certificate support -> implement write and parse */
5970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005971int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005972{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005973 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005974 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00005976 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5977 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005980
Hanno Becker7177a882019-02-05 13:36:46 +00005981 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005984 ssl->state++;
5985 return( 0 );
5986 }
5987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005989 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005990 {
5991 if( ssl->client_auth == 0 )
5992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005994 ssl->state++;
5995 return( 0 );
5996 }
5997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005998#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00005999 /*
6000 * If using SSLv3 and got no cert, send an Alert message
6001 * (otherwise an empty Certificate message will be sent).
6002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006003 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6004 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006005 {
6006 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006007 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6008 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6009 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006012 goto write_msg;
6013 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006014#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006015 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016#endif /* MBEDTLS_SSL_CLI_C */
6017#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006018 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6023 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006024 }
6025 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006029
6030 /*
6031 * 0 . 0 handshake type
6032 * 1 . 3 handshake length
6033 * 4 . 6 length of all certs
6034 * 7 . 9 length of cert. 1
6035 * 10 . n-1 peer certificate
6036 * n . n+2 length of cert. 2
6037 * n+3 . ... upper level cert, etc.
6038 */
6039 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006040 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006041
Paul Bakker29087132010-03-21 21:03:34 +00006042 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006043 {
6044 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006045 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006048 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006050 }
6051
6052 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6053 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6054 ssl->out_msg[i + 2] = (unsigned char)( n );
6055
6056 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6057 i += n; crt = crt->next;
6058 }
6059
6060 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6061 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6062 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6063
6064 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006065 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6066 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006067
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006068#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006069write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006070#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006071
6072 ssl->state++;
6073
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006074 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006075 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006076 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006077 return( ret );
6078 }
6079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006081
Paul Bakkered27a042013-04-18 22:46:23 +02006082 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006083}
6084
Hanno Becker84879e32019-01-31 07:44:03 +00006085#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006086
6087#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006088static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6089 unsigned char *crt_buf,
6090 size_t crt_buf_len )
6091{
6092 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6093
6094 if( peer_crt == NULL )
6095 return( -1 );
6096
6097 if( peer_crt->raw.len != crt_buf_len )
6098 return( -1 );
6099
Hanno Becker46f34d02019-02-08 14:00:04 +00006100 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006101}
Hanno Becker177475a2019-02-05 17:02:46 +00006102#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6103static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6104 unsigned char *crt_buf,
6105 size_t crt_buf_len )
6106{
6107 int ret;
6108 unsigned char const * const peer_cert_digest =
6109 ssl->session->peer_cert_digest;
6110 mbedtls_md_type_t const peer_cert_digest_type =
6111 ssl->session->peer_cert_digest_type;
6112 mbedtls_md_info_t const * const digest_info =
6113 mbedtls_md_info_from_type( peer_cert_digest_type );
6114 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6115 size_t digest_len;
6116
6117 if( peer_cert_digest == NULL || digest_info == NULL )
6118 return( -1 );
6119
6120 digest_len = mbedtls_md_get_size( digest_info );
6121 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6122 return( -1 );
6123
6124 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6125 if( ret != 0 )
6126 return( -1 );
6127
6128 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6129}
6130#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006131#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006132
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006133/*
6134 * Once the certificate message is read, parse it into a cert chain and
6135 * perform basic checks, but leave actual verification to the caller
6136 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006137static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6138 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006139{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006140 int ret;
6141#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6142 int crt_cnt=0;
6143#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006144 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006145 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006150 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6151 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006152 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006153 }
6154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006155 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6156 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006159 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6160 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006162 }
6163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006164 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006165
Paul Bakker5121ce52009-01-03 21:22:43 +00006166 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006168 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006169 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006170
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006171 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006172 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006175 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6176 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006178 }
6179
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006180 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6181 i += 3;
6182
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006183 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006184 while( i < ssl->in_hslen )
6185 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006186 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006187 if ( i + 3 > ssl->in_hslen ) {
6188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006189 mbedtls_ssl_send_alert_message( ssl,
6190 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6191 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006192 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6193 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006194 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6195 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006196 if( ssl->in_msg[i] != 0 )
6197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006199 mbedtls_ssl_send_alert_message( ssl,
6200 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6201 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006202 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006203 }
6204
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006205 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006206 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6207 | (unsigned int) ssl->in_msg[i + 2];
6208 i += 3;
6209
6210 if( n < 128 || i + n > ssl->in_hslen )
6211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006213 mbedtls_ssl_send_alert_message( ssl,
6214 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6215 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006216 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006217 }
6218
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006219 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006220#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6221 if( crt_cnt++ == 0 &&
6222 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6223 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006224 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006225 /* During client-side renegotiation, check that the server's
6226 * end-CRTs hasn't changed compared to the initial handshake,
6227 * mitigating the triple handshake attack. On success, reuse
6228 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006229 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6230 if( ssl_check_peer_crt_unchanged( ssl,
6231 &ssl->in_msg[i],
6232 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006233 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6235 mbedtls_ssl_send_alert_message( ssl,
6236 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6237 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6238 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006239 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006240
6241 /* Now we can safely free the original chain. */
6242 ssl_clear_peer_cert( ssl->session );
6243 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006244#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6245
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006246 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006247#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006248 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006249#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006250 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006251 * it in-place from the input buffer instead of making a copy. */
6252 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6253#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006254 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006255 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006256 case 0: /*ok*/
6257 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6258 /* Ignore certificate with an unknown algorithm: maybe a
6259 prior certificate was already trusted. */
6260 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006261
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006262 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6263 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6264 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006265
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006266 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6267 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6268 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006269
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006270 default:
6271 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6272 crt_parse_der_failed:
6273 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6274 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6275 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006276 }
6277
6278 i += n;
6279 }
6280
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006281 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006282 return( 0 );
6283}
6284
Hanno Becker4a55f632019-02-05 12:49:06 +00006285#if defined(MBEDTLS_SSL_SRV_C)
6286static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6287{
6288 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6289 return( -1 );
6290
6291#if defined(MBEDTLS_SSL_PROTO_SSL3)
6292 /*
6293 * Check if the client sent an empty certificate
6294 */
6295 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6296 {
6297 if( ssl->in_msglen == 2 &&
6298 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6299 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6300 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6301 {
6302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6303 return( 0 );
6304 }
6305
6306 return( -1 );
6307 }
6308#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6309
6310#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6311 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6312 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6313 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6314 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6315 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6316 {
6317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6318 return( 0 );
6319 }
6320
6321 return( -1 );
6322#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6323 MBEDTLS_SSL_PROTO_TLS1_2 */
6324}
6325#endif /* MBEDTLS_SSL_SRV_C */
6326
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006327/* Check if a certificate message is expected.
6328 * Return either
6329 * - SSL_CERTIFICATE_EXPECTED, or
6330 * - SSL_CERTIFICATE_SKIP
6331 * indicating whether a Certificate message is expected or not.
6332 */
6333#define SSL_CERTIFICATE_EXPECTED 0
6334#define SSL_CERTIFICATE_SKIP 1
6335static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6336 int authmode )
6337{
6338 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006339 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006340
6341 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6342 return( SSL_CERTIFICATE_SKIP );
6343
6344#if defined(MBEDTLS_SSL_SRV_C)
6345 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6346 {
6347 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6348 return( SSL_CERTIFICATE_SKIP );
6349
6350 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6351 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006352 ssl->session_negotiate->verify_result =
6353 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6354 return( SSL_CERTIFICATE_SKIP );
6355 }
6356 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006357#else
6358 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006359#endif /* MBEDTLS_SSL_SRV_C */
6360
6361 return( SSL_CERTIFICATE_EXPECTED );
6362}
6363
Hanno Becker68636192019-02-05 14:36:34 +00006364static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6365 int authmode,
6366 mbedtls_x509_crt *chain,
6367 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006368{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006369 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006370 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006371 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006372 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006373
Hanno Becker8927c832019-04-03 12:52:50 +01006374 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6375 void *p_vrfy;
6376
Hanno Becker68636192019-02-05 14:36:34 +00006377 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6378 return( 0 );
6379
Hanno Becker8927c832019-04-03 12:52:50 +01006380 if( ssl->f_vrfy != NULL )
6381 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006382 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006383 f_vrfy = ssl->f_vrfy;
6384 p_vrfy = ssl->p_vrfy;
6385 }
6386 else
6387 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006388 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006389 f_vrfy = ssl->conf->f_vrfy;
6390 p_vrfy = ssl->conf->p_vrfy;
6391 }
6392
Hanno Becker68636192019-02-05 14:36:34 +00006393 /*
6394 * Main check: verify certificate
6395 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006396#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6397 if( ssl->conf->f_ca_cb != NULL )
6398 {
6399 ((void) rs_ctx);
6400 have_ca_chain = 1;
6401
6402 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006403 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006404 chain,
6405 ssl->conf->f_ca_cb,
6406 ssl->conf->p_ca_cb,
6407 ssl->conf->cert_profile,
6408 ssl->hostname,
6409 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006410 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006411 }
6412 else
6413#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6414 {
6415 mbedtls_x509_crt *ca_chain;
6416 mbedtls_x509_crl *ca_crl;
6417
6418#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6419 if( ssl->handshake->sni_ca_chain != NULL )
6420 {
6421 ca_chain = ssl->handshake->sni_ca_chain;
6422 ca_crl = ssl->handshake->sni_ca_crl;
6423 }
6424 else
6425#endif
6426 {
6427 ca_chain = ssl->conf->ca_chain;
6428 ca_crl = ssl->conf->ca_crl;
6429 }
6430
6431 if( ca_chain != NULL )
6432 have_ca_chain = 1;
6433
6434 ret = mbedtls_x509_crt_verify_restartable(
6435 chain,
6436 ca_chain, ca_crl,
6437 ssl->conf->cert_profile,
6438 ssl->hostname,
6439 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006440 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006441 }
Hanno Becker68636192019-02-05 14:36:34 +00006442
6443 if( ret != 0 )
6444 {
6445 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6446 }
6447
6448#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6449 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6450 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6451#endif
6452
6453 /*
6454 * Secondary checks: always done, but change 'ret' only if it was 0
6455 */
6456
6457#if defined(MBEDTLS_ECP_C)
6458 {
6459 const mbedtls_pk_context *pk = &chain->pk;
6460
6461 /* If certificate uses an EC key, make sure the curve is OK */
6462 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6463 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6464 {
6465 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6466
6467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6468 if( ret == 0 )
6469 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6470 }
6471 }
6472#endif /* MBEDTLS_ECP_C */
6473
6474 if( mbedtls_ssl_check_cert_usage( chain,
6475 ciphersuite_info,
6476 ! ssl->conf->endpoint,
6477 &ssl->session_negotiate->verify_result ) != 0 )
6478 {
6479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6480 if( ret == 0 )
6481 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6482 }
6483
6484 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6485 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6486 * with details encoded in the verification flags. All other kinds
6487 * of error codes, including those from the user provided f_vrfy
6488 * functions, are treated as fatal and lead to a failure of
6489 * ssl_parse_certificate even if verification was optional. */
6490 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6491 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6492 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6493 {
6494 ret = 0;
6495 }
6496
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006497 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006498 {
6499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6500 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6501 }
6502
6503 if( ret != 0 )
6504 {
6505 uint8_t alert;
6506
6507 /* The certificate may have been rejected for several reasons.
6508 Pick one and send the corresponding alert. Which alert to send
6509 may be a subject of debate in some cases. */
6510 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6511 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6512 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6513 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6514 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6515 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6516 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6517 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6518 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6519 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6520 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6521 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6522 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6523 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6524 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6525 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6526 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6527 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6528 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6529 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6530 else
6531 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6532 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6533 alert );
6534 }
6535
6536#if defined(MBEDTLS_DEBUG_C)
6537 if( ssl->session_negotiate->verify_result != 0 )
6538 {
6539 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6540 ssl->session_negotiate->verify_result ) );
6541 }
6542 else
6543 {
6544 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6545 }
6546#endif /* MBEDTLS_DEBUG_C */
6547
6548 return( ret );
6549}
6550
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006551#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6552static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6553 unsigned char *start, size_t len )
6554{
6555 int ret;
6556 /* Remember digest of the peer's end-CRT. */
6557 ssl->session_negotiate->peer_cert_digest =
6558 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6559 if( ssl->session_negotiate->peer_cert_digest == NULL )
6560 {
6561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6562 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6563 mbedtls_ssl_send_alert_message( ssl,
6564 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6565 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6566
6567 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6568 }
6569
6570 ret = mbedtls_md( mbedtls_md_info_from_type(
6571 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6572 start, len,
6573 ssl->session_negotiate->peer_cert_digest );
6574
6575 ssl->session_negotiate->peer_cert_digest_type =
6576 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6577 ssl->session_negotiate->peer_cert_digest_len =
6578 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6579
6580 return( ret );
6581}
6582
6583static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6584 unsigned char *start, size_t len )
6585{
6586 unsigned char *end = start + len;
6587 int ret;
6588
6589 /* Make a copy of the peer's raw public key. */
6590 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6591 ret = mbedtls_pk_parse_subpubkey( &start, end,
6592 &ssl->handshake->peer_pubkey );
6593 if( ret != 0 )
6594 {
6595 /* We should have parsed the public key before. */
6596 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6597 }
6598
6599 return( 0 );
6600}
6601#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6602
Hanno Becker68636192019-02-05 14:36:34 +00006603int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6604{
6605 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006606 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006607#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6608 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6609 ? ssl->handshake->sni_authmode
6610 : ssl->conf->authmode;
6611#else
6612 const int authmode = ssl->conf->authmode;
6613#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006614 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006615 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006616
6617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6618
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006619 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6620 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006621 {
6622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006623 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006624 }
6625
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006626#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6627 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006628 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006629 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006630 chain = ssl->handshake->ecrs_peer_cert;
6631 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006632 goto crt_verify;
6633 }
6634#endif
6635
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006636 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006637 {
6638 /* mbedtls_ssl_read_record may have sent an alert already. We
6639 let it decide whether to alert. */
6640 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006641 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006642 }
6643
Hanno Becker4a55f632019-02-05 12:49:06 +00006644#if defined(MBEDTLS_SSL_SRV_C)
6645 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6646 {
6647 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006648
6649 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006650 ret = 0;
6651 else
6652 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006653
Hanno Becker6bdfab22019-02-05 13:11:17 +00006654 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006655 }
6656#endif /* MBEDTLS_SSL_SRV_C */
6657
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006658 /* Clear existing peer CRT structure in case we tried to
6659 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006660 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006661
6662 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6663 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006664 {
6665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6666 sizeof( mbedtls_x509_crt ) ) );
6667 mbedtls_ssl_send_alert_message( ssl,
6668 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6669 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006670
Hanno Becker3dad3112019-02-05 17:19:52 +00006671 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6672 goto exit;
6673 }
6674 mbedtls_x509_crt_init( chain );
6675
6676 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006677 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006678 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006679
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006680#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6681 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006682 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006683
6684crt_verify:
6685 if( ssl->handshake->ecrs_enabled)
6686 rs_ctx = &ssl->handshake->ecrs_ctx;
6687#endif
6688
Hanno Becker68636192019-02-05 14:36:34 +00006689 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006690 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006691 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006692 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006693
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006694#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006695 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006696 unsigned char *crt_start, *pk_start;
6697 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006698
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006699 /* We parse the CRT chain without copying, so
6700 * these pointers point into the input buffer,
6701 * and are hence still valid after freeing the
6702 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006703
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006704 crt_start = chain->raw.p;
6705 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006706
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006707 pk_start = chain->pk_raw.p;
6708 pk_len = chain->pk_raw.len;
6709
6710 /* Free the CRT structures before computing
6711 * digest and copying the peer's public key. */
6712 mbedtls_x509_crt_free( chain );
6713 mbedtls_free( chain );
6714 chain = NULL;
6715
6716 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006717 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006718 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006719
6720 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6721 if( ret != 0 )
6722 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006723 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006724#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6725 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006726 ssl->session_negotiate->peer_cert = chain;
6727 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006728#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006731
Hanno Becker6bdfab22019-02-05 13:11:17 +00006732exit:
6733
Hanno Becker3dad3112019-02-05 17:19:52 +00006734 if( ret == 0 )
6735 ssl->state++;
6736
6737#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6738 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6739 {
6740 ssl->handshake->ecrs_peer_cert = chain;
6741 chain = NULL;
6742 }
6743#endif
6744
6745 if( chain != NULL )
6746 {
6747 mbedtls_x509_crt_free( chain );
6748 mbedtls_free( chain );
6749 }
6750
Paul Bakker5121ce52009-01-03 21:22:43 +00006751 return( ret );
6752}
Hanno Becker21489932019-02-05 13:20:55 +00006753#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006756{
6757 int ret;
6758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006762 ssl->out_msglen = 1;
6763 ssl->out_msg[0] = 1;
6764
Paul Bakker5121ce52009-01-03 21:22:43 +00006765 ssl->state++;
6766
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006767 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006768 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006769 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006770 return( ret );
6771 }
6772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006774
6775 return( 0 );
6776}
6777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006779{
6780 int ret;
6781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006783
Hanno Becker327c93b2018-08-15 13:56:18 +01006784 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006787 return( ret );
6788 }
6789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006793 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6794 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006796 }
6797
Hanno Beckere678eaa2018-08-21 14:57:46 +01006798 /* CCS records are only accepted if they have length 1 and content '1',
6799 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006800
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006801 /*
6802 * Switch to our negotiated transform and session parameters for inbound
6803 * data.
6804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006805 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006806 ssl->transform_in = ssl->transform_negotiate;
6807 ssl->session_in = ssl->session_negotiate;
6808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006810 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006813 ssl_dtls_replay_reset( ssl );
6814#endif
6815
6816 /* Increment epoch */
6817 if( ++ssl->in_epoch == 0 )
6818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006820 /* This is highly unlikely to happen for legitimate reasons, so
6821 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006822 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006823 }
6824 }
6825 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006827 memset( ssl->in_ctr, 0, 8 );
6828
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006829 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6832 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006837 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6838 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006840 }
6841 }
6842#endif
6843
Paul Bakker5121ce52009-01-03 21:22:43 +00006844 ssl->state++;
6845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006847
6848 return( 0 );
6849}
6850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6852 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006853{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006854 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006856#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6857 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6858 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006859 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006860 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006861#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006862#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6863#if defined(MBEDTLS_SHA512_C)
6864 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006865 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6866 else
6867#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006868#if defined(MBEDTLS_SHA256_C)
6869 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006870 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006871 else
6872#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006873#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006876 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006877 }
Paul Bakker380da532012-04-18 16:10:25 +00006878}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006880void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006881{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006882#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6883 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006884 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6885 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006886#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006887#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6888#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006889#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006890 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006891 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6892#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006893 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006894#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006895#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006897#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006898 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006899 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006900#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006901 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006902#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006903#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006905}
6906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006908 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006909{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6911 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006912 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6913 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006914#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006915#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6916#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006917#if defined(MBEDTLS_USE_PSA_CRYPTO)
6918 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6919#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006920 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006921#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006922#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006924#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006925 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006926#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006927 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006928#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006929#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006931}
6932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6934 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6935static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006936 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006937{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006938 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6939 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006940}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006941#endif
Paul Bakker380da532012-04-18 16:10:25 +00006942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6944#if defined(MBEDTLS_SHA256_C)
6945static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006946 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006947{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006948#if defined(MBEDTLS_USE_PSA_CRYPTO)
6949 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6950#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006951 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006952#endif
Paul Bakker380da532012-04-18 16:10:25 +00006953}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006954#endif
Paul Bakker380da532012-04-18 16:10:25 +00006955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956#if defined(MBEDTLS_SHA512_C)
6957static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006958 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006959{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006960#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006961 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006962#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006963 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006964#endif
Paul Bakker380da532012-04-18 16:10:25 +00006965}
Paul Bakker769075d2012-11-24 11:26:46 +01006966#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006967#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006970static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006971 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006972{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006973 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006974 mbedtls_md5_context md5;
6975 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006976
Paul Bakker5121ce52009-01-03 21:22:43 +00006977 unsigned char padbuf[48];
6978 unsigned char md5sum[16];
6979 unsigned char sha1sum[20];
6980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006981 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006982 if( !session )
6983 session = ssl->session;
6984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006986
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006987 mbedtls_md5_init( &md5 );
6988 mbedtls_sha1_init( &sha1 );
6989
6990 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6991 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006992
6993 /*
6994 * SSLv3:
6995 * hash =
6996 * MD5( master + pad2 +
6997 * MD5( handshake + sender + master + pad1 ) )
6998 * + SHA1( master + pad2 +
6999 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007000 */
7001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007003 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7004 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007005#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007008 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7009 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007010#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007012 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007013 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007014
Paul Bakker1ef83d62012-04-11 12:09:53 +00007015 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007016
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007017 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7018 mbedtls_md5_update_ret( &md5, session->master, 48 );
7019 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7020 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007021
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007022 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7023 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7024 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7025 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007026
Paul Bakker1ef83d62012-04-11 12:09:53 +00007027 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007028
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007029 mbedtls_md5_starts_ret( &md5 );
7030 mbedtls_md5_update_ret( &md5, session->master, 48 );
7031 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7032 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7033 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007034
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007035 mbedtls_sha1_starts_ret( &sha1 );
7036 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7037 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7038 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7039 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007042
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007043 mbedtls_md5_free( &md5 );
7044 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007045
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007046 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7047 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7048 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007051}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007052#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007054#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007055static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007056 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007057{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007058 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007059 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007060 mbedtls_md5_context md5;
7061 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007062 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007064 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007065 if( !session )
7066 session = ssl->session;
7067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007069
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007070 mbedtls_md5_init( &md5 );
7071 mbedtls_sha1_init( &sha1 );
7072
7073 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7074 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007075
Paul Bakker1ef83d62012-04-11 12:09:53 +00007076 /*
7077 * TLSv1:
7078 * hash = PRF( master, finished_label,
7079 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7080 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007083 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7084 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007085#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007087#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007088 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7089 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007090#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007092 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007093 ? "client finished"
7094 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007095
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007096 mbedtls_md5_finish_ret( &md5, padbuf );
7097 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007098
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007099 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007100 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007103
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007104 mbedtls_md5_free( &md5 );
7105 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007106
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007107 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7114#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007115static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007117{
7118 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007119 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007120 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007121#if defined(MBEDTLS_USE_PSA_CRYPTO)
7122 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007123 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007124 psa_status_t status;
7125#else
7126 mbedtls_sha256_context sha256;
7127#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007130 if( !session )
7131 session = ssl->session;
7132
Andrzej Kurekeb342242019-01-29 09:14:33 -05007133 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7134 ? "client finished"
7135 : "server finished";
7136
7137#if defined(MBEDTLS_USE_PSA_CRYPTO)
7138 sha256_psa = psa_hash_operation_init();
7139
7140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7141
7142 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7143 if( status != PSA_SUCCESS )
7144 {
7145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7146 return;
7147 }
7148
7149 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7150 if( status != PSA_SUCCESS )
7151 {
7152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7153 return;
7154 }
7155 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7156#else
7157
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007158 mbedtls_sha256_init( &sha256 );
7159
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007160 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007161
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007162 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007163
7164 /*
7165 * TLSv1.2:
7166 * hash = PRF( master, finished_label,
7167 * Hash( handshake ) )[0.11]
7168 */
7169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170#if !defined(MBEDTLS_SHA256_ALT)
7171 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007172 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007173#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007174
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007175 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007176 mbedtls_sha256_free( &sha256 );
7177#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007178
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007179 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007180 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007183
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007184 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007187}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007188#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007190#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007191static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007193{
7194 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007195 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007196 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007197#if defined(MBEDTLS_USE_PSA_CRYPTO)
7198 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007199 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007200 psa_status_t status;
7201#else
7202 mbedtls_sha512_context sha512;
7203#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007206 if( !session )
7207 session = ssl->session;
7208
Andrzej Kurekeb342242019-01-29 09:14:33 -05007209 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7210 ? "client finished"
7211 : "server finished";
7212
7213#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007214 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007215
7216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7217
Andrzej Kurek972fba52019-01-30 03:29:12 -05007218 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007219 if( status != PSA_SUCCESS )
7220 {
7221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7222 return;
7223 }
7224
Andrzej Kurek972fba52019-01-30 03:29:12 -05007225 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007226 if( status != PSA_SUCCESS )
7227 {
7228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7229 return;
7230 }
7231 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7232#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007233 mbedtls_sha512_init( &sha512 );
7234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007236
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007237 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007238
7239 /*
7240 * TLSv1.2:
7241 * hash = PRF( master, finished_label,
7242 * Hash( handshake ) )[0.11]
7243 */
7244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007245#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007246 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7247 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007248#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007249
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007250 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007251 mbedtls_sha512_free( &sha512 );
7252#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007253
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007254 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007255 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007258
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007259 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007262}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263#endif /* MBEDTLS_SHA512_C */
7264#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007267{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007269
7270 /*
7271 * Free our handshake params
7272 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007273 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007275 ssl->handshake = NULL;
7276
7277 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007278 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007279 */
7280 if( ssl->transform )
7281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282 mbedtls_ssl_transform_free( ssl->transform );
7283 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007284 }
7285 ssl->transform = ssl->transform_negotiate;
7286 ssl->transform_negotiate = NULL;
7287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007289}
7290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007292{
7293 int resume = ssl->handshake->resume;
7294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297#if defined(MBEDTLS_SSL_RENEGOTIATION)
7298 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007300 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007301 ssl->renego_records_seen = 0;
7302 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007303#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007304
7305 /*
7306 * Free the previous session and switch in the current one
7307 */
Paul Bakker0a597072012-09-25 21:55:46 +00007308 if( ssl->session )
7309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007311 /* RFC 7366 3.1: keep the EtM state */
7312 ssl->session_negotiate->encrypt_then_mac =
7313 ssl->session->encrypt_then_mac;
7314#endif
7315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316 mbedtls_ssl_session_free( ssl->session );
7317 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007318 }
7319 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007320 ssl->session_negotiate = NULL;
7321
Paul Bakker0a597072012-09-25 21:55:46 +00007322 /*
7323 * Add cache entry
7324 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007325 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007326 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007327 resume == 0 )
7328 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007329 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007331 }
Paul Bakker0a597072012-09-25 21:55:46 +00007332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007334 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007335 ssl->handshake->flight != NULL )
7336 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007337 /* Cancel handshake timer */
7338 ssl_set_timer( ssl, 0 );
7339
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007340 /* Keep last flight around in case we need to resend it:
7341 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007343 }
7344 else
7345#endif
7346 ssl_handshake_wrapup_free_hs_transform( ssl );
7347
Paul Bakker48916f92012-09-16 19:57:18 +00007348 ssl->state++;
7349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007351}
7352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007353int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007354{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007355 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007358
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007359 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007360
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007361 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007362
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007363 /*
7364 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7365 * may define some other value. Currently (early 2016), no defined
7366 * ciphersuite does this (and this is unlikely to change as activity has
7367 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007372 ssl->verify_data_len = hash_len;
7373 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007374#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007375
Paul Bakker5121ce52009-01-03 21:22:43 +00007376 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7378 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007379
7380 /*
7381 * In case of session resuming, invert the client and server
7382 * ChangeCipherSpec messages order.
7383 */
Paul Bakker0a597072012-09-25 21:55:46 +00007384 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007387 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007389#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007391 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007393#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007394 }
7395 else
7396 ssl->state++;
7397
Paul Bakker48916f92012-09-16 19:57:18 +00007398 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007399 * Switch to our negotiated transform and session parameters for outbound
7400 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007405 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007406 {
7407 unsigned char i;
7408
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007409 /* Remember current epoch settings for resending */
7410 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007411 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007412
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007413 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007414 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007415
7416 /* Increment epoch */
7417 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007418 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007419 break;
7420
7421 /* The loop goes to its end iff the counter is wrapping */
7422 if( i == 0 )
7423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7425 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007426 }
7427 }
7428 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007429#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007430 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007431
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007432 ssl->transform_out = ssl->transform_negotiate;
7433 ssl->session_out = ssl->session_negotiate;
7434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7436 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007438 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7441 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007442 }
7443 }
7444#endif
7445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007447 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007448 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007449#endif
7450
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007451 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007452 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007453 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007454 return( ret );
7455 }
7456
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007457#if defined(MBEDTLS_SSL_PROTO_DTLS)
7458 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7459 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7460 {
7461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7462 return( ret );
7463 }
7464#endif
7465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007467
7468 return( 0 );
7469}
7470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007471#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007472#define SSL_MAX_HASH_LEN 36
7473#else
7474#define SSL_MAX_HASH_LEN 12
7475#endif
7476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007478{
Paul Bakker23986e52011-04-24 08:57:21 +00007479 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007480 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007481 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007484
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007485 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007486
Hanno Becker327c93b2018-08-15 13:56:18 +01007487 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007490 return( ret );
7491 }
7492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007496 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7497 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007499 }
7500
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007501 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502#if defined(MBEDTLS_SSL_PROTO_SSL3)
7503 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007504 hash_len = 36;
7505 else
7506#endif
7507 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007509 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7510 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007513 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7514 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007516 }
7517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007519 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007522 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7523 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007525 }
7526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007528 ssl->verify_data_len = hash_len;
7529 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007530#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007531
Paul Bakker0a597072012-09-25 21:55:46 +00007532 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007535 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007536 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007537#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007539 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007541#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007542 }
7543 else
7544 ssl->state++;
7545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007547 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007548 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007549#endif
7550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007552
7553 return( 0 );
7554}
7555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007557{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7561 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7562 mbedtls_md5_init( &handshake->fin_md5 );
7563 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007564 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7565 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007566#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007567#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7568#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007569#if defined(MBEDTLS_USE_PSA_CRYPTO)
7570 handshake->fin_sha256_psa = psa_hash_operation_init();
7571 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7572#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007574 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007575#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007576#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007578#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007579 handshake->fin_sha384_psa = psa_hash_operation_init();
7580 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007581#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007582 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007583 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007584#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007585#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007587
7588 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007589
7590#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7591 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7592 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7593#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007595#if defined(MBEDTLS_DHM_C)
7596 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007597#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007598#if defined(MBEDTLS_ECDH_C)
7599 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007600#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007601#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007602 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007603#if defined(MBEDTLS_SSL_CLI_C)
7604 handshake->ecjpake_cache = NULL;
7605 handshake->ecjpake_cache_len = 0;
7606#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007607#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007608
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007609#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007610 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007611#endif
7612
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007613#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7614 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7615#endif
Hanno Becker75173122019-02-06 16:18:31 +00007616
7617#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7618 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7619 mbedtls_pk_init( &handshake->peer_pubkey );
7620#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007621}
7622
Hanno Beckera18d1322018-01-03 14:27:32 +00007623void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007624{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007627 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7628 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007629
Hanno Beckerd56ed242018-01-03 15:32:51 +00007630#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007631 mbedtls_md_init( &transform->md_ctx_enc );
7632 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007633#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007634}
7635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007637{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007639}
7640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007642{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007643 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007644 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007646 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007647 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007648 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007649 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007650
7651 /*
7652 * Either the pointers are now NULL or cleared properly and can be freed.
7653 * Now allocate missing structures.
7654 */
7655 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007656 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007657 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007658 }
Paul Bakker48916f92012-09-16 19:57:18 +00007659
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007660 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007661 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007662 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007663 }
Paul Bakker48916f92012-09-16 19:57:18 +00007664
Paul Bakker82788fb2014-10-20 13:59:19 +02007665 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007666 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007667 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007668 }
Paul Bakker48916f92012-09-16 19:57:18 +00007669
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007670 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007671 if( ssl->handshake == NULL ||
7672 ssl->transform_negotiate == NULL ||
7673 ssl->session_negotiate == NULL )
7674 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007677 mbedtls_free( ssl->handshake );
7678 mbedtls_free( ssl->transform_negotiate );
7679 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007680
7681 ssl->handshake = NULL;
7682 ssl->transform_negotiate = NULL;
7683 ssl->session_negotiate = NULL;
7684
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007685 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007686 }
7687
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007688 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007690 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007691 ssl_handshake_params_init( ssl->handshake );
7692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007694 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7695 {
7696 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007697
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007698 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7699 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7700 else
7701 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007702
7703 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007704 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007705#endif
7706
Paul Bakker48916f92012-09-16 19:57:18 +00007707 return( 0 );
7708}
7709
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007710#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007711/* Dummy cookie callbacks for defaults */
7712static int ssl_cookie_write_dummy( void *ctx,
7713 unsigned char **p, unsigned char *end,
7714 const unsigned char *cli_id, size_t cli_id_len )
7715{
7716 ((void) ctx);
7717 ((void) p);
7718 ((void) end);
7719 ((void) cli_id);
7720 ((void) cli_id_len);
7721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007723}
7724
7725static int ssl_cookie_check_dummy( void *ctx,
7726 const unsigned char *cookie, size_t cookie_len,
7727 const unsigned char *cli_id, size_t cli_id_len )
7728{
7729 ((void) ctx);
7730 ((void) cookie);
7731 ((void) cookie_len);
7732 ((void) cli_id);
7733 ((void) cli_id_len);
7734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007736}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007737#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007738
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007739/* Once ssl->out_hdr as the address of the beginning of the
7740 * next outgoing record is set, deduce the other pointers.
7741 *
7742 * Note: For TLS, we save the implicit record sequence number
7743 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7744 * and the caller has to make sure there's space for this.
7745 */
7746
7747static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7748 mbedtls_ssl_transform *transform )
7749{
7750#if defined(MBEDTLS_SSL_PROTO_DTLS)
7751 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7752 {
7753 ssl->out_ctr = ssl->out_hdr + 3;
7754 ssl->out_len = ssl->out_hdr + 11;
7755 ssl->out_iv = ssl->out_hdr + 13;
7756 }
7757 else
7758#endif
7759 {
7760 ssl->out_ctr = ssl->out_hdr - 8;
7761 ssl->out_len = ssl->out_hdr + 3;
7762 ssl->out_iv = ssl->out_hdr + 5;
7763 }
7764
7765 /* Adjust out_msg to make space for explicit IV, if used. */
7766 if( transform != NULL &&
7767 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7768 {
7769 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7770 }
7771 else
7772 ssl->out_msg = ssl->out_iv;
7773}
7774
7775/* Once ssl->in_hdr as the address of the beginning of the
7776 * next incoming record is set, deduce the other pointers.
7777 *
7778 * Note: For TLS, we save the implicit record sequence number
7779 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7780 * and the caller has to make sure there's space for this.
7781 */
7782
7783static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7784 mbedtls_ssl_transform *transform )
7785{
7786#if defined(MBEDTLS_SSL_PROTO_DTLS)
7787 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7788 {
7789 ssl->in_ctr = ssl->in_hdr + 3;
7790 ssl->in_len = ssl->in_hdr + 11;
7791 ssl->in_iv = ssl->in_hdr + 13;
7792 }
7793 else
7794#endif
7795 {
7796 ssl->in_ctr = ssl->in_hdr - 8;
7797 ssl->in_len = ssl->in_hdr + 3;
7798 ssl->in_iv = ssl->in_hdr + 5;
7799 }
7800
7801 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7802 if( transform != NULL &&
7803 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7804 {
7805 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7806 }
7807 else
7808 ssl->in_msg = ssl->in_iv;
7809}
7810
Paul Bakker5121ce52009-01-03 21:22:43 +00007811/*
7812 * Initialize an SSL context
7813 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007814void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7815{
7816 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7817}
7818
7819/*
7820 * Setup an SSL context
7821 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007822
7823static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7824{
7825 /* Set the incoming and outgoing record pointers. */
7826#if defined(MBEDTLS_SSL_PROTO_DTLS)
7827 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7828 {
7829 ssl->out_hdr = ssl->out_buf;
7830 ssl->in_hdr = ssl->in_buf;
7831 }
7832 else
7833#endif /* MBEDTLS_SSL_PROTO_DTLS */
7834 {
7835 ssl->out_hdr = ssl->out_buf + 8;
7836 ssl->in_hdr = ssl->in_buf + 8;
7837 }
7838
7839 /* Derive other internal pointers. */
7840 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7841 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7842}
7843
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007844int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007845 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007846{
Paul Bakker48916f92012-09-16 19:57:18 +00007847 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007848
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007849 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007850
7851 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007852 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007853 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007854
7855 /* Set to NULL in case of an error condition */
7856 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007857
Angus Grattond8213d02016-05-25 20:56:48 +10007858 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7859 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007860 {
Angus Grattond8213d02016-05-25 20:56:48 +10007861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007862 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007863 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007864 }
7865
7866 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7867 if( ssl->out_buf == NULL )
7868 {
7869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007870 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007871 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007872 }
7873
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007874 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007875
Paul Bakker48916f92012-09-16 19:57:18 +00007876 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007877 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007878
7879 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007880
7881error:
7882 mbedtls_free( ssl->in_buf );
7883 mbedtls_free( ssl->out_buf );
7884
7885 ssl->conf = NULL;
7886
7887 ssl->in_buf = NULL;
7888 ssl->out_buf = NULL;
7889
7890 ssl->in_hdr = NULL;
7891 ssl->in_ctr = NULL;
7892 ssl->in_len = NULL;
7893 ssl->in_iv = NULL;
7894 ssl->in_msg = NULL;
7895
7896 ssl->out_hdr = NULL;
7897 ssl->out_ctr = NULL;
7898 ssl->out_len = NULL;
7899 ssl->out_iv = NULL;
7900 ssl->out_msg = NULL;
7901
k-stachowiak9f7798e2018-07-31 16:52:32 +02007902 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007903}
7904
7905/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007906 * Reset an initialized and used SSL context for re-use while retaining
7907 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007908 *
7909 * If partial is non-zero, keep data in the input buffer and client ID.
7910 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007911 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007912static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007913{
Paul Bakker48916f92012-09-16 19:57:18 +00007914 int ret;
7915
Hanno Becker7e772132018-08-10 12:38:21 +01007916#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7917 !defined(MBEDTLS_SSL_SRV_C)
7918 ((void) partial);
7919#endif
7920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007921 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007922
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007923 /* Cancel any possibly running timer */
7924 ssl_set_timer( ssl, 0 );
7925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926#if defined(MBEDTLS_SSL_RENEGOTIATION)
7927 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007928 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007929
7930 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007931 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7932 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007933#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007935
Paul Bakker7eb013f2011-10-06 12:37:39 +00007936 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007937 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007938
7939 ssl->in_msgtype = 0;
7940 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007942 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007943 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007944#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007946 ssl_dtls_replay_reset( ssl );
7947#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007948
7949 ssl->in_hslen = 0;
7950 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007951
7952 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007953
7954 ssl->out_msgtype = 0;
7955 ssl->out_msglen = 0;
7956 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007957#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7958 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007959 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007960#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007961
Hanno Becker19859472018-08-06 09:40:20 +01007962 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7963
Paul Bakker48916f92012-09-16 19:57:18 +00007964 ssl->transform_in = NULL;
7965 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007966
Hanno Becker78640902018-08-13 16:35:15 +01007967 ssl->session_in = NULL;
7968 ssl->session_out = NULL;
7969
Angus Grattond8213d02016-05-25 20:56:48 +10007970 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007971
7972#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007973 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007974#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7975 {
7976 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007977 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007978 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007980#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7981 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7984 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7987 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007988 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007989 }
7990#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007991
Paul Bakker48916f92012-09-16 19:57:18 +00007992 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994 mbedtls_ssl_transform_free( ssl->transform );
7995 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007996 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007997 }
Paul Bakker48916f92012-09-16 19:57:18 +00007998
Paul Bakkerc0463502013-02-14 11:19:38 +01007999 if( ssl->session )
8000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001 mbedtls_ssl_session_free( ssl->session );
8002 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008003 ssl->session = NULL;
8004 }
8005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008006#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008007 ssl->alpn_chosen = NULL;
8008#endif
8009
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008010#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008011#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008012 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008013#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008014 {
8015 mbedtls_free( ssl->cli_id );
8016 ssl->cli_id = NULL;
8017 ssl->cli_id_len = 0;
8018 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008019#endif
8020
Paul Bakker48916f92012-09-16 19:57:18 +00008021 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8022 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008023
8024 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008025}
8026
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008027/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008028 * Reset an initialized and used SSL context for re-use while retaining
8029 * all application-set variables, function pointers and data.
8030 */
8031int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8032{
8033 return( ssl_session_reset_int( ssl, 0 ) );
8034}
8035
8036/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008037 * SSL set accessors
8038 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008039void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008040{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008041 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008042}
8043
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008044void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008045{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008046 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008047}
8048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008049#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008050void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008051{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008052 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008053}
8054#endif
8055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008056#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008057void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008058{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008059 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008060}
8061#endif
8062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008063#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008064
Hanno Becker1841b0a2018-08-24 11:13:57 +01008065void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8066 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008067{
8068 ssl->disable_datagram_packing = !allow_packing;
8069}
8070
8071void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8072 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008073{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008074 conf->hs_timeout_min = min;
8075 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008076}
8077#endif
8078
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008079void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008080{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008081 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008082}
8083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008084#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008085void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008086 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008087 void *p_vrfy )
8088{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008089 conf->f_vrfy = f_vrfy;
8090 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008091}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008092#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008093
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008094void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008095 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008096 void *p_rng )
8097{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008098 conf->f_rng = f_rng;
8099 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008100}
8101
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008102void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008103 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008104 void *p_dbg )
8105{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008106 conf->f_dbg = f_dbg;
8107 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008108}
8109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008110void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008111 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008112 mbedtls_ssl_send_t *f_send,
8113 mbedtls_ssl_recv_t *f_recv,
8114 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008115{
8116 ssl->p_bio = p_bio;
8117 ssl->f_send = f_send;
8118 ssl->f_recv = f_recv;
8119 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008120}
8121
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008122#if defined(MBEDTLS_SSL_PROTO_DTLS)
8123void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8124{
8125 ssl->mtu = mtu;
8126}
8127#endif
8128
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008129void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008130{
8131 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008132}
8133
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008134void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8135 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008136 mbedtls_ssl_set_timer_t *f_set_timer,
8137 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008138{
8139 ssl->p_timer = p_timer;
8140 ssl->f_set_timer = f_set_timer;
8141 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008142
8143 /* Make sure we start with no timer running */
8144 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008145}
8146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008147#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008148void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008149 void *p_cache,
8150 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8151 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008152{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008153 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008154 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008155 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008156}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008157#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008159#if defined(MBEDTLS_SSL_CLI_C)
8160int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008161{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008162 int ret;
8163
8164 if( ssl == NULL ||
8165 session == NULL ||
8166 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008167 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008169 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008170 }
8171
Hanno Becker52055ae2019-02-06 14:30:46 +00008172 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8173 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008174 return( ret );
8175
Paul Bakker0a597072012-09-25 21:55:46 +00008176 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008177
8178 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008179}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008180#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008181
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008182void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008183 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008184{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008185 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8186 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8187 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8188 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008189}
8190
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008191void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008192 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008193 int major, int minor )
8194{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008195 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008196 return;
8197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008198 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008199 return;
8200
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008201 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008202}
8203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008204#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008205void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008206 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008207{
8208 conf->cert_profile = profile;
8209}
8210
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008211/* Append a new keycert entry to a (possibly empty) list */
8212static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8213 mbedtls_x509_crt *cert,
8214 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008215{
niisato8ee24222018-06-25 19:05:48 +09008216 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008217
niisato8ee24222018-06-25 19:05:48 +09008218 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8219 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008220 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008221
niisato8ee24222018-06-25 19:05:48 +09008222 new_cert->cert = cert;
8223 new_cert->key = key;
8224 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008225
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008226 /* Update head is the list was null, else add to the end */
8227 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008228 {
niisato8ee24222018-06-25 19:05:48 +09008229 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008230 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008231 else
8232 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008233 mbedtls_ssl_key_cert *cur = *head;
8234 while( cur->next != NULL )
8235 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008236 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008237 }
8238
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008239 return( 0 );
8240}
8241
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008242int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008243 mbedtls_x509_crt *own_cert,
8244 mbedtls_pk_context *pk_key )
8245{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008246 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008247}
8248
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008249void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008250 mbedtls_x509_crt *ca_chain,
8251 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008252{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008253 conf->ca_chain = ca_chain;
8254 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008255
8256#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8257 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8258 * cannot be used together. */
8259 conf->f_ca_cb = NULL;
8260 conf->p_ca_cb = NULL;
8261#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008262}
Hanno Becker5adaad92019-03-27 16:54:37 +00008263
8264#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8265void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008266 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008267 void *p_ca_cb )
8268{
8269 conf->f_ca_cb = f_ca_cb;
8270 conf->p_ca_cb = p_ca_cb;
8271
8272 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8273 * cannot be used together. */
8274 conf->ca_chain = NULL;
8275 conf->ca_crl = NULL;
8276}
8277#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008278#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008279
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008280#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8281int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8282 mbedtls_x509_crt *own_cert,
8283 mbedtls_pk_context *pk_key )
8284{
8285 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8286 own_cert, pk_key ) );
8287}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008288
8289void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8290 mbedtls_x509_crt *ca_chain,
8291 mbedtls_x509_crl *ca_crl )
8292{
8293 ssl->handshake->sni_ca_chain = ca_chain;
8294 ssl->handshake->sni_ca_crl = ca_crl;
8295}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008296
8297void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8298 int authmode )
8299{
8300 ssl->handshake->sni_authmode = authmode;
8301}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008302#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8303
Hanno Becker8927c832019-04-03 12:52:50 +01008304#if defined(MBEDTLS_X509_CRT_PARSE_C)
8305void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8306 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8307 void *p_vrfy )
8308{
8309 ssl->f_vrfy = f_vrfy;
8310 ssl->p_vrfy = p_vrfy;
8311}
8312#endif
8313
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008314#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008315/*
8316 * Set EC J-PAKE password for current handshake
8317 */
8318int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8319 const unsigned char *pw,
8320 size_t pw_len )
8321{
8322 mbedtls_ecjpake_role role;
8323
Janos Follath8eb64132016-06-03 15:40:57 +01008324 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008325 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8326
8327 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8328 role = MBEDTLS_ECJPAKE_SERVER;
8329 else
8330 role = MBEDTLS_ECJPAKE_CLIENT;
8331
8332 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8333 role,
8334 MBEDTLS_MD_SHA256,
8335 MBEDTLS_ECP_DP_SECP256R1,
8336 pw, pw_len ) );
8337}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008338#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008340#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008341
8342static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8343{
8344 /* Remove reference to existing PSK, if any. */
8345#if defined(MBEDTLS_USE_PSA_CRYPTO)
8346 if( conf->psk_opaque != 0 )
8347 {
8348 /* The maintenance of the PSK key slot is the
8349 * user's responsibility. */
8350 conf->psk_opaque = 0;
8351 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008352 /* This and the following branch should never
8353 * be taken simultaenously as we maintain the
8354 * invariant that raw and opaque PSKs are never
8355 * configured simultaneously. As a safeguard,
8356 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008357#endif /* MBEDTLS_USE_PSA_CRYPTO */
8358 if( conf->psk != NULL )
8359 {
8360 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8361
8362 mbedtls_free( conf->psk );
8363 conf->psk = NULL;
8364 conf->psk_len = 0;
8365 }
8366
8367 /* Remove reference to PSK identity, if any. */
8368 if( conf->psk_identity != NULL )
8369 {
8370 mbedtls_free( conf->psk_identity );
8371 conf->psk_identity = NULL;
8372 conf->psk_identity_len = 0;
8373 }
8374}
8375
Hanno Becker7390c712018-11-15 13:33:04 +00008376/* This function assumes that PSK identity in the SSL config is unset.
8377 * It checks that the provided identity is well-formed and attempts
8378 * to make a copy of it in the SSL config.
8379 * On failure, the PSK identity in the config remains unset. */
8380static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8381 unsigned char const *psk_identity,
8382 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008383{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008384 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008385 if( psk_identity == NULL ||
8386 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008387 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008388 {
8389 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8390 }
8391
Hanno Becker7390c712018-11-15 13:33:04 +00008392 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8393 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008394 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008395
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008396 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008397 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008398
8399 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008400}
8401
Hanno Becker7390c712018-11-15 13:33:04 +00008402int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8403 const unsigned char *psk, size_t psk_len,
8404 const unsigned char *psk_identity, size_t psk_identity_len )
8405{
8406 int ret;
8407 /* Remove opaque/raw PSK + PSK Identity */
8408 ssl_conf_remove_psk( conf );
8409
8410 /* Check and set raw PSK */
8411 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8412 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8413 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8414 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8415 conf->psk_len = psk_len;
8416 memcpy( conf->psk, psk, conf->psk_len );
8417
8418 /* Check and set PSK Identity */
8419 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8420 if( ret != 0 )
8421 ssl_conf_remove_psk( conf );
8422
8423 return( ret );
8424}
8425
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008426static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8427{
8428#if defined(MBEDTLS_USE_PSA_CRYPTO)
8429 if( ssl->handshake->psk_opaque != 0 )
8430 {
8431 ssl->handshake->psk_opaque = 0;
8432 }
8433 else
8434#endif /* MBEDTLS_USE_PSA_CRYPTO */
8435 if( ssl->handshake->psk != NULL )
8436 {
8437 mbedtls_platform_zeroize( ssl->handshake->psk,
8438 ssl->handshake->psk_len );
8439 mbedtls_free( ssl->handshake->psk );
8440 ssl->handshake->psk_len = 0;
8441 }
8442}
8443
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008444int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8445 const unsigned char *psk, size_t psk_len )
8446{
8447 if( psk == NULL || ssl->handshake == NULL )
8448 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8449
8450 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8451 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8452
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008453 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008454
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008455 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008456 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008457
8458 ssl->handshake->psk_len = psk_len;
8459 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8460
8461 return( 0 );
8462}
8463
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008464#if defined(MBEDTLS_USE_PSA_CRYPTO)
8465int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008466 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008467 const unsigned char *psk_identity,
8468 size_t psk_identity_len )
8469{
Hanno Becker7390c712018-11-15 13:33:04 +00008470 int ret;
8471 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008472 ssl_conf_remove_psk( conf );
8473
Hanno Becker7390c712018-11-15 13:33:04 +00008474 /* Check and set opaque PSK */
8475 if( psk_slot == 0 )
8476 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008477 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008478
8479 /* Check and set PSK Identity */
8480 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8481 psk_identity_len );
8482 if( ret != 0 )
8483 ssl_conf_remove_psk( conf );
8484
8485 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008486}
8487
8488int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008489 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008490{
8491 if( psk_slot == 0 || ssl->handshake == NULL )
8492 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8493
8494 ssl_remove_psk( ssl );
8495 ssl->handshake->psk_opaque = psk_slot;
8496 return( 0 );
8497}
8498#endif /* MBEDTLS_USE_PSA_CRYPTO */
8499
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008500void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008501 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008502 size_t),
8503 void *p_psk )
8504{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008505 conf->f_psk = f_psk;
8506 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008507}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008508#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008509
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008510#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008511
8512#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008513int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008514{
8515 int ret;
8516
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008517 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8518 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8519 {
8520 mbedtls_mpi_free( &conf->dhm_P );
8521 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008522 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008523 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008524
8525 return( 0 );
8526}
Hanno Becker470a8c42017-10-04 15:28:46 +01008527#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008528
Hanno Beckera90658f2017-10-04 15:29:08 +01008529int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8530 const unsigned char *dhm_P, size_t P_len,
8531 const unsigned char *dhm_G, size_t G_len )
8532{
8533 int ret;
8534
8535 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8536 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8537 {
8538 mbedtls_mpi_free( &conf->dhm_P );
8539 mbedtls_mpi_free( &conf->dhm_G );
8540 return( ret );
8541 }
8542
8543 return( 0 );
8544}
Paul Bakker5121ce52009-01-03 21:22:43 +00008545
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008546int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008547{
8548 int ret;
8549
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008550 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8551 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8552 {
8553 mbedtls_mpi_free( &conf->dhm_P );
8554 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008555 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008556 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008557
8558 return( 0 );
8559}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008560#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008561
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008562#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8563/*
8564 * Set the minimum length for Diffie-Hellman parameters
8565 */
8566void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8567 unsigned int bitlen )
8568{
8569 conf->dhm_min_bitlen = bitlen;
8570}
8571#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8572
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008573#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008574/*
8575 * Set allowed/preferred hashes for handshake signatures
8576 */
8577void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8578 const int *hashes )
8579{
8580 conf->sig_hashes = hashes;
8581}
Hanno Becker947194e2017-04-07 13:25:49 +01008582#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008583
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008584#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008585/*
8586 * Set the allowed elliptic curves
8587 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008588void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008589 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008590{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008591 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008592}
Hanno Becker947194e2017-04-07 13:25:49 +01008593#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008594
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008595#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008596int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008597{
Hanno Becker947194e2017-04-07 13:25:49 +01008598 /* Initialize to suppress unnecessary compiler warning */
8599 size_t hostname_len = 0;
8600
8601 /* Check if new hostname is valid before
8602 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008603 if( hostname != NULL )
8604 {
8605 hostname_len = strlen( hostname );
8606
8607 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8609 }
8610
8611 /* Now it's clear that we will overwrite the old hostname,
8612 * so we can free it safely */
8613
8614 if( ssl->hostname != NULL )
8615 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008616 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008617 mbedtls_free( ssl->hostname );
8618 }
8619
8620 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008621
Paul Bakker5121ce52009-01-03 21:22:43 +00008622 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008623 {
8624 ssl->hostname = NULL;
8625 }
8626 else
8627 {
8628 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008629 if( ssl->hostname == NULL )
8630 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008631
Hanno Becker947194e2017-04-07 13:25:49 +01008632 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008633
Hanno Becker947194e2017-04-07 13:25:49 +01008634 ssl->hostname[hostname_len] = '\0';
8635 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008636
8637 return( 0 );
8638}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008639#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008640
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008641#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008642void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008643 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008644 const unsigned char *, size_t),
8645 void *p_sni )
8646{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008647 conf->f_sni = f_sni;
8648 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008650#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008652#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008653int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008654{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008655 size_t cur_len, tot_len;
8656 const char **p;
8657
8658 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008659 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8660 * MUST NOT be truncated."
8661 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008662 */
8663 tot_len = 0;
8664 for( p = protos; *p != NULL; p++ )
8665 {
8666 cur_len = strlen( *p );
8667 tot_len += cur_len;
8668
8669 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008670 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008671 }
8672
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008673 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008674
8675 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008676}
8677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008678const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008679{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008680 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008681}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008682#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008683
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008684void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008685{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008686 conf->max_major_ver = major;
8687 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008688}
8689
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008690void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008691{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008692 conf->min_major_ver = major;
8693 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008694}
8695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008696#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008697void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008698{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008699 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008700}
8701#endif
8702
Janos Follath088ce432017-04-10 12:42:31 +01008703#if defined(MBEDTLS_SSL_SRV_C)
8704void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8705 char cert_req_ca_list )
8706{
8707 conf->cert_req_ca_list = cert_req_ca_list;
8708}
8709#endif
8710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008711#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008712void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008713{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008714 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008715}
8716#endif
8717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008718#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008719void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008720{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008721 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008722}
8723#endif
8724
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008725#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008726void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008727{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008728 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008729}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008730#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008732#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008733int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008734{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008735 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008736 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008738 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008739 }
8740
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008741 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008742
8743 return( 0 );
8744}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008745#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008747#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008748void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008749{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008750 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008751}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008752#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008754#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008755void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008756{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008757 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008758}
8759#endif
8760
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008761void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008762{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008763 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008764}
8765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008766#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008767void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008768{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008769 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008770}
8771
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008772void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008773{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008774 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008775}
8776
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008777void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008778 const unsigned char period[8] )
8779{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008780 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008781}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008782#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008784#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008785#if defined(MBEDTLS_SSL_CLI_C)
8786void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008787{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008788 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008789}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008790#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008791
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008792#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008793void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8794 mbedtls_ssl_ticket_write_t *f_ticket_write,
8795 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8796 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008797{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008798 conf->f_ticket_write = f_ticket_write;
8799 conf->f_ticket_parse = f_ticket_parse;
8800 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008801}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008802#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008803#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008804
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008805#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8806void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8807 mbedtls_ssl_export_keys_t *f_export_keys,
8808 void *p_export_keys )
8809{
8810 conf->f_export_keys = f_export_keys;
8811 conf->p_export_keys = p_export_keys;
8812}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03008813
8814void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
8815 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
8816 void *p_export_keys )
8817{
8818 conf->f_export_keys_ext = f_export_keys_ext;
8819 conf->p_export_keys = p_export_keys;
8820}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008821#endif
8822
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008823#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008824void mbedtls_ssl_conf_async_private_cb(
8825 mbedtls_ssl_config *conf,
8826 mbedtls_ssl_async_sign_t *f_async_sign,
8827 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8828 mbedtls_ssl_async_resume_t *f_async_resume,
8829 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008830 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008831{
8832 conf->f_async_sign_start = f_async_sign;
8833 conf->f_async_decrypt_start = f_async_decrypt;
8834 conf->f_async_resume = f_async_resume;
8835 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008836 conf->p_async_config_data = async_config_data;
8837}
8838
Gilles Peskine8f97af72018-04-26 11:46:10 +02008839void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8840{
8841 return( conf->p_async_config_data );
8842}
8843
Gilles Peskine1febfef2018-04-30 11:54:39 +02008844void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008845{
8846 if( ssl->handshake == NULL )
8847 return( NULL );
8848 else
8849 return( ssl->handshake->user_async_ctx );
8850}
8851
Gilles Peskine1febfef2018-04-30 11:54:39 +02008852void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008853 void *ctx )
8854{
8855 if( ssl->handshake != NULL )
8856 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008857}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008858#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008859
Paul Bakker5121ce52009-01-03 21:22:43 +00008860/*
8861 * SSL get accessors
8862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008863size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008864{
8865 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8866}
8867
Hanno Becker8b170a02017-10-10 11:51:19 +01008868int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8869{
8870 /*
8871 * Case A: We're currently holding back
8872 * a message for further processing.
8873 */
8874
8875 if( ssl->keep_current_message == 1 )
8876 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008877 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008878 return( 1 );
8879 }
8880
8881 /*
8882 * Case B: Further records are pending in the current datagram.
8883 */
8884
8885#if defined(MBEDTLS_SSL_PROTO_DTLS)
8886 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8887 ssl->in_left > ssl->next_record_offset )
8888 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008889 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008890 return( 1 );
8891 }
8892#endif /* MBEDTLS_SSL_PROTO_DTLS */
8893
8894 /*
8895 * Case C: A handshake message is being processed.
8896 */
8897
Hanno Becker8b170a02017-10-10 11:51:19 +01008898 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8899 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008900 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008901 return( 1 );
8902 }
8903
8904 /*
8905 * Case D: An application data message is being processed
8906 */
8907 if( ssl->in_offt != NULL )
8908 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008909 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008910 return( 1 );
8911 }
8912
8913 /*
8914 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008915 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008916 * we implement support for multiple alerts in single records.
8917 */
8918
8919 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8920 return( 0 );
8921}
8922
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008923uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008924{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008925 if( ssl->session != NULL )
8926 return( ssl->session->verify_result );
8927
8928 if( ssl->session_negotiate != NULL )
8929 return( ssl->session_negotiate->verify_result );
8930
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008931 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008932}
8933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008934const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008935{
Paul Bakker926c8e42013-03-06 10:23:34 +01008936 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008937 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008939 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008940}
8941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008942const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008943{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008944#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008945 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008946 {
8947 switch( ssl->minor_ver )
8948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008949 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008950 return( "DTLSv1.0" );
8951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008952 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008953 return( "DTLSv1.2" );
8954
8955 default:
8956 return( "unknown (DTLS)" );
8957 }
8958 }
8959#endif
8960
Paul Bakker43ca69c2011-01-15 17:35:19 +00008961 switch( ssl->minor_ver )
8962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008963 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008964 return( "SSLv3.0" );
8965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008966 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008967 return( "TLSv1.0" );
8968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008969 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008970 return( "TLSv1.1" );
8971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008972 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008973 return( "TLSv1.2" );
8974
Paul Bakker43ca69c2011-01-15 17:35:19 +00008975 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008976 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008977 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008978}
8979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008980int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008981{
Hanno Becker3136ede2018-08-17 15:28:19 +01008982 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008983 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008984 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008985
Hanno Becker78640902018-08-13 16:35:15 +01008986 if( transform == NULL )
8987 return( (int) mbedtls_ssl_hdr_len( ssl ) );
8988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008989#if defined(MBEDTLS_ZLIB_SUPPORT)
8990 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8991 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008992#endif
8993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008994 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008996 case MBEDTLS_MODE_GCM:
8997 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008998 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008999 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009000 transform_expansion = transform->minlen;
9001 break;
9002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009003 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009004
9005 block_size = mbedtls_cipher_get_block_size(
9006 &transform->cipher_ctx_enc );
9007
Hanno Becker3136ede2018-08-17 15:28:19 +01009008 /* Expansion due to the addition of the MAC. */
9009 transform_expansion += transform->maclen;
9010
9011 /* Expansion due to the addition of CBC padding;
9012 * Theoretically up to 256 bytes, but we never use
9013 * more than the block size of the underlying cipher. */
9014 transform_expansion += block_size;
9015
9016 /* For TLS 1.1 or higher, an explicit IV is added
9017 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009018#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9019 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009020 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009021#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009022
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009023 break;
9024
9025 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009027 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009028 }
9029
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02009030 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009031}
9032
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009033#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9034size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9035{
9036 size_t max_len;
9037
9038 /*
9039 * Assume mfl_code is correct since it was checked when set
9040 */
Angus Grattond8213d02016-05-25 20:56:48 +10009041 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009042
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009043 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009044 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009045 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009046 {
Angus Grattond8213d02016-05-25 20:56:48 +10009047 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009048 }
9049
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009050 /* During a handshake, use the value being negotiated */
9051 if( ssl->session_negotiate != NULL &&
9052 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9053 {
9054 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9055 }
9056
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009057 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009058}
9059#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9060
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009061#if defined(MBEDTLS_SSL_PROTO_DTLS)
9062static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9063{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009064 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9065 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9066 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9067 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9068 return ( 0 );
9069
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009070 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9071 return( ssl->mtu );
9072
9073 if( ssl->mtu == 0 )
9074 return( ssl->handshake->mtu );
9075
9076 return( ssl->mtu < ssl->handshake->mtu ?
9077 ssl->mtu : ssl->handshake->mtu );
9078}
9079#endif /* MBEDTLS_SSL_PROTO_DTLS */
9080
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009081int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9082{
9083 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9084
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009085#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9086 !defined(MBEDTLS_SSL_PROTO_DTLS)
9087 (void) ssl;
9088#endif
9089
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009090#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9091 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9092
9093 if( max_len > mfl )
9094 max_len = mfl;
9095#endif
9096
9097#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009098 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009099 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009100 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009101 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9102 const size_t overhead = (size_t) ret;
9103
9104 if( ret < 0 )
9105 return( ret );
9106
9107 if( mtu <= overhead )
9108 {
9109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9110 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9111 }
9112
9113 if( max_len > mtu - overhead )
9114 max_len = mtu - overhead;
9115 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009116#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009117
Hanno Becker0defedb2018-08-10 12:35:02 +01009118#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9119 !defined(MBEDTLS_SSL_PROTO_DTLS)
9120 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009121#endif
9122
9123 return( (int) max_len );
9124}
9125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009126#if defined(MBEDTLS_X509_CRT_PARSE_C)
9127const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009128{
9129 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009130 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009131
Hanno Beckere6824572019-02-07 13:18:46 +00009132#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009133 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009134#else
9135 return( NULL );
9136#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009137}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009138#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009140#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009141int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9142 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009143{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009144 if( ssl == NULL ||
9145 dst == NULL ||
9146 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009147 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009149 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009150 }
9151
Hanno Becker52055ae2019-02-06 14:30:46 +00009152 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009153}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009154#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009155
Paul Bakker5121ce52009-01-03 21:22:43 +00009156/*
Paul Bakker1961b702013-01-25 14:49:24 +01009157 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009159int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009160{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009161 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009162
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009163 if( ssl == NULL || ssl->conf == NULL )
9164 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009166#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009167 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009168 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009169#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009170#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009171 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009172 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009173#endif
9174
Paul Bakker1961b702013-01-25 14:49:24 +01009175 return( ret );
9176}
9177
9178/*
9179 * Perform the SSL handshake
9180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009181int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009182{
9183 int ret = 0;
9184
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009185 if( ssl == NULL || ssl->conf == NULL )
9186 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009190 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009192 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009193
9194 if( ret != 0 )
9195 break;
9196 }
9197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009199
9200 return( ret );
9201}
9202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009203#if defined(MBEDTLS_SSL_RENEGOTIATION)
9204#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009205/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009206 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009208static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009209{
9210 int ret;
9211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009213
9214 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009215 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9216 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009217
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009218 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009219 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009221 return( ret );
9222 }
9223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009225
9226 return( 0 );
9227}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009228#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009229
9230/*
9231 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009232 * - any side: calling mbedtls_ssl_renegotiate(),
9233 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9234 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009235 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009236 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009237 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009239static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009240{
9241 int ret;
9242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009244
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009245 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9246 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009247
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009248 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9249 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009250#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009251 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009252 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009253 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009254 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009255 ssl->handshake->out_msg_seq = 1;
9256 else
9257 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009258 }
9259#endif
9260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009261 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9262 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009264 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009267 return( ret );
9268 }
9269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009271
9272 return( 0 );
9273}
9274
9275/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009276 * Renegotiate current connection on client,
9277 * or request renegotiation on server
9278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009279int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009280{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009281 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009282
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009283 if( ssl == NULL || ssl->conf == NULL )
9284 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009286#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009287 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009288 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009290 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009293 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009294
9295 /* Did we already try/start sending HelloRequest? */
9296 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009297 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009298
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009299 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009300 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009301#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009304 /*
9305 * On client, either start the renegotiation process or,
9306 * if already in progress, continue the handshake
9307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9311 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009312
9313 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009316 return( ret );
9317 }
9318 }
9319 else
9320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009321 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009324 return( ret );
9325 }
9326 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009327#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009328
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009329 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009330}
9331
9332/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009333 * Check record counters and renegotiate if they're above the limit.
9334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009335static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009336{
Andres AG2196c7f2016-12-15 17:01:16 +00009337 size_t ep_len = ssl_ep_len( ssl );
9338 int in_ctr_cmp;
9339 int out_ctr_cmp;
9340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9342 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009343 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009344 {
9345 return( 0 );
9346 }
9347
Andres AG2196c7f2016-12-15 17:01:16 +00009348 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9349 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009350 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009351 ssl->conf->renego_period + ep_len, 8 - ep_len );
9352
9353 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009354 {
9355 return( 0 );
9356 }
9357
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009360}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009361#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009362
9363/*
9364 * Receive application data decrypted from the SSL layer
9365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009366int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009367{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009368 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009369 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009370
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009371 if( ssl == NULL || ssl->conf == NULL )
9372 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009376#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009377 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009379 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009380 return( ret );
9381
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009382 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009383 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009384 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009385 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009386 return( ret );
9387 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009388 }
9389#endif
9390
Hanno Becker4a810fb2017-05-24 16:27:30 +01009391 /*
9392 * Check if renegotiation is necessary and/or handshake is
9393 * in process. If yes, perform/continue, and fall through
9394 * if an unexpected packet is received while the client
9395 * is waiting for the ServerHello.
9396 *
9397 * (There is no equivalent to the last condition on
9398 * the server-side as it is not treated as within
9399 * a handshake while waiting for the ClientHello
9400 * after a renegotiation request.)
9401 */
9402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009403#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009404 ret = ssl_check_ctr_renegotiate( ssl );
9405 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9406 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009408 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009409 return( ret );
9410 }
9411#endif
9412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009413 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009415 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009416 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9417 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009420 return( ret );
9421 }
9422 }
9423
Hanno Beckere41158b2017-10-23 13:30:32 +01009424 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009425 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009426 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009427 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009428 if( ssl->f_get_timer != NULL &&
9429 ssl->f_get_timer( ssl->p_timer ) == -1 )
9430 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009431 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009432 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009433
Hanno Becker327c93b2018-08-15 13:56:18 +01009434 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009435 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009436 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9437 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009438
Hanno Becker4a810fb2017-05-24 16:27:30 +01009439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9440 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009441 }
9442
9443 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009444 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009445 {
9446 /*
9447 * OpenSSL sends empty messages to randomize the IV
9448 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009449 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009451 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009452 return( 0 );
9453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009455 return( ret );
9456 }
9457 }
9458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009459 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009462
Hanno Becker4a810fb2017-05-24 16:27:30 +01009463 /*
9464 * - For client-side, expect SERVER_HELLO_REQUEST.
9465 * - For server-side, expect CLIENT_HELLO.
9466 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9467 */
9468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009469#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009470 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009471 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009472 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009475
9476 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009478 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009479 {
9480 continue;
9481 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009482#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009483 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009484 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009485#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009486
Hanno Becker4a810fb2017-05-24 16:27:30 +01009487#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009488 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009489 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009492
9493 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009494#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009495 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009496 {
9497 continue;
9498 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009499#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009500 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009501 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009502#endif /* MBEDTLS_SSL_SRV_C */
9503
Hanno Becker21df7f92017-10-17 11:03:26 +01009504#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009505 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009506 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9507 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9508 ssl->conf->allow_legacy_renegotiation ==
9509 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9510 {
9511 /*
9512 * Accept renegotiation request
9513 */
Paul Bakker48916f92012-09-16 19:57:18 +00009514
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009515 /* DTLS clients need to know renego is server-initiated */
9516#if defined(MBEDTLS_SSL_PROTO_DTLS)
9517 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9518 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9519 {
9520 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9521 }
9522#endif
9523 ret = ssl_start_renegotiation( ssl );
9524 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9525 ret != 0 )
9526 {
9527 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9528 return( ret );
9529 }
9530 }
9531 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009532#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009533 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009534 /*
9535 * Refuse renegotiation
9536 */
9537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009538 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009540#if defined(MBEDTLS_SSL_PROTO_SSL3)
9541 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009542 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009543 /* SSLv3 does not have a "no_renegotiation" warning, so
9544 we send a fatal alert and abort the connection. */
9545 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9546 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9547 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009548 }
9549 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009550#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9551#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9552 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9553 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009555 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9556 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9557 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009558 {
9559 return( ret );
9560 }
Paul Bakker48916f92012-09-16 19:57:18 +00009561 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009562 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9564 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9567 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009568 }
Paul Bakker48916f92012-09-16 19:57:18 +00009569 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009570
Hanno Becker90333da2017-10-10 11:27:13 +01009571 /* At this point, we don't know whether the renegotiation has been
9572 * completed or not. The cases to consider are the following:
9573 * 1) The renegotiation is complete. In this case, no new record
9574 * has been read yet.
9575 * 2) The renegotiation is incomplete because the client received
9576 * an application data record while awaiting the ServerHello.
9577 * 3) The renegotiation is incomplete because the client received
9578 * a non-handshake, non-application data message while awaiting
9579 * the ServerHello.
9580 * In each of these case, looping will be the proper action:
9581 * - For 1), the next iteration will read a new record and check
9582 * if it's application data.
9583 * - For 2), the loop condition isn't satisfied as application data
9584 * is present, hence continue is the same as break
9585 * - For 3), the loop condition is satisfied and read_record
9586 * will re-deliver the message that was held back by the client
9587 * when expecting the ServerHello.
9588 */
9589 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009590 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009591#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009592 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009593 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009594 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009595 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009596 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009599 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009600 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009601 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009602 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009603 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009606 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9607 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009610 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009611 }
9612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009613 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9616 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009617 }
9618
9619 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009620
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009621 /* We're going to return something now, cancel timer,
9622 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009624 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009625
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009626#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009627 /* If we requested renego but received AppData, resend HelloRequest.
9628 * Do it now, after setting in_offt, to avoid taking this branch
9629 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009631 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009632 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009633 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009634 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009636 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009637 return( ret );
9638 }
9639 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009640#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009641#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009642 }
9643
9644 n = ( len < ssl->in_msglen )
9645 ? len : ssl->in_msglen;
9646
9647 memcpy( buf, ssl->in_offt, n );
9648 ssl->in_msglen -= n;
9649
9650 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009651 {
9652 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009653 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009654 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009655 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009656 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009657 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009658 /* more data available */
9659 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009660 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009663
Paul Bakker23986e52011-04-24 08:57:21 +00009664 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009665}
9666
9667/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009668 * Send application data to be encrypted by the SSL layer, taking care of max
9669 * fragment length and buffer size.
9670 *
9671 * According to RFC 5246 Section 6.2.1:
9672 *
9673 * Zero-length fragments of Application data MAY be sent as they are
9674 * potentially useful as a traffic analysis countermeasure.
9675 *
9676 * Therefore, it is possible that the input message length is 0 and the
9677 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009678 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009679static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009680 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009681{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009682 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9683 const size_t max_len = (size_t) ret;
9684
9685 if( ret < 0 )
9686 {
9687 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9688 return( ret );
9689 }
9690
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009691 if( len > max_len )
9692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009693#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009694 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009697 "maximum fragment length: %d > %d",
9698 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009699 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009700 }
9701 else
9702#endif
9703 len = max_len;
9704 }
Paul Bakker887bd502011-06-08 13:10:54 +00009705
Paul Bakker5121ce52009-01-03 21:22:43 +00009706 if( ssl->out_left != 0 )
9707 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009708 /*
9709 * The user has previously tried to send the data and
9710 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9711 * written. In this case, we expect the high-level write function
9712 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009714 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009716 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009717 return( ret );
9718 }
9719 }
Paul Bakker887bd502011-06-08 13:10:54 +00009720 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009721 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009722 /*
9723 * The user is trying to send a message the first time, so we need to
9724 * copy the data into the internal buffers and setup the data structure
9725 * to keep track of partial writes
9726 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009727 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009728 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009729 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009730
Hanno Becker67bc7c32018-08-06 11:33:50 +01009731 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009733 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009734 return( ret );
9735 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009736 }
9737
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009738 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009739}
9740
9741/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009742 * Write application data, doing 1/n-1 splitting if necessary.
9743 *
9744 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009745 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009746 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009748#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009749static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009750 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009751{
9752 int ret;
9753
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009754 if( ssl->conf->cbc_record_splitting ==
9755 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009756 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009757 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9758 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9759 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009760 {
9761 return( ssl_write_real( ssl, buf, len ) );
9762 }
9763
9764 if( ssl->split_done == 0 )
9765 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009766 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009767 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009768 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009769 }
9770
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009771 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9772 return( ret );
9773 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009774
9775 return( ret + 1 );
9776}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009777#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009778
9779/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009780 * Write application data (public-facing wrapper)
9781 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009782int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009783{
9784 int ret;
9785
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009787
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009788 if( ssl == NULL || ssl->conf == NULL )
9789 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9790
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009791#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009792 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9793 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009794 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009795 return( ret );
9796 }
9797#endif
9798
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009799 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009800 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009801 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009802 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009803 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009804 return( ret );
9805 }
9806 }
9807
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009808#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009809 ret = ssl_write_split( ssl, buf, len );
9810#else
9811 ret = ssl_write_real( ssl, buf, len );
9812#endif
9813
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009815
9816 return( ret );
9817}
9818
9819/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009820 * Notify the peer that the connection is being closed
9821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009823{
9824 int ret;
9825
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009826 if( ssl == NULL || ssl->conf == NULL )
9827 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009830
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009831 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009832 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009834 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009836 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9837 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9838 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009841 return( ret );
9842 }
9843 }
9844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009846
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009847 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009848}
9849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009851{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009852 if( transform == NULL )
9853 return;
9854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009856 deflateEnd( &transform->ctx_deflate );
9857 inflateEnd( &transform->ctx_inflate );
9858#endif
9859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009860 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9861 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009862
Hanno Beckerd56ed242018-01-03 15:32:51 +00009863#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009864 mbedtls_md_free( &transform->md_ctx_enc );
9865 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00009866#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009867
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009868 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009869}
9870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009871#if defined(MBEDTLS_X509_CRT_PARSE_C)
9872static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009873{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009874 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009875
9876 while( cur != NULL )
9877 {
9878 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009879 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009880 cur = next;
9881 }
9882}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009884
Hanno Becker0271f962018-08-16 13:23:47 +01009885#if defined(MBEDTLS_SSL_PROTO_DTLS)
9886
9887static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9888{
9889 unsigned offset;
9890 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9891
9892 if( hs == NULL )
9893 return;
9894
Hanno Becker283f5ef2018-08-24 09:34:47 +01009895 ssl_free_buffered_record( ssl );
9896
Hanno Becker0271f962018-08-16 13:23:47 +01009897 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009898 ssl_buffering_free_slot( ssl, offset );
9899}
9900
9901static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9902 uint8_t slot )
9903{
9904 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9905 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009906
9907 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9908 return;
9909
Hanno Beckere605b192018-08-21 15:59:07 +01009910 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009911 {
Hanno Beckere605b192018-08-21 15:59:07 +01009912 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009913 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009914 mbedtls_free( hs_buf->data );
9915 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009916 }
9917}
9918
9919#endif /* MBEDTLS_SSL_PROTO_DTLS */
9920
Gilles Peskine9b562d52018-04-25 20:32:43 +02009921void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009922{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009923 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9924
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009925 if( handshake == NULL )
9926 return;
9927
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009928#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9929 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9930 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009931 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009932 handshake->async_in_progress = 0;
9933 }
9934#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9935
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009936#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9937 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9938 mbedtls_md5_free( &handshake->fin_md5 );
9939 mbedtls_sha1_free( &handshake->fin_sha1 );
9940#endif
9941#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9942#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009943#if defined(MBEDTLS_USE_PSA_CRYPTO)
9944 psa_hash_abort( &handshake->fin_sha256_psa );
9945#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009946 mbedtls_sha256_free( &handshake->fin_sha256 );
9947#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009948#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009949#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009950#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009951 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009952#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009953 mbedtls_sha512_free( &handshake->fin_sha512 );
9954#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009955#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009956#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009958#if defined(MBEDTLS_DHM_C)
9959 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009960#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009961#if defined(MBEDTLS_ECDH_C)
9962 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009963#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009964#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009965 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009966#if defined(MBEDTLS_SSL_CLI_C)
9967 mbedtls_free( handshake->ecjpake_cache );
9968 handshake->ecjpake_cache = NULL;
9969 handshake->ecjpake_cache_len = 0;
9970#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009971#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009972
Janos Follath4ae5c292016-02-10 11:27:43 +00009973#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9974 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009975 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009976 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009977#endif
9978
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009979#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9980 if( handshake->psk != NULL )
9981 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009982 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009983 mbedtls_free( handshake->psk );
9984 }
9985#endif
9986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009987#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9988 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009989 /*
9990 * Free only the linked list wrapper, not the keys themselves
9991 * since the belong to the SNI callback
9992 */
9993 if( handshake->sni_key_cert != NULL )
9994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009995 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02009996
9997 while( cur != NULL )
9998 {
9999 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010000 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010001 cur = next;
10002 }
10003 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010004#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010005
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010006#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010007 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010008 if( handshake->ecrs_peer_cert != NULL )
10009 {
10010 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10011 mbedtls_free( handshake->ecrs_peer_cert );
10012 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010013#endif
10014
Hanno Becker75173122019-02-06 16:18:31 +000010015#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10016 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10017 mbedtls_pk_free( &handshake->peer_pubkey );
10018#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010020#if defined(MBEDTLS_SSL_PROTO_DTLS)
10021 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010022 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010023 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010024#endif
10025
Hanno Becker4a63ed42019-01-08 11:39:35 +000010026#if defined(MBEDTLS_ECDH_C) && \
10027 defined(MBEDTLS_USE_PSA_CRYPTO)
10028 psa_destroy_key( handshake->ecdh_psa_privkey );
10029#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10030
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010031 mbedtls_platform_zeroize( handshake,
10032 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010033}
10034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010035void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010036{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010037 if( session == NULL )
10038 return;
10039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010040#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010041 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010042#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010043
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010044#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010045 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010046#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010047
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010048 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010049}
10050
Paul Bakker5121ce52009-01-03 21:22:43 +000010051/*
10052 * Free an SSL context
10053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010054void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010055{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010056 if( ssl == NULL )
10057 return;
10058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010060
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010061 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010062 {
Angus Grattond8213d02016-05-25 20:56:48 +100010063 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010064 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010065 }
10066
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010067 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010068 {
Angus Grattond8213d02016-05-25 20:56:48 +100010069 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010070 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010071 }
10072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010073#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010074 if( ssl->compress_buf != NULL )
10075 {
Angus Grattond8213d02016-05-25 20:56:48 +100010076 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010077 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010078 }
10079#endif
10080
Paul Bakker48916f92012-09-16 19:57:18 +000010081 if( ssl->transform )
10082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010083 mbedtls_ssl_transform_free( ssl->transform );
10084 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010085 }
10086
10087 if( ssl->handshake )
10088 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010089 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010090 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10091 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010093 mbedtls_free( ssl->handshake );
10094 mbedtls_free( ssl->transform_negotiate );
10095 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010096 }
10097
Paul Bakkerc0463502013-02-14 11:19:38 +010010098 if( ssl->session )
10099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010100 mbedtls_ssl_session_free( ssl->session );
10101 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010102 }
10103
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010104#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010105 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010106 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010107 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010108 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010109 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010110#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010112#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10113 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10116 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010117 }
10118#endif
10119
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010120#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010121 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010122#endif
10123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010125
Paul Bakker86f04f42013-02-14 11:20:09 +010010126 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010127 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010128}
10129
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010130/*
10131 * Initialze mbedtls_ssl_config
10132 */
10133void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10134{
10135 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10136}
10137
Simon Butcherc97b6972015-12-27 23:48:17 +000010138#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010139static int ssl_preset_default_hashes[] = {
10140#if defined(MBEDTLS_SHA512_C)
10141 MBEDTLS_MD_SHA512,
10142 MBEDTLS_MD_SHA384,
10143#endif
10144#if defined(MBEDTLS_SHA256_C)
10145 MBEDTLS_MD_SHA256,
10146 MBEDTLS_MD_SHA224,
10147#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010148#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010149 MBEDTLS_MD_SHA1,
10150#endif
10151 MBEDTLS_MD_NONE
10152};
Simon Butcherc97b6972015-12-27 23:48:17 +000010153#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010154
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010155static int ssl_preset_suiteb_ciphersuites[] = {
10156 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10157 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10158 0
10159};
10160
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010161#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010162static int ssl_preset_suiteb_hashes[] = {
10163 MBEDTLS_MD_SHA256,
10164 MBEDTLS_MD_SHA384,
10165 MBEDTLS_MD_NONE
10166};
10167#endif
10168
10169#if defined(MBEDTLS_ECP_C)
10170static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10171 MBEDTLS_ECP_DP_SECP256R1,
10172 MBEDTLS_ECP_DP_SECP384R1,
10173 MBEDTLS_ECP_DP_NONE
10174};
10175#endif
10176
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010177/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010178 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010179 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010180int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010181 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010182{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010183#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010184 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010185#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010186
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010187 /* Use the functions here so that they are covered in tests,
10188 * but otherwise access member directly for efficiency */
10189 mbedtls_ssl_conf_endpoint( conf, endpoint );
10190 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010191
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010192 /*
10193 * Things that are common to all presets
10194 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010195#if defined(MBEDTLS_SSL_CLI_C)
10196 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10197 {
10198 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10199#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10200 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10201#endif
10202 }
10203#endif
10204
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010205#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010206 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010207#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010208
10209#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10210 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10211#endif
10212
10213#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10214 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10215#endif
10216
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010217#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10218 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10219#endif
10220
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010221#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010222 conf->f_cookie_write = ssl_cookie_write_dummy;
10223 conf->f_cookie_check = ssl_cookie_check_dummy;
10224#endif
10225
10226#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10227 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10228#endif
10229
Janos Follath088ce432017-04-10 12:42:31 +010010230#if defined(MBEDTLS_SSL_SRV_C)
10231 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10232#endif
10233
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010234#if defined(MBEDTLS_SSL_PROTO_DTLS)
10235 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10236 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10237#endif
10238
10239#if defined(MBEDTLS_SSL_RENEGOTIATION)
10240 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010241 memset( conf->renego_period, 0x00, 2 );
10242 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010243#endif
10244
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010245#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10246 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10247 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010248 const unsigned char dhm_p[] =
10249 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10250 const unsigned char dhm_g[] =
10251 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10252
Hanno Beckera90658f2017-10-04 15:29:08 +010010253 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10254 dhm_p, sizeof( dhm_p ),
10255 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010256 {
10257 return( ret );
10258 }
10259 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010260#endif
10261
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010262 /*
10263 * Preset-specific defaults
10264 */
10265 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010266 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010267 /*
10268 * NSA Suite B
10269 */
10270 case MBEDTLS_SSL_PRESET_SUITEB:
10271 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10272 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10273 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10274 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10275
10276 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10277 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10278 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10279 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10280 ssl_preset_suiteb_ciphersuites;
10281
10282#if defined(MBEDTLS_X509_CRT_PARSE_C)
10283 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010284#endif
10285
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010286#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010287 conf->sig_hashes = ssl_preset_suiteb_hashes;
10288#endif
10289
10290#if defined(MBEDTLS_ECP_C)
10291 conf->curve_list = ssl_preset_suiteb_curves;
10292#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010293 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010294
10295 /*
10296 * Default
10297 */
10298 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010299 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10300 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10301 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10302 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10303 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10304 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10305 MBEDTLS_SSL_MIN_MINOR_VERSION :
10306 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010307 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10308 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10309
10310#if defined(MBEDTLS_SSL_PROTO_DTLS)
10311 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10312 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10313#endif
10314
10315 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10316 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10317 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10318 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10319 mbedtls_ssl_list_ciphersuites();
10320
10321#if defined(MBEDTLS_X509_CRT_PARSE_C)
10322 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10323#endif
10324
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010325#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010326 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010327#endif
10328
10329#if defined(MBEDTLS_ECP_C)
10330 conf->curve_list = mbedtls_ecp_grp_id_list();
10331#endif
10332
10333#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10334 conf->dhm_min_bitlen = 1024;
10335#endif
10336 }
10337
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010338 return( 0 );
10339}
10340
10341/*
10342 * Free mbedtls_ssl_config
10343 */
10344void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10345{
10346#if defined(MBEDTLS_DHM_C)
10347 mbedtls_mpi_free( &conf->dhm_P );
10348 mbedtls_mpi_free( &conf->dhm_G );
10349#endif
10350
10351#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10352 if( conf->psk != NULL )
10353 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010354 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010355 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010356 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010357 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010358 }
10359
10360 if( conf->psk_identity != NULL )
10361 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010362 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010363 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010364 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010365 conf->psk_identity_len = 0;
10366 }
10367#endif
10368
10369#if defined(MBEDTLS_X509_CRT_PARSE_C)
10370 ssl_key_cert_free( conf->key_cert );
10371#endif
10372
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010373 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010374}
10375
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010376#if defined(MBEDTLS_PK_C) && \
10377 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010378/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010379 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010380 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010381unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010382{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010383#if defined(MBEDTLS_RSA_C)
10384 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10385 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010386#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010387#if defined(MBEDTLS_ECDSA_C)
10388 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10389 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010390#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010391 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010392}
10393
Hanno Becker7e5437a2017-04-28 17:15:26 +010010394unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10395{
10396 switch( type ) {
10397 case MBEDTLS_PK_RSA:
10398 return( MBEDTLS_SSL_SIG_RSA );
10399 case MBEDTLS_PK_ECDSA:
10400 case MBEDTLS_PK_ECKEY:
10401 return( MBEDTLS_SSL_SIG_ECDSA );
10402 default:
10403 return( MBEDTLS_SSL_SIG_ANON );
10404 }
10405}
10406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010407mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010408{
10409 switch( sig )
10410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010411#if defined(MBEDTLS_RSA_C)
10412 case MBEDTLS_SSL_SIG_RSA:
10413 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010414#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010415#if defined(MBEDTLS_ECDSA_C)
10416 case MBEDTLS_SSL_SIG_ECDSA:
10417 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010418#endif
10419 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010420 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010421 }
10422}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010423#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010424
Hanno Becker7e5437a2017-04-28 17:15:26 +010010425#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10426 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10427
10428/* Find an entry in a signature-hash set matching a given hash algorithm. */
10429mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10430 mbedtls_pk_type_t sig_alg )
10431{
10432 switch( sig_alg )
10433 {
10434 case MBEDTLS_PK_RSA:
10435 return( set->rsa );
10436 case MBEDTLS_PK_ECDSA:
10437 return( set->ecdsa );
10438 default:
10439 return( MBEDTLS_MD_NONE );
10440 }
10441}
10442
10443/* Add a signature-hash-pair to a signature-hash set */
10444void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10445 mbedtls_pk_type_t sig_alg,
10446 mbedtls_md_type_t md_alg )
10447{
10448 switch( sig_alg )
10449 {
10450 case MBEDTLS_PK_RSA:
10451 if( set->rsa == MBEDTLS_MD_NONE )
10452 set->rsa = md_alg;
10453 break;
10454
10455 case MBEDTLS_PK_ECDSA:
10456 if( set->ecdsa == MBEDTLS_MD_NONE )
10457 set->ecdsa = md_alg;
10458 break;
10459
10460 default:
10461 break;
10462 }
10463}
10464
10465/* Allow exactly one hash algorithm for each signature. */
10466void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10467 mbedtls_md_type_t md_alg )
10468{
10469 set->rsa = md_alg;
10470 set->ecdsa = md_alg;
10471}
10472
10473#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10474 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10475
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010476/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010477 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010479mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010480{
10481 switch( hash )
10482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010483#if defined(MBEDTLS_MD5_C)
10484 case MBEDTLS_SSL_HASH_MD5:
10485 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010486#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010487#if defined(MBEDTLS_SHA1_C)
10488 case MBEDTLS_SSL_HASH_SHA1:
10489 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010490#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010491#if defined(MBEDTLS_SHA256_C)
10492 case MBEDTLS_SSL_HASH_SHA224:
10493 return( MBEDTLS_MD_SHA224 );
10494 case MBEDTLS_SSL_HASH_SHA256:
10495 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010496#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010497#if defined(MBEDTLS_SHA512_C)
10498 case MBEDTLS_SSL_HASH_SHA384:
10499 return( MBEDTLS_MD_SHA384 );
10500 case MBEDTLS_SSL_HASH_SHA512:
10501 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010502#endif
10503 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010504 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010505 }
10506}
10507
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010508/*
10509 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10510 */
10511unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10512{
10513 switch( md )
10514 {
10515#if defined(MBEDTLS_MD5_C)
10516 case MBEDTLS_MD_MD5:
10517 return( MBEDTLS_SSL_HASH_MD5 );
10518#endif
10519#if defined(MBEDTLS_SHA1_C)
10520 case MBEDTLS_MD_SHA1:
10521 return( MBEDTLS_SSL_HASH_SHA1 );
10522#endif
10523#if defined(MBEDTLS_SHA256_C)
10524 case MBEDTLS_MD_SHA224:
10525 return( MBEDTLS_SSL_HASH_SHA224 );
10526 case MBEDTLS_MD_SHA256:
10527 return( MBEDTLS_SSL_HASH_SHA256 );
10528#endif
10529#if defined(MBEDTLS_SHA512_C)
10530 case MBEDTLS_MD_SHA384:
10531 return( MBEDTLS_SSL_HASH_SHA384 );
10532 case MBEDTLS_MD_SHA512:
10533 return( MBEDTLS_SSL_HASH_SHA512 );
10534#endif
10535 default:
10536 return( MBEDTLS_SSL_HASH_NONE );
10537 }
10538}
10539
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010540#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010541/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010542 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010543 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010544 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010545int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010546{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010547 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010548
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010549 if( ssl->conf->curve_list == NULL )
10550 return( -1 );
10551
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010552 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010553 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010554 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010555
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010556 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010557}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010558#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010559
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010560#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010561/*
10562 * Check if a hash proposed by the peer is in our list.
10563 * Return 0 if we're willing to use it, -1 otherwise.
10564 */
10565int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10566 mbedtls_md_type_t md )
10567{
10568 const int *cur;
10569
10570 if( ssl->conf->sig_hashes == NULL )
10571 return( -1 );
10572
10573 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10574 if( *cur == (int) md )
10575 return( 0 );
10576
10577 return( -1 );
10578}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010579#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010581#if defined(MBEDTLS_X509_CRT_PARSE_C)
10582int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10583 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010584 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010585 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010586{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010587 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010588#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010589 int usage = 0;
10590#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010591#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010592 const char *ext_oid;
10593 size_t ext_len;
10594#endif
10595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010596#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10597 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010598 ((void) cert);
10599 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010600 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010601#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010603#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10604 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010605 {
10606 /* Server part of the key exchange */
10607 switch( ciphersuite->key_exchange )
10608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010609 case MBEDTLS_KEY_EXCHANGE_RSA:
10610 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010611 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010612 break;
10613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010614 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10615 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10616 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10617 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010618 break;
10619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010620 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10621 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010622 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010623 break;
10624
10625 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010626 case MBEDTLS_KEY_EXCHANGE_NONE:
10627 case MBEDTLS_KEY_EXCHANGE_PSK:
10628 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10629 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010630 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010631 usage = 0;
10632 }
10633 }
10634 else
10635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010636 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10637 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010638 }
10639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010640 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010641 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010642 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010643 ret = -1;
10644 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010645#else
10646 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010647#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010649#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10650 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010652 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10653 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010654 }
10655 else
10656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010657 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10658 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010659 }
10660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010661 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010662 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010663 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010664 ret = -1;
10665 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010666#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010667
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010668 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010669}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010670#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010671
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010672/*
10673 * Convert version numbers to/from wire format
10674 * and, for DTLS, to/from TLS equivalent.
10675 *
10676 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010677 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010678 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10679 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010681void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010682 unsigned char ver[2] )
10683{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010684#if defined(MBEDTLS_SSL_PROTO_DTLS)
10685 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010687 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010688 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10689
10690 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10691 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10692 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010693 else
10694#else
10695 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010696#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010697 {
10698 ver[0] = (unsigned char) major;
10699 ver[1] = (unsigned char) minor;
10700 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010701}
10702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010703void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010704 const unsigned char ver[2] )
10705{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010706#if defined(MBEDTLS_SSL_PROTO_DTLS)
10707 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010708 {
10709 *major = 255 - ver[0] + 2;
10710 *minor = 255 - ver[1] + 1;
10711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010712 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010713 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10714 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010715 else
10716#else
10717 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010718#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010719 {
10720 *major = ver[0];
10721 *minor = ver[1];
10722 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010723}
10724
Simon Butcher99000142016-10-13 17:21:01 +010010725int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10726{
10727#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10728 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10729 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10730
10731 switch( md )
10732 {
10733#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10734#if defined(MBEDTLS_MD5_C)
10735 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010736 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010737#endif
10738#if defined(MBEDTLS_SHA1_C)
10739 case MBEDTLS_SSL_HASH_SHA1:
10740 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10741 break;
10742#endif
10743#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10744#if defined(MBEDTLS_SHA512_C)
10745 case MBEDTLS_SSL_HASH_SHA384:
10746 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10747 break;
10748#endif
10749#if defined(MBEDTLS_SHA256_C)
10750 case MBEDTLS_SSL_HASH_SHA256:
10751 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10752 break;
10753#endif
10754 default:
10755 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10756 }
10757
10758 return 0;
10759#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10760 (void) ssl;
10761 (void) md;
10762
10763 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10764#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10765}
10766
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010767#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10768 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10769int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10770 unsigned char *output,
10771 unsigned char *data, size_t data_len )
10772{
10773 int ret = 0;
10774 mbedtls_md5_context mbedtls_md5;
10775 mbedtls_sha1_context mbedtls_sha1;
10776
10777 mbedtls_md5_init( &mbedtls_md5 );
10778 mbedtls_sha1_init( &mbedtls_sha1 );
10779
10780 /*
10781 * digitally-signed struct {
10782 * opaque md5_hash[16];
10783 * opaque sha_hash[20];
10784 * };
10785 *
10786 * md5_hash
10787 * MD5(ClientHello.random + ServerHello.random
10788 * + ServerParams);
10789 * sha_hash
10790 * SHA(ClientHello.random + ServerHello.random
10791 * + ServerParams);
10792 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010793 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010794 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010796 goto exit;
10797 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010798 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010799 ssl->handshake->randbytes, 64 ) ) != 0 )
10800 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010801 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010802 goto exit;
10803 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010804 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010805 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010806 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010807 goto exit;
10808 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010809 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010810 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010812 goto exit;
10813 }
10814
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010815 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010816 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010818 goto exit;
10819 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010820 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010821 ssl->handshake->randbytes, 64 ) ) != 0 )
10822 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010824 goto exit;
10825 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010826 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010827 data_len ) ) != 0 )
10828 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010830 goto exit;
10831 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010832 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010833 output + 16 ) ) != 0 )
10834 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010835 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010836 goto exit;
10837 }
10838
10839exit:
10840 mbedtls_md5_free( &mbedtls_md5 );
10841 mbedtls_sha1_free( &mbedtls_sha1 );
10842
10843 if( ret != 0 )
10844 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10845 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10846
10847 return( ret );
10848
10849}
10850#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10851 MBEDTLS_SSL_PROTO_TLS1_1 */
10852
10853#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10854 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010855
10856#if defined(MBEDTLS_USE_PSA_CRYPTO)
10857int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10858 unsigned char *hash, size_t *hashlen,
10859 unsigned char *data, size_t data_len,
10860 mbedtls_md_type_t md_alg )
10861{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010862 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010863 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010864 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10865
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010866 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010867
10868 if( ( status = psa_hash_setup( &hash_operation,
10869 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010870 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010871 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010872 goto exit;
10873 }
10874
Andrzej Kurek814feff2019-01-14 04:35:19 -050010875 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10876 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010877 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010878 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010879 goto exit;
10880 }
10881
Andrzej Kurek814feff2019-01-14 04:35:19 -050010882 if( ( status = psa_hash_update( &hash_operation,
10883 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010884 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010885 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010886 goto exit;
10887 }
10888
Andrzej Kurek814feff2019-01-14 04:35:19 -050010889 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10890 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010891 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010892 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010893 goto exit;
10894 }
10895
10896exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010897 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010898 {
10899 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10900 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010901 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010902 {
10903 case PSA_ERROR_NOT_SUPPORTED:
10904 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010905 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010906 case PSA_ERROR_BUFFER_TOO_SMALL:
10907 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10908 case PSA_ERROR_INSUFFICIENT_MEMORY:
10909 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10910 default:
10911 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10912 }
10913 }
10914 return( 0 );
10915}
10916
10917#else
10918
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010919int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010920 unsigned char *hash, size_t *hashlen,
10921 unsigned char *data, size_t data_len,
10922 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010923{
10924 int ret = 0;
10925 mbedtls_md_context_t ctx;
10926 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010927 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010928
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010930
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010931 mbedtls_md_init( &ctx );
10932
10933 /*
10934 * digitally-signed struct {
10935 * opaque client_random[32];
10936 * opaque server_random[32];
10937 * ServerDHParams params;
10938 * };
10939 */
10940 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10941 {
10942 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10943 goto exit;
10944 }
10945 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10946 {
10947 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10948 goto exit;
10949 }
10950 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10951 {
10952 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10953 goto exit;
10954 }
10955 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10956 {
10957 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10958 goto exit;
10959 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010960 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010961 {
10962 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10963 goto exit;
10964 }
10965
10966exit:
10967 mbedtls_md_free( &ctx );
10968
10969 if( ret != 0 )
10970 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10971 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10972
10973 return( ret );
10974}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010975#endif /* MBEDTLS_USE_PSA_CRYPTO */
10976
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010977#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10978 MBEDTLS_SSL_PROTO_TLS1_2 */
10979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010980#endif /* MBEDTLS_SSL_TLS_C */