blob: 5cb15f572fdb255801c108ac2329b319965d5b6f [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 );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100148 /* Truncation is not an issue here because
149 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
150 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100151
152 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100153 return( 0 );
154}
155
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100156/* WARNING: The CID feature isn't fully implemented yet
157 * and will not be used. */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100158int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
159 int *enabled,
160 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
161 size_t *peer_cid_len )
162{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100163 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100164
165 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
167
Hanno Beckerc5f24222019-05-03 12:54:52 +0100168 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
169 * were used, but client and server requested the empty CID.
170 * This is indistinguishable from not using the CID extension
171 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100172 if( ssl->transform_in->in_cid_len == 0 &&
173 ssl->transform_in->out_cid_len == 0 )
174 {
175 return( 0 );
176 }
177
178 *peer_cid_len = ssl->transform_in->out_cid_len;
179 memcpy( peer_cid, ssl->transform_in->out_cid,
180 ssl->transform_in->out_cid_len );
181
182 *enabled = MBEDTLS_SSL_CID_ENABLED;
183
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100184 return( 0 );
185}
Hanno Becker35c36a62019-04-23 12:31:42 +0100186#endif /* MBEDTLS_SSL_CID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100187
Hanno Beckerd5847772018-08-28 10:09:23 +0100188/* Forward declarations for functions related to message buffering. */
189static void ssl_buffering_free( mbedtls_ssl_context *ssl );
190static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
191 uint8_t slot );
192static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
193static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
194static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
195static int ssl_buffer_message( mbedtls_ssl_context *ssl );
196static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100197static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100198
Hanno Beckera67dee22018-08-22 10:05:20 +0100199static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100200static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100201{
Hanno Becker11682cc2018-08-22 14:41:02 +0100202 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100203
204 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100205 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100206
207 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
208}
209
Hanno Becker67bc7c32018-08-06 11:33:50 +0100210static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
211{
Hanno Becker11682cc2018-08-22 14:41:02 +0100212 size_t const bytes_written = ssl->out_left;
213 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100214
215 /* Double-check that the write-index hasn't gone
216 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100217 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100218 {
219 /* Should never happen... */
220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
221 }
222
223 return( (int) ( mtu - bytes_written ) );
224}
225
226static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
227{
228 int ret;
229 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400230 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100231
232#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
233 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
234
235 if( max_len > mfl )
236 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100237
238 /* By the standard (RFC 6066 Sect. 4), the MFL extension
239 * only limits the maximum record payload size, so in theory
240 * we would be allowed to pack multiple records of payload size
241 * MFL into a single datagram. However, this would mean that there's
242 * no way to explicitly communicate MTU restrictions to the peer.
243 *
244 * The following reduction of max_len makes sure that we never
245 * write datagrams larger than MFL + Record Expansion Overhead.
246 */
247 if( max_len <= ssl->out_left )
248 return( 0 );
249
250 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100251#endif
252
253 ret = ssl_get_remaining_space_in_datagram( ssl );
254 if( ret < 0 )
255 return( ret );
256 remaining = (size_t) ret;
257
258 ret = mbedtls_ssl_get_record_expansion( ssl );
259 if( ret < 0 )
260 return( ret );
261 expansion = (size_t) ret;
262
263 if( remaining <= expansion )
264 return( 0 );
265
266 remaining -= expansion;
267 if( remaining >= max_len )
268 remaining = max_len;
269
270 return( (int) remaining );
271}
272
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200273/*
274 * Double the retransmit timeout value, within the allowed range,
275 * returning -1 if the maximum value has already been reached.
276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200278{
279 uint32_t new_timeout;
280
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200281 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200282 return( -1 );
283
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200284 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
285 * in the following way: after the initial transmission and a first
286 * retransmission, back off to a temporary estimated MTU of 508 bytes.
287 * This value is guaranteed to be deliverable (if not guaranteed to be
288 * delivered) of any compliant IPv4 (and IPv6) network, and should work
289 * on most non-IP stacks too. */
290 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400291 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200292 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
294 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200295
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200296 new_timeout = 2 * ssl->handshake->retransmit_timeout;
297
298 /* Avoid arithmetic overflow and range overflow */
299 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200300 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200301 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200302 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200303 }
304
305 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200307 ssl->handshake->retransmit_timeout ) );
308
309 return( 0 );
310}
311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200313{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200314 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200316 ssl->handshake->retransmit_timeout ) );
317}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200321/*
322 * Convert max_fragment_length codes to length.
323 * RFC 6066 says:
324 * enum{
325 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
326 * } MaxFragmentLength;
327 * and we add 0 -> extension unused
328 */
Angus Grattond8213d02016-05-25 20:56:48 +1000329static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200330{
Angus Grattond8213d02016-05-25 20:56:48 +1000331 switch( mfl )
332 {
333 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
334 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
335 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
336 return 512;
337 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
338 return 1024;
339 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
340 return 2048;
341 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
342 return 4096;
343 default:
344 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
345 }
346}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200348
Hanno Becker52055ae2019-02-06 14:30:46 +0000349int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
350 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200351{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_ssl_session_free( dst );
353 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000356
357#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200358 if( src->peer_cert != NULL )
359 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200360 int ret;
361
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200362 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200363 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200364 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200369 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200372 dst->peer_cert = NULL;
373 return( ret );
374 }
375 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000376#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000377 if( src->peer_cert_digest != NULL )
378 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000379 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000380 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000381 if( dst->peer_cert_digest == NULL )
382 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
383
384 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
385 src->peer_cert_digest_len );
386 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000387 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000388 }
389#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200393#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200394 if( src->ticket != NULL )
395 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200396 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200397 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200398 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200399
400 memcpy( dst->ticket, src->ticket, src->ticket_len );
401 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200402#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200403
404 return( 0 );
405}
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
408int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200409 const unsigned char *key_enc, const unsigned char *key_dec,
410 size_t keylen,
411 const unsigned char *iv_enc, const unsigned char *iv_dec,
412 size_t ivlen,
413 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200414 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
416int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
417int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
418int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
419int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
420#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000421
Paul Bakker5121ce52009-01-03 21:22:43 +0000422/*
423 * Key material generation
424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200426static int ssl3_prf( const unsigned char *secret, size_t slen,
427 const char *label,
428 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000429 unsigned char *dstbuf, size_t dlen )
430{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100431 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000432 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200433 mbedtls_md5_context md5;
434 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000435 unsigned char padding[16];
436 unsigned char sha1sum[20];
437 ((void)label);
438
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200439 mbedtls_md5_init( &md5 );
440 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200441
Paul Bakker5f70b252012-09-13 14:23:06 +0000442 /*
443 * SSLv3:
444 * block =
445 * MD5( secret + SHA1( 'A' + secret + random ) ) +
446 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
447 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
448 * ...
449 */
450 for( i = 0; i < dlen / 16; i++ )
451 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200452 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000453
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100454 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100455 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100456 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100457 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100458 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100459 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100460 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100461 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100462 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100463 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000464
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100465 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100466 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100467 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100468 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100469 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100470 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100471 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100472 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000473 }
474
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100475exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200476 mbedtls_md5_free( &md5 );
477 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000478
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500479 mbedtls_platform_zeroize( padding, sizeof( padding ) );
480 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000481
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100482 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200487static int tls1_prf( const unsigned char *secret, size_t slen,
488 const char *label,
489 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000490 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000491{
Paul Bakker23986e52011-04-24 08:57:21 +0000492 size_t nb, hs;
493 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200494 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300495 unsigned char *tmp;
496 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 const mbedtls_md_info_t *md_info;
499 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100500 int ret;
501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
Ron Eldor3b350852019-05-07 18:31:49 +0300504 tmp_len = 20 + strlen( label ) + rlen;
505 tmp = mbedtls_calloc( 1, tmp_len );
506 if( tmp == NULL )
507 {
508 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
509 goto exit;
510 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
512 hs = ( slen + 1 ) / 2;
513 S1 = secret;
514 S2 = secret + slen - hs;
515
516 nb = strlen( label );
517 memcpy( tmp + 20, label, nb );
518 memcpy( tmp + 20 + nb, random, rlen );
519 nb += rlen;
520
521 /*
522 * First compute P_md5(secret,label+random)[0..dlen]
523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300525 {
526 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
527 goto exit;
528 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300531 {
532 goto exit;
533 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
536 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
537 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
539 for( i = 0; i < dlen; i += 16 )
540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_md_hmac_reset ( &md_ctx );
542 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
543 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_md_hmac_reset ( &md_ctx );
546 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
547 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
550
551 for( j = 0; j < k; j++ )
552 dstbuf[i + j] = h_i[j];
553 }
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100556
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 /*
558 * XOR out with P_sha1(secret,label+random)[0..dlen]
559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300561 {
562 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
563 goto exit;
564 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300567 {
568 goto exit;
569 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
572 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
573 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
575 for( i = 0; i < dlen; i += 20 )
576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_md_hmac_reset ( &md_ctx );
578 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
579 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_md_hmac_reset ( &md_ctx );
582 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
583 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
585 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
586
587 for( j = 0; j < k; j++ )
588 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
589 }
590
Ron Eldor3b350852019-05-07 18:31:49 +0300591exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100593
Ron Eldor3b350852019-05-07 18:31:49 +0300594 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500595 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Ron Eldor3b350852019-05-07 18:31:49 +0300597 mbedtls_free( tmp );
598 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000599}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500603#if defined(MBEDTLS_USE_PSA_CRYPTO)
604static int tls_prf_generic( mbedtls_md_type_t md_type,
605 const unsigned char *secret, size_t slen,
606 const char *label,
607 const unsigned char *random, size_t rlen,
608 unsigned char *dstbuf, size_t dlen )
609{
610 psa_status_t status;
611 psa_algorithm_t alg;
612 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500613 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500614 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
615
Andrzej Kurek2f760752019-01-28 08:08:15 -0500616 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500617 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500618
Andrzej Kurekc929a822019-01-14 03:51:11 -0500619 if( md_type == MBEDTLS_MD_SHA384 )
620 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
621 else
622 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
623
Andrzej Kurek2f760752019-01-28 08:08:15 -0500624 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500625 psa_key_policy_set_usage( &policy,
626 PSA_KEY_USAGE_DERIVE,
627 alg );
628 status = psa_set_key_policy( master_slot, &policy );
629 if( status != PSA_SUCCESS )
630 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
631
632 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
633 if( status != PSA_SUCCESS )
634 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
635
636 status = psa_key_derivation( &generator,
637 master_slot, alg,
638 random, rlen,
639 (unsigned char const *) label,
640 (size_t) strlen( label ),
641 dlen );
642 if( status != PSA_SUCCESS )
643 {
644 psa_generator_abort( &generator );
645 psa_destroy_key( master_slot );
646 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
647 }
648
649 status = psa_generator_read( &generator, dstbuf, dlen );
650 if( status != PSA_SUCCESS )
651 {
652 psa_generator_abort( &generator );
653 psa_destroy_key( master_slot );
654 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
655 }
656
657 status = psa_generator_abort( &generator );
658 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500659 {
660 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500661 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500662 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500663
664 status = psa_destroy_key( master_slot );
665 if( status != PSA_SUCCESS )
666 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
667
Andrzej Kurek33171262019-01-15 03:25:18 -0500668 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500669}
670
671#else /* MBEDTLS_USE_PSA_CRYPTO */
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100674 const unsigned char *secret, size_t slen,
675 const char *label,
676 const unsigned char *random, size_t rlen,
677 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000678{
679 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100680 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300681 unsigned char *tmp;
682 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
684 const mbedtls_md_info_t *md_info;
685 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100686 int ret;
687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
691 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100694
Ron Eldor3b350852019-05-07 18:31:49 +0300695 tmp_len = md_len + strlen( label ) + rlen;
696 tmp = mbedtls_calloc( 1, tmp_len );
697 if( tmp == NULL )
698 {
699 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
700 goto exit;
701 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000702
703 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100704 memcpy( tmp + md_len, label, nb );
705 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000706 nb += rlen;
707
708 /*
709 * Compute P_<hash>(secret, label + random)[0..dlen]
710 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300712 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
715 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
716 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100717
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100718 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_md_hmac_reset ( &md_ctx );
721 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
722 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_md_hmac_reset ( &md_ctx );
725 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
726 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000727
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100728 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000729
730 for( j = 0; j < k; j++ )
731 dstbuf[i + j] = h_i[j];
732 }
733
Ron Eldor3b350852019-05-07 18:31:49 +0300734exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100736
Ron Eldor3b350852019-05-07 18:31:49 +0300737 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500738 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000739
Ron Eldor3b350852019-05-07 18:31:49 +0300740 mbedtls_free( tmp );
741
742 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000743}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500744#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100746static int tls_prf_sha256( const unsigned char *secret, size_t slen,
747 const char *label,
748 const unsigned char *random, size_t rlen,
749 unsigned char *dstbuf, size_t dlen )
750{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100752 label, random, rlen, dstbuf, dlen ) );
753}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200757static int tls_prf_sha384( const unsigned char *secret, size_t slen,
758 const char *label,
759 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000760 unsigned char *dstbuf, size_t dlen )
761{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100763 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000764}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#endif /* MBEDTLS_SHA512_C */
766#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
771 defined(MBEDTLS_SSL_PROTO_TLS1_1)
772static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200773#endif
Paul Bakker380da532012-04-18 16:10:25 +0000774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775#if defined(MBEDTLS_SSL_PROTO_SSL3)
776static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
777static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200778#endif
779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
781static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
782static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200783#endif
784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
786#if defined(MBEDTLS_SHA256_C)
787static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
788static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
789static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200790#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#if defined(MBEDTLS_SHA512_C)
793static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
794static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
795static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100796#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000798
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100799#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100800 defined(MBEDTLS_USE_PSA_CRYPTO)
801static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
802{
803 if( ssl->conf->f_psk != NULL )
804 {
805 /* If we've used a callback to select the PSK,
806 * the static configuration is irrelevant. */
807 if( ssl->handshake->psk_opaque != 0 )
808 return( 1 );
809
810 return( 0 );
811 }
812
813 if( ssl->conf->psk_opaque != 0 )
814 return( 1 );
815
816 return( 0 );
817}
818#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100819 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100820
Ron Eldorcf280092019-05-14 20:19:13 +0300821#if defined(MBEDTLS_SSL_EXPORT_KEYS)
822static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
823{
824#if defined(MBEDTLS_SSL_PROTO_SSL3)
825 if( tls_prf == ssl3_prf )
826 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300827 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300828 }
829 else
830#endif
831#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
832 if( tls_prf == tls1_prf )
833 {
834 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
835 }
836 else
837#endif
838#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
839#if defined(MBEDTLS_SHA512_C)
840 if( tls_prf == tls_prf_sha384 )
841 {
842 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
843 }
844 else
845#endif
846#if defined(MBEDTLS_SHA256_C)
847 if( tls_prf == tls_prf_sha256 )
848 {
849 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
850 }
851 else
852#endif
853#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
854 return( MBEDTLS_SSL_TLS_PRF_NONE );
855}
856#endif /* MBEDTLS_SSL_EXPORT_KEYS */
857
Ron Eldor51d3ab52019-05-12 14:54:30 +0300858int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
859 const unsigned char *secret, size_t slen,
860 const char *label,
861 const unsigned char *random, size_t rlen,
862 unsigned char *dstbuf, size_t dlen )
863{
864 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
865
866 switch( prf )
867 {
868#if defined(MBEDTLS_SSL_PROTO_SSL3)
869 case MBEDTLS_SSL_TLS_PRF_SSL3:
870 tls_prf = ssl3_prf;
871 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300872#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300873#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
874 case MBEDTLS_SSL_TLS_PRF_TLS1:
875 tls_prf = tls1_prf;
876 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300877#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
878
879#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300880#if defined(MBEDTLS_SHA512_C)
881 case MBEDTLS_SSL_TLS_PRF_SHA384:
882 tls_prf = tls_prf_sha384;
883 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300884#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300885#if defined(MBEDTLS_SHA256_C)
886 case MBEDTLS_SSL_TLS_PRF_SHA256:
887 tls_prf = tls_prf_sha256;
888 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300889#endif /* MBEDTLS_SHA256_C */
890#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300891 default:
892 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
893 }
894
895 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
896}
897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000899{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200900 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000901#if defined(MBEDTLS_USE_PSA_CRYPTO)
902 int psa_fallthrough;
903#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000904 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000905 unsigned char keyblk[256];
906 unsigned char *key1;
907 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100908 unsigned char *mac_enc;
909 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000910 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200911 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000912 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000913 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 const mbedtls_cipher_info_t *cipher_info;
915 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100916
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000917 /* cf. RFC 5246, Section 8.1:
918 * "The master secret is always exactly 48 bytes in length." */
919 size_t const master_secret_len = 48;
920
Hanno Becker35b23c72018-10-23 12:10:41 +0100921#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
922 unsigned char session_hash[48];
923#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_ssl_session *session = ssl->session_negotiate;
926 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
927 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000930
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000931#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
932 transform->encrypt_then_mac = session->encrypt_then_mac;
933#endif
934 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000935
936 ciphersuite_info = handshake->ciphersuite_info;
937 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100938 if( cipher_info == NULL )
939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000941 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100943 }
944
Hanno Beckere694c3e2017-12-27 21:34:08 +0000945 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100946 if( md_info == NULL )
947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000949 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100951 }
952
Hanno Becker4bf74652019-04-26 16:22:27 +0100953#if defined(MBEDTLS_SSL_CID)
954 /* Copy own and peer's CID if the use of the CID
955 * extension has been negotiated. */
956 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
957 {
958 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
959 transform->in_cid_len = ssl->own_cid_len;
960 transform->out_cid_len = ssl->handshake->peer_cid_len;
961 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
962 memcpy( transform->out_cid, ssl->handshake->peer_cid,
963 ssl->handshake->peer_cid_len );
964
965 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
966 transform->out_cid_len );
967 MBEDTLS_SSL_DEBUG_BUF( 3, "Ingoing CID", transform->in_cid,
968 transform->in_cid_len );
969 }
970#endif /* MBEDTLS_SSL_CID */
971
Paul Bakker5121ce52009-01-03 21:22:43 +0000972 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000973 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975#if defined(MBEDTLS_SSL_PROTO_SSL3)
976 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000977 {
Paul Bakker48916f92012-09-16 19:57:18 +0000978 handshake->tls_prf = ssl3_prf;
979 handshake->calc_verify = ssl_calc_verify_ssl;
980 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000981 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200982 else
983#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
985 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000986 {
Paul Bakker48916f92012-09-16 19:57:18 +0000987 handshake->tls_prf = tls1_prf;
988 handshake->calc_verify = ssl_calc_verify_tls;
989 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000990 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200991 else
992#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
994#if defined(MBEDTLS_SHA512_C)
995 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +0000996 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000997 {
Paul Bakker48916f92012-09-16 19:57:18 +0000998 handshake->tls_prf = tls_prf_sha384;
999 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1000 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001001 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001002 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001003#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004#if defined(MBEDTLS_SHA256_C)
1005 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001006 {
Paul Bakker48916f92012-09-16 19:57:18 +00001007 handshake->tls_prf = tls_prf_sha256;
1008 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1009 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001010 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001011 else
1012#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1016 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001017 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001018
1019 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001020 * SSLv3:
1021 * master =
1022 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1023 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1024 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001025 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001026 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001027 * master = PRF( premaster, "master secret", randbytes )[0..47]
1028 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001029 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001031 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1032 }
1033 else
1034 {
1035 /* The label for the KDF used for key expansion.
1036 * This is either "master secret" or "extended master secret"
1037 * depending on whether the Extended Master Secret extension
1038 * is used. */
1039 char const *lbl = "master secret";
1040
1041 /* The salt for the KDF used for key expansion.
1042 * - If the Extended Master Secret extension is not used,
1043 * this is ClientHello.Random + ServerHello.Random
1044 * (see Sect. 8.1 in RFC 5246).
1045 * - If the Extended Master Secret extension is used,
1046 * this is the transcript of the handshake so far.
1047 * (see Sect. 4 in RFC 7627). */
1048 unsigned char const *salt = handshake->randbytes;
1049 size_t salt_len = 64;
1050
1051#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001055
Hanno Becker35b23c72018-10-23 12:10:41 +01001056 lbl = "extended master secret";
1057 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001058 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1060 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001063 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1064 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001065 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001066 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001067 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001068#endif /* MBEDTLS_SHA512_C */
1069 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001070 }
1071 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001073 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001074
Hanno Becker35b23c72018-10-23 12:10:41 +01001075 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001076 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001077#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1078
Hanno Becker7d0a5692018-10-23 15:26:22 +01001079#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1080 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1081 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1082 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1083 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001084 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001085 /* Perform PSK-to-MS expansion in a single step. */
1086 psa_status_t status;
1087 psa_algorithm_t alg;
1088 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001089 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001090
1091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1092
1093 psk = ssl->conf->psk_opaque;
1094 if( ssl->handshake->psk_opaque != 0 )
1095 psk = ssl->handshake->psk_opaque;
1096
Hanno Becker22bf1452019-04-05 11:21:08 +01001097 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001098 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1099 else
1100 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1101
1102 status = psa_key_derivation( &generator, psk, alg,
1103 salt, salt_len,
1104 (unsigned char const *) lbl,
1105 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001106 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001107 if( status != PSA_SUCCESS )
1108 {
1109 psa_generator_abort( &generator );
1110 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1111 }
1112
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001113 status = psa_generator_read( &generator, session->master,
1114 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001115 if( status != PSA_SUCCESS )
1116 {
1117 psa_generator_abort( &generator );
1118 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1119 }
1120
1121 status = psa_generator_abort( &generator );
1122 if( status != PSA_SUCCESS )
1123 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001124 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001125 else
1126#endif
1127 {
1128 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1129 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001130 session->master,
1131 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001132 if( ret != 0 )
1133 {
1134 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1135 return( ret );
1136 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001137
Hanno Becker7d0a5692018-10-23 15:26:22 +01001138 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1139 handshake->premaster,
1140 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001141
Hanno Becker7d0a5692018-10-23 15:26:22 +01001142 mbedtls_platform_zeroize( handshake->premaster,
1143 sizeof(handshake->premaster) );
1144 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001145 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001146
1147 /*
1148 * Swap the client and server random values.
1149 */
Paul Bakker48916f92012-09-16 19:57:18 +00001150 memcpy( tmp, handshake->randbytes, 64 );
1151 memcpy( handshake->randbytes, tmp + 32, 32 );
1152 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001153 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001154
1155 /*
1156 * SSLv3:
1157 * key block =
1158 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1159 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1160 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1161 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1162 * ...
1163 *
1164 * TLSv1:
1165 * key block = PRF( master, "key expansion", randbytes )
1166 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001167 ret = handshake->tls_prf( session->master, 48, "key expansion",
1168 handshake->randbytes, 64, keyblk, 256 );
1169 if( ret != 0 )
1170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001172 return( ret );
1173 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1176 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1177 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1178 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1179 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001180
Paul Bakker5121ce52009-01-03 21:22:43 +00001181 /*
1182 * Determine the appropriate key, IV and MAC length.
1183 */
Paul Bakker68884e32013-01-07 18:20:04 +01001184
Hanno Becker88aaf652017-12-27 08:17:40 +00001185 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001186
Hanno Becker8031d062018-01-03 15:32:31 +00001187#if defined(MBEDTLS_GCM_C) || \
1188 defined(MBEDTLS_CCM_C) || \
1189 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001191 cipher_info->mode == MBEDTLS_MODE_CCM ||
1192 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001194 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001195
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001196 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001197 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001198 transform->taglen =
1199 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001200
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001201 /* All modes haves 96-bit IVs;
1202 * GCM and CCM has 4 implicit and 8 explicit bytes
1203 * ChachaPoly has all 12 bytes implicit
1204 */
Paul Bakker68884e32013-01-07 18:20:04 +01001205 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001206 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1207 transform->fixed_ivlen = 12;
1208 else
1209 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001210
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001211 /* Minimum length of encrypted record */
1212 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001213 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001214 }
1215 else
Hanno Becker8031d062018-01-03 15:32:31 +00001216#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1217#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1218 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1219 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001220 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001221 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1223 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001226 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001227 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001228
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001229 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001230 mac_key_len = mbedtls_md_get_size( md_info );
1231 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001234 /*
1235 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1236 * (rfc 6066 page 13 or rfc 2104 section 4),
1237 * so we only need to adjust the length here.
1238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001242
1243#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1244 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001245 * HMAC implementation which also truncates the key
1246 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001247 mac_key_len = transform->maclen;
1248#endif
1249 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001251
1252 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001253 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001254
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001255 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001257 transform->minlen = transform->maclen;
1258 else
Paul Bakker68884e32013-01-07 18:20:04 +01001259 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001260 /*
1261 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001262 * 1. if EtM is in use: one block plus MAC
1263 * otherwise: * first multiple of blocklen greater than maclen
1264 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1267 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001268 {
1269 transform->minlen = transform->maclen
1270 + cipher_info->block_size;
1271 }
1272 else
1273#endif
1274 {
1275 transform->minlen = transform->maclen
1276 + cipher_info->block_size
1277 - transform->maclen % cipher_info->block_size;
1278 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1281 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1282 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001283 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001284 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001285#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1287 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1288 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001289 {
1290 transform->minlen += transform->ivlen;
1291 }
1292 else
1293#endif
1294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001296 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1297 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001298 }
Paul Bakker68884e32013-01-07 18:20:04 +01001299 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001300 }
Hanno Becker8031d062018-01-03 15:32:31 +00001301 else
1302#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1303 {
1304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1305 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1306 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001307
Hanno Becker88aaf652017-12-27 08:17:40 +00001308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1309 (unsigned) keylen,
1310 (unsigned) transform->minlen,
1311 (unsigned) transform->ivlen,
1312 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001313
1314 /*
1315 * Finally setup the cipher contexts, IVs and MAC secrets.
1316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001318 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001320 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001321 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
Paul Bakker68884e32013-01-07 18:20:04 +01001323 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001324 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001325
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001326 /*
1327 * This is not used in TLS v1.1.
1328 */
Paul Bakker48916f92012-09-16 19:57:18 +00001329 iv_copy_len = ( transform->fixed_ivlen ) ?
1330 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001331 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1332 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001333 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 }
1335 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336#endif /* MBEDTLS_SSL_CLI_C */
1337#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001338 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001340 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001341 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001342
Hanno Becker81c7b182017-11-09 18:39:33 +00001343 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001344 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001345
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001346 /*
1347 * This is not used in TLS v1.1.
1348 */
Paul Bakker48916f92012-09-16 19:57:18 +00001349 iv_copy_len = ( transform->fixed_ivlen ) ?
1350 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001351 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1352 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001353 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001355 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001359 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1360 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001361 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001362
Hanno Beckerd56ed242018-01-03 15:32:51 +00001363#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364#if defined(MBEDTLS_SSL_PROTO_SSL3)
1365 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001366 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001367 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001370 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1371 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001372 }
1373
Hanno Becker81c7b182017-11-09 18:39:33 +00001374 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1375 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001376 }
1377 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1379#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1380 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1381 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001382 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001383 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1384 For AEAD-based ciphersuites, there is nothing to do here. */
1385 if( mac_key_len != 0 )
1386 {
1387 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1388 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1389 }
Paul Bakker68884e32013-01-07 18:20:04 +01001390 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001391 else
1392#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001395 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1396 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001397 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001398#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1401 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001402 {
1403 int ret = 0;
1404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001406
Hanno Becker88aaf652017-12-27 08:17:40 +00001407 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001408 transform->iv_enc, transform->iv_dec,
1409 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001410 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001411 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001414 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1415 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001416 }
1417 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001418#else
1419 ((void) mac_dec);
1420 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001422
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001423#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1424 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001425 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001426 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1427 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001428 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001429 iv_copy_len );
1430 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001431
1432 if( ssl->conf->f_export_keys_ext != NULL )
1433 {
1434 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1435 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001436 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001437 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001438 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001439 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001440 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001441 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001442#endif
1443
Hanno Beckerf704bef2018-11-16 15:21:18 +00001444#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001445
1446 /* Only use PSA-based ciphers for TLS-1.2.
1447 * That's relevant at least for TLS-1.0, where
1448 * we assume that mbedtls_cipher_crypt() updates
1449 * the structure field for the IV, which the PSA-based
1450 * implementation currently doesn't. */
1451#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1452 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001453 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001454 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001455 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001456 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1457 {
1458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001459 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001460 }
1461
1462 if( ret == 0 )
1463 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001464 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001465 psa_fallthrough = 0;
1466 }
1467 else
1468 {
1469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1470 psa_fallthrough = 1;
1471 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001472 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001473 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001474 psa_fallthrough = 1;
1475#else
1476 psa_fallthrough = 1;
1477#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001478
Hanno Beckercb1cc802018-11-17 22:27:38 +00001479 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001480#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001481 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001482 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001484 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001485 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001486 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001487
Hanno Beckerf704bef2018-11-16 15:21:18 +00001488#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001489 /* Only use PSA-based ciphers for TLS-1.2.
1490 * That's relevant at least for TLS-1.0, where
1491 * we assume that mbedtls_cipher_crypt() updates
1492 * the structure field for the IV, which the PSA-based
1493 * implementation currently doesn't. */
1494#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1495 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001496 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001497 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001498 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001499 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1500 {
1501 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001502 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001503 }
1504
1505 if( ret == 0 )
1506 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001507 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001508 psa_fallthrough = 0;
1509 }
1510 else
1511 {
1512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1513 psa_fallthrough = 1;
1514 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001515 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001516 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001517 psa_fallthrough = 1;
1518#else
1519 psa_fallthrough = 1;
1520#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001521
Hanno Beckercb1cc802018-11-17 22:27:38 +00001522 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001523#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001524 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001525 cipher_info ) ) != 0 )
1526 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001527 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001528 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001529 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001532 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001536 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001537 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001540 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 MBEDTLS_DECRYPT ) ) != 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_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001544 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001545 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#if defined(MBEDTLS_CIPHER_MODE_CBC)
1548 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1551 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001554 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001555 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1558 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001561 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001562 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001563 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001565
Paul Bakker5121ce52009-01-03 21:22:43 +00001566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001568 // Initialize compression
1569 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001571 {
Paul Bakker16770332013-10-11 09:59:44 +02001572 if( ssl->compress_buf == NULL )
1573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001575 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001576 if( ssl->compress_buf == NULL )
1577 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001579 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001580 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1581 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001582 }
1583 }
1584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001586
Paul Bakker48916f92012-09-16 19:57:18 +00001587 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1588 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001589
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001590 if( deflateInit( &transform->ctx_deflate,
1591 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001592 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001595 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1596 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001597 }
1598 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001602end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001603 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1604 mbedtls_platform_zeroize( handshake->randbytes,
1605 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001606 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001607}
1608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609#if defined(MBEDTLS_SSL_PROTO_SSL3)
1610void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001611{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001612 mbedtls_md5_context md5;
1613 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001614 unsigned char pad_1[48];
1615 unsigned char pad_2[48];
1616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001618
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001619 mbedtls_md5_init( &md5 );
1620 mbedtls_sha1_init( &sha1 );
1621
1622 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1623 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001624
Paul Bakker380da532012-04-18 16:10:25 +00001625 memset( pad_1, 0x36, 48 );
1626 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001627
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001628 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1629 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1630 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001631
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001632 mbedtls_md5_starts_ret( &md5 );
1633 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1634 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1635 mbedtls_md5_update_ret( &md5, hash, 16 );
1636 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001637
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001638 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1639 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1640 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001641
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001642 mbedtls_sha1_starts_ret( &sha1 );
1643 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1644 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1645 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1646 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001650
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001651 mbedtls_md5_free( &md5 );
1652 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001653
Paul Bakker380da532012-04-18 16:10:25 +00001654 return;
1655}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1659void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001660{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001661 mbedtls_md5_context md5;
1662 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001665
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001666 mbedtls_md5_init( &md5 );
1667 mbedtls_sha1_init( &sha1 );
1668
1669 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1670 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001671
Andrzej Kurekeb342242019-01-29 09:14:33 -05001672 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001673 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001677
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001678 mbedtls_md5_free( &md5 );
1679 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001680
Paul Bakker380da532012-04-18 16:10:25 +00001681 return;
1682}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1686#if defined(MBEDTLS_SHA256_C)
1687void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001688{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001689#if defined(MBEDTLS_USE_PSA_CRYPTO)
1690 size_t hash_size;
1691 psa_status_t status;
1692 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1693
1694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1695 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1696 if( status != PSA_SUCCESS )
1697 {
1698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1699 return;
1700 }
1701
1702 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1703 if( status != PSA_SUCCESS )
1704 {
1705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1706 return;
1707 }
1708 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1710#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001711 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001712
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001713 mbedtls_sha256_init( &sha256 );
1714
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001716
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001717 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001718 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001722
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001723 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001724#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001725 return;
1726}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729#if defined(MBEDTLS_SHA512_C)
1730void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001731{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001732#if defined(MBEDTLS_USE_PSA_CRYPTO)
1733 size_t hash_size;
1734 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001735 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001736
1737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001738 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001739 if( status != PSA_SUCCESS )
1740 {
1741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1742 return;
1743 }
1744
Andrzej Kurek972fba52019-01-30 03:29:12 -05001745 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001746 if( status != PSA_SUCCESS )
1747 {
1748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1749 return;
1750 }
1751 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1753#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001754 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001755
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001756 mbedtls_sha512_init( &sha512 );
1757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001759
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001760 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001761 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001766 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001767#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001768 return;
1769}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770#endif /* MBEDTLS_SHA512_C */
1771#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1774int 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 +02001775{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001776 unsigned char *p = ssl->handshake->premaster;
1777 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001778 const unsigned char *psk = ssl->conf->psk;
1779 size_t psk_len = ssl->conf->psk_len;
1780
1781 /* If the psk callback was called, use its result */
1782 if( ssl->handshake->psk != NULL )
1783 {
1784 psk = ssl->handshake->psk;
1785 psk_len = ssl->handshake->psk_len;
1786 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001787
1788 /*
1789 * PMS = struct {
1790 * opaque other_secret<0..2^16-1>;
1791 * opaque psk<0..2^16-1>;
1792 * };
1793 * with "other_secret" depending on the particular key exchange
1794 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1796 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001797 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001798 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001800
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001801 *(p++) = (unsigned char)( psk_len >> 8 );
1802 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001803
1804 if( end < p || (size_t)( end - p ) < psk_len )
1805 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1806
1807 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001808 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001809 }
1810 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1812#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1813 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001814 {
1815 /*
1816 * other_secret already set by the ClientKeyExchange message,
1817 * and is 48 bytes long
1818 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001819 if( end - p < 2 )
1820 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1821
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001822 *p++ = 0;
1823 *p++ = 48;
1824 p += 48;
1825 }
1826 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1828#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1829 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001830 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001831 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001832 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001833
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001834 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001836 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001837 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001840 return( ret );
1841 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001842 *(p++) = (unsigned char)( len >> 8 );
1843 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001844 p += len;
1845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001847 }
1848 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1850#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1851 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001852 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001853 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001854 size_t zlen;
1855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001857 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001858 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001861 return( ret );
1862 }
1863
1864 *(p++) = (unsigned char)( zlen >> 8 );
1865 *(p++) = (unsigned char)( zlen );
1866 p += zlen;
1867
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001868 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1869 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001870 }
1871 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1875 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001876 }
1877
1878 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001879 if( end - p < 2 )
1880 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001881
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001882 *(p++) = (unsigned char)( psk_len >> 8 );
1883 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001884
1885 if( end < p || (size_t)( end - p ) < psk_len )
1886 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1887
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001888 memcpy( p, psk, psk_len );
1889 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001890
1891 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1892
1893 return( 0 );
1894}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001898/*
1899 * SSLv3.0 MAC functions
1900 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001901#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001902static void ssl_mac( mbedtls_md_context_t *md_ctx,
1903 const unsigned char *secret,
1904 const unsigned char *buf, size_t len,
1905 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001906 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001907{
1908 unsigned char header[11];
1909 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001910 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1912 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001913
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001914 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001916 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001917 else
Paul Bakker68884e32013-01-07 18:20:04 +01001918 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001919
1920 memcpy( header, ctr, 8 );
1921 header[ 8] = (unsigned char) type;
1922 header[ 9] = (unsigned char)( len >> 8 );
1923 header[10] = (unsigned char)( len );
1924
Paul Bakker68884e32013-01-07 18:20:04 +01001925 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 mbedtls_md_starts( md_ctx );
1927 mbedtls_md_update( md_ctx, secret, md_size );
1928 mbedtls_md_update( md_ctx, padding, padlen );
1929 mbedtls_md_update( md_ctx, header, 11 );
1930 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001931 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001932
Paul Bakker68884e32013-01-07 18:20:04 +01001933 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 mbedtls_md_starts( md_ctx );
1935 mbedtls_md_update( md_ctx, secret, md_size );
1936 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001937 mbedtls_md_update( md_ctx, out, md_size );
1938 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001939}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001941
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001942/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001943 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001944#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001945 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1946 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1947 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1948/* This function makes sure every byte in the memory region is accessed
1949 * (in ascending addresses order) */
1950static void ssl_read_memory( unsigned char *p, size_t len )
1951{
1952 unsigned char acc = 0;
1953 volatile unsigned char force;
1954
1955 for( ; len != 0; p++, len-- )
1956 acc ^= *p;
1957
1958 force = acc;
1959 (void) force;
1960}
1961#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1962
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001963/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001964 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001965 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001966
1967static void ssl_extract_add_data_from_record( unsigned char* add_data,
1968 mbedtls_record *rec )
1969{
1970 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1971 add_data[8] = rec->type;
1972 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
1973 add_data[11] = ( rec->data_len >> 8 ) & 0xFF;
1974 add_data[12] = rec->data_len & 0xFF;
1975}
1976
Hanno Beckera18d1322018-01-03 14:27:32 +00001977int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1978 mbedtls_ssl_transform *transform,
1979 mbedtls_record *rec,
1980 int (*f_rng)(void *, unsigned char *, size_t),
1981 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001982{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001984 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001985 unsigned char * data;
1986 unsigned char add_data[13];
1987 size_t post_avail;
1988
1989 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00001990#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001991 ((void) ssl);
1992#endif
1993
1994 /* The PRNG is used for dynamic IV generation that's used
1995 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1996#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1997 ( defined(MBEDTLS_AES_C) || \
1998 defined(MBEDTLS_ARIA_C) || \
1999 defined(MBEDTLS_CAMELLIA_C) ) && \
2000 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2001 ((void) f_rng);
2002 ((void) p_rng);
2003#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002006
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002007 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002008 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2010 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2011 }
2012 if( rec == NULL ||
2013 rec->buf == NULL ||
2014 rec->buf_len < rec->data_offset ||
2015 rec->buf_len - rec->data_offset < rec->data_len )
2016 {
2017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002019 }
2020
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002021 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2022 data = rec->buf + rec->data_offset;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002024 data, rec->data_len );
2025
2026 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2027
2028 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2029 {
2030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2031 (unsigned) rec->data_len,
2032 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2033 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2034 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002035
Paul Bakker5121ce52009-01-03 21:22:43 +00002036 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002037 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002038 */
Hanno Becker52344c22018-01-03 15:24:20 +00002039#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 if( mode == MBEDTLS_MODE_STREAM ||
2041 ( mode == MBEDTLS_MODE_CBC
2042#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002043 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002044#endif
2045 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002046 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002047 if( post_avail < transform->maclen )
2048 {
2049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2050 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2051 }
2052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002054 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002055 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002056 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002057 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2058 data, rec->data_len, rec->ctr, rec->type, mac );
2059 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002060 }
2061 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002062#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2064 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002065 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002066 {
Hanno Becker992b6872017-11-09 18:57:39 +00002067 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2068
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002069 ssl_extract_add_data_from_record( add_data, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002070
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002071 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2072 sizeof( add_data ) );
2073 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2074 data, rec->data_len );
2075 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2076 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2077
2078 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002079 }
2080 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002081#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2084 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002085 }
2086
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002087 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2088 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002089
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002090 rec->data_len += transform->maclen;
2091 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002092 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002093 }
Hanno Becker52344c22018-01-03 15:24:20 +00002094#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002095
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002096 /*
2097 * Encrypt
2098 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2100 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002102 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002103 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002105 "including %d bytes of padding",
2106 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002107
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002108 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2109 transform->iv_enc, transform->ivlen,
2110 data, rec->data_len,
2111 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002114 return( ret );
2115 }
2116
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002117 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2120 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002121 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 }
Paul Bakker68884e32013-01-07 18:20:04 +01002123 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002125
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002126#if defined(MBEDTLS_GCM_C) || \
2127 defined(MBEDTLS_CCM_C) || \
2128 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002130 mode == MBEDTLS_MODE_CCM ||
2131 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002132 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002133 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002134 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002135 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002136
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002137 /* Check that there's space for both the authentication tag
2138 * and the explicit IV before and after the record content. */
2139 if( post_avail < transform->taglen ||
2140 rec->data_offset < explicit_iv_len )
2141 {
2142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2143 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2144 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002145
Paul Bakker68884e32013-01-07 18:20:04 +01002146 /*
2147 * Generate IV
2148 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002149 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2150 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002151 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002152 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002153 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2154 explicit_iv_len );
2155 /* Prefix record content with explicit IV. */
2156 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002157 }
2158 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2159 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002160 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002161 unsigned char i;
2162
2163 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2164
2165 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002166 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002167 }
2168 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002169 {
2170 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2172 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002173 }
2174
Hanno Becker1f10d762019-04-26 13:34:37 +01002175 ssl_extract_add_data_from_record( add_data, rec );
2176
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002177 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2178 iv, transform->ivlen );
2179 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002180 data - explicit_iv_len, explicit_iv_len );
2181 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2182 add_data, 13 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002184 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002185 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002186
Paul Bakker68884e32013-01-07 18:20:04 +01002187 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002188 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002189 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002190
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002191 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002192 iv, transform->ivlen,
2193 add_data, 13, /* add data */
2194 data, rec->data_len, /* source */
2195 data, &rec->data_len, /* destination */
2196 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002199 return( ret );
2200 }
2201
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002202 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2203 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002204
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002205 rec->data_len += transform->taglen + explicit_iv_len;
2206 rec->data_offset -= explicit_iv_len;
2207 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002208 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002209 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2212#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002213 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002216 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002217 size_t padlen, i;
2218 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002219
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002220 /* Currently we're always using minimal padding
2221 * (up to 255 bytes would be allowed). */
2222 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2223 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002224 padlen = 0;
2225
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002226 /* Check there's enough space in the buffer for the padding. */
2227 if( post_avail < padlen + 1 )
2228 {
2229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2230 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2231 }
2232
Paul Bakker5121ce52009-01-03 21:22:43 +00002233 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002234 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002235
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002236 rec->data_len += padlen + 1;
2237 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002240 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002241 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2242 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002243 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002244 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002245 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002246 if( f_rng == NULL )
2247 {
2248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2249 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2250 }
2251
2252 if( rec->data_offset < transform->ivlen )
2253 {
2254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2255 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2256 }
2257
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002258 /*
2259 * Generate IV
2260 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002261 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002262 if( ret != 0 )
2263 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002264
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002265 memcpy( data - transform->ivlen, transform->iv_enc,
2266 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002267
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002268 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002272 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002273 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002274 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002275
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002276 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2277 transform->iv_enc,
2278 transform->ivlen,
2279 data, rec->data_len,
2280 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002283 return( ret );
2284 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002285
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002286 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2289 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002290 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002293 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002294 {
2295 /*
2296 * Save IV in SSL3 and TLS1
2297 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002298 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2299 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002300 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002301 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002302#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002303 {
2304 data -= transform->ivlen;
2305 rec->data_offset -= transform->ivlen;
2306 rec->data_len += transform->ivlen;
2307 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002310 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002311 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002312 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2313
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002314 /*
2315 * MAC(MAC_write_key, seq_num +
2316 * TLSCipherText.type +
2317 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002318 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002319 * IV + // except for TLS 1.0
2320 * ENC(content + padding + padding_length));
2321 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002322
2323 if( post_avail < transform->maclen)
2324 {
2325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2326 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2327 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002328
Hanno Becker1f10d762019-04-26 13:34:37 +01002329 ssl_extract_add_data_from_record( add_data, rec );
2330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002332 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2333 sizeof( add_data ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002334
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002335 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
2336 sizeof( add_data ) );
2337 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2338 data, rec->data_len );
2339 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2340 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002341
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002342 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002343
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002344 rec->data_len += transform->maclen;
2345 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002346 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002347 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002349 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002350 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002352 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2355 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002356 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002357
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002358 /* Make extra sure authentication was performed, exactly once */
2359 if( auth_done != 1 )
2360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2362 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002363 }
2364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002366
2367 return( 0 );
2368}
2369
Hanno Beckera18d1322018-01-03 14:27:32 +00002370int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2371 mbedtls_ssl_transform *transform,
2372 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002373{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002374 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002376 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002377#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002378 size_t padlen = 0, correct = 1;
2379#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002380 unsigned char* data;
2381 unsigned char add_data[13];
2382
Hanno Beckera18d1322018-01-03 14:27:32 +00002383#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002384 ((void) ssl);
2385#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002388 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002389 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2391 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2392 }
2393 if( rec == NULL ||
2394 rec->buf == NULL ||
2395 rec->buf_len < rec->data_offset ||
2396 rec->buf_len - rec->data_offset < rec->data_len )
2397 {
2398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002400 }
2401
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002402 data = rec->buf + rec->data_offset;
2403 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2406 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002407 {
2408 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002409 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2410 transform->iv_dec,
2411 transform->ivlen,
2412 data, rec->data_len,
2413 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002416 return( ret );
2417 }
2418
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002419 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2422 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002423 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 }
Paul Bakker68884e32013-01-07 18:20:04 +01002425 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002427#if defined(MBEDTLS_GCM_C) || \
2428 defined(MBEDTLS_CCM_C) || \
2429 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002431 mode == MBEDTLS_MODE_CCM ||
2432 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002433 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002434 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002435 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002436
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002437 /*
2438 * Compute and update sizes
2439 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002440 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002443 "+ taglen (%d)", rec->data_len,
2444 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002446 }
Paul Bakker68884e32013-01-07 18:20:04 +01002447
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002448 /*
2449 * Prepare IV
2450 */
2451 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2452 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002453 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002454 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002455 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002456
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002457 }
2458 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2459 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002460 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002461 unsigned char i;
2462
2463 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2464
2465 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002466 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002467 }
2468 else
2469 {
2470 /* Reminder if we ever add an AEAD mode with a different size */
2471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2472 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2473 }
2474
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002475 data += explicit_iv_len;
2476 rec->data_offset += explicit_iv_len;
2477 rec->data_len -= explicit_iv_len + transform->taglen;
2478
2479 ssl_extract_add_data_from_record( add_data, rec );
2480 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
2481 add_data, 13 );
2482
2483 memcpy( transform->iv_dec + transform->fixed_ivlen,
2484 data - explicit_iv_len, explicit_iv_len );
2485
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002486 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002487 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002488 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002489
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002490
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002491 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002492 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002493 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002494 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2495 iv, transform->ivlen,
2496 add_data, 13,
2497 data, rec->data_len,
2498 data, &olen,
2499 data + rec->data_len,
2500 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2505 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002506
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002507 return( ret );
2508 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002509 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002510
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002511 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2514 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002515 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002516 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2519#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002520 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002523 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002524
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 /*
Paul Bakker45829992013-01-03 14:52:21 +01002526 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002529 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2530 {
2531 /* The ciphertext is prefixed with the CBC IV. */
2532 minlen += transform->ivlen;
2533 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002534#endif
Paul Bakker45829992013-01-03 14:52:21 +01002535
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002536 /* Size considerations:
2537 *
2538 * - The CBC cipher text must not be empty and hence
2539 * at least of size transform->ivlen.
2540 *
2541 * Together with the potential IV-prefix, this explains
2542 * the first of the two checks below.
2543 *
2544 * - The record must contain a MAC, either in plain or
2545 * encrypted, depending on whether Encrypt-then-MAC
2546 * is used or not.
2547 * - If it is, the message contains the IV-prefix,
2548 * the CBC ciphertext, and the MAC.
2549 * - If it is not, the padded plaintext, and hence
2550 * the CBC ciphertext, has at least length maclen + 1
2551 * because there is at least the padding length byte.
2552 *
2553 * As the CBC ciphertext is not empty, both cases give the
2554 * lower bound minlen + maclen + 1 on the record size, which
2555 * we test for in the second check below.
2556 */
2557 if( rec->data_len < minlen + transform->ivlen ||
2558 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002561 "+ 1 ) ( + expl IV )", rec->data_len,
2562 transform->ivlen,
2563 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002565 }
2566
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002567 /*
2568 * Authenticate before decrypt if enabled
2569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002571 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002572 {
Hanno Becker992b6872017-11-09 18:57:39 +00002573 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002576
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002577 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2578 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002579
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002580 ssl_extract_add_data_from_record( add_data, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002581
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002582 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, 13 );
2583 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2584 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2585 data, rec->data_len );
2586 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2587 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002588
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002589 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2590 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002591 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002592 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002593
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002594 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2595 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002599 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002600 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002601 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002603
2604 /*
2605 * Check length sanity
2606 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002607 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002610 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002612 }
2613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002615 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002616 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002617 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002618 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002619 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002620 /* This is safe because data_len >= minlen + maclen + 1 initially,
2621 * and at this point we have at most subtracted maclen (note that
2622 * minlen == transform->ivlen here). */
2623 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002624
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002625 data += transform->ivlen;
2626 rec->data_offset += transform->ivlen;
2627 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002628 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002630
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002631 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2632 transform->iv_dec, transform->ivlen,
2633 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002636 return( ret );
2637 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002638
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002639 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2642 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002643 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002646 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002647 {
2648 /*
2649 * Save IV in SSL3 and TLS1
2650 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002651 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2652 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002654#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002656 /* Safe since data_len >= minlen + maclen + 1, so after having
2657 * subtracted at most minlen and maclen up to this point,
2658 * data_len > 0. */
2659 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002660
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002661 if( auth_done == 1 )
2662 {
2663 correct *= ( rec->data_len >= padlen + 1 );
2664 padlen *= ( rec->data_len >= padlen + 1 );
2665 }
2666 else
Paul Bakker45829992013-01-03 14:52:21 +01002667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002669 if( rec->data_len < transform->maclen + padlen + 1 )
2670 {
2671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2672 rec->data_len,
2673 transform->maclen,
2674 padlen + 1 ) );
2675 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002676#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002677
2678 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2679 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002680 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002681
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002682 padlen++;
2683
2684 /* Regardless of the validity of the padding,
2685 * we have data_len >= padlen here. */
2686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002688 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002690 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692#if defined(MBEDTLS_SSL_DEBUG_ALL)
2693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002694 "should be no more than %d",
2695 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002696#endif
Paul Bakker45829992013-01-03 14:52:21 +01002697 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002698 }
2699 }
2700 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2702#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2703 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002704 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002706 /* The padding check involves a series of up to 256
2707 * consecutive memory reads at the end of the record
2708 * plaintext buffer. In order to hide the length and
2709 * validity of the padding, always perform exactly
2710 * `min(256,plaintext_len)` reads (but take into account
2711 * only the last `padlen` bytes for the padding check). */
2712 size_t pad_count = 0;
2713 size_t real_count = 0;
2714 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002715
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002716 /* Index of first padding byte; it has been ensured above
2717 * that the subtraction is safe. */
2718 size_t const padding_idx = rec->data_len - padlen;
2719 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2720 size_t const start_idx = rec->data_len - num_checks;
2721 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002722
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002723 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002724 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002725 real_count |= ( idx >= padding_idx );
2726 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002727 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002728 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002731 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002733#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002734 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002735 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002736 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2738 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2741 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002742 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002743
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002744 /* If the padding was found to be invalid, padlen == 0
2745 * and the subtraction is safe. If the padding was found valid,
2746 * padlen hasn't been changed and the previous assertion
2747 * data_len >= padlen still holds. */
2748 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002750 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002752 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2755 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002756 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002757
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002758#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002760 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002761#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002762
2763 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002764 * Authenticate if not done yet.
2765 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002766 */
Hanno Becker52344c22018-01-03 15:24:20 +00002767#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002768 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002769 {
Hanno Becker992b6872017-11-09 18:57:39 +00002770 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002771
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002772 /* If the initial value of padlen was such that
2773 * data_len < maclen + padlen + 1, then padlen
2774 * got reset to 1, and the initial check
2775 * data_len >= minlen + maclen + 1
2776 * guarantees that at this point we still
2777 * have at least data_len >= maclen.
2778 *
2779 * If the initial value of padlen was such that
2780 * data_len >= maclen + padlen + 1, then we have
2781 * subtracted either padlen + 1 (if the padding was correct)
2782 * or 0 (if the padding was incorrect) since then,
2783 * hence data_len >= maclen in any case.
2784 */
2785 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002786
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002787 ssl_extract_add_data_from_record( add_data, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002790 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002791 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002792 ssl_mac( &transform->md_ctx_dec,
2793 transform->mac_dec,
2794 data, rec->data_len,
2795 rec->ctr, rec->type,
2796 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002797 }
2798 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2800#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2801 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002802 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002803 {
2804 /*
2805 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002806 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002807 *
2808 * Known timing attacks:
2809 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2810 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002811 * To compensate for different timings for the MAC calculation
2812 * depending on how much padding was removed (which is determined
2813 * by padlen), process extra_run more blocks through the hash
2814 * function.
2815 *
2816 * The formula in the paper is
2817 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2818 * where L1 is the size of the header plus the decrypted message
2819 * plus CBC padding and L2 is the size of the header plus the
2820 * decrypted message. This is for an underlying hash function
2821 * with 64-byte blocks.
2822 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2823 * correctly. We round down instead of up, so -56 is the correct
2824 * value for our calculations instead of -55.
2825 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002826 * Repeat the formula rather than defining a block_size variable.
2827 * This avoids requiring division by a variable at runtime
2828 * (which would be marginally less efficient and would require
2829 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002830 */
2831 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002832 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002833
2834 /*
2835 * The next two sizes are the minimum and maximum values of
2836 * in_msglen over all padlen values.
2837 *
2838 * They're independent of padlen, since we previously did
2839 * in_msglen -= padlen.
2840 *
2841 * Note that max_len + maclen is never more than the buffer
2842 * length, as we previously did in_msglen -= maclen too.
2843 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002844 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002845 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2846
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002847 memset( tmp, 0, sizeof( tmp ) );
2848
2849 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002850 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002851#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2852 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002853 case MBEDTLS_MD_MD5:
2854 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002855 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002856 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002857 extra_run = ( 13 + rec->data_len + padlen + 8 ) / 64 -
2858 ( 13 + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002859 break;
2860#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002861#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002862 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002863 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002864 extra_run = ( 13 + rec->data_len + padlen + 16 ) / 128 -
2865 ( 13 + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002866 break;
2867#endif
2868 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002870 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2871 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002872
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002873 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002874
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002875 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, 13 );
2876 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2877 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002878 /* Make sure we access everything even when padlen > 0. This
2879 * makes the synchronisation requirements for just-in-time
2880 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002881 ssl_read_memory( data + rec->data_len, padlen );
2882 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002883
2884 /* Call mbedtls_md_process at least once due to cache attacks
2885 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002886 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002887 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002888
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002889 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002890
2891 /* Make sure we access all the memory that could contain the MAC,
2892 * before we check it in the next code block. This makes the
2893 * synchronisation requirements for just-in-time Prime+Probe
2894 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002895 ssl_read_memory( data + min_len,
2896 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002897 }
2898 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2900 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2903 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002904 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002905
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002906#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002907 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2908 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002909#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002910
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002911 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2912 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914#if defined(MBEDTLS_SSL_DEBUG_ALL)
2915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002916#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002917 correct = 0;
2918 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002919 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002920 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002921
2922 /*
2923 * Finally check the correct flag
2924 */
2925 if( correct == 0 )
2926 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00002927#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002928
2929 /* Make extra sure authentication was performed, exactly once */
2930 if( auth_done != 1 )
2931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2933 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002934 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002937
2938 return( 0 );
2939}
2940
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002941#undef MAC_NONE
2942#undef MAC_PLAINTEXT
2943#undef MAC_CIPHERTEXT
2944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002946/*
2947 * Compression/decompression functions
2948 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002950{
2951 int ret;
2952 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002953 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002954 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002955 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002958
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002959 if( len_pre == 0 )
2960 return( 0 );
2961
Paul Bakker2770fbd2012-07-03 13:30:23 +00002962 memcpy( msg_pre, ssl->out_msg, len_pre );
2963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002965 ssl->out_msglen ) );
2966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002968 ssl->out_msg, ssl->out_msglen );
2969
Paul Bakker48916f92012-09-16 19:57:18 +00002970 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2971 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2972 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002973 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002974
Paul Bakker48916f92012-09-16 19:57:18 +00002975 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002976 if( ret != Z_OK )
2977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2979 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002980 }
2981
Angus Grattond8213d02016-05-25 20:56:48 +10002982 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002983 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002986 ssl->out_msglen ) );
2987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002989 ssl->out_msg, ssl->out_msglen );
2990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002992
2993 return( 0 );
2994}
2995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002997{
2998 int ret;
2999 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003000 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003001 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003002 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003005
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003006 if( len_pre == 0 )
3007 return( 0 );
3008
Paul Bakker2770fbd2012-07-03 13:30:23 +00003009 memcpy( msg_pre, ssl->in_msg, len_pre );
3010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003012 ssl->in_msglen ) );
3013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003015 ssl->in_msg, ssl->in_msglen );
3016
Paul Bakker48916f92012-09-16 19:57:18 +00003017 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3018 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3019 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003020 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003021 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003022
Paul Bakker48916f92012-09-16 19:57:18 +00003023 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003024 if( ret != Z_OK )
3025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3027 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003028 }
3029
Angus Grattond8213d02016-05-25 20:56:48 +10003030 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003031 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003033 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003034 ssl->in_msglen ) );
3035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003037 ssl->in_msg, ssl->in_msglen );
3038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003040
3041 return( 0 );
3042}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3046static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048#if defined(MBEDTLS_SSL_PROTO_DTLS)
3049static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003050{
3051 /* If renegotiation is not enforced, retransmit until we would reach max
3052 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003053 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003054 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003055 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003056 unsigned char doublings = 1;
3057
3058 while( ratio != 0 )
3059 {
3060 ++doublings;
3061 ratio >>= 1;
3062 }
3063
3064 if( ++ssl->renego_records_seen > doublings )
3065 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003067 return( 0 );
3068 }
3069 }
3070
3071 return( ssl_write_hello_request( ssl ) );
3072}
3073#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003075
Paul Bakker5121ce52009-01-03 21:22:43 +00003076/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003077 * Fill the input message buffer by appending data to it.
3078 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003079 *
3080 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3081 * available (from this read and/or a previous one). Otherwise, an error code
3082 * is returned (possibly EOF or WANT_READ).
3083 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003084 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3085 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3086 * since we always read a whole datagram at once.
3087 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003088 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003089 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003090 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003092{
Paul Bakker23986e52011-04-24 08:57:21 +00003093 int ret;
3094 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003097
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003098 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003101 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003103 }
3104
Angus Grattond8213d02016-05-25 20:56:48 +10003105 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003109 }
3110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003112 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003113 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003114 uint32_t timeout;
3115
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003116 /* Just to be sure */
3117 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3118 {
3119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3120 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3121 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3122 }
3123
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003124 /*
3125 * The point is, we need to always read a full datagram at once, so we
3126 * sometimes read more then requested, and handle the additional data.
3127 * It could be the rest of the current record (while fetching the
3128 * header) and/or some other records in the same datagram.
3129 */
3130
3131 /*
3132 * Move to the next record in the already read datagram if applicable
3133 */
3134 if( ssl->next_record_offset != 0 )
3135 {
3136 if( ssl->in_left < ssl->next_record_offset )
3137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3139 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003140 }
3141
3142 ssl->in_left -= ssl->next_record_offset;
3143
3144 if( ssl->in_left != 0 )
3145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003147 ssl->next_record_offset ) );
3148 memmove( ssl->in_hdr,
3149 ssl->in_hdr + ssl->next_record_offset,
3150 ssl->in_left );
3151 }
3152
3153 ssl->next_record_offset = 0;
3154 }
3155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003157 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003158
3159 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003160 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003161 */
3162 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003165 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003166 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003167
3168 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003169 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003170 * are not at the beginning of a new record, the caller did something
3171 * wrong.
3172 */
3173 if( ssl->in_left != 0 )
3174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3176 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003177 }
3178
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003179 /*
3180 * Don't even try to read if time's out already.
3181 * This avoids by-passing the timer when repeatedly receiving messages
3182 * that will end up being dropped.
3183 */
3184 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003185 {
3186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003187 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003188 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003189 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003190 {
Angus Grattond8213d02016-05-25 20:56:48 +10003191 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003194 timeout = ssl->handshake->retransmit_timeout;
3195 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003196 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003199
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003200 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003201 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3202 timeout );
3203 else
3204 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003207
3208 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003210 }
3211
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003212 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003215 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003218 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003219 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003222 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003223 }
3224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003228 return( ret );
3229 }
3230
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003231 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003232 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003234 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003236 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003237 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003240 return( ret );
3241 }
3242
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003243 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003244 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003246 }
3247
Paul Bakker5121ce52009-01-03 21:22:43 +00003248 if( ret < 0 )
3249 return( ret );
3250
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003251 ssl->in_left = ret;
3252 }
3253 else
3254#endif
3255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003257 ssl->in_left, nb_want ) );
3258
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003259 while( ssl->in_left < nb_want )
3260 {
3261 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003262
3263 if( ssl_check_timer( ssl ) != 0 )
3264 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3265 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003266 {
3267 if( ssl->f_recv_timeout != NULL )
3268 {
3269 ret = ssl->f_recv_timeout( ssl->p_bio,
3270 ssl->in_hdr + ssl->in_left, len,
3271 ssl->conf->read_timeout );
3272 }
3273 else
3274 {
3275 ret = ssl->f_recv( ssl->p_bio,
3276 ssl->in_hdr + ssl->in_left, len );
3277 }
3278 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003281 ssl->in_left, nb_want ) );
3282 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003283
3284 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003285 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003286
3287 if( ret < 0 )
3288 return( ret );
3289
mohammad160352aecb92018-03-28 23:41:40 -07003290 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003291 {
Darryl Green11999bb2018-03-13 15:22:58 +00003292 MBEDTLS_SSL_DEBUG_MSG( 1,
3293 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003294 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003295 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3296 }
3297
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003298 ssl->in_left += ret;
3299 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003300 }
3301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003303
3304 return( 0 );
3305}
3306
3307/*
3308 * Flush any data not yet written
3309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003311{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003312 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003313 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003316
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003317 if( ssl->f_send == NULL )
3318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003320 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003322 }
3323
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003324 /* Avoid incrementing counter if data is flushed */
3325 if( ssl->out_left == 0 )
3326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003328 return( 0 );
3329 }
3330
Paul Bakker5121ce52009-01-03 21:22:43 +00003331 while( ssl->out_left > 0 )
3332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
3334 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003335
Hanno Becker2b1e3542018-08-06 11:19:13 +01003336 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003337 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003340
3341 if( ret <= 0 )
3342 return( ret );
3343
mohammad160352aecb92018-03-28 23:41:40 -07003344 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003345 {
Darryl Green11999bb2018-03-13 15:22:58 +00003346 MBEDTLS_SSL_DEBUG_MSG( 1,
3347 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003348 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003349 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3350 }
3351
Paul Bakker5121ce52009-01-03 21:22:43 +00003352 ssl->out_left -= ret;
3353 }
3354
Hanno Becker2b1e3542018-08-06 11:19:13 +01003355#if defined(MBEDTLS_SSL_PROTO_DTLS)
3356 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003357 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003358 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003359 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003360 else
3361#endif
3362 {
3363 ssl->out_hdr = ssl->out_buf + 8;
3364 }
3365 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003368
3369 return( 0 );
3370}
3371
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003372/*
3373 * Functions to handle the DTLS retransmission state machine
3374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003376/*
3377 * Append current handshake message to current outgoing flight
3378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003380{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3383 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3384 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003385
3386 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003387 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003388 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003391 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003392 }
3393
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003394 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003395 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003398 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003399 }
3400
3401 /* Copy current handshake message with headers */
3402 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3403 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003404 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003405 msg->next = NULL;
3406
3407 /* Append to the current flight */
3408 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003409 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003410 else
3411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003413 while( cur->next != NULL )
3414 cur = cur->next;
3415 cur->next = msg;
3416 }
3417
Hanno Becker3b235902018-08-06 09:54:53 +01003418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003419 return( 0 );
3420}
3421
3422/*
3423 * Free the current flight of handshake messages
3424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003426{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 mbedtls_ssl_flight_item *cur = flight;
3428 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003429
3430 while( cur != NULL )
3431 {
3432 next = cur->next;
3433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434 mbedtls_free( cur->p );
3435 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003436
3437 cur = next;
3438 }
3439}
3440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3442static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003443#endif
3444
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003445/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003446 * Swap transform_out and out_ctr with the alternative ones
3447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003449{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003451 unsigned char tmp_out_ctr[8];
3452
3453 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003456 return;
3457 }
3458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003459 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003460
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003461 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003462 tmp_transform = ssl->transform_out;
3463 ssl->transform_out = ssl->handshake->alt_transform_out;
3464 ssl->handshake->alt_transform_out = tmp_transform;
3465
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003466 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003467 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3468 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003469 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003470
3471 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003472 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3475 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3480 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003481 }
3482 }
3483#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003484}
3485
3486/*
3487 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003488 */
3489int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3490{
3491 int ret = 0;
3492
3493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3494
3495 ret = mbedtls_ssl_flight_transmit( ssl );
3496
3497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3498
3499 return( ret );
3500}
3501
3502/*
3503 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003504 *
3505 * Need to remember the current message in case flush_output returns
3506 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003507 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003508 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003509int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003510{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003511 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003515 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003517
3518 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003519 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003520 ssl_swap_epochs( ssl );
3521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003523 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003524
3525 while( ssl->handshake->cur_msg != NULL )
3526 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003527 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003528 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003529
Hanno Beckere1dcb032018-08-17 16:47:58 +01003530 int const is_finished =
3531 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3532 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3533
Hanno Becker04da1892018-08-14 13:22:10 +01003534 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3535 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3536
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003537 /* Swap epochs before sending Finished: we can't do it after
3538 * sending ChangeCipherSpec, in case write returns WANT_READ.
3539 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003540 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003541 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003543 ssl_swap_epochs( ssl );
3544 }
3545
Hanno Becker67bc7c32018-08-06 11:33:50 +01003546 ret = ssl_get_remaining_payload_in_datagram( ssl );
3547 if( ret < 0 )
3548 return( ret );
3549 max_frag_len = (size_t) ret;
3550
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003551 /* CCS is copied as is, while HS messages may need fragmentation */
3552 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3553 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003554 if( max_frag_len == 0 )
3555 {
3556 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3557 return( ret );
3558
3559 continue;
3560 }
3561
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003562 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003563 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003564 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003565
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003566 /* Update position inside current message */
3567 ssl->handshake->cur_msg_p += cur->len;
3568 }
3569 else
3570 {
3571 const unsigned char * const p = ssl->handshake->cur_msg_p;
3572 const size_t hs_len = cur->len - 12;
3573 const size_t frag_off = p - ( cur->p + 12 );
3574 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003575 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003576
Hanno Beckere1dcb032018-08-17 16:47:58 +01003577 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003578 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003579 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003580 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003581
Hanno Becker67bc7c32018-08-06 11:33:50 +01003582 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3583 return( ret );
3584
3585 continue;
3586 }
3587 max_hs_frag_len = max_frag_len - 12;
3588
3589 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3590 max_hs_frag_len : rem_len;
3591
3592 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003593 {
3594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003595 (unsigned) cur_hs_frag_len,
3596 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003597 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003598
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003599 /* Messages are stored with handshake headers as if not fragmented,
3600 * copy beginning of headers then fill fragmentation fields.
3601 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3602 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003603
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003604 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3605 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3606 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3607
Hanno Becker67bc7c32018-08-06 11:33:50 +01003608 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3609 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3610 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003611
3612 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3613
Hanno Becker3f7b9732018-08-28 09:53:25 +01003614 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003615 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3616 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003617 ssl->out_msgtype = cur->type;
3618
3619 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003620 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003621 }
3622
3623 /* If done with the current message move to the next one if any */
3624 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3625 {
3626 if( cur->next != NULL )
3627 {
3628 ssl->handshake->cur_msg = cur->next;
3629 ssl->handshake->cur_msg_p = cur->next->p + 12;
3630 }
3631 else
3632 {
3633 ssl->handshake->cur_msg = NULL;
3634 ssl->handshake->cur_msg_p = NULL;
3635 }
3636 }
3637
3638 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003639 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003642 return( ret );
3643 }
3644 }
3645
Hanno Becker67bc7c32018-08-06 11:33:50 +01003646 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3647 return( ret );
3648
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003649 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3651 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003652 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003655 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3656 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003657
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003659
3660 return( 0 );
3661}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003662
3663/*
3664 * To be called when the last message of an incoming flight is received.
3665 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003667{
3668 /* We won't need to resend that one any more */
3669 ssl_flight_free( ssl->handshake->flight );
3670 ssl->handshake->flight = NULL;
3671 ssl->handshake->cur_msg = NULL;
3672
3673 /* The next incoming flight will start with this msg_seq */
3674 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3675
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003676 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003677 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003678
Hanno Becker0271f962018-08-16 13:23:47 +01003679 /* Clear future message buffering structure. */
3680 ssl_buffering_free( ssl );
3681
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003682 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003683 ssl_set_timer( ssl, 0 );
3684
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é-Gonnard5d8ba532014-09-19 15:09:21 +02003687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003689 }
3690 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003691 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003692}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003693
3694/*
3695 * To be called when the last message of an outgoing flight is send.
3696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003697void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003698{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003699 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003700 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003702 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3703 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003705 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003706 }
3707 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003709}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003711
Paul Bakker5121ce52009-01-03 21:22:43 +00003712/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003713 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003714 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003715
3716/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003717 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003718 *
3719 * - fill in handshake headers
3720 * - update handshake checksum
3721 * - DTLS: save message for resending
3722 * - then pass to the record layer
3723 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003724 * DTLS: except for HelloRequest, messages are only queued, and will only be
3725 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003726 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003727 * Inputs:
3728 * - ssl->out_msglen: 4 + actual handshake message len
3729 * (4 is the size of handshake headers for TLS)
3730 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3731 * - ssl->out_msg + 4: the handshake message body
3732 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003733 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003734 * - ssl->out_msglen: the length of the record contents
3735 * (including handshake headers but excluding record headers)
3736 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003737 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003738int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003739{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003740 int ret;
3741 const size_t hs_len = ssl->out_msglen - 4;
3742 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003743
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3745
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003746 /*
3747 * Sanity checks
3748 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003749 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003750 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3751 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003752 /* In SSLv3, the client might send a NoCertificate alert. */
3753#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3754 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3755 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3756 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3757#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3758 {
3759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3760 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3761 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003762 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003763
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003764 /* Whenever we send anything different from a
3765 * HelloRequest we should be in a handshake - double check. */
3766 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3767 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003768 ssl->handshake == NULL )
3769 {
3770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3771 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3772 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003775 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003776 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003778 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3780 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003781 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003782#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003783
Hanno Beckerb50a2532018-08-06 11:52:54 +01003784 /* Double-check that we did not exceed the bounds
3785 * of the outgoing record buffer.
3786 * This should never fail as the various message
3787 * writing functions must obey the bounds of the
3788 * outgoing record buffer, but better be safe.
3789 *
3790 * Note: We deliberately do not check for the MTU or MFL here.
3791 */
3792 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3793 {
3794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3795 "size %u, maximum %u",
3796 (unsigned) ssl->out_msglen,
3797 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3798 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3799 }
3800
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003801 /*
3802 * Fill handshake headers
3803 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003804 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003805 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003806 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3807 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3808 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003809
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003810 /*
3811 * DTLS has additional fields in the Handshake layer,
3812 * between the length field and the actual payload:
3813 * uint16 message_seq;
3814 * uint24 fragment_offset;
3815 * uint24 fragment_length;
3816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003817#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003818 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003819 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003820 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003821 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003822 {
3823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3824 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003825 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003826 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003827 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3828 }
3829
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003830 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003831 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003832
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003833 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003834 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003835 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003836 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3837 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3838 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003839 }
3840 else
3841 {
3842 ssl->out_msg[4] = 0;
3843 ssl->out_msg[5] = 0;
3844 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003845
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003846 /* Handshake hashes are computed without fragmentation,
3847 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003848 memset( ssl->out_msg + 6, 0x00, 3 );
3849 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003850 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003851#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003852
Hanno Becker0207e532018-08-28 10:28:28 +01003853 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003854 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3855 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003856 }
3857
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003858 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003859#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003860 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003861 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3862 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003863 {
3864 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003867 return( ret );
3868 }
3869 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003870 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003871#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003872 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003873 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003874 {
3875 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3876 return( ret );
3877 }
3878 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003879
3880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3881
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003882 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003883}
3884
3885/*
3886 * Record layer functions
3887 */
3888
3889/*
3890 * Write current record.
3891 *
3892 * Uses:
3893 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3894 * - ssl->out_msglen: length of the record content (excl headers)
3895 * - ssl->out_msg: record content
3896 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003897int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003898{
3899 int ret, done = 0;
3900 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003901 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003902
3903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003906 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003907 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003908 {
3909 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003911 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003912 return( ret );
3913 }
3914
3915 len = ssl->out_msglen;
3916 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003917#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3920 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003924 ret = mbedtls_ssl_hw_record_write( ssl );
3925 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3928 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003929 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003930
3931 if( ret == 0 )
3932 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003933 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003935 if( !done )
3936 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003937 unsigned i;
3938 size_t protected_record_size;
3939
Paul Bakker05ef8352012-05-08 09:17:57 +00003940 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003941 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003942 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003943
Hanno Becker19859472018-08-06 09:40:20 +01003944 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003945 ssl->out_len[0] = (unsigned char)( len >> 8 );
3946 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003947
Paul Bakker48916f92012-09-16 19:57:18 +00003948 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003949 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003950 mbedtls_record rec;
3951
3952 rec.buf = ssl->out_iv;
3953 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3954 ( ssl->out_iv - ssl->out_buf );
3955 rec.data_len = ssl->out_msglen;
3956 rec.data_offset = ssl->out_msg - rec.buf;
3957
3958 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3959 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3960 ssl->conf->transport, rec.ver );
3961 rec.type = ssl->out_msgtype;
3962
Hanno Beckera18d1322018-01-03 14:27:32 +00003963 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003964 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003966 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003967 return( ret );
3968 }
3969
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003970 if( rec.data_offset != 0 )
3971 {
3972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3973 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3974 }
3975
Hanno Becker78f839d2019-03-14 12:56:23 +00003976 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00003977 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3978 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003979 }
3980
Hanno Becker2b1e3542018-08-06 11:19:13 +01003981 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3982
3983#if defined(MBEDTLS_SSL_PROTO_DTLS)
3984 /* In case of DTLS, double-check that we don't exceed
3985 * the remaining space in the datagram. */
3986 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3987 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003988 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003989 if( ret < 0 )
3990 return( ret );
3991
3992 if( protected_record_size > (size_t) ret )
3993 {
3994 /* Should never happen */
3995 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3996 }
3997 }
3998#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004000 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004001 "version = [%d:%d], msglen = %d",
4002 ssl->out_hdr[0], ssl->out_hdr[1],
4003 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004006 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004007
4008 ssl->out_left += protected_record_size;
4009 ssl->out_hdr += protected_record_size;
4010 ssl_update_out_pointers( ssl, ssl->transform_out );
4011
Hanno Becker04484622018-08-06 09:49:38 +01004012 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4013 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4014 break;
4015
4016 /* The loop goes to its end iff the counter is wrapping */
4017 if( i == ssl_ep_len( ssl ) )
4018 {
4019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4020 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4021 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004022 }
4023
Hanno Becker67bc7c32018-08-06 11:33:50 +01004024#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004025 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4026 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004027 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004028 size_t remaining;
4029 ret = ssl_get_remaining_payload_in_datagram( ssl );
4030 if( ret < 0 )
4031 {
4032 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4033 ret );
4034 return( ret );
4035 }
4036
4037 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004038 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004039 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004040 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004041 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004042 else
4043 {
Hanno Becker513815a2018-08-20 11:56:09 +01004044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004045 }
4046 }
4047#endif /* MBEDTLS_SSL_PROTO_DTLS */
4048
4049 if( ( flush == SSL_FORCE_FLUSH ) &&
4050 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004052 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004053 return( ret );
4054 }
4055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004057
4058 return( 0 );
4059}
4060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004061#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004062
4063static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4064{
4065 if( ssl->in_msglen < ssl->in_hslen ||
4066 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4067 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4068 {
4069 return( 1 );
4070 }
4071 return( 0 );
4072}
Hanno Becker44650b72018-08-16 12:51:11 +01004073
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004074static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004075{
4076 return( ( ssl->in_msg[9] << 16 ) |
4077 ( ssl->in_msg[10] << 8 ) |
4078 ssl->in_msg[11] );
4079}
4080
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004081static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004082{
4083 return( ( ssl->in_msg[6] << 16 ) |
4084 ( ssl->in_msg[7] << 8 ) |
4085 ssl->in_msg[8] );
4086}
4087
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004088static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004089{
4090 uint32_t msg_len, frag_off, frag_len;
4091
4092 msg_len = ssl_get_hs_total_len( ssl );
4093 frag_off = ssl_get_hs_frag_off( ssl );
4094 frag_len = ssl_get_hs_frag_len( ssl );
4095
4096 if( frag_off > msg_len )
4097 return( -1 );
4098
4099 if( frag_len > msg_len - frag_off )
4100 return( -1 );
4101
4102 if( frag_len + 12 > ssl->in_msglen )
4103 return( -1 );
4104
4105 return( 0 );
4106}
4107
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004108/*
4109 * Mark bits in bitmask (used for DTLS HS reassembly)
4110 */
4111static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4112{
4113 unsigned int start_bits, end_bits;
4114
4115 start_bits = 8 - ( offset % 8 );
4116 if( start_bits != 8 )
4117 {
4118 size_t first_byte_idx = offset / 8;
4119
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004120 /* Special case */
4121 if( len <= start_bits )
4122 {
4123 for( ; len != 0; len-- )
4124 mask[first_byte_idx] |= 1 << ( start_bits - len );
4125
4126 /* Avoid potential issues with offset or len becoming invalid */
4127 return;
4128 }
4129
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004130 offset += start_bits; /* Now offset % 8 == 0 */
4131 len -= start_bits;
4132
4133 for( ; start_bits != 0; start_bits-- )
4134 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4135 }
4136
4137 end_bits = len % 8;
4138 if( end_bits != 0 )
4139 {
4140 size_t last_byte_idx = ( offset + len ) / 8;
4141
4142 len -= end_bits; /* Now len % 8 == 0 */
4143
4144 for( ; end_bits != 0; end_bits-- )
4145 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4146 }
4147
4148 memset( mask + offset / 8, 0xFF, len / 8 );
4149}
4150
4151/*
4152 * Check that bitmask is full
4153 */
4154static int ssl_bitmask_check( unsigned char *mask, size_t len )
4155{
4156 size_t i;
4157
4158 for( i = 0; i < len / 8; i++ )
4159 if( mask[i] != 0xFF )
4160 return( -1 );
4161
4162 for( i = 0; i < len % 8; i++ )
4163 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4164 return( -1 );
4165
4166 return( 0 );
4167}
4168
Hanno Becker56e205e2018-08-16 09:06:12 +01004169/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004170static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004171 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004172{
Hanno Becker56e205e2018-08-16 09:06:12 +01004173 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004174
Hanno Becker56e205e2018-08-16 09:06:12 +01004175 alloc_len = 12; /* Handshake header */
4176 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004177
Hanno Beckerd07df862018-08-16 09:14:58 +01004178 if( add_bitmap )
4179 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004180
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004181 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004182}
Hanno Becker56e205e2018-08-16 09:06:12 +01004183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004184#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004185
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004186static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004187{
4188 return( ( ssl->in_msg[1] << 16 ) |
4189 ( ssl->in_msg[2] << 8 ) |
4190 ssl->in_msg[3] );
4191}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004192
Simon Butcher99000142016-10-13 17:21:01 +01004193int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004194{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004195 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004198 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004199 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004200 }
4201
Hanno Becker12555c62018-08-16 12:47:53 +01004202 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004205 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004206 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004208#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004209 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004210 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004211 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004212 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004213
Hanno Becker44650b72018-08-16 12:51:11 +01004214 if( ssl_check_hs_header( ssl ) != 0 )
4215 {
4216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4217 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4218 }
4219
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004220 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004221 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4222 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4223 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4224 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004225 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004226 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4227 {
4228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4229 recv_msg_seq,
4230 ssl->handshake->in_msg_seq ) );
4231 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4232 }
4233
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004234 /* Retransmit only on last message from previous flight, to avoid
4235 * too many retransmissions.
4236 * Besides, No sane server ever retransmits HelloVerifyRequest */
4237 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004238 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004241 "message_seq = %d, start_of_flight = %d",
4242 recv_msg_seq,
4243 ssl->handshake->in_flight_start_seq ) );
4244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004245 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004248 return( ret );
4249 }
4250 }
4251 else
4252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004254 "message_seq = %d, expected = %d",
4255 recv_msg_seq,
4256 ssl->handshake->in_msg_seq ) );
4257 }
4258
Hanno Becker90333da2017-10-10 11:27:13 +01004259 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004260 }
4261 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004262
Hanno Becker6d97ef52018-08-16 13:09:04 +01004263 /* Message reassembly is handled alongside buffering of future
4264 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004265 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004266 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004267 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004270 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004271 }
4272 }
4273 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004274#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004275 /* With TLS we don't handle fragmentation (for now) */
4276 if( ssl->in_msglen < ssl->in_hslen )
4277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4279 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004280 }
4281
Simon Butcher99000142016-10-13 17:21:01 +01004282 return( 0 );
4283}
4284
4285void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4286{
Hanno Becker0271f962018-08-16 13:23:47 +01004287 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004288
Hanno Becker0271f962018-08-16 13:23:47 +01004289 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004290 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004291 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004292 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004293
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004294 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004295#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004296 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004297 ssl->handshake != NULL )
4298 {
Hanno Becker0271f962018-08-16 13:23:47 +01004299 unsigned offset;
4300 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004301
Hanno Becker0271f962018-08-16 13:23:47 +01004302 /* Increment handshake sequence number */
4303 hs->in_msg_seq++;
4304
4305 /*
4306 * Clear up handshake buffering and reassembly structure.
4307 */
4308
4309 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004310 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004311
4312 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004313 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4314 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004315 offset++, hs_buf++ )
4316 {
4317 *hs_buf = *(hs_buf + 1);
4318 }
4319
4320 /* Create a fresh last entry */
4321 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004322 }
4323#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004324}
4325
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004326/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004327 * DTLS anti-replay: RFC 6347 4.1.2.6
4328 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004329 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4330 * Bit n is set iff record number in_window_top - n has been seen.
4331 *
4332 * Usually, in_window_top is the last record number seen and the lsb of
4333 * in_window is set. The only exception is the initial state (record number 0
4334 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004336#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4337static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004338{
4339 ssl->in_window_top = 0;
4340 ssl->in_window = 0;
4341}
4342
4343static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4344{
4345 return( ( (uint64_t) buf[0] << 40 ) |
4346 ( (uint64_t) buf[1] << 32 ) |
4347 ( (uint64_t) buf[2] << 24 ) |
4348 ( (uint64_t) buf[3] << 16 ) |
4349 ( (uint64_t) buf[4] << 8 ) |
4350 ( (uint64_t) buf[5] ) );
4351}
4352
4353/*
4354 * Return 0 if sequence number is acceptable, -1 otherwise
4355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004357{
4358 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4359 uint64_t bit;
4360
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004361 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004362 return( 0 );
4363
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004364 if( rec_seqnum > ssl->in_window_top )
4365 return( 0 );
4366
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004367 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004368
4369 if( bit >= 64 )
4370 return( -1 );
4371
4372 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4373 return( -1 );
4374
4375 return( 0 );
4376}
4377
4378/*
4379 * Update replay window on new validated record
4380 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004382{
4383 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4384
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004385 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004386 return;
4387
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004388 if( rec_seqnum > ssl->in_window_top )
4389 {
4390 /* Update window_top and the contents of the window */
4391 uint64_t shift = rec_seqnum - ssl->in_window_top;
4392
4393 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004394 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004395 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004396 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004397 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004398 ssl->in_window |= 1;
4399 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004400
4401 ssl->in_window_top = rec_seqnum;
4402 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004403 else
4404 {
4405 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004406 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004407
4408 if( bit < 64 ) /* Always true, but be extra sure */
4409 ssl->in_window |= (uint64_t) 1 << bit;
4410 }
4411}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004412#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004413
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004414#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004415/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004416static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4417
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004418/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004419 * Without any SSL context, check if a datagram looks like a ClientHello with
4420 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004421 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004422 *
4423 * - if cookie is valid, return 0
4424 * - if ClientHello looks superficially valid but cookie is not,
4425 * fill obuf and set olen, then
4426 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4427 * - otherwise return a specific error code
4428 */
4429static int ssl_check_dtls_clihlo_cookie(
4430 mbedtls_ssl_cookie_write_t *f_cookie_write,
4431 mbedtls_ssl_cookie_check_t *f_cookie_check,
4432 void *p_cookie,
4433 const unsigned char *cli_id, size_t cli_id_len,
4434 const unsigned char *in, size_t in_len,
4435 unsigned char *obuf, size_t buf_len, size_t *olen )
4436{
4437 size_t sid_len, cookie_len;
4438 unsigned char *p;
4439
4440 if( f_cookie_write == NULL || f_cookie_check == NULL )
4441 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4442
4443 /*
4444 * Structure of ClientHello with record and handshake headers,
4445 * and expected values. We don't need to check a lot, more checks will be
4446 * done when actually parsing the ClientHello - skipping those checks
4447 * avoids code duplication and does not make cookie forging any easier.
4448 *
4449 * 0-0 ContentType type; copied, must be handshake
4450 * 1-2 ProtocolVersion version; copied
4451 * 3-4 uint16 epoch; copied, must be 0
4452 * 5-10 uint48 sequence_number; copied
4453 * 11-12 uint16 length; (ignored)
4454 *
4455 * 13-13 HandshakeType msg_type; (ignored)
4456 * 14-16 uint24 length; (ignored)
4457 * 17-18 uint16 message_seq; copied
4458 * 19-21 uint24 fragment_offset; copied, must be 0
4459 * 22-24 uint24 fragment_length; (ignored)
4460 *
4461 * 25-26 ProtocolVersion client_version; (ignored)
4462 * 27-58 Random random; (ignored)
4463 * 59-xx SessionID session_id; 1 byte len + sid_len content
4464 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4465 * ...
4466 *
4467 * Minimum length is 61 bytes.
4468 */
4469 if( in_len < 61 ||
4470 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4471 in[3] != 0 || in[4] != 0 ||
4472 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4473 {
4474 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4475 }
4476
4477 sid_len = in[59];
4478 if( sid_len > in_len - 61 )
4479 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4480
4481 cookie_len = in[60 + sid_len];
4482 if( cookie_len > in_len - 60 )
4483 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4484
4485 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4486 cli_id, cli_id_len ) == 0 )
4487 {
4488 /* Valid cookie */
4489 return( 0 );
4490 }
4491
4492 /*
4493 * If we get here, we've got an invalid cookie, let's prepare HVR.
4494 *
4495 * 0-0 ContentType type; copied
4496 * 1-2 ProtocolVersion version; copied
4497 * 3-4 uint16 epoch; copied
4498 * 5-10 uint48 sequence_number; copied
4499 * 11-12 uint16 length; olen - 13
4500 *
4501 * 13-13 HandshakeType msg_type; hello_verify_request
4502 * 14-16 uint24 length; olen - 25
4503 * 17-18 uint16 message_seq; copied
4504 * 19-21 uint24 fragment_offset; copied
4505 * 22-24 uint24 fragment_length; olen - 25
4506 *
4507 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4508 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4509 *
4510 * Minimum length is 28.
4511 */
4512 if( buf_len < 28 )
4513 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4514
4515 /* Copy most fields and adapt others */
4516 memcpy( obuf, in, 25 );
4517 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4518 obuf[25] = 0xfe;
4519 obuf[26] = 0xff;
4520
4521 /* Generate and write actual cookie */
4522 p = obuf + 28;
4523 if( f_cookie_write( p_cookie,
4524 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4525 {
4526 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4527 }
4528
4529 *olen = p - obuf;
4530
4531 /* Go back and fill length fields */
4532 obuf[27] = (unsigned char)( *olen - 28 );
4533
4534 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4535 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4536 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4537
4538 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4539 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4540
4541 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4542}
4543
4544/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004545 * Handle possible client reconnect with the same UDP quadruplet
4546 * (RFC 6347 Section 4.2.8).
4547 *
4548 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4549 * that looks like a ClientHello.
4550 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004551 * - if the input looks like a ClientHello without cookies,
4552 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004553 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004554 * - if the input looks like a ClientHello with a valid cookie,
4555 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004556 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004557 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004558 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004559 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004560 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4561 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004562 */
4563static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4564{
4565 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004566 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004567
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004568 ret = ssl_check_dtls_clihlo_cookie(
4569 ssl->conf->f_cookie_write,
4570 ssl->conf->f_cookie_check,
4571 ssl->conf->p_cookie,
4572 ssl->cli_id, ssl->cli_id_len,
4573 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004574 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004575
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004576 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4577
4578 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004579 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004580 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004581 * If the error is permanent we'll catch it later,
4582 * if it's not, then hopefully it'll work next time. */
4583 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4584
4585 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004586 }
4587
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004588 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004589 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004590 /* Got a valid cookie, partially reset context */
4591 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4592 {
4593 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4594 return( ret );
4595 }
4596
4597 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004598 }
4599
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004600 return( ret );
4601}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004602#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004603
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004604/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004605 * ContentType type;
4606 * ProtocolVersion version;
4607 * uint16 epoch; // DTLS only
4608 * uint48 sequence_number; // DTLS only
4609 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004610 *
4611 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004612 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004613 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4614 *
4615 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004616 * 1. proceed with the record if this function returns 0
4617 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4618 * 3. return CLIENT_RECONNECT if this function return that value
4619 * 4. drop the whole datagram if this function returns anything else.
4620 * Point 2 is needed when the peer is resending, and we have already received
4621 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004623static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004624{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004625 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004627 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 +02004628
Paul Bakker5121ce52009-01-03 21:22:43 +00004629 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004630 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004631 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004633 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004634 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004635 ssl->in_msgtype,
4636 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004637
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004638 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004639 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4640 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4641 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4642 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004645
4646#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004647 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4648 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004649 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4650#endif /* MBEDTLS_SSL_PROTO_DTLS */
4651 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4652 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004654 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004655 }
4656
4657 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004658 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4661 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004662 }
4663
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004664 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4667 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004668 }
4669
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004670 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004671 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004672 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4675 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004676 }
4677
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004678 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004679 * DTLS-related tests.
4680 * Check epoch before checking length constraint because
4681 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4682 * message gets duplicated before the corresponding Finished message,
4683 * the second ChangeCipherSpec should be discarded because it belongs
4684 * to an old epoch, but not because its length is shorter than
4685 * the minimum record length for packets using the new record transform.
4686 * Note that these two kinds of failures are handled differently,
4687 * as an unexpected record is silently skipped but an invalid
4688 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004689 */
4690#if defined(MBEDTLS_SSL_PROTO_DTLS)
4691 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4692 {
4693 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4694
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004695 /* Check epoch (and sequence number) with DTLS */
4696 if( rec_epoch != ssl->in_epoch )
4697 {
4698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4699 "expected %d, received %d",
4700 ssl->in_epoch, rec_epoch ) );
4701
4702#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4703 /*
4704 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4705 * access the first byte of record content (handshake type), as we
4706 * have an active transform (possibly iv_len != 0), so use the
4707 * fact that the record header len is 13 instead.
4708 */
4709 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4710 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4711 rec_epoch == 0 &&
4712 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4713 ssl->in_left > 13 &&
4714 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4715 {
4716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4717 "from the same port" ) );
4718 return( ssl_handle_possible_reconnect( ssl ) );
4719 }
4720 else
4721#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004722 {
4723 /* Consider buffering the record. */
4724 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4725 {
4726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4727 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4728 }
4729
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004730 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004731 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004732 }
4733
4734#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4735 /* Replay detection only works for the current epoch */
4736 if( rec_epoch == ssl->in_epoch &&
4737 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4738 {
4739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4740 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4741 }
4742#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004743
Hanno Becker52c6dc62017-05-26 16:07:36 +01004744 /* Drop unexpected ApplicationData records,
4745 * except at the beginning of renegotiations */
4746 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4747 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4748#if defined(MBEDTLS_SSL_RENEGOTIATION)
4749 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4750 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4751#endif
4752 )
4753 {
4754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4755 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4756 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004757 }
4758#endif /* MBEDTLS_SSL_PROTO_DTLS */
4759
Hanno Becker52c6dc62017-05-26 16:07:36 +01004760
4761 /* Check length against bounds of the current transform and version */
4762 if( ssl->transform_in == NULL )
4763 {
4764 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004765 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004766 {
4767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4768 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4769 }
4770 }
4771 else
4772 {
4773 if( ssl->in_msglen < ssl->transform_in->minlen )
4774 {
4775 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4776 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4777 }
4778
4779#if defined(MBEDTLS_SSL_PROTO_SSL3)
4780 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004781 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004782 {
4783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4784 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4785 }
4786#endif
4787#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4788 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4789 /*
4790 * TLS encrypted messages can have up to 256 bytes of padding
4791 */
4792 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4793 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004794 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004795 {
4796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4797 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4798 }
4799#endif
4800 }
4801
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004802 return( 0 );
4803}
Paul Bakker5121ce52009-01-03 21:22:43 +00004804
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004805/*
4806 * If applicable, decrypt (and decompress) record content
4807 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004808static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004809{
4810 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004812 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4813 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4816 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820 ret = mbedtls_ssl_hw_record_read( ssl );
4821 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4824 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004825 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004826
4827 if( ret == 0 )
4828 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004829 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004830#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004831 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004832 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004833 mbedtls_record rec;
4834
4835 rec.buf = ssl->in_iv;
4836 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4837 - ( ssl->in_iv - ssl->in_buf );
4838 rec.data_len = ssl->in_msglen;
4839 rec.data_offset = 0;
4840
4841 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4842 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4843 ssl->conf->transport, rec.ver );
4844 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00004845 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4846 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004848 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004849 return( ret );
4850 }
4851
Hanno Becker29800d22018-08-07 14:30:18 +01004852 if( ssl->in_iv + rec.data_offset != ssl->in_msg )
4853 {
4854 /* Should never happen */
4855 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4856 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004857
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004858 ssl->in_msglen = rec.data_len;
4859 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4860 ssl->in_len[1] = (unsigned char)( rec.data_len );
4861
Hanno Becker1c0c37f2018-08-07 14:29:29 +01004862 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4863 ssl->in_msg, ssl->in_msglen );
4864
Angus Grattond8213d02016-05-25 20:56:48 +10004865 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4868 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004869 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004870 else if( ssl->in_msglen == 0 )
4871 {
4872#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4873 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4874 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4875 {
4876 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4878 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4879 }
4880#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4881
4882 ssl->nb_zero++;
4883
4884 /*
4885 * Three or more empty messages may be a DoS attack
4886 * (excessive CPU consumption).
4887 */
4888 if( ssl->nb_zero > 3 )
4889 {
4890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
4891 "messages, possible DoS attack" ) );
4892 /* Q: Is that the right error code? */
4893 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4894 }
4895 }
4896 else
4897 ssl->nb_zero = 0;
4898
4899#if defined(MBEDTLS_SSL_PROTO_DTLS)
4900 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4901 {
4902 ; /* in_ctr read from peer, not maintained internally */
4903 }
4904 else
4905#endif
4906 {
4907 unsigned i;
4908 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4909 if( ++ssl->in_ctr[i - 1] != 0 )
4910 break;
4911
4912 /* The loop goes to its end iff the counter is wrapping */
4913 if( i == ssl_ep_len( ssl ) )
4914 {
4915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4916 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4917 }
4918 }
4919
Paul Bakker5121ce52009-01-03 21:22:43 +00004920 }
4921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004922#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004923 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004924 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004925 {
4926 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004928 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004929 return( ret );
4930 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004931 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004932#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004935 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004937 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004938 }
4939#endif
4940
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004941 return( 0 );
4942}
4943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004945
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004946/*
4947 * Read a record.
4948 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004949 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4950 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4951 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004952 */
Hanno Becker1097b342018-08-15 14:09:41 +01004953
4954/* Helper functions for mbedtls_ssl_read_record(). */
4955static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004956static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4957static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004958
Hanno Becker327c93b2018-08-15 13:56:18 +01004959int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004960 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004961{
4962 int ret;
4963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004965
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004966 if( ssl->keep_current_message == 0 )
4967 {
4968 do {
Simon Butcher99000142016-10-13 17:21:01 +01004969
Hanno Becker26994592018-08-15 14:14:59 +01004970 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004971 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004972 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004973
Hanno Beckere74d5562018-08-15 14:26:08 +01004974 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004975 {
Hanno Becker40f50842018-08-15 14:48:01 +01004976#if defined(MBEDTLS_SSL_PROTO_DTLS)
4977 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004978
Hanno Becker40f50842018-08-15 14:48:01 +01004979 /* We only check for buffered messages if the
4980 * current datagram is fully consumed. */
4981 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004982 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004983 {
Hanno Becker40f50842018-08-15 14:48:01 +01004984 if( ssl_load_buffered_message( ssl ) == 0 )
4985 have_buffered = 1;
4986 }
4987
4988 if( have_buffered == 0 )
4989#endif /* MBEDTLS_SSL_PROTO_DTLS */
4990 {
4991 ret = ssl_get_next_record( ssl );
4992 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4993 continue;
4994
4995 if( ret != 0 )
4996 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004997 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004998 return( ret );
4999 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005000 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005001 }
5002
5003 ret = mbedtls_ssl_handle_message_type( ssl );
5004
Hanno Becker40f50842018-08-15 14:48:01 +01005005#if defined(MBEDTLS_SSL_PROTO_DTLS)
5006 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5007 {
5008 /* Buffer future message */
5009 ret = ssl_buffer_message( ssl );
5010 if( ret != 0 )
5011 return( ret );
5012
5013 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5014 }
5015#endif /* MBEDTLS_SSL_PROTO_DTLS */
5016
Hanno Becker90333da2017-10-10 11:27:13 +01005017 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5018 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005019
5020 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005021 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005022 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005023 return( ret );
5024 }
5025
Hanno Becker327c93b2018-08-15 13:56:18 +01005026 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005027 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005028 {
5029 mbedtls_ssl_update_handshake_status( ssl );
5030 }
Simon Butcher99000142016-10-13 17:21:01 +01005031 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005032 else
Simon Butcher99000142016-10-13 17:21:01 +01005033 {
Hanno Becker02f59072018-08-15 14:00:24 +01005034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005035 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005036 }
5037
5038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5039
5040 return( 0 );
5041}
5042
Hanno Becker40f50842018-08-15 14:48:01 +01005043#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005044static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005045{
Hanno Becker40f50842018-08-15 14:48:01 +01005046 if( ssl->in_left > ssl->next_record_offset )
5047 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005048
Hanno Becker40f50842018-08-15 14:48:01 +01005049 return( 0 );
5050}
5051
5052static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5053{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005054 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005055 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005056 int ret = 0;
5057
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005058 if( hs == NULL )
5059 return( -1 );
5060
Hanno Beckere00ae372018-08-20 09:39:42 +01005061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5062
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005063 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5064 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5065 {
5066 /* Check if we have seen a ChangeCipherSpec before.
5067 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005068 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005069 {
5070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5071 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005072 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005073 }
5074
Hanno Becker39b8bc92018-08-28 17:17:13 +01005075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005076 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5077 ssl->in_msglen = 1;
5078 ssl->in_msg[0] = 1;
5079
5080 /* As long as they are equal, the exact value doesn't matter. */
5081 ssl->in_left = 0;
5082 ssl->next_record_offset = 0;
5083
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005084 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005085 goto exit;
5086 }
Hanno Becker37f95322018-08-16 13:55:32 +01005087
Hanno Beckerb8f50142018-08-28 10:01:34 +01005088#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005089 /* Debug only */
5090 {
5091 unsigned offset;
5092 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5093 {
5094 hs_buf = &hs->buffering.hs[offset];
5095 if( hs_buf->is_valid == 1 )
5096 {
5097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5098 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005099 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005100 }
5101 }
5102 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005103#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005104
5105 /* Check if we have buffered and/or fully reassembled the
5106 * next handshake message. */
5107 hs_buf = &hs->buffering.hs[0];
5108 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5109 {
5110 /* Synthesize a record containing the buffered HS message. */
5111 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5112 ( hs_buf->data[2] << 8 ) |
5113 hs_buf->data[3];
5114
5115 /* Double-check that we haven't accidentally buffered
5116 * a message that doesn't fit into the input buffer. */
5117 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5118 {
5119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5120 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5121 }
5122
5123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5124 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5125 hs_buf->data, msg_len + 12 );
5126
5127 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5128 ssl->in_hslen = msg_len + 12;
5129 ssl->in_msglen = msg_len + 12;
5130 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5131
5132 ret = 0;
5133 goto exit;
5134 }
5135 else
5136 {
5137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5138 hs->in_msg_seq ) );
5139 }
5140
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005141 ret = -1;
5142
5143exit:
5144
5145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5146 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005147}
5148
Hanno Beckera02b0b42018-08-21 17:20:27 +01005149static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5150 size_t desired )
5151{
5152 int offset;
5153 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5155 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005156
Hanno Becker01315ea2018-08-21 17:22:17 +01005157 /* Get rid of future records epoch first, if such exist. */
5158 ssl_free_buffered_record( ssl );
5159
5160 /* Check if we have enough space available now. */
5161 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5162 hs->buffering.total_bytes_buffered ) )
5163 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005165 return( 0 );
5166 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005167
Hanno Becker4f432ad2018-08-28 10:02:32 +01005168 /* We don't have enough space to buffer the next expected handshake
5169 * message. Remove buffers used for future messages to gain space,
5170 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005171 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5172 offset >= 0; offset-- )
5173 {
5174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5175 offset ) );
5176
Hanno Beckerb309b922018-08-23 13:18:05 +01005177 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005178
5179 /* Check if we have enough space available now. */
5180 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5181 hs->buffering.total_bytes_buffered ) )
5182 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005183 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005184 return( 0 );
5185 }
5186 }
5187
5188 return( -1 );
5189}
5190
Hanno Becker40f50842018-08-15 14:48:01 +01005191static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5192{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005193 int ret = 0;
5194 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5195
5196 if( hs == NULL )
5197 return( 0 );
5198
5199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5200
5201 switch( ssl->in_msgtype )
5202 {
5203 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005205
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005206 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005207 break;
5208
5209 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005210 {
5211 unsigned recv_msg_seq_offset;
5212 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5213 mbedtls_ssl_hs_buffer *hs_buf;
5214 size_t msg_len = ssl->in_hslen - 12;
5215
5216 /* We should never receive an old handshake
5217 * message - double-check nonetheless. */
5218 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5219 {
5220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5221 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5222 }
5223
5224 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5225 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5226 {
5227 /* Silently ignore -- message too far in the future */
5228 MBEDTLS_SSL_DEBUG_MSG( 2,
5229 ( "Ignore future HS message with sequence number %u, "
5230 "buffering window %u - %u",
5231 recv_msg_seq, ssl->handshake->in_msg_seq,
5232 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5233
5234 goto exit;
5235 }
5236
5237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5238 recv_msg_seq, recv_msg_seq_offset ) );
5239
5240 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5241
5242 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005243 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005244 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005245 size_t reassembly_buf_sz;
5246
Hanno Becker37f95322018-08-16 13:55:32 +01005247 hs_buf->is_fragmented =
5248 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5249
5250 /* We copy the message back into the input buffer
5251 * after reassembly, so check that it's not too large.
5252 * This is an implementation-specific limitation
5253 * and not one from the standard, hence it is not
5254 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005255 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005256 {
5257 /* Ignore message */
5258 goto exit;
5259 }
5260
Hanno Beckere0b150f2018-08-21 15:51:03 +01005261 /* Check if we have enough space to buffer the message. */
5262 if( hs->buffering.total_bytes_buffered >
5263 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5264 {
5265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5266 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5267 }
5268
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005269 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5270 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005271
5272 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5273 hs->buffering.total_bytes_buffered ) )
5274 {
5275 if( recv_msg_seq_offset > 0 )
5276 {
5277 /* If we can't buffer a future message because
5278 * of space limitations -- ignore. */
5279 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",
5280 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5281 (unsigned) hs->buffering.total_bytes_buffered ) );
5282 goto exit;
5283 }
Hanno Beckere1801392018-08-21 16:51:05 +01005284 else
5285 {
5286 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",
5287 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5288 (unsigned) hs->buffering.total_bytes_buffered ) );
5289 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005290
Hanno Beckera02b0b42018-08-21 17:20:27 +01005291 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005292 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005293 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",
5294 (unsigned) msg_len,
5295 (unsigned) reassembly_buf_sz,
5296 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005297 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005298 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5299 goto exit;
5300 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005301 }
5302
5303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5304 msg_len ) );
5305
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005306 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5307 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005308 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005309 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005310 goto exit;
5311 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005312 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005313
5314 /* Prepare final header: copy msg_type, length and message_seq,
5315 * then add standardised fragment_offset and fragment_length */
5316 memcpy( hs_buf->data, ssl->in_msg, 6 );
5317 memset( hs_buf->data + 6, 0, 3 );
5318 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5319
5320 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005321
5322 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005323 }
5324 else
5325 {
5326 /* Make sure msg_type and length are consistent */
5327 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5328 {
5329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5330 /* Ignore */
5331 goto exit;
5332 }
5333 }
5334
Hanno Becker4422bbb2018-08-20 09:40:19 +01005335 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005336 {
5337 size_t frag_len, frag_off;
5338 unsigned char * const msg = hs_buf->data + 12;
5339
5340 /*
5341 * Check and copy current fragment
5342 */
5343
5344 /* Validation of header fields already done in
5345 * mbedtls_ssl_prepare_handshake_record(). */
5346 frag_off = ssl_get_hs_frag_off( ssl );
5347 frag_len = ssl_get_hs_frag_len( ssl );
5348
5349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5350 frag_off, frag_len ) );
5351 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5352
5353 if( hs_buf->is_fragmented )
5354 {
5355 unsigned char * const bitmask = msg + msg_len;
5356 ssl_bitmask_set( bitmask, frag_off, frag_len );
5357 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5358 msg_len ) == 0 );
5359 }
5360 else
5361 {
5362 hs_buf->is_complete = 1;
5363 }
5364
5365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5366 hs_buf->is_complete ? "" : "not yet " ) );
5367 }
5368
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005369 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005370 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005371
5372 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005373 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005374 break;
5375 }
5376
5377exit:
5378
5379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5380 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005381}
5382#endif /* MBEDTLS_SSL_PROTO_DTLS */
5383
Hanno Becker1097b342018-08-15 14:09:41 +01005384static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005385{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005386 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005387 * Consume last content-layer message and potentially
5388 * update in_msglen which keeps track of the contents'
5389 * consumption state.
5390 *
5391 * (1) Handshake messages:
5392 * Remove last handshake message, move content
5393 * and adapt in_msglen.
5394 *
5395 * (2) Alert messages:
5396 * Consume whole record content, in_msglen = 0.
5397 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005398 * (3) Change cipher spec:
5399 * Consume whole record content, in_msglen = 0.
5400 *
5401 * (4) Application data:
5402 * Don't do anything - the record layer provides
5403 * the application data as a stream transport
5404 * and consumes through mbedtls_ssl_read only.
5405 *
5406 */
5407
5408 /* Case (1): Handshake messages */
5409 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005410 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005411 /* Hard assertion to be sure that no application data
5412 * is in flight, as corrupting ssl->in_msglen during
5413 * ssl->in_offt != NULL is fatal. */
5414 if( ssl->in_offt != NULL )
5415 {
5416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5417 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5418 }
5419
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005420 /*
5421 * Get next Handshake message in the current record
5422 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005423
Hanno Becker4a810fb2017-05-24 16:27:30 +01005424 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005425 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005426 * current handshake content: If DTLS handshake
5427 * fragmentation is used, that's the fragment
5428 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005429 * size here is faulty and should be changed at
5430 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005431 * (2) While it doesn't seem to cause problems, one
5432 * has to be very careful not to assume that in_hslen
5433 * is always <= in_msglen in a sensible communication.
5434 * Again, it's wrong for DTLS handshake fragmentation.
5435 * The following check is therefore mandatory, and
5436 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005437 * Additionally, ssl->in_hslen might be arbitrarily out of
5438 * bounds after handling a DTLS message with an unexpected
5439 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005440 */
5441 if( ssl->in_hslen < ssl->in_msglen )
5442 {
5443 ssl->in_msglen -= ssl->in_hslen;
5444 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5445 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005446
Hanno Becker4a810fb2017-05-24 16:27:30 +01005447 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5448 ssl->in_msg, ssl->in_msglen );
5449 }
5450 else
5451 {
5452 ssl->in_msglen = 0;
5453 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005454
Hanno Becker4a810fb2017-05-24 16:27:30 +01005455 ssl->in_hslen = 0;
5456 }
5457 /* Case (4): Application data */
5458 else if( ssl->in_offt != NULL )
5459 {
5460 return( 0 );
5461 }
5462 /* Everything else (CCS & Alerts) */
5463 else
5464 {
5465 ssl->in_msglen = 0;
5466 }
5467
Hanno Becker1097b342018-08-15 14:09:41 +01005468 return( 0 );
5469}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005470
Hanno Beckere74d5562018-08-15 14:26:08 +01005471static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5472{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005473 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005474 return( 1 );
5475
5476 return( 0 );
5477}
5478
Hanno Becker5f066e72018-08-16 14:56:31 +01005479#if defined(MBEDTLS_SSL_PROTO_DTLS)
5480
5481static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5482{
5483 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5484 if( hs == NULL )
5485 return;
5486
Hanno Becker01315ea2018-08-21 17:22:17 +01005487 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005488 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005489 hs->buffering.total_bytes_buffered -=
5490 hs->buffering.future_record.len;
5491
5492 mbedtls_free( hs->buffering.future_record.data );
5493 hs->buffering.future_record.data = NULL;
5494 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005495}
5496
5497static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5498{
5499 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5500 unsigned char * rec;
5501 size_t rec_len;
5502 unsigned rec_epoch;
5503
5504 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5505 return( 0 );
5506
5507 if( hs == NULL )
5508 return( 0 );
5509
Hanno Becker5f066e72018-08-16 14:56:31 +01005510 rec = hs->buffering.future_record.data;
5511 rec_len = hs->buffering.future_record.len;
5512 rec_epoch = hs->buffering.future_record.epoch;
5513
5514 if( rec == NULL )
5515 return( 0 );
5516
Hanno Becker4cb782d2018-08-20 11:19:05 +01005517 /* Only consider loading future records if the
5518 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005519 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005520 return( 0 );
5521
Hanno Becker5f066e72018-08-16 14:56:31 +01005522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5523
5524 if( rec_epoch != ssl->in_epoch )
5525 {
5526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5527 goto exit;
5528 }
5529
5530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5531
5532 /* Double-check that the record is not too large */
5533 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5534 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5535 {
5536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5537 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5538 }
5539
5540 memcpy( ssl->in_hdr, rec, rec_len );
5541 ssl->in_left = rec_len;
5542 ssl->next_record_offset = 0;
5543
5544 ssl_free_buffered_record( ssl );
5545
5546exit:
5547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5548 return( 0 );
5549}
5550
5551static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5552{
5553 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5554 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005555 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005556
5557 /* Don't buffer future records outside handshakes. */
5558 if( hs == NULL )
5559 return( 0 );
5560
5561 /* Only buffer handshake records (we are only interested
5562 * in Finished messages). */
5563 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5564 return( 0 );
5565
5566 /* Don't buffer more than one future epoch record. */
5567 if( hs->buffering.future_record.data != NULL )
5568 return( 0 );
5569
Hanno Becker01315ea2018-08-21 17:22:17 +01005570 /* Don't buffer record if there's not enough buffering space remaining. */
5571 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5572 hs->buffering.total_bytes_buffered ) )
5573 {
5574 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",
5575 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5576 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005577 return( 0 );
5578 }
5579
Hanno Becker5f066e72018-08-16 14:56:31 +01005580 /* Buffer record */
5581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5582 ssl->in_epoch + 1 ) );
5583 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5584 rec_hdr_len + ssl->in_msglen );
5585
5586 /* ssl_parse_record_header() only considers records
5587 * of the next epoch as candidates for buffering. */
5588 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005589 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005590
5591 hs->buffering.future_record.data =
5592 mbedtls_calloc( 1, hs->buffering.future_record.len );
5593 if( hs->buffering.future_record.data == NULL )
5594 {
5595 /* If we run out of RAM trying to buffer a
5596 * record from the next epoch, just ignore. */
5597 return( 0 );
5598 }
5599
Hanno Becker01315ea2018-08-21 17:22:17 +01005600 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005601
Hanno Becker01315ea2018-08-21 17:22:17 +01005602 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005603 return( 0 );
5604}
5605
5606#endif /* MBEDTLS_SSL_PROTO_DTLS */
5607
Hanno Beckere74d5562018-08-15 14:26:08 +01005608static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005609{
5610 int ret;
5611
Hanno Becker5f066e72018-08-16 14:56:31 +01005612#if defined(MBEDTLS_SSL_PROTO_DTLS)
5613 /* We might have buffered a future record; if so,
5614 * and if the epoch matches now, load it.
5615 * On success, this call will set ssl->in_left to
5616 * the length of the buffered record, so that
5617 * the calls to ssl_fetch_input() below will
5618 * essentially be no-ops. */
5619 ret = ssl_load_buffered_record( ssl );
5620 if( ret != 0 )
5621 return( ret );
5622#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005624 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005626 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005627 return( ret );
5628 }
5629
5630 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005633 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5634 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005635 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005636 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5637 {
5638 ret = ssl_buffer_future_record( ssl );
5639 if( ret != 0 )
5640 return( ret );
5641
5642 /* Fall through to handling of unexpected records */
5643 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5644 }
5645
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005646 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5647 {
5648 /* Skip unexpected record (but not whole datagram) */
5649 ssl->next_record_offset = ssl->in_msglen
5650 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005651
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5653 "(header)" ) );
5654 }
5655 else
5656 {
5657 /* Skip invalid record and the rest of the datagram */
5658 ssl->next_record_offset = 0;
5659 ssl->in_left = 0;
5660
5661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5662 "(header)" ) );
5663 }
5664
5665 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005666 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005667 }
5668#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005669 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005670 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005671
5672 /*
5673 * Read and optionally decrypt the message contents
5674 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005675 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5676 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005678 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005679 return( ret );
5680 }
5681
5682 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005683#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005684 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005686 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005687 if( ssl->next_record_offset < ssl->in_left )
5688 {
5689 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5690 }
5691 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005692 else
5693#endif
5694 ssl->in_left = 0;
5695
5696 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005698#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005699 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005700 {
5701 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005702 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5703 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005704 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005705 /* Except when waiting for Finished as a bad mac here
5706 * probably means something went wrong in the handshake
5707 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5708 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5709 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5710 {
5711#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5712 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5713 {
5714 mbedtls_ssl_send_alert_message( ssl,
5715 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5716 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5717 }
5718#endif
5719 return( ret );
5720 }
5721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005723 if( ssl->conf->badmac_limit != 0 &&
5724 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5727 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005728 }
5729#endif
5730
Hanno Becker4a810fb2017-05-24 16:27:30 +01005731 /* As above, invalid records cause
5732 * dismissal of the whole datagram. */
5733
5734 ssl->next_record_offset = 0;
5735 ssl->in_left = 0;
5736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005738 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005739 }
5740
5741 return( ret );
5742 }
5743 else
5744#endif
5745 {
5746 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005747#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5748 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005750 mbedtls_ssl_send_alert_message( ssl,
5751 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5752 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005753 }
5754#endif
5755 return( ret );
5756 }
5757 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005758
Simon Butcher99000142016-10-13 17:21:01 +01005759 return( 0 );
5760}
5761
5762int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5763{
5764 int ret;
5765
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005766 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005767 * Handle particular types of records
5768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005770 {
Simon Butcher99000142016-10-13 17:21:01 +01005771 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5772 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005773 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005774 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005775 }
5776
Hanno Beckere678eaa2018-08-21 14:57:46 +01005777 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005778 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005779 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005780 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5782 ssl->in_msglen ) );
5783 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005784 }
5785
Hanno Beckere678eaa2018-08-21 14:57:46 +01005786 if( ssl->in_msg[0] != 1 )
5787 {
5788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5789 ssl->in_msg[0] ) );
5790 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5791 }
5792
5793#if defined(MBEDTLS_SSL_PROTO_DTLS)
5794 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5795 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5796 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5797 {
5798 if( ssl->handshake == NULL )
5799 {
5800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5801 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5802 }
5803
5804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5805 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5806 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005807#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005808 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005810 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005811 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005812 if( ssl->in_msglen != 2 )
5813 {
5814 /* Note: Standard allows for more than one 2 byte alert
5815 to be packed in a single message, but Mbed TLS doesn't
5816 currently support this. */
5817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5818 ssl->in_msglen ) );
5819 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5820 }
5821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005823 ssl->in_msg[0], ssl->in_msg[1] ) );
5824
5825 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005826 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005828 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005831 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005832 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005833 }
5834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005835 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5836 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5839 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005840 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005841
5842#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5843 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5844 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5845 {
Hanno Becker90333da2017-10-10 11:27:13 +01005846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005847 /* Will be handled when trying to parse ServerHello */
5848 return( 0 );
5849 }
5850#endif
5851
5852#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5853 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5854 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5855 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5856 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5857 {
5858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5859 /* Will be handled in mbedtls_ssl_parse_certificate() */
5860 return( 0 );
5861 }
5862#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5863
5864 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005865 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005866 }
5867
Hanno Beckerc76c6192017-06-06 10:03:17 +01005868#if defined(MBEDTLS_SSL_PROTO_DTLS)
5869 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5870 ssl->handshake != NULL &&
5871 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5872 {
5873 ssl_handshake_wrapup_free_hs_transform( ssl );
5874 }
5875#endif
5876
Paul Bakker5121ce52009-01-03 21:22:43 +00005877 return( 0 );
5878}
5879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005880int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005881{
5882 int ret;
5883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005884 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5885 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5886 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005887 {
5888 return( ret );
5889 }
5890
5891 return( 0 );
5892}
5893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005894int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005895 unsigned char level,
5896 unsigned char message )
5897{
5898 int ret;
5899
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005900 if( ssl == NULL || ssl->conf == NULL )
5901 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005906 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005907 ssl->out_msglen = 2;
5908 ssl->out_msg[0] = level;
5909 ssl->out_msg[1] = message;
5910
Hanno Becker67bc7c32018-08-06 11:33:50 +01005911 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005913 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005914 return( ret );
5915 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005917
5918 return( 0 );
5919}
5920
Hanno Beckerb9d44792019-02-08 07:19:04 +00005921#if defined(MBEDTLS_X509_CRT_PARSE_C)
5922static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5923{
5924#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5925 if( session->peer_cert != NULL )
5926 {
5927 mbedtls_x509_crt_free( session->peer_cert );
5928 mbedtls_free( session->peer_cert );
5929 session->peer_cert = NULL;
5930 }
5931#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5932 if( session->peer_cert_digest != NULL )
5933 {
5934 /* Zeroization is not necessary. */
5935 mbedtls_free( session->peer_cert_digest );
5936 session->peer_cert_digest = NULL;
5937 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
5938 session->peer_cert_digest_len = 0;
5939 }
5940#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5941}
5942#endif /* MBEDTLS_X509_CRT_PARSE_C */
5943
Paul Bakker5121ce52009-01-03 21:22:43 +00005944/*
5945 * Handshake functions
5946 */
Hanno Becker21489932019-02-05 13:20:55 +00005947#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005948/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005949int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005950{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005951 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5952 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005955
Hanno Becker7177a882019-02-05 13:36:46 +00005956 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005959 ssl->state++;
5960 return( 0 );
5961 }
5962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5964 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005965}
5966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005967int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005968{
Hanno Beckere694c3e2017-12-27 21:34:08 +00005969 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5970 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005973
Hanno Becker7177a882019-02-05 13:36:46 +00005974 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005977 ssl->state++;
5978 return( 0 );
5979 }
5980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5982 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005983}
Gilles Peskinef9828522017-05-03 12:28:43 +02005984
Hanno Becker21489932019-02-05 13:20:55 +00005985#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02005986/* Some certificate support -> implement write and parse */
5987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005989{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005990 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005991 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005992 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00005993 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
5994 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005997
Hanno Becker7177a882019-02-05 13:36:46 +00005998 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006001 ssl->state++;
6002 return( 0 );
6003 }
6004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006006 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006007 {
6008 if( ssl->client_auth == 0 )
6009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006011 ssl->state++;
6012 return( 0 );
6013 }
6014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006016 /*
6017 * If using SSLv3 and got no cert, send an Alert message
6018 * (otherwise an empty Certificate message will be sent).
6019 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6021 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006022 {
6023 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006024 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6025 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6026 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006029 goto write_msg;
6030 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006032 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033#endif /* MBEDTLS_SSL_CLI_C */
6034#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006035 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006037 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6040 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006041 }
6042 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006045 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006046
6047 /*
6048 * 0 . 0 handshake type
6049 * 1 . 3 handshake length
6050 * 4 . 6 length of all certs
6051 * 7 . 9 length of cert. 1
6052 * 10 . n-1 peer certificate
6053 * n . n+2 length of cert. 2
6054 * n+3 . ... upper level cert, etc.
6055 */
6056 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006057 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006058
Paul Bakker29087132010-03-21 21:03:34 +00006059 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006060 {
6061 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006062 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006065 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006067 }
6068
6069 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6070 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6071 ssl->out_msg[i + 2] = (unsigned char)( n );
6072
6073 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6074 i += n; crt = crt->next;
6075 }
6076
6077 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6078 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6079 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6080
6081 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006082 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6083 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006084
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006085#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006086write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006087#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006088
6089 ssl->state++;
6090
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006091 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006092 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006093 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006094 return( ret );
6095 }
6096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006098
Paul Bakkered27a042013-04-18 22:46:23 +02006099 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006100}
6101
Hanno Becker84879e32019-01-31 07:44:03 +00006102#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006103
6104#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006105static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6106 unsigned char *crt_buf,
6107 size_t crt_buf_len )
6108{
6109 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6110
6111 if( peer_crt == NULL )
6112 return( -1 );
6113
6114 if( peer_crt->raw.len != crt_buf_len )
6115 return( -1 );
6116
Hanno Becker46f34d02019-02-08 14:00:04 +00006117 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006118}
Hanno Becker177475a2019-02-05 17:02:46 +00006119#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6120static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6121 unsigned char *crt_buf,
6122 size_t crt_buf_len )
6123{
6124 int ret;
6125 unsigned char const * const peer_cert_digest =
6126 ssl->session->peer_cert_digest;
6127 mbedtls_md_type_t const peer_cert_digest_type =
6128 ssl->session->peer_cert_digest_type;
6129 mbedtls_md_info_t const * const digest_info =
6130 mbedtls_md_info_from_type( peer_cert_digest_type );
6131 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6132 size_t digest_len;
6133
6134 if( peer_cert_digest == NULL || digest_info == NULL )
6135 return( -1 );
6136
6137 digest_len = mbedtls_md_get_size( digest_info );
6138 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6139 return( -1 );
6140
6141 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6142 if( ret != 0 )
6143 return( -1 );
6144
6145 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6146}
6147#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006148#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006149
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006150/*
6151 * Once the certificate message is read, parse it into a cert chain and
6152 * perform basic checks, but leave actual verification to the caller
6153 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006154static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6155 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006156{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006157 int ret;
6158#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6159 int crt_cnt=0;
6160#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006161 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006162 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006164 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006167 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6168 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006169 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006170 }
6171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006172 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6173 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006176 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6177 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006178 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006179 }
6180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006181 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006182
Paul Bakker5121ce52009-01-03 21:22:43 +00006183 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006184 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006185 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006186 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006187
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006188 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006189 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006192 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6193 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006194 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006195 }
6196
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006197 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6198 i += 3;
6199
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006200 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006201 while( i < ssl->in_hslen )
6202 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006203 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006204 if ( i + 3 > ssl->in_hslen ) {
6205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006206 mbedtls_ssl_send_alert_message( ssl,
6207 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6208 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006209 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6210 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006211 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6212 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006213 if( ssl->in_msg[i] != 0 )
6214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006216 mbedtls_ssl_send_alert_message( ssl,
6217 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6218 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006219 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006220 }
6221
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006222 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006223 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6224 | (unsigned int) ssl->in_msg[i + 2];
6225 i += 3;
6226
6227 if( n < 128 || i + n > ssl->in_hslen )
6228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006230 mbedtls_ssl_send_alert_message( ssl,
6231 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6232 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006233 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006234 }
6235
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006236 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006237#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6238 if( crt_cnt++ == 0 &&
6239 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6240 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006241 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006242 /* During client-side renegotiation, check that the server's
6243 * end-CRTs hasn't changed compared to the initial handshake,
6244 * mitigating the triple handshake attack. On success, reuse
6245 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006246 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6247 if( ssl_check_peer_crt_unchanged( ssl,
6248 &ssl->in_msg[i],
6249 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006250 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6252 mbedtls_ssl_send_alert_message( ssl,
6253 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6254 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6255 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006256 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006257
6258 /* Now we can safely free the original chain. */
6259 ssl_clear_peer_cert( ssl->session );
6260 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006261#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6262
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006263 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006264#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006265 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006266#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006267 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006268 * it in-place from the input buffer instead of making a copy. */
6269 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6270#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006271 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006272 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006273 case 0: /*ok*/
6274 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6275 /* Ignore certificate with an unknown algorithm: maybe a
6276 prior certificate was already trusted. */
6277 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006278
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006279 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6280 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6281 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006282
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006283 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6284 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6285 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006286
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006287 default:
6288 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6289 crt_parse_der_failed:
6290 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6291 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6292 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006293 }
6294
6295 i += n;
6296 }
6297
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006298 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006299 return( 0 );
6300}
6301
Hanno Becker4a55f632019-02-05 12:49:06 +00006302#if defined(MBEDTLS_SSL_SRV_C)
6303static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6304{
6305 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6306 return( -1 );
6307
6308#if defined(MBEDTLS_SSL_PROTO_SSL3)
6309 /*
6310 * Check if the client sent an empty certificate
6311 */
6312 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6313 {
6314 if( ssl->in_msglen == 2 &&
6315 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6316 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6317 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6318 {
6319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6320 return( 0 );
6321 }
6322
6323 return( -1 );
6324 }
6325#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6326
6327#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6328 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6329 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6330 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6331 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6332 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6333 {
6334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6335 return( 0 );
6336 }
6337
6338 return( -1 );
6339#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6340 MBEDTLS_SSL_PROTO_TLS1_2 */
6341}
6342#endif /* MBEDTLS_SSL_SRV_C */
6343
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006344/* Check if a certificate message is expected.
6345 * Return either
6346 * - SSL_CERTIFICATE_EXPECTED, or
6347 * - SSL_CERTIFICATE_SKIP
6348 * indicating whether a Certificate message is expected or not.
6349 */
6350#define SSL_CERTIFICATE_EXPECTED 0
6351#define SSL_CERTIFICATE_SKIP 1
6352static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6353 int authmode )
6354{
6355 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006356 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006357
6358 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6359 return( SSL_CERTIFICATE_SKIP );
6360
6361#if defined(MBEDTLS_SSL_SRV_C)
6362 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6363 {
6364 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6365 return( SSL_CERTIFICATE_SKIP );
6366
6367 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6368 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006369 ssl->session_negotiate->verify_result =
6370 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6371 return( SSL_CERTIFICATE_SKIP );
6372 }
6373 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006374#else
6375 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006376#endif /* MBEDTLS_SSL_SRV_C */
6377
6378 return( SSL_CERTIFICATE_EXPECTED );
6379}
6380
Hanno Becker68636192019-02-05 14:36:34 +00006381static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6382 int authmode,
6383 mbedtls_x509_crt *chain,
6384 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006385{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006386 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006387 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006388 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006389 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006390
Hanno Becker8927c832019-04-03 12:52:50 +01006391 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6392 void *p_vrfy;
6393
Hanno Becker68636192019-02-05 14:36:34 +00006394 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6395 return( 0 );
6396
Hanno Becker8927c832019-04-03 12:52:50 +01006397 if( ssl->f_vrfy != NULL )
6398 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006399 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006400 f_vrfy = ssl->f_vrfy;
6401 p_vrfy = ssl->p_vrfy;
6402 }
6403 else
6404 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006405 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006406 f_vrfy = ssl->conf->f_vrfy;
6407 p_vrfy = ssl->conf->p_vrfy;
6408 }
6409
Hanno Becker68636192019-02-05 14:36:34 +00006410 /*
6411 * Main check: verify certificate
6412 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006413#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6414 if( ssl->conf->f_ca_cb != NULL )
6415 {
6416 ((void) rs_ctx);
6417 have_ca_chain = 1;
6418
6419 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006420 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006421 chain,
6422 ssl->conf->f_ca_cb,
6423 ssl->conf->p_ca_cb,
6424 ssl->conf->cert_profile,
6425 ssl->hostname,
6426 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006427 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006428 }
6429 else
6430#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6431 {
6432 mbedtls_x509_crt *ca_chain;
6433 mbedtls_x509_crl *ca_crl;
6434
6435#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6436 if( ssl->handshake->sni_ca_chain != NULL )
6437 {
6438 ca_chain = ssl->handshake->sni_ca_chain;
6439 ca_crl = ssl->handshake->sni_ca_crl;
6440 }
6441 else
6442#endif
6443 {
6444 ca_chain = ssl->conf->ca_chain;
6445 ca_crl = ssl->conf->ca_crl;
6446 }
6447
6448 if( ca_chain != NULL )
6449 have_ca_chain = 1;
6450
6451 ret = mbedtls_x509_crt_verify_restartable(
6452 chain,
6453 ca_chain, ca_crl,
6454 ssl->conf->cert_profile,
6455 ssl->hostname,
6456 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006457 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006458 }
Hanno Becker68636192019-02-05 14:36:34 +00006459
6460 if( ret != 0 )
6461 {
6462 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6463 }
6464
6465#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6466 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6467 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6468#endif
6469
6470 /*
6471 * Secondary checks: always done, but change 'ret' only if it was 0
6472 */
6473
6474#if defined(MBEDTLS_ECP_C)
6475 {
6476 const mbedtls_pk_context *pk = &chain->pk;
6477
6478 /* If certificate uses an EC key, make sure the curve is OK */
6479 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6480 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6481 {
6482 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6483
6484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6485 if( ret == 0 )
6486 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6487 }
6488 }
6489#endif /* MBEDTLS_ECP_C */
6490
6491 if( mbedtls_ssl_check_cert_usage( chain,
6492 ciphersuite_info,
6493 ! ssl->conf->endpoint,
6494 &ssl->session_negotiate->verify_result ) != 0 )
6495 {
6496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6497 if( ret == 0 )
6498 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6499 }
6500
6501 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6502 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6503 * with details encoded in the verification flags. All other kinds
6504 * of error codes, including those from the user provided f_vrfy
6505 * functions, are treated as fatal and lead to a failure of
6506 * ssl_parse_certificate even if verification was optional. */
6507 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6508 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6509 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6510 {
6511 ret = 0;
6512 }
6513
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006514 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006515 {
6516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6517 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6518 }
6519
6520 if( ret != 0 )
6521 {
6522 uint8_t alert;
6523
6524 /* The certificate may have been rejected for several reasons.
6525 Pick one and send the corresponding alert. Which alert to send
6526 may be a subject of debate in some cases. */
6527 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6528 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6529 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6530 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6531 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6532 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6533 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6534 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6535 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6536 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6537 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6538 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6539 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6540 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6541 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6542 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6543 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6544 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6545 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6546 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6547 else
6548 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6549 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6550 alert );
6551 }
6552
6553#if defined(MBEDTLS_DEBUG_C)
6554 if( ssl->session_negotiate->verify_result != 0 )
6555 {
6556 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6557 ssl->session_negotiate->verify_result ) );
6558 }
6559 else
6560 {
6561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6562 }
6563#endif /* MBEDTLS_DEBUG_C */
6564
6565 return( ret );
6566}
6567
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006568#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6569static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6570 unsigned char *start, size_t len )
6571{
6572 int ret;
6573 /* Remember digest of the peer's end-CRT. */
6574 ssl->session_negotiate->peer_cert_digest =
6575 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6576 if( ssl->session_negotiate->peer_cert_digest == NULL )
6577 {
6578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6579 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6580 mbedtls_ssl_send_alert_message( ssl,
6581 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6582 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6583
6584 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6585 }
6586
6587 ret = mbedtls_md( mbedtls_md_info_from_type(
6588 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6589 start, len,
6590 ssl->session_negotiate->peer_cert_digest );
6591
6592 ssl->session_negotiate->peer_cert_digest_type =
6593 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6594 ssl->session_negotiate->peer_cert_digest_len =
6595 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6596
6597 return( ret );
6598}
6599
6600static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6601 unsigned char *start, size_t len )
6602{
6603 unsigned char *end = start + len;
6604 int ret;
6605
6606 /* Make a copy of the peer's raw public key. */
6607 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6608 ret = mbedtls_pk_parse_subpubkey( &start, end,
6609 &ssl->handshake->peer_pubkey );
6610 if( ret != 0 )
6611 {
6612 /* We should have parsed the public key before. */
6613 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6614 }
6615
6616 return( 0 );
6617}
6618#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6619
Hanno Becker68636192019-02-05 14:36:34 +00006620int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6621{
6622 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006623 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006624#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6625 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6626 ? ssl->handshake->sni_authmode
6627 : ssl->conf->authmode;
6628#else
6629 const int authmode = ssl->conf->authmode;
6630#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006631 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006632 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006633
6634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6635
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006636 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6637 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006638 {
6639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006640 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006641 }
6642
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006643#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6644 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006645 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006646 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006647 chain = ssl->handshake->ecrs_peer_cert;
6648 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006649 goto crt_verify;
6650 }
6651#endif
6652
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006653 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006654 {
6655 /* mbedtls_ssl_read_record may have sent an alert already. We
6656 let it decide whether to alert. */
6657 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006658 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006659 }
6660
Hanno Becker4a55f632019-02-05 12:49:06 +00006661#if defined(MBEDTLS_SSL_SRV_C)
6662 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6663 {
6664 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006665
6666 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006667 ret = 0;
6668 else
6669 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006670
Hanno Becker6bdfab22019-02-05 13:11:17 +00006671 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006672 }
6673#endif /* MBEDTLS_SSL_SRV_C */
6674
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006675 /* Clear existing peer CRT structure in case we tried to
6676 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006677 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006678
6679 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6680 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006681 {
6682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6683 sizeof( mbedtls_x509_crt ) ) );
6684 mbedtls_ssl_send_alert_message( ssl,
6685 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6686 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006687
Hanno Becker3dad3112019-02-05 17:19:52 +00006688 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6689 goto exit;
6690 }
6691 mbedtls_x509_crt_init( chain );
6692
6693 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006694 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006695 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006696
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006697#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6698 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006699 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006700
6701crt_verify:
6702 if( ssl->handshake->ecrs_enabled)
6703 rs_ctx = &ssl->handshake->ecrs_ctx;
6704#endif
6705
Hanno Becker68636192019-02-05 14:36:34 +00006706 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006707 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006708 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006709 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006710
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006711#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006712 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006713 unsigned char *crt_start, *pk_start;
6714 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006715
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006716 /* We parse the CRT chain without copying, so
6717 * these pointers point into the input buffer,
6718 * and are hence still valid after freeing the
6719 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006720
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006721 crt_start = chain->raw.p;
6722 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006723
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006724 pk_start = chain->pk_raw.p;
6725 pk_len = chain->pk_raw.len;
6726
6727 /* Free the CRT structures before computing
6728 * digest and copying the peer's public key. */
6729 mbedtls_x509_crt_free( chain );
6730 mbedtls_free( chain );
6731 chain = NULL;
6732
6733 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006734 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006735 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006736
6737 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6738 if( ret != 0 )
6739 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006740 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006741#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6742 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006743 ssl->session_negotiate->peer_cert = chain;
6744 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006745#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006748
Hanno Becker6bdfab22019-02-05 13:11:17 +00006749exit:
6750
Hanno Becker3dad3112019-02-05 17:19:52 +00006751 if( ret == 0 )
6752 ssl->state++;
6753
6754#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6755 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6756 {
6757 ssl->handshake->ecrs_peer_cert = chain;
6758 chain = NULL;
6759 }
6760#endif
6761
6762 if( chain != NULL )
6763 {
6764 mbedtls_x509_crt_free( chain );
6765 mbedtls_free( chain );
6766 }
6767
Paul Bakker5121ce52009-01-03 21:22:43 +00006768 return( ret );
6769}
Hanno Becker21489932019-02-05 13:20:55 +00006770#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006773{
6774 int ret;
6775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006779 ssl->out_msglen = 1;
6780 ssl->out_msg[0] = 1;
6781
Paul Bakker5121ce52009-01-03 21:22:43 +00006782 ssl->state++;
6783
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006784 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006785 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006786 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006787 return( ret );
6788 }
6789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006791
6792 return( 0 );
6793}
6794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006796{
6797 int ret;
6798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006800
Hanno Becker327c93b2018-08-15 13:56:18 +01006801 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006804 return( ret );
6805 }
6806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006810 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6811 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006813 }
6814
Hanno Beckere678eaa2018-08-21 14:57:46 +01006815 /* CCS records are only accepted if they have length 1 and content '1',
6816 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006817
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006818 /*
6819 * Switch to our negotiated transform and session parameters for inbound
6820 * data.
6821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006822 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006823 ssl->transform_in = ssl->transform_negotiate;
6824 ssl->session_in = ssl->session_negotiate;
6825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006827 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006829#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006830 ssl_dtls_replay_reset( ssl );
6831#endif
6832
6833 /* Increment epoch */
6834 if( ++ssl->in_epoch == 0 )
6835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006837 /* This is highly unlikely to happen for legitimate reasons, so
6838 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006840 }
6841 }
6842 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006844 memset( ssl->in_ctr, 0, 8 );
6845
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006846 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006848#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6849 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006853 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006854 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6855 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006856 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006857 }
6858 }
6859#endif
6860
Paul Bakker5121ce52009-01-03 21:22:43 +00006861 ssl->state++;
6862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006864
6865 return( 0 );
6866}
6867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006868void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6869 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006870{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006871 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006873#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6874 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6875 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006876 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006877 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006878#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006879#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6880#if defined(MBEDTLS_SHA512_C)
6881 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006882 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6883 else
6884#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885#if defined(MBEDTLS_SHA256_C)
6886 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006887 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006888 else
6889#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006893 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006894 }
Paul Bakker380da532012-04-18 16:10:25 +00006895}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006897void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006898{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006899#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6900 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006901 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6902 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006903#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6905#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006906#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006907 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006908 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
6909#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006910 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006911#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006912#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006913#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006914#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05006915 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05006916 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006917#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006918 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006919#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006920#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006921#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006922}
6923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006925 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006926{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006927#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6928 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006929 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6930 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006931#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6933#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006934#if defined(MBEDTLS_USE_PSA_CRYPTO)
6935 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6936#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006937 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006938#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006939#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006940#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006941#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006942 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006943#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006944 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006945#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006946#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006948}
6949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6951 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6952static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006953 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006954{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006955 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6956 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006957}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006958#endif
Paul Bakker380da532012-04-18 16:10:25 +00006959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006960#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6961#if defined(MBEDTLS_SHA256_C)
6962static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006963 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006964{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006965#if defined(MBEDTLS_USE_PSA_CRYPTO)
6966 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
6967#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006968 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006969#endif
Paul Bakker380da532012-04-18 16:10:25 +00006970}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006971#endif
Paul Bakker380da532012-04-18 16:10:25 +00006972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006973#if defined(MBEDTLS_SHA512_C)
6974static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006975 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006976{
Andrzej Kurekeb342242019-01-29 09:14:33 -05006977#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05006978 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006979#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006980 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05006981#endif
Paul Bakker380da532012-04-18 16:10:25 +00006982}
Paul Bakker769075d2012-11-24 11:26:46 +01006983#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006987static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006989{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006990 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006991 mbedtls_md5_context md5;
6992 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006993
Paul Bakker5121ce52009-01-03 21:22:43 +00006994 unsigned char padbuf[48];
6995 unsigned char md5sum[16];
6996 unsigned char sha1sum[20];
6997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006998 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006999 if( !session )
7000 session = ssl->session;
7001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007003
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007004 mbedtls_md5_init( &md5 );
7005 mbedtls_sha1_init( &sha1 );
7006
7007 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7008 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007009
7010 /*
7011 * SSLv3:
7012 * hash =
7013 * MD5( master + pad2 +
7014 * MD5( handshake + sender + master + pad1 ) )
7015 * + SHA1( master + pad2 +
7016 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007017 */
7018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007020 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7021 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007022#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007025 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7026 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007027#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007029 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007030 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007031
Paul Bakker1ef83d62012-04-11 12:09:53 +00007032 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007033
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007034 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7035 mbedtls_md5_update_ret( &md5, session->master, 48 );
7036 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7037 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007038
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007039 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7040 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7041 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7042 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007043
Paul Bakker1ef83d62012-04-11 12:09:53 +00007044 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007045
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007046 mbedtls_md5_starts_ret( &md5 );
7047 mbedtls_md5_update_ret( &md5, session->master, 48 );
7048 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7049 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7050 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007051
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007052 mbedtls_sha1_starts_ret( &sha1 );
7053 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7054 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7055 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7056 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007059
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007060 mbedtls_md5_free( &md5 );
7061 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007062
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007063 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7064 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7065 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007068}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007069#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007072static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007073 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007074{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007075 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007076 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007077 mbedtls_md5_context md5;
7078 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007079 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007082 if( !session )
7083 session = ssl->session;
7084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007086
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007087 mbedtls_md5_init( &md5 );
7088 mbedtls_sha1_init( &sha1 );
7089
7090 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7091 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007092
Paul Bakker1ef83d62012-04-11 12:09:53 +00007093 /*
7094 * TLSv1:
7095 * hash = PRF( master, finished_label,
7096 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7097 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007100 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7101 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007102#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007105 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7106 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007107#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007110 ? "client finished"
7111 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007112
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007113 mbedtls_md5_finish_ret( &md5, padbuf );
7114 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007115
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007116 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007117 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007120
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007121 mbedtls_md5_free( &md5 );
7122 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007123
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007124 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007127}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7131#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007132static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007134{
7135 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007136 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007137 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007138#if defined(MBEDTLS_USE_PSA_CRYPTO)
7139 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007140 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007141 psa_status_t status;
7142#else
7143 mbedtls_sha256_context sha256;
7144#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007147 if( !session )
7148 session = ssl->session;
7149
Andrzej Kurekeb342242019-01-29 09:14:33 -05007150 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7151 ? "client finished"
7152 : "server finished";
7153
7154#if defined(MBEDTLS_USE_PSA_CRYPTO)
7155 sha256_psa = psa_hash_operation_init();
7156
7157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7158
7159 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7160 if( status != PSA_SUCCESS )
7161 {
7162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7163 return;
7164 }
7165
7166 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7167 if( status != PSA_SUCCESS )
7168 {
7169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7170 return;
7171 }
7172 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7173#else
7174
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007175 mbedtls_sha256_init( &sha256 );
7176
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007178
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007179 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007180
7181 /*
7182 * TLSv1.2:
7183 * hash = PRF( master, finished_label,
7184 * Hash( handshake ) )[0.11]
7185 */
7186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187#if !defined(MBEDTLS_SHA256_ALT)
7188 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007189 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007190#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007191
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007192 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007193 mbedtls_sha256_free( &sha256 );
7194#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007195
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007196 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007197 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007200
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007201 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007204}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007208static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007209 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007210{
7211 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007212 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007213 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007214#if defined(MBEDTLS_USE_PSA_CRYPTO)
7215 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007216 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007217 psa_status_t status;
7218#else
7219 mbedtls_sha512_context sha512;
7220#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007222 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007223 if( !session )
7224 session = ssl->session;
7225
Andrzej Kurekeb342242019-01-29 09:14:33 -05007226 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7227 ? "client finished"
7228 : "server finished";
7229
7230#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007231 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007232
7233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7234
Andrzej Kurek972fba52019-01-30 03:29:12 -05007235 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007236 if( status != PSA_SUCCESS )
7237 {
7238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7239 return;
7240 }
7241
Andrzej Kurek972fba52019-01-30 03:29:12 -05007242 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007243 if( status != PSA_SUCCESS )
7244 {
7245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7246 return;
7247 }
7248 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7249#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007250 mbedtls_sha512_init( &sha512 );
7251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007253
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007254 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007255
7256 /*
7257 * TLSv1.2:
7258 * hash = PRF( master, finished_label,
7259 * Hash( handshake ) )[0.11]
7260 */
7261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007263 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7264 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007265#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007266
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007267 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007268 mbedtls_sha512_free( &sha512 );
7269#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007270
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007271 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007272 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007275
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007276 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007279}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007280#endif /* MBEDTLS_SHA512_C */
7281#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007283static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007286
7287 /*
7288 * Free our handshake params
7289 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007290 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007292 ssl->handshake = NULL;
7293
7294 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007295 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007296 */
7297 if( ssl->transform )
7298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299 mbedtls_ssl_transform_free( ssl->transform );
7300 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007301 }
7302 ssl->transform = ssl->transform_negotiate;
7303 ssl->transform_negotiate = NULL;
7304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007305 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007306}
7307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007308void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007309{
7310 int resume = ssl->handshake->resume;
7311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007312 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314#if defined(MBEDTLS_SSL_RENEGOTIATION)
7315 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007318 ssl->renego_records_seen = 0;
7319 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007320#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007321
7322 /*
7323 * Free the previous session and switch in the current one
7324 */
Paul Bakker0a597072012-09-25 21:55:46 +00007325 if( ssl->session )
7326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007328 /* RFC 7366 3.1: keep the EtM state */
7329 ssl->session_negotiate->encrypt_then_mac =
7330 ssl->session->encrypt_then_mac;
7331#endif
7332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333 mbedtls_ssl_session_free( ssl->session );
7334 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007335 }
7336 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007337 ssl->session_negotiate = NULL;
7338
Paul Bakker0a597072012-09-25 21:55:46 +00007339 /*
7340 * Add cache entry
7341 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007342 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007343 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007344 resume == 0 )
7345 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007346 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007348 }
Paul Bakker0a597072012-09-25 21:55:46 +00007349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007351 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007352 ssl->handshake->flight != NULL )
7353 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007354 /* Cancel handshake timer */
7355 ssl_set_timer( ssl, 0 );
7356
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007357 /* Keep last flight around in case we need to resend it:
7358 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007360 }
7361 else
7362#endif
7363 ssl_handshake_wrapup_free_hs_transform( ssl );
7364
Paul Bakker48916f92012-09-16 19:57:18 +00007365 ssl->state++;
7366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007368}
7369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007371{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007372 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007375
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007376 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007377
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007378 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007379
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007380 /*
7381 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7382 * may define some other value. Currently (early 2016), no defined
7383 * ciphersuite does this (and this is unlikely to change as activity has
7384 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007389 ssl->verify_data_len = hash_len;
7390 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007391#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007392
Paul Bakker5121ce52009-01-03 21:22:43 +00007393 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007394 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7395 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007396
7397 /*
7398 * In case of session resuming, invert the client and server
7399 * ChangeCipherSpec messages order.
7400 */
Paul Bakker0a597072012-09-25 21:55:46 +00007401 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007404 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007406#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007408 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007410#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007411 }
7412 else
7413 ssl->state++;
7414
Paul Bakker48916f92012-09-16 19:57:18 +00007415 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007416 * Switch to our negotiated transform and session parameters for outbound
7417 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007419 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007421#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007422 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007423 {
7424 unsigned char i;
7425
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007426 /* Remember current epoch settings for resending */
7427 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007428 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007429
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007430 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007431 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007432
7433 /* Increment epoch */
7434 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007435 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007436 break;
7437
7438 /* The loop goes to its end iff the counter is wrapping */
7439 if( i == 0 )
7440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7442 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007443 }
7444 }
7445 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007447 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007448
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007449 ssl->transform_out = ssl->transform_negotiate;
7450 ssl->session_out = ssl->session_negotiate;
7451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7453 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007455 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7458 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007459 }
7460 }
7461#endif
7462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007464 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007465 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007466#endif
7467
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007468 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007469 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007470 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007471 return( ret );
7472 }
7473
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007474#if defined(MBEDTLS_SSL_PROTO_DTLS)
7475 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7476 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7477 {
7478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7479 return( ret );
7480 }
7481#endif
7482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007484
7485 return( 0 );
7486}
7487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007489#define SSL_MAX_HASH_LEN 36
7490#else
7491#define SSL_MAX_HASH_LEN 12
7492#endif
7493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007494int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007495{
Paul Bakker23986e52011-04-24 08:57:21 +00007496 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007497 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007498 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007501
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007502 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007503
Hanno Becker327c93b2018-08-15 13:56:18 +01007504 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007507 return( ret );
7508 }
7509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
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_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007516 }
7517
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007518 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007519#if defined(MBEDTLS_SSL_PROTO_SSL3)
7520 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007521 hash_len = 36;
7522 else
7523#endif
7524 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7527 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007530 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7531 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007533 }
7534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007535 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007536 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007539 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7540 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007541 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007542 }
7543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007544#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007545 ssl->verify_data_len = hash_len;
7546 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007547#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007548
Paul Bakker0a597072012-09-25 21:55:46 +00007549 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007552 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007554#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007555#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007556 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007558#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007559 }
7560 else
7561 ssl->state++;
7562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007564 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007566#endif
7567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007569
7570 return( 0 );
7571}
7572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007574{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007575 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7578 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7579 mbedtls_md5_init( &handshake->fin_md5 );
7580 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007581 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7582 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007583#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7585#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007586#if defined(MBEDTLS_USE_PSA_CRYPTO)
7587 handshake->fin_sha256_psa = psa_hash_operation_init();
7588 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7589#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007591 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007592#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007593#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007595#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007596 handshake->fin_sha384_psa = psa_hash_operation_init();
7597 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007598#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007600 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007601#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007602#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007604
7605 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007606
7607#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7608 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7609 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7610#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612#if defined(MBEDTLS_DHM_C)
7613 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007614#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615#if defined(MBEDTLS_ECDH_C)
7616 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007617#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007618#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007619 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007620#if defined(MBEDTLS_SSL_CLI_C)
7621 handshake->ecjpake_cache = NULL;
7622 handshake->ecjpake_cache_len = 0;
7623#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007624#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007625
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007626#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007627 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007628#endif
7629
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007630#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7631 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7632#endif
Hanno Becker75173122019-02-06 16:18:31 +00007633
7634#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7635 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7636 mbedtls_pk_init( &handshake->peer_pubkey );
7637#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007638}
7639
Hanno Beckera18d1322018-01-03 14:27:32 +00007640void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007641{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007644 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7645 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007646
Hanno Beckerd56ed242018-01-03 15:32:51 +00007647#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648 mbedtls_md_init( &transform->md_ctx_enc );
7649 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007650#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007651}
7652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007654{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007656}
7657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007658static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007659{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007660 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007661 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007663 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007665 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007666 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007667
7668 /*
7669 * Either the pointers are now NULL or cleared properly and can be freed.
7670 * Now allocate missing structures.
7671 */
7672 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007673 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007674 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007675 }
Paul Bakker48916f92012-09-16 19:57:18 +00007676
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007677 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007678 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007679 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007680 }
Paul Bakker48916f92012-09-16 19:57:18 +00007681
Paul Bakker82788fb2014-10-20 13:59:19 +02007682 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007683 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007684 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007685 }
Paul Bakker48916f92012-09-16 19:57:18 +00007686
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007687 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007688 if( ssl->handshake == NULL ||
7689 ssl->transform_negotiate == NULL ||
7690 ssl->session_negotiate == NULL )
7691 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007694 mbedtls_free( ssl->handshake );
7695 mbedtls_free( ssl->transform_negotiate );
7696 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007697
7698 ssl->handshake = NULL;
7699 ssl->transform_negotiate = NULL;
7700 ssl->session_negotiate = NULL;
7701
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007702 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007703 }
7704
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007705 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007707 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007708 ssl_handshake_params_init( ssl->handshake );
7709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007711 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7712 {
7713 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007714
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007715 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7716 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7717 else
7718 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007719
7720 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007721 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007722#endif
7723
Paul Bakker48916f92012-09-16 19:57:18 +00007724 return( 0 );
7725}
7726
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007727#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007728/* Dummy cookie callbacks for defaults */
7729static int ssl_cookie_write_dummy( void *ctx,
7730 unsigned char **p, unsigned char *end,
7731 const unsigned char *cli_id, size_t cli_id_len )
7732{
7733 ((void) ctx);
7734 ((void) p);
7735 ((void) end);
7736 ((void) cli_id);
7737 ((void) cli_id_len);
7738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007740}
7741
7742static int ssl_cookie_check_dummy( void *ctx,
7743 const unsigned char *cookie, size_t cookie_len,
7744 const unsigned char *cli_id, size_t cli_id_len )
7745{
7746 ((void) ctx);
7747 ((void) cookie);
7748 ((void) cookie_len);
7749 ((void) cli_id);
7750 ((void) cli_id_len);
7751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007753}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007754#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007755
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007756/* Once ssl->out_hdr as the address of the beginning of the
7757 * next outgoing record is set, deduce the other pointers.
7758 *
7759 * Note: For TLS, we save the implicit record sequence number
7760 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7761 * and the caller has to make sure there's space for this.
7762 */
7763
7764static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7765 mbedtls_ssl_transform *transform )
7766{
7767#if defined(MBEDTLS_SSL_PROTO_DTLS)
7768 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7769 {
7770 ssl->out_ctr = ssl->out_hdr + 3;
7771 ssl->out_len = ssl->out_hdr + 11;
7772 ssl->out_iv = ssl->out_hdr + 13;
7773 }
7774 else
7775#endif
7776 {
7777 ssl->out_ctr = ssl->out_hdr - 8;
7778 ssl->out_len = ssl->out_hdr + 3;
7779 ssl->out_iv = ssl->out_hdr + 5;
7780 }
7781
7782 /* Adjust out_msg to make space for explicit IV, if used. */
7783 if( transform != NULL &&
7784 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7785 {
7786 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7787 }
7788 else
7789 ssl->out_msg = ssl->out_iv;
7790}
7791
7792/* Once ssl->in_hdr as the address of the beginning of the
7793 * next incoming record is set, deduce the other pointers.
7794 *
7795 * Note: For TLS, we save the implicit record sequence number
7796 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7797 * and the caller has to make sure there's space for this.
7798 */
7799
7800static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
7801 mbedtls_ssl_transform *transform )
7802{
7803#if defined(MBEDTLS_SSL_PROTO_DTLS)
7804 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7805 {
7806 ssl->in_ctr = ssl->in_hdr + 3;
7807 ssl->in_len = ssl->in_hdr + 11;
7808 ssl->in_iv = ssl->in_hdr + 13;
7809 }
7810 else
7811#endif
7812 {
7813 ssl->in_ctr = ssl->in_hdr - 8;
7814 ssl->in_len = ssl->in_hdr + 3;
7815 ssl->in_iv = ssl->in_hdr + 5;
7816 }
7817
7818 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
7819 if( transform != NULL &&
7820 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7821 {
7822 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
7823 }
7824 else
7825 ssl->in_msg = ssl->in_iv;
7826}
7827
Paul Bakker5121ce52009-01-03 21:22:43 +00007828/*
7829 * Initialize an SSL context
7830 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007831void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7832{
7833 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7834}
7835
7836/*
7837 * Setup an SSL context
7838 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007839
7840static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7841{
7842 /* Set the incoming and outgoing record pointers. */
7843#if defined(MBEDTLS_SSL_PROTO_DTLS)
7844 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7845 {
7846 ssl->out_hdr = ssl->out_buf;
7847 ssl->in_hdr = ssl->in_buf;
7848 }
7849 else
7850#endif /* MBEDTLS_SSL_PROTO_DTLS */
7851 {
7852 ssl->out_hdr = ssl->out_buf + 8;
7853 ssl->in_hdr = ssl->in_buf + 8;
7854 }
7855
7856 /* Derive other internal pointers. */
7857 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
7858 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
7859}
7860
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007861int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007862 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007863{
Paul Bakker48916f92012-09-16 19:57:18 +00007864 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007865
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007866 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007867
7868 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007869 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007870 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007871
7872 /* Set to NULL in case of an error condition */
7873 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007874
Angus Grattond8213d02016-05-25 20:56:48 +10007875 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7876 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007877 {
Angus Grattond8213d02016-05-25 20:56:48 +10007878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007879 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007880 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007881 }
7882
7883 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7884 if( ssl->out_buf == NULL )
7885 {
7886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007887 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007888 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007889 }
7890
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007891 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007892
Paul Bakker48916f92012-09-16 19:57:18 +00007893 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007894 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007895
7896 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007897
7898error:
7899 mbedtls_free( ssl->in_buf );
7900 mbedtls_free( ssl->out_buf );
7901
7902 ssl->conf = NULL;
7903
7904 ssl->in_buf = NULL;
7905 ssl->out_buf = NULL;
7906
7907 ssl->in_hdr = NULL;
7908 ssl->in_ctr = NULL;
7909 ssl->in_len = NULL;
7910 ssl->in_iv = NULL;
7911 ssl->in_msg = NULL;
7912
7913 ssl->out_hdr = NULL;
7914 ssl->out_ctr = NULL;
7915 ssl->out_len = NULL;
7916 ssl->out_iv = NULL;
7917 ssl->out_msg = NULL;
7918
k-stachowiak9f7798e2018-07-31 16:52:32 +02007919 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007920}
7921
7922/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007923 * Reset an initialized and used SSL context for re-use while retaining
7924 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007925 *
7926 * If partial is non-zero, keep data in the input buffer and client ID.
7927 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007928 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007929static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007930{
Paul Bakker48916f92012-09-16 19:57:18 +00007931 int ret;
7932
Hanno Becker7e772132018-08-10 12:38:21 +01007933#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7934 !defined(MBEDTLS_SSL_SRV_C)
7935 ((void) partial);
7936#endif
7937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007939
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007940 /* Cancel any possibly running timer */
7941 ssl_set_timer( ssl, 0 );
7942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943#if defined(MBEDTLS_SSL_RENEGOTIATION)
7944 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007945 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007946
7947 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7949 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007950#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007952
Paul Bakker7eb013f2011-10-06 12:37:39 +00007953 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007954 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007955
7956 ssl->in_msgtype = 0;
7957 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007959 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007960 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007961#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007962#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007963 ssl_dtls_replay_reset( ssl );
7964#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007965
7966 ssl->in_hslen = 0;
7967 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007968
7969 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007970
7971 ssl->out_msgtype = 0;
7972 ssl->out_msglen = 0;
7973 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7975 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007976 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007977#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007978
Hanno Becker19859472018-08-06 09:40:20 +01007979 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7980
Paul Bakker48916f92012-09-16 19:57:18 +00007981 ssl->transform_in = NULL;
7982 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007983
Hanno Becker78640902018-08-13 16:35:15 +01007984 ssl->session_in = NULL;
7985 ssl->session_out = NULL;
7986
Angus Grattond8213d02016-05-25 20:56:48 +10007987 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007988
7989#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007990 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007991#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7992 {
7993 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007994 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007995 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7998 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8001 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008003 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8004 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008005 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008006 }
8007#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008008
Paul Bakker48916f92012-09-16 19:57:18 +00008009 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011 mbedtls_ssl_transform_free( ssl->transform );
8012 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008013 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008014 }
Paul Bakker48916f92012-09-16 19:57:18 +00008015
Paul Bakkerc0463502013-02-14 11:19:38 +01008016 if( ssl->session )
8017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018 mbedtls_ssl_session_free( ssl->session );
8019 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008020 ssl->session = NULL;
8021 }
8022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008023#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008024 ssl->alpn_chosen = NULL;
8025#endif
8026
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008027#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008028#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008029 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008030#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008031 {
8032 mbedtls_free( ssl->cli_id );
8033 ssl->cli_id = NULL;
8034 ssl->cli_id_len = 0;
8035 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008036#endif
8037
Paul Bakker48916f92012-09-16 19:57:18 +00008038 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8039 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008040
8041 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008042}
8043
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008044/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008045 * Reset an initialized and used SSL context for re-use while retaining
8046 * all application-set variables, function pointers and data.
8047 */
8048int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8049{
8050 return( ssl_session_reset_int( ssl, 0 ) );
8051}
8052
8053/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008054 * SSL set accessors
8055 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008056void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008057{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008058 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008059}
8060
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008061void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008062{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008063 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008064}
8065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008066#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008067void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008068{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008069 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008070}
8071#endif
8072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008073#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008074void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008075{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008076 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008077}
8078#endif
8079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008081
Hanno Becker1841b0a2018-08-24 11:13:57 +01008082void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8083 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008084{
8085 ssl->disable_datagram_packing = !allow_packing;
8086}
8087
8088void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8089 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008090{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008091 conf->hs_timeout_min = min;
8092 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008093}
8094#endif
8095
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008096void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008097{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008098 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008099}
8100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008101#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008102void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008103 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008104 void *p_vrfy )
8105{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008106 conf->f_vrfy = f_vrfy;
8107 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008108}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008109#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008110
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008111void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008112 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008113 void *p_rng )
8114{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008115 conf->f_rng = f_rng;
8116 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008117}
8118
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008119void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008120 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008121 void *p_dbg )
8122{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008123 conf->f_dbg = f_dbg;
8124 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008125}
8126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008127void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008128 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008129 mbedtls_ssl_send_t *f_send,
8130 mbedtls_ssl_recv_t *f_recv,
8131 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008132{
8133 ssl->p_bio = p_bio;
8134 ssl->f_send = f_send;
8135 ssl->f_recv = f_recv;
8136 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008137}
8138
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008139#if defined(MBEDTLS_SSL_PROTO_DTLS)
8140void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8141{
8142 ssl->mtu = mtu;
8143}
8144#endif
8145
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008146void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008147{
8148 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008149}
8150
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008151void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8152 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008153 mbedtls_ssl_set_timer_t *f_set_timer,
8154 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008155{
8156 ssl->p_timer = p_timer;
8157 ssl->f_set_timer = f_set_timer;
8158 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008159
8160 /* Make sure we start with no timer running */
8161 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008162}
8163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008165void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008166 void *p_cache,
8167 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8168 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008169{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008170 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008171 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008172 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008173}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008174#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176#if defined(MBEDTLS_SSL_CLI_C)
8177int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008178{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008179 int ret;
8180
8181 if( ssl == NULL ||
8182 session == NULL ||
8183 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008184 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008187 }
8188
Hanno Becker52055ae2019-02-06 14:30:46 +00008189 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8190 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008191 return( ret );
8192
Paul Bakker0a597072012-09-25 21:55:46 +00008193 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008194
8195 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008196}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008197#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008198
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008199void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008200 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008201{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008202 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8203 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8204 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8205 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008206}
8207
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008208void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008209 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008210 int major, int minor )
8211{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008213 return;
8214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008215 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008216 return;
8217
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008218 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008219}
8220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008221#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008222void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008223 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008224{
8225 conf->cert_profile = profile;
8226}
8227
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008228/* Append a new keycert entry to a (possibly empty) list */
8229static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8230 mbedtls_x509_crt *cert,
8231 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008232{
niisato8ee24222018-06-25 19:05:48 +09008233 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008234
niisato8ee24222018-06-25 19:05:48 +09008235 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8236 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008237 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008238
niisato8ee24222018-06-25 19:05:48 +09008239 new_cert->cert = cert;
8240 new_cert->key = key;
8241 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008242
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008243 /* Update head is the list was null, else add to the end */
8244 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008245 {
niisato8ee24222018-06-25 19:05:48 +09008246 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008247 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008248 else
8249 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008250 mbedtls_ssl_key_cert *cur = *head;
8251 while( cur->next != NULL )
8252 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008253 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008254 }
8255
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008256 return( 0 );
8257}
8258
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008259int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008260 mbedtls_x509_crt *own_cert,
8261 mbedtls_pk_context *pk_key )
8262{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008263 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008264}
8265
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008266void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008267 mbedtls_x509_crt *ca_chain,
8268 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008269{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008270 conf->ca_chain = ca_chain;
8271 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008272
8273#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8274 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8275 * cannot be used together. */
8276 conf->f_ca_cb = NULL;
8277 conf->p_ca_cb = NULL;
8278#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008279}
Hanno Becker5adaad92019-03-27 16:54:37 +00008280
8281#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8282void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008283 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008284 void *p_ca_cb )
8285{
8286 conf->f_ca_cb = f_ca_cb;
8287 conf->p_ca_cb = p_ca_cb;
8288
8289 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8290 * cannot be used together. */
8291 conf->ca_chain = NULL;
8292 conf->ca_crl = NULL;
8293}
8294#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008295#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008296
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008297#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8298int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8299 mbedtls_x509_crt *own_cert,
8300 mbedtls_pk_context *pk_key )
8301{
8302 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8303 own_cert, pk_key ) );
8304}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008305
8306void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8307 mbedtls_x509_crt *ca_chain,
8308 mbedtls_x509_crl *ca_crl )
8309{
8310 ssl->handshake->sni_ca_chain = ca_chain;
8311 ssl->handshake->sni_ca_crl = ca_crl;
8312}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008313
8314void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8315 int authmode )
8316{
8317 ssl->handshake->sni_authmode = authmode;
8318}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008319#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8320
Hanno Becker8927c832019-04-03 12:52:50 +01008321#if defined(MBEDTLS_X509_CRT_PARSE_C)
8322void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8323 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8324 void *p_vrfy )
8325{
8326 ssl->f_vrfy = f_vrfy;
8327 ssl->p_vrfy = p_vrfy;
8328}
8329#endif
8330
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008331#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008332/*
8333 * Set EC J-PAKE password for current handshake
8334 */
8335int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8336 const unsigned char *pw,
8337 size_t pw_len )
8338{
8339 mbedtls_ecjpake_role role;
8340
Janos Follath8eb64132016-06-03 15:40:57 +01008341 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008342 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8343
8344 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8345 role = MBEDTLS_ECJPAKE_SERVER;
8346 else
8347 role = MBEDTLS_ECJPAKE_CLIENT;
8348
8349 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8350 role,
8351 MBEDTLS_MD_SHA256,
8352 MBEDTLS_ECP_DP_SECP256R1,
8353 pw, pw_len ) );
8354}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008355#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008357#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008358
8359static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8360{
8361 /* Remove reference to existing PSK, if any. */
8362#if defined(MBEDTLS_USE_PSA_CRYPTO)
8363 if( conf->psk_opaque != 0 )
8364 {
8365 /* The maintenance of the PSK key slot is the
8366 * user's responsibility. */
8367 conf->psk_opaque = 0;
8368 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008369 /* This and the following branch should never
8370 * be taken simultaenously as we maintain the
8371 * invariant that raw and opaque PSKs are never
8372 * configured simultaneously. As a safeguard,
8373 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008374#endif /* MBEDTLS_USE_PSA_CRYPTO */
8375 if( conf->psk != NULL )
8376 {
8377 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8378
8379 mbedtls_free( conf->psk );
8380 conf->psk = NULL;
8381 conf->psk_len = 0;
8382 }
8383
8384 /* Remove reference to PSK identity, if any. */
8385 if( conf->psk_identity != NULL )
8386 {
8387 mbedtls_free( conf->psk_identity );
8388 conf->psk_identity = NULL;
8389 conf->psk_identity_len = 0;
8390 }
8391}
8392
Hanno Becker7390c712018-11-15 13:33:04 +00008393/* This function assumes that PSK identity in the SSL config is unset.
8394 * It checks that the provided identity is well-formed and attempts
8395 * to make a copy of it in the SSL config.
8396 * On failure, the PSK identity in the config remains unset. */
8397static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8398 unsigned char const *psk_identity,
8399 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008400{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008401 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008402 if( psk_identity == NULL ||
8403 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008404 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008405 {
8406 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8407 }
8408
Hanno Becker7390c712018-11-15 13:33:04 +00008409 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8410 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008411 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008412
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008413 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008414 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008415
8416 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008417}
8418
Hanno Becker7390c712018-11-15 13:33:04 +00008419int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8420 const unsigned char *psk, size_t psk_len,
8421 const unsigned char *psk_identity, size_t psk_identity_len )
8422{
8423 int ret;
8424 /* Remove opaque/raw PSK + PSK Identity */
8425 ssl_conf_remove_psk( conf );
8426
8427 /* Check and set raw PSK */
8428 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8429 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8430 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8431 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8432 conf->psk_len = psk_len;
8433 memcpy( conf->psk, psk, conf->psk_len );
8434
8435 /* Check and set PSK Identity */
8436 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8437 if( ret != 0 )
8438 ssl_conf_remove_psk( conf );
8439
8440 return( ret );
8441}
8442
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008443static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8444{
8445#if defined(MBEDTLS_USE_PSA_CRYPTO)
8446 if( ssl->handshake->psk_opaque != 0 )
8447 {
8448 ssl->handshake->psk_opaque = 0;
8449 }
8450 else
8451#endif /* MBEDTLS_USE_PSA_CRYPTO */
8452 if( ssl->handshake->psk != NULL )
8453 {
8454 mbedtls_platform_zeroize( ssl->handshake->psk,
8455 ssl->handshake->psk_len );
8456 mbedtls_free( ssl->handshake->psk );
8457 ssl->handshake->psk_len = 0;
8458 }
8459}
8460
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008461int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8462 const unsigned char *psk, size_t psk_len )
8463{
8464 if( psk == NULL || ssl->handshake == NULL )
8465 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8466
8467 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8468 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8469
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008470 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008471
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008472 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008473 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008474
8475 ssl->handshake->psk_len = psk_len;
8476 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8477
8478 return( 0 );
8479}
8480
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008481#if defined(MBEDTLS_USE_PSA_CRYPTO)
8482int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008483 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008484 const unsigned char *psk_identity,
8485 size_t psk_identity_len )
8486{
Hanno Becker7390c712018-11-15 13:33:04 +00008487 int ret;
8488 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008489 ssl_conf_remove_psk( conf );
8490
Hanno Becker7390c712018-11-15 13:33:04 +00008491 /* Check and set opaque PSK */
8492 if( psk_slot == 0 )
8493 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008494 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008495
8496 /* Check and set PSK Identity */
8497 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8498 psk_identity_len );
8499 if( ret != 0 )
8500 ssl_conf_remove_psk( conf );
8501
8502 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008503}
8504
8505int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008506 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008507{
8508 if( psk_slot == 0 || ssl->handshake == NULL )
8509 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8510
8511 ssl_remove_psk( ssl );
8512 ssl->handshake->psk_opaque = psk_slot;
8513 return( 0 );
8514}
8515#endif /* MBEDTLS_USE_PSA_CRYPTO */
8516
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008517void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008518 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008519 size_t),
8520 void *p_psk )
8521{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008522 conf->f_psk = f_psk;
8523 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008524}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008525#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008526
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008527#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008528
8529#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008530int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008531{
8532 int ret;
8533
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008534 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8535 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8536 {
8537 mbedtls_mpi_free( &conf->dhm_P );
8538 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008539 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008540 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008541
8542 return( 0 );
8543}
Hanno Becker470a8c42017-10-04 15:28:46 +01008544#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008545
Hanno Beckera90658f2017-10-04 15:29:08 +01008546int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8547 const unsigned char *dhm_P, size_t P_len,
8548 const unsigned char *dhm_G, size_t G_len )
8549{
8550 int ret;
8551
8552 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8553 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8554 {
8555 mbedtls_mpi_free( &conf->dhm_P );
8556 mbedtls_mpi_free( &conf->dhm_G );
8557 return( ret );
8558 }
8559
8560 return( 0 );
8561}
Paul Bakker5121ce52009-01-03 21:22:43 +00008562
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008563int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008564{
8565 int ret;
8566
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008567 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8568 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8569 {
8570 mbedtls_mpi_free( &conf->dhm_P );
8571 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008572 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008573 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008574
8575 return( 0 );
8576}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008577#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008578
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008579#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8580/*
8581 * Set the minimum length for Diffie-Hellman parameters
8582 */
8583void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8584 unsigned int bitlen )
8585{
8586 conf->dhm_min_bitlen = bitlen;
8587}
8588#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8589
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008590#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008591/*
8592 * Set allowed/preferred hashes for handshake signatures
8593 */
8594void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8595 const int *hashes )
8596{
8597 conf->sig_hashes = hashes;
8598}
Hanno Becker947194e2017-04-07 13:25:49 +01008599#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008600
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008601#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008602/*
8603 * Set the allowed elliptic curves
8604 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008605void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008606 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008607{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008608 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008609}
Hanno Becker947194e2017-04-07 13:25:49 +01008610#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008611
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008612#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008614{
Hanno Becker947194e2017-04-07 13:25:49 +01008615 /* Initialize to suppress unnecessary compiler warning */
8616 size_t hostname_len = 0;
8617
8618 /* Check if new hostname is valid before
8619 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008620 if( hostname != NULL )
8621 {
8622 hostname_len = strlen( hostname );
8623
8624 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8625 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8626 }
8627
8628 /* Now it's clear that we will overwrite the old hostname,
8629 * so we can free it safely */
8630
8631 if( ssl->hostname != NULL )
8632 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008633 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008634 mbedtls_free( ssl->hostname );
8635 }
8636
8637 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008638
Paul Bakker5121ce52009-01-03 21:22:43 +00008639 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008640 {
8641 ssl->hostname = NULL;
8642 }
8643 else
8644 {
8645 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008646 if( ssl->hostname == NULL )
8647 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008648
Hanno Becker947194e2017-04-07 13:25:49 +01008649 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008650
Hanno Becker947194e2017-04-07 13:25:49 +01008651 ssl->hostname[hostname_len] = '\0';
8652 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008653
8654 return( 0 );
8655}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008656#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008657
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008658#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008659void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008660 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008661 const unsigned char *, size_t),
8662 void *p_sni )
8663{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008664 conf->f_sni = f_sni;
8665 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008666}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008667#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008669#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008670int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008671{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008672 size_t cur_len, tot_len;
8673 const char **p;
8674
8675 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008676 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8677 * MUST NOT be truncated."
8678 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008679 */
8680 tot_len = 0;
8681 for( p = protos; *p != NULL; p++ )
8682 {
8683 cur_len = strlen( *p );
8684 tot_len += cur_len;
8685
8686 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008687 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008688 }
8689
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008690 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008691
8692 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008693}
8694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008695const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008696{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008697 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008698}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008699#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008700
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008701void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008702{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008703 conf->max_major_ver = major;
8704 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008705}
8706
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008707void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008708{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008709 conf->min_major_ver = major;
8710 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008711}
8712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008713#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008714void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008715{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008716 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008717}
8718#endif
8719
Janos Follath088ce432017-04-10 12:42:31 +01008720#if defined(MBEDTLS_SSL_SRV_C)
8721void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8722 char cert_req_ca_list )
8723{
8724 conf->cert_req_ca_list = cert_req_ca_list;
8725}
8726#endif
8727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008728#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008729void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008730{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008731 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008732}
8733#endif
8734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008735#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008736void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008737{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008738 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008739}
8740#endif
8741
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008742#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008743void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008744{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008745 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008746}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008747#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008749#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008750int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008751{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008752 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008753 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008755 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008756 }
8757
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008758 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008759
8760 return( 0 );
8761}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008762#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008764#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008765void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008766{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008767 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008768}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008771#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008772void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008773{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008774 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008775}
8776#endif
8777
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008778void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008779{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008780 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008781}
8782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008783#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008784void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008785{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008786 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008787}
8788
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008789void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008790{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008791 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008792}
8793
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008794void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008795 const unsigned char period[8] )
8796{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008797 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008798}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008799#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008801#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008802#if defined(MBEDTLS_SSL_CLI_C)
8803void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008804{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008805 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008806}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008807#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008808
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008809#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008810void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8811 mbedtls_ssl_ticket_write_t *f_ticket_write,
8812 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8813 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008814{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008815 conf->f_ticket_write = f_ticket_write;
8816 conf->f_ticket_parse = f_ticket_parse;
8817 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008818}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008819#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008820#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008821
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008822#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8823void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8824 mbedtls_ssl_export_keys_t *f_export_keys,
8825 void *p_export_keys )
8826{
8827 conf->f_export_keys = f_export_keys;
8828 conf->p_export_keys = p_export_keys;
8829}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03008830
8831void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
8832 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
8833 void *p_export_keys )
8834{
8835 conf->f_export_keys_ext = f_export_keys_ext;
8836 conf->p_export_keys = p_export_keys;
8837}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008838#endif
8839
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008840#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008841void mbedtls_ssl_conf_async_private_cb(
8842 mbedtls_ssl_config *conf,
8843 mbedtls_ssl_async_sign_t *f_async_sign,
8844 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8845 mbedtls_ssl_async_resume_t *f_async_resume,
8846 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008847 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008848{
8849 conf->f_async_sign_start = f_async_sign;
8850 conf->f_async_decrypt_start = f_async_decrypt;
8851 conf->f_async_resume = f_async_resume;
8852 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008853 conf->p_async_config_data = async_config_data;
8854}
8855
Gilles Peskine8f97af72018-04-26 11:46:10 +02008856void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8857{
8858 return( conf->p_async_config_data );
8859}
8860
Gilles Peskine1febfef2018-04-30 11:54:39 +02008861void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008862{
8863 if( ssl->handshake == NULL )
8864 return( NULL );
8865 else
8866 return( ssl->handshake->user_async_ctx );
8867}
8868
Gilles Peskine1febfef2018-04-30 11:54:39 +02008869void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008870 void *ctx )
8871{
8872 if( ssl->handshake != NULL )
8873 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008874}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008875#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008876
Paul Bakker5121ce52009-01-03 21:22:43 +00008877/*
8878 * SSL get accessors
8879 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008880size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008881{
8882 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8883}
8884
Hanno Becker8b170a02017-10-10 11:51:19 +01008885int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8886{
8887 /*
8888 * Case A: We're currently holding back
8889 * a message for further processing.
8890 */
8891
8892 if( ssl->keep_current_message == 1 )
8893 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008894 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008895 return( 1 );
8896 }
8897
8898 /*
8899 * Case B: Further records are pending in the current datagram.
8900 */
8901
8902#if defined(MBEDTLS_SSL_PROTO_DTLS)
8903 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8904 ssl->in_left > ssl->next_record_offset )
8905 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008906 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008907 return( 1 );
8908 }
8909#endif /* MBEDTLS_SSL_PROTO_DTLS */
8910
8911 /*
8912 * Case C: A handshake message is being processed.
8913 */
8914
Hanno Becker8b170a02017-10-10 11:51:19 +01008915 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8916 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008917 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008918 return( 1 );
8919 }
8920
8921 /*
8922 * Case D: An application data message is being processed
8923 */
8924 if( ssl->in_offt != NULL )
8925 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008926 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008927 return( 1 );
8928 }
8929
8930 /*
8931 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008932 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008933 * we implement support for multiple alerts in single records.
8934 */
8935
8936 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8937 return( 0 );
8938}
8939
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008940uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008941{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008942 if( ssl->session != NULL )
8943 return( ssl->session->verify_result );
8944
8945 if( ssl->session_negotiate != NULL )
8946 return( ssl->session_negotiate->verify_result );
8947
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008948 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008949}
8950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008951const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008952{
Paul Bakker926c8e42013-03-06 10:23:34 +01008953 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008954 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008956 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008957}
8958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008959const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008960{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008961#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008962 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008963 {
8964 switch( ssl->minor_ver )
8965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008966 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008967 return( "DTLSv1.0" );
8968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008969 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008970 return( "DTLSv1.2" );
8971
8972 default:
8973 return( "unknown (DTLS)" );
8974 }
8975 }
8976#endif
8977
Paul Bakker43ca69c2011-01-15 17:35:19 +00008978 switch( ssl->minor_ver )
8979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008980 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008981 return( "SSLv3.0" );
8982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008983 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008984 return( "TLSv1.0" );
8985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008986 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00008987 return( "TLSv1.1" );
8988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008989 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00008990 return( "TLSv1.2" );
8991
Paul Bakker43ca69c2011-01-15 17:35:19 +00008992 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008993 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008994 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008995}
8996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008997int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008998{
Hanno Becker3136ede2018-08-17 15:28:19 +01008999 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009000 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009001 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009002
Hanno Becker78640902018-08-13 16:35:15 +01009003 if( transform == NULL )
9004 return( (int) mbedtls_ssl_hdr_len( ssl ) );
9005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009006#if defined(MBEDTLS_ZLIB_SUPPORT)
9007 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9008 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009009#endif
9010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009011 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009013 case MBEDTLS_MODE_GCM:
9014 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009015 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009016 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009017 transform_expansion = transform->minlen;
9018 break;
9019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009020 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009021
9022 block_size = mbedtls_cipher_get_block_size(
9023 &transform->cipher_ctx_enc );
9024
Hanno Becker3136ede2018-08-17 15:28:19 +01009025 /* Expansion due to the addition of the MAC. */
9026 transform_expansion += transform->maclen;
9027
9028 /* Expansion due to the addition of CBC padding;
9029 * Theoretically up to 256 bytes, but we never use
9030 * more than the block size of the underlying cipher. */
9031 transform_expansion += block_size;
9032
9033 /* For TLS 1.1 or higher, an explicit IV is added
9034 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009035#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9036 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009037 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009038#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009039
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009040 break;
9041
9042 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009044 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009045 }
9046
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02009047 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009048}
9049
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009050#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9051size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9052{
9053 size_t max_len;
9054
9055 /*
9056 * Assume mfl_code is correct since it was checked when set
9057 */
Angus Grattond8213d02016-05-25 20:56:48 +10009058 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009059
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009060 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009061 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009062 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009063 {
Angus Grattond8213d02016-05-25 20:56:48 +10009064 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009065 }
9066
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009067 /* During a handshake, use the value being negotiated */
9068 if( ssl->session_negotiate != NULL &&
9069 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9070 {
9071 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9072 }
9073
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009074 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009075}
9076#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9077
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009078#if defined(MBEDTLS_SSL_PROTO_DTLS)
9079static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9080{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009081 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9082 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9083 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9084 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9085 return ( 0 );
9086
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009087 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9088 return( ssl->mtu );
9089
9090 if( ssl->mtu == 0 )
9091 return( ssl->handshake->mtu );
9092
9093 return( ssl->mtu < ssl->handshake->mtu ?
9094 ssl->mtu : ssl->handshake->mtu );
9095}
9096#endif /* MBEDTLS_SSL_PROTO_DTLS */
9097
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009098int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9099{
9100 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9101
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009102#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9103 !defined(MBEDTLS_SSL_PROTO_DTLS)
9104 (void) ssl;
9105#endif
9106
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009107#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9108 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9109
9110 if( max_len > mfl )
9111 max_len = mfl;
9112#endif
9113
9114#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009115 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009116 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009117 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009118 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9119 const size_t overhead = (size_t) ret;
9120
9121 if( ret < 0 )
9122 return( ret );
9123
9124 if( mtu <= overhead )
9125 {
9126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9127 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9128 }
9129
9130 if( max_len > mtu - overhead )
9131 max_len = mtu - overhead;
9132 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009133#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009134
Hanno Becker0defedb2018-08-10 12:35:02 +01009135#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9136 !defined(MBEDTLS_SSL_PROTO_DTLS)
9137 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009138#endif
9139
9140 return( (int) max_len );
9141}
9142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009143#if defined(MBEDTLS_X509_CRT_PARSE_C)
9144const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009145{
9146 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009147 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009148
Hanno Beckere6824572019-02-07 13:18:46 +00009149#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009150 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009151#else
9152 return( NULL );
9153#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009154}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009155#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009157#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009158int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9159 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009160{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009161 if( ssl == NULL ||
9162 dst == NULL ||
9163 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009164 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009167 }
9168
Hanno Becker52055ae2019-02-06 14:30:46 +00009169 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009170}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009171#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009172
Paul Bakker5121ce52009-01-03 21:22:43 +00009173/*
Paul Bakker1961b702013-01-25 14:49:24 +01009174 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009176int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009177{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009178 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009179
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009180 if( ssl == NULL || ssl->conf == NULL )
9181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009183#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009184 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009185 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009186#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009187#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009188 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009189 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009190#endif
9191
Paul Bakker1961b702013-01-25 14:49:24 +01009192 return( ret );
9193}
9194
9195/*
9196 * Perform the SSL handshake
9197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009198int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009199{
9200 int ret = 0;
9201
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009202 if( ssl == NULL || ssl->conf == NULL )
9203 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009207 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009209 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009210
9211 if( ret != 0 )
9212 break;
9213 }
9214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009216
9217 return( ret );
9218}
9219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009220#if defined(MBEDTLS_SSL_RENEGOTIATION)
9221#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009222/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009223 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009225static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009226{
9227 int ret;
9228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009230
9231 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009232 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9233 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009234
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009235 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009236 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009237 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009238 return( ret );
9239 }
9240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009242
9243 return( 0 );
9244}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009245#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009246
9247/*
9248 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009249 * - any side: calling mbedtls_ssl_renegotiate(),
9250 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9251 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009252 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009253 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009254 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009256static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009257{
9258 int ret;
9259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009261
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009262 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9263 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009264
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009265 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9266 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009267#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009268 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009269 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009270 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009271 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009272 ssl->handshake->out_msg_seq = 1;
9273 else
9274 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009275 }
9276#endif
9277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009278 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9279 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009281 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009284 return( ret );
9285 }
9286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009288
9289 return( 0 );
9290}
9291
9292/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009293 * Renegotiate current connection on client,
9294 * or request renegotiation on server
9295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009297{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009298 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009299
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009300 if( ssl == NULL || ssl->conf == NULL )
9301 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009304 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009305 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009307 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9308 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009311
9312 /* Did we already try/start sending HelloRequest? */
9313 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009314 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009315
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009316 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009317 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009318#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009320#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009321 /*
9322 * On client, either start the renegotiation process or,
9323 * if already in progress, continue the handshake
9324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009325 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009327 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9328 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009329
9330 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009332 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009333 return( ret );
9334 }
9335 }
9336 else
9337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009338 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009340 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009341 return( ret );
9342 }
9343 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009345
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009346 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009347}
9348
9349/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009350 * Check record counters and renegotiate if they're above the limit.
9351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009352static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009353{
Andres AG2196c7f2016-12-15 17:01:16 +00009354 size_t ep_len = ssl_ep_len( ssl );
9355 int in_ctr_cmp;
9356 int out_ctr_cmp;
9357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009358 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9359 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009360 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009361 {
9362 return( 0 );
9363 }
9364
Andres AG2196c7f2016-12-15 17:01:16 +00009365 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9366 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009367 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009368 ssl->conf->renego_period + ep_len, 8 - ep_len );
9369
9370 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009371 {
9372 return( 0 );
9373 }
9374
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009376 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009377}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009378#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009379
9380/*
9381 * Receive application data decrypted from the SSL layer
9382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009383int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009384{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009385 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009386 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009387
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009388 if( ssl == NULL || ssl->conf == NULL )
9389 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009393#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009394 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009396 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009397 return( ret );
9398
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009399 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009400 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009401 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009402 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009403 return( ret );
9404 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009405 }
9406#endif
9407
Hanno Becker4a810fb2017-05-24 16:27:30 +01009408 /*
9409 * Check if renegotiation is necessary and/or handshake is
9410 * in process. If yes, perform/continue, and fall through
9411 * if an unexpected packet is received while the client
9412 * is waiting for the ServerHello.
9413 *
9414 * (There is no equivalent to the last condition on
9415 * the server-side as it is not treated as within
9416 * a handshake while waiting for the ClientHello
9417 * after a renegotiation request.)
9418 */
9419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009420#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009421 ret = ssl_check_ctr_renegotiate( ssl );
9422 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9423 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009425 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009426 return( ret );
9427 }
9428#endif
9429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009430 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009432 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009433 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9434 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009437 return( ret );
9438 }
9439 }
9440
Hanno Beckere41158b2017-10-23 13:30:32 +01009441 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009442 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009443 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009444 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009445 if( ssl->f_get_timer != NULL &&
9446 ssl->f_get_timer( ssl->p_timer ) == -1 )
9447 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009448 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009449 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009450
Hanno Becker327c93b2018-08-15 13:56:18 +01009451 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009452 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009453 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9454 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009455
Hanno Becker4a810fb2017-05-24 16:27:30 +01009456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9457 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009458 }
9459
9460 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009461 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009462 {
9463 /*
9464 * OpenSSL sends empty messages to randomize the IV
9465 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009466 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009468 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009469 return( 0 );
9470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009471 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009472 return( ret );
9473 }
9474 }
9475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009476 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009479
Hanno Becker4a810fb2017-05-24 16:27:30 +01009480 /*
9481 * - For client-side, expect SERVER_HELLO_REQUEST.
9482 * - For server-side, expect CLIENT_HELLO.
9483 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9484 */
9485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009487 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009488 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009489 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
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 );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009501 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009502#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009503
Hanno Becker4a810fb2017-05-24 16:27:30 +01009504#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009505 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009509
9510 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009512 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009513 {
9514 continue;
9515 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009516#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009518 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009519#endif /* MBEDTLS_SSL_SRV_C */
9520
Hanno Becker21df7f92017-10-17 11:03:26 +01009521#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009522 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009523 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9524 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9525 ssl->conf->allow_legacy_renegotiation ==
9526 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9527 {
9528 /*
9529 * Accept renegotiation request
9530 */
Paul Bakker48916f92012-09-16 19:57:18 +00009531
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009532 /* DTLS clients need to know renego is server-initiated */
9533#if defined(MBEDTLS_SSL_PROTO_DTLS)
9534 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9535 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9536 {
9537 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9538 }
9539#endif
9540 ret = ssl_start_renegotiation( ssl );
9541 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9542 ret != 0 )
9543 {
9544 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9545 return( ret );
9546 }
9547 }
9548 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009549#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009550 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009551 /*
9552 * Refuse renegotiation
9553 */
9554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009555 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009557#if defined(MBEDTLS_SSL_PROTO_SSL3)
9558 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009559 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009560 /* SSLv3 does not have a "no_renegotiation" warning, so
9561 we send a fatal alert and abort the connection. */
9562 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9563 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9564 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009565 }
9566 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9568#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9569 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9570 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9573 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9574 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009575 {
9576 return( ret );
9577 }
Paul Bakker48916f92012-09-16 19:57:18 +00009578 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009579 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009580#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9581 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9584 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009585 }
Paul Bakker48916f92012-09-16 19:57:18 +00009586 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009587
Hanno Becker90333da2017-10-10 11:27:13 +01009588 /* At this point, we don't know whether the renegotiation has been
9589 * completed or not. The cases to consider are the following:
9590 * 1) The renegotiation is complete. In this case, no new record
9591 * has been read yet.
9592 * 2) The renegotiation is incomplete because the client received
9593 * an application data record while awaiting the ServerHello.
9594 * 3) The renegotiation is incomplete because the client received
9595 * a non-handshake, non-application data message while awaiting
9596 * the ServerHello.
9597 * In each of these case, looping will be the proper action:
9598 * - For 1), the next iteration will read a new record and check
9599 * if it's application data.
9600 * - For 2), the loop condition isn't satisfied as application data
9601 * is present, hence continue is the same as break
9602 * - For 3), the loop condition is satisfied and read_record
9603 * will re-deliver the message that was held back by the client
9604 * when expecting the ServerHello.
9605 */
9606 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009607 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009608#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009609 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009610 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009611 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009612 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009613 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009616 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009617 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009618 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009619 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009620 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009621#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9624 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009627 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009628 }
9629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9633 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009634 }
9635
9636 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009637
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009638 /* We're going to return something now, cancel timer,
9639 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009640 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009641 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009642
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009643#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009644 /* If we requested renego but received AppData, resend HelloRequest.
9645 * Do it now, after setting in_offt, to avoid taking this branch
9646 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009647#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009648 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009649 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009650 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009651 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009653 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009654 return( ret );
9655 }
9656 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009657#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009658#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009659 }
9660
9661 n = ( len < ssl->in_msglen )
9662 ? len : ssl->in_msglen;
9663
9664 memcpy( buf, ssl->in_offt, n );
9665 ssl->in_msglen -= n;
9666
9667 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009668 {
9669 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009670 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009671 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009672 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009673 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009674 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009675 /* more data available */
9676 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009677 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009680
Paul Bakker23986e52011-04-24 08:57:21 +00009681 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009682}
9683
9684/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009685 * Send application data to be encrypted by the SSL layer, taking care of max
9686 * fragment length and buffer size.
9687 *
9688 * According to RFC 5246 Section 6.2.1:
9689 *
9690 * Zero-length fragments of Application data MAY be sent as they are
9691 * potentially useful as a traffic analysis countermeasure.
9692 *
9693 * Therefore, it is possible that the input message length is 0 and the
9694 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009695 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009696static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009697 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009698{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009699 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9700 const size_t max_len = (size_t) ret;
9701
9702 if( ret < 0 )
9703 {
9704 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9705 return( ret );
9706 }
9707
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009708 if( len > max_len )
9709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009710#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009711 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009714 "maximum fragment length: %d > %d",
9715 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009716 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009717 }
9718 else
9719#endif
9720 len = max_len;
9721 }
Paul Bakker887bd502011-06-08 13:10:54 +00009722
Paul Bakker5121ce52009-01-03 21:22:43 +00009723 if( ssl->out_left != 0 )
9724 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009725 /*
9726 * The user has previously tried to send the data and
9727 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9728 * written. In this case, we expect the high-level write function
9729 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9730 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009731 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009733 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009734 return( ret );
9735 }
9736 }
Paul Bakker887bd502011-06-08 13:10:54 +00009737 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009738 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009739 /*
9740 * The user is trying to send a message the first time, so we need to
9741 * copy the data into the internal buffers and setup the data structure
9742 * to keep track of partial writes
9743 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009744 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009746 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009747
Hanno Becker67bc7c32018-08-06 11:33:50 +01009748 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009750 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009751 return( ret );
9752 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009753 }
9754
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009755 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009756}
9757
9758/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009759 * Write application data, doing 1/n-1 splitting if necessary.
9760 *
9761 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009762 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009763 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009764 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009765#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009766static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009767 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009768{
9769 int ret;
9770
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009771 if( ssl->conf->cbc_record_splitting ==
9772 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009773 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9775 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9776 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009777 {
9778 return( ssl_write_real( ssl, buf, len ) );
9779 }
9780
9781 if( ssl->split_done == 0 )
9782 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009783 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009784 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009785 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009786 }
9787
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009788 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9789 return( ret );
9790 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009791
9792 return( ret + 1 );
9793}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009794#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009795
9796/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009797 * Write application data (public-facing wrapper)
9798 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009799int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009800{
9801 int ret;
9802
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009804
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009805 if( ssl == NULL || ssl->conf == NULL )
9806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9807
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009808#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009809 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9810 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009811 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009812 return( ret );
9813 }
9814#endif
9815
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009816 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009817 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009818 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009819 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009820 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009821 return( ret );
9822 }
9823 }
9824
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009825#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009826 ret = ssl_write_split( ssl, buf, len );
9827#else
9828 ret = ssl_write_real( ssl, buf, len );
9829#endif
9830
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009832
9833 return( ret );
9834}
9835
9836/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009837 * Notify the peer that the connection is being closed
9838 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009840{
9841 int ret;
9842
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009843 if( ssl == NULL || ssl->conf == NULL )
9844 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009847
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009848 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009851 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009853 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9854 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9855 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009858 return( ret );
9859 }
9860 }
9861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009863
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02009864 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009865}
9866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009867void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00009868{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009869 if( transform == NULL )
9870 return;
9871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009872#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00009873 deflateEnd( &transform->ctx_deflate );
9874 inflateEnd( &transform->ctx_inflate );
9875#endif
9876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009877 mbedtls_cipher_free( &transform->cipher_ctx_enc );
9878 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02009879
Hanno Beckerd56ed242018-01-03 15:32:51 +00009880#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009881 mbedtls_md_free( &transform->md_ctx_enc );
9882 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00009883#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009884
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009885 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009886}
9887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009888#if defined(MBEDTLS_X509_CRT_PARSE_C)
9889static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009890{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009891 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009892
9893 while( cur != NULL )
9894 {
9895 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009896 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009897 cur = next;
9898 }
9899}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02009901
Hanno Becker0271f962018-08-16 13:23:47 +01009902#if defined(MBEDTLS_SSL_PROTO_DTLS)
9903
9904static void ssl_buffering_free( mbedtls_ssl_context *ssl )
9905{
9906 unsigned offset;
9907 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9908
9909 if( hs == NULL )
9910 return;
9911
Hanno Becker283f5ef2018-08-24 09:34:47 +01009912 ssl_free_buffered_record( ssl );
9913
Hanno Becker0271f962018-08-16 13:23:47 +01009914 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01009915 ssl_buffering_free_slot( ssl, offset );
9916}
9917
9918static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
9919 uint8_t slot )
9920{
9921 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
9922 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01009923
9924 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
9925 return;
9926
Hanno Beckere605b192018-08-21 15:59:07 +01009927 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01009928 {
Hanno Beckere605b192018-08-21 15:59:07 +01009929 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01009930 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01009931 mbedtls_free( hs_buf->data );
9932 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01009933 }
9934}
9935
9936#endif /* MBEDTLS_SSL_PROTO_DTLS */
9937
Gilles Peskine9b562d52018-04-25 20:32:43 +02009938void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009939{
Gilles Peskine9b562d52018-04-25 20:32:43 +02009940 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
9941
Paul Bakkeraccaffe2014-06-26 13:37:14 +02009942 if( handshake == NULL )
9943 return;
9944
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009945#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
9946 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
9947 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02009948 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009949 handshake->async_in_progress = 0;
9950 }
9951#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
9952
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009953#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
9954 defined(MBEDTLS_SSL_PROTO_TLS1_1)
9955 mbedtls_md5_free( &handshake->fin_md5 );
9956 mbedtls_sha1_free( &handshake->fin_sha1 );
9957#endif
9958#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9959#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009960#if defined(MBEDTLS_USE_PSA_CRYPTO)
9961 psa_hash_abort( &handshake->fin_sha256_psa );
9962#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009963 mbedtls_sha256_free( &handshake->fin_sha256 );
9964#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009965#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009966#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05009967#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05009968 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05009969#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009970 mbedtls_sha512_free( &handshake->fin_sha512 );
9971#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05009972#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02009973#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009975#if defined(MBEDTLS_DHM_C)
9976 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00009977#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009978#if defined(MBEDTLS_ECDH_C)
9979 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02009980#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009981#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009982 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02009983#if defined(MBEDTLS_SSL_CLI_C)
9984 mbedtls_free( handshake->ecjpake_cache );
9985 handshake->ecjpake_cache = NULL;
9986 handshake->ecjpake_cache_len = 0;
9987#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02009988#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02009989
Janos Follath4ae5c292016-02-10 11:27:43 +00009990#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
9991 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02009992 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009993 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02009994#endif
9995
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009996#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
9997 if( handshake->psk != NULL )
9998 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009999 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010000 mbedtls_free( handshake->psk );
10001 }
10002#endif
10003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010004#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10005 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010006 /*
10007 * Free only the linked list wrapper, not the keys themselves
10008 * since the belong to the SNI callback
10009 */
10010 if( handshake->sni_key_cert != NULL )
10011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010012 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010013
10014 while( cur != NULL )
10015 {
10016 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010017 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010018 cur = next;
10019 }
10020 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010021#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010022
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010023#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010024 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010025 if( handshake->ecrs_peer_cert != NULL )
10026 {
10027 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10028 mbedtls_free( handshake->ecrs_peer_cert );
10029 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010030#endif
10031
Hanno Becker75173122019-02-06 16:18:31 +000010032#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10033 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10034 mbedtls_pk_free( &handshake->peer_pubkey );
10035#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010037#if defined(MBEDTLS_SSL_PROTO_DTLS)
10038 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010039 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010040 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010041#endif
10042
Hanno Becker4a63ed42019-01-08 11:39:35 +000010043#if defined(MBEDTLS_ECDH_C) && \
10044 defined(MBEDTLS_USE_PSA_CRYPTO)
10045 psa_destroy_key( handshake->ecdh_psa_privkey );
10046#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10047
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010048 mbedtls_platform_zeroize( handshake,
10049 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010050}
10051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010052void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010053{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010054 if( session == NULL )
10055 return;
10056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010057#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010058 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010059#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010060
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010061#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010062 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010063#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010064
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010065 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010066}
10067
Paul Bakker5121ce52009-01-03 21:22:43 +000010068/*
10069 * Free an SSL context
10070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010071void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010072{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010073 if( ssl == NULL )
10074 return;
10075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010077
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010078 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010079 {
Angus Grattond8213d02016-05-25 20:56:48 +100010080 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010081 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010082 }
10083
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010084 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010085 {
Angus Grattond8213d02016-05-25 20:56:48 +100010086 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010087 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010088 }
10089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010090#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010091 if( ssl->compress_buf != NULL )
10092 {
Angus Grattond8213d02016-05-25 20:56:48 +100010093 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010095 }
10096#endif
10097
Paul Bakker48916f92012-09-16 19:57:18 +000010098 if( ssl->transform )
10099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010100 mbedtls_ssl_transform_free( ssl->transform );
10101 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010102 }
10103
10104 if( ssl->handshake )
10105 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010106 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010107 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10108 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010110 mbedtls_free( ssl->handshake );
10111 mbedtls_free( ssl->transform_negotiate );
10112 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010113 }
10114
Paul Bakkerc0463502013-02-14 11:19:38 +010010115 if( ssl->session )
10116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010117 mbedtls_ssl_session_free( ssl->session );
10118 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010119 }
10120
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010121#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010122 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010123 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010124 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010125 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010126 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010127#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010129#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10130 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10133 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010134 }
10135#endif
10136
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010137#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010138 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010139#endif
10140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010142
Paul Bakker86f04f42013-02-14 11:20:09 +010010143 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010144 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010145}
10146
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010147/*
10148 * Initialze mbedtls_ssl_config
10149 */
10150void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10151{
10152 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10153}
10154
Simon Butcherc97b6972015-12-27 23:48:17 +000010155#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010156static int ssl_preset_default_hashes[] = {
10157#if defined(MBEDTLS_SHA512_C)
10158 MBEDTLS_MD_SHA512,
10159 MBEDTLS_MD_SHA384,
10160#endif
10161#if defined(MBEDTLS_SHA256_C)
10162 MBEDTLS_MD_SHA256,
10163 MBEDTLS_MD_SHA224,
10164#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010165#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010166 MBEDTLS_MD_SHA1,
10167#endif
10168 MBEDTLS_MD_NONE
10169};
Simon Butcherc97b6972015-12-27 23:48:17 +000010170#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010171
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010172static int ssl_preset_suiteb_ciphersuites[] = {
10173 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10174 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10175 0
10176};
10177
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010178#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010179static int ssl_preset_suiteb_hashes[] = {
10180 MBEDTLS_MD_SHA256,
10181 MBEDTLS_MD_SHA384,
10182 MBEDTLS_MD_NONE
10183};
10184#endif
10185
10186#if defined(MBEDTLS_ECP_C)
10187static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10188 MBEDTLS_ECP_DP_SECP256R1,
10189 MBEDTLS_ECP_DP_SECP384R1,
10190 MBEDTLS_ECP_DP_NONE
10191};
10192#endif
10193
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010194/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010195 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010196 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010197int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010198 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010199{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010200#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010201 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010202#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010203
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010204 /* Use the functions here so that they are covered in tests,
10205 * but otherwise access member directly for efficiency */
10206 mbedtls_ssl_conf_endpoint( conf, endpoint );
10207 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010208
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010209 /*
10210 * Things that are common to all presets
10211 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010212#if defined(MBEDTLS_SSL_CLI_C)
10213 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10214 {
10215 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10216#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10217 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10218#endif
10219 }
10220#endif
10221
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010222#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010223 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010224#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010225
10226#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10227 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10228#endif
10229
10230#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10231 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10232#endif
10233
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010234#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10235 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10236#endif
10237
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010238#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010239 conf->f_cookie_write = ssl_cookie_write_dummy;
10240 conf->f_cookie_check = ssl_cookie_check_dummy;
10241#endif
10242
10243#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10244 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10245#endif
10246
Janos Follath088ce432017-04-10 12:42:31 +010010247#if defined(MBEDTLS_SSL_SRV_C)
10248 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10249#endif
10250
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010251#if defined(MBEDTLS_SSL_PROTO_DTLS)
10252 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10253 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10254#endif
10255
10256#if defined(MBEDTLS_SSL_RENEGOTIATION)
10257 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010258 memset( conf->renego_period, 0x00, 2 );
10259 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010260#endif
10261
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010262#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10263 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10264 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010265 const unsigned char dhm_p[] =
10266 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10267 const unsigned char dhm_g[] =
10268 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10269
Hanno Beckera90658f2017-10-04 15:29:08 +010010270 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10271 dhm_p, sizeof( dhm_p ),
10272 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010273 {
10274 return( ret );
10275 }
10276 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010277#endif
10278
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010279 /*
10280 * Preset-specific defaults
10281 */
10282 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010283 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010284 /*
10285 * NSA Suite B
10286 */
10287 case MBEDTLS_SSL_PRESET_SUITEB:
10288 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10289 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10290 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10291 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10292
10293 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10294 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10295 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10296 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10297 ssl_preset_suiteb_ciphersuites;
10298
10299#if defined(MBEDTLS_X509_CRT_PARSE_C)
10300 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010301#endif
10302
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010303#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010304 conf->sig_hashes = ssl_preset_suiteb_hashes;
10305#endif
10306
10307#if defined(MBEDTLS_ECP_C)
10308 conf->curve_list = ssl_preset_suiteb_curves;
10309#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010310 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010311
10312 /*
10313 * Default
10314 */
10315 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010316 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10317 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10318 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10319 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10320 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10321 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10322 MBEDTLS_SSL_MIN_MINOR_VERSION :
10323 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010324 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10325 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10326
10327#if defined(MBEDTLS_SSL_PROTO_DTLS)
10328 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10329 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10330#endif
10331
10332 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10333 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10334 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10335 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10336 mbedtls_ssl_list_ciphersuites();
10337
10338#if defined(MBEDTLS_X509_CRT_PARSE_C)
10339 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10340#endif
10341
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010342#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010343 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010344#endif
10345
10346#if defined(MBEDTLS_ECP_C)
10347 conf->curve_list = mbedtls_ecp_grp_id_list();
10348#endif
10349
10350#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10351 conf->dhm_min_bitlen = 1024;
10352#endif
10353 }
10354
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010355 return( 0 );
10356}
10357
10358/*
10359 * Free mbedtls_ssl_config
10360 */
10361void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10362{
10363#if defined(MBEDTLS_DHM_C)
10364 mbedtls_mpi_free( &conf->dhm_P );
10365 mbedtls_mpi_free( &conf->dhm_G );
10366#endif
10367
10368#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10369 if( conf->psk != NULL )
10370 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010371 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010372 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010373 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010374 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010375 }
10376
10377 if( conf->psk_identity != NULL )
10378 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010379 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010380 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010381 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010382 conf->psk_identity_len = 0;
10383 }
10384#endif
10385
10386#if defined(MBEDTLS_X509_CRT_PARSE_C)
10387 ssl_key_cert_free( conf->key_cert );
10388#endif
10389
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010390 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010391}
10392
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010393#if defined(MBEDTLS_PK_C) && \
10394 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010395/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010396 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010398unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010399{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010400#if defined(MBEDTLS_RSA_C)
10401 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10402 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010403#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010404#if defined(MBEDTLS_ECDSA_C)
10405 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10406 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010407#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010408 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010409}
10410
Hanno Becker7e5437a2017-04-28 17:15:26 +010010411unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10412{
10413 switch( type ) {
10414 case MBEDTLS_PK_RSA:
10415 return( MBEDTLS_SSL_SIG_RSA );
10416 case MBEDTLS_PK_ECDSA:
10417 case MBEDTLS_PK_ECKEY:
10418 return( MBEDTLS_SSL_SIG_ECDSA );
10419 default:
10420 return( MBEDTLS_SSL_SIG_ANON );
10421 }
10422}
10423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010424mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010425{
10426 switch( sig )
10427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010428#if defined(MBEDTLS_RSA_C)
10429 case MBEDTLS_SSL_SIG_RSA:
10430 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010431#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010432#if defined(MBEDTLS_ECDSA_C)
10433 case MBEDTLS_SSL_SIG_ECDSA:
10434 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010435#endif
10436 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010437 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010438 }
10439}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010440#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010441
Hanno Becker7e5437a2017-04-28 17:15:26 +010010442#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10443 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10444
10445/* Find an entry in a signature-hash set matching a given hash algorithm. */
10446mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10447 mbedtls_pk_type_t sig_alg )
10448{
10449 switch( sig_alg )
10450 {
10451 case MBEDTLS_PK_RSA:
10452 return( set->rsa );
10453 case MBEDTLS_PK_ECDSA:
10454 return( set->ecdsa );
10455 default:
10456 return( MBEDTLS_MD_NONE );
10457 }
10458}
10459
10460/* Add a signature-hash-pair to a signature-hash set */
10461void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10462 mbedtls_pk_type_t sig_alg,
10463 mbedtls_md_type_t md_alg )
10464{
10465 switch( sig_alg )
10466 {
10467 case MBEDTLS_PK_RSA:
10468 if( set->rsa == MBEDTLS_MD_NONE )
10469 set->rsa = md_alg;
10470 break;
10471
10472 case MBEDTLS_PK_ECDSA:
10473 if( set->ecdsa == MBEDTLS_MD_NONE )
10474 set->ecdsa = md_alg;
10475 break;
10476
10477 default:
10478 break;
10479 }
10480}
10481
10482/* Allow exactly one hash algorithm for each signature. */
10483void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10484 mbedtls_md_type_t md_alg )
10485{
10486 set->rsa = md_alg;
10487 set->ecdsa = md_alg;
10488}
10489
10490#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10491 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10492
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010493/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010494 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010496mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010497{
10498 switch( hash )
10499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010500#if defined(MBEDTLS_MD5_C)
10501 case MBEDTLS_SSL_HASH_MD5:
10502 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010503#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010504#if defined(MBEDTLS_SHA1_C)
10505 case MBEDTLS_SSL_HASH_SHA1:
10506 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010507#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010508#if defined(MBEDTLS_SHA256_C)
10509 case MBEDTLS_SSL_HASH_SHA224:
10510 return( MBEDTLS_MD_SHA224 );
10511 case MBEDTLS_SSL_HASH_SHA256:
10512 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010513#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010514#if defined(MBEDTLS_SHA512_C)
10515 case MBEDTLS_SSL_HASH_SHA384:
10516 return( MBEDTLS_MD_SHA384 );
10517 case MBEDTLS_SSL_HASH_SHA512:
10518 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010519#endif
10520 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010521 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010522 }
10523}
10524
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010525/*
10526 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10527 */
10528unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10529{
10530 switch( md )
10531 {
10532#if defined(MBEDTLS_MD5_C)
10533 case MBEDTLS_MD_MD5:
10534 return( MBEDTLS_SSL_HASH_MD5 );
10535#endif
10536#if defined(MBEDTLS_SHA1_C)
10537 case MBEDTLS_MD_SHA1:
10538 return( MBEDTLS_SSL_HASH_SHA1 );
10539#endif
10540#if defined(MBEDTLS_SHA256_C)
10541 case MBEDTLS_MD_SHA224:
10542 return( MBEDTLS_SSL_HASH_SHA224 );
10543 case MBEDTLS_MD_SHA256:
10544 return( MBEDTLS_SSL_HASH_SHA256 );
10545#endif
10546#if defined(MBEDTLS_SHA512_C)
10547 case MBEDTLS_MD_SHA384:
10548 return( MBEDTLS_SSL_HASH_SHA384 );
10549 case MBEDTLS_MD_SHA512:
10550 return( MBEDTLS_SSL_HASH_SHA512 );
10551#endif
10552 default:
10553 return( MBEDTLS_SSL_HASH_NONE );
10554 }
10555}
10556
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010557#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010558/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010559 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010560 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010561 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010562int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010563{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010564 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010565
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010566 if( ssl->conf->curve_list == NULL )
10567 return( -1 );
10568
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010569 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010570 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010571 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010572
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010573 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010574}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010575#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010576
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010577#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010578/*
10579 * Check if a hash proposed by the peer is in our list.
10580 * Return 0 if we're willing to use it, -1 otherwise.
10581 */
10582int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10583 mbedtls_md_type_t md )
10584{
10585 const int *cur;
10586
10587 if( ssl->conf->sig_hashes == NULL )
10588 return( -1 );
10589
10590 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10591 if( *cur == (int) md )
10592 return( 0 );
10593
10594 return( -1 );
10595}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010596#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010598#if defined(MBEDTLS_X509_CRT_PARSE_C)
10599int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10600 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010601 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010602 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010603{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010604 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010605#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010606 int usage = 0;
10607#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010608#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010609 const char *ext_oid;
10610 size_t ext_len;
10611#endif
10612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010613#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10614 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010615 ((void) cert);
10616 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010617 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010618#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010620#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10621 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010622 {
10623 /* Server part of the key exchange */
10624 switch( ciphersuite->key_exchange )
10625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010626 case MBEDTLS_KEY_EXCHANGE_RSA:
10627 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010628 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010629 break;
10630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010631 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10632 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10633 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10634 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010635 break;
10636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010637 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10638 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010639 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010640 break;
10641
10642 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010643 case MBEDTLS_KEY_EXCHANGE_NONE:
10644 case MBEDTLS_KEY_EXCHANGE_PSK:
10645 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10646 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010647 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010648 usage = 0;
10649 }
10650 }
10651 else
10652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010653 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10654 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010655 }
10656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010657 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010658 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010659 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010660 ret = -1;
10661 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010662#else
10663 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010664#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010666#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10667 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010669 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10670 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010671 }
10672 else
10673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010674 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10675 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010676 }
10677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010678 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010679 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010680 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010681 ret = -1;
10682 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010683#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010684
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010685 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010687#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010688
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010689/*
10690 * Convert version numbers to/from wire format
10691 * and, for DTLS, to/from TLS equivalent.
10692 *
10693 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010694 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010695 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10696 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010698void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010699 unsigned char ver[2] )
10700{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010701#if defined(MBEDTLS_SSL_PROTO_DTLS)
10702 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010704 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010705 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10706
10707 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10708 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10709 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010710 else
10711#else
10712 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010713#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010714 {
10715 ver[0] = (unsigned char) major;
10716 ver[1] = (unsigned char) minor;
10717 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010718}
10719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010720void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010721 const unsigned char ver[2] )
10722{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010723#if defined(MBEDTLS_SSL_PROTO_DTLS)
10724 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010725 {
10726 *major = 255 - ver[0] + 2;
10727 *minor = 255 - ver[1] + 1;
10728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010729 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010730 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10731 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010732 else
10733#else
10734 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010735#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010736 {
10737 *major = ver[0];
10738 *minor = ver[1];
10739 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010740}
10741
Simon Butcher99000142016-10-13 17:21:01 +010010742int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10743{
10744#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10745 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10746 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10747
10748 switch( md )
10749 {
10750#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10751#if defined(MBEDTLS_MD5_C)
10752 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010753 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010754#endif
10755#if defined(MBEDTLS_SHA1_C)
10756 case MBEDTLS_SSL_HASH_SHA1:
10757 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10758 break;
10759#endif
10760#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10761#if defined(MBEDTLS_SHA512_C)
10762 case MBEDTLS_SSL_HASH_SHA384:
10763 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10764 break;
10765#endif
10766#if defined(MBEDTLS_SHA256_C)
10767 case MBEDTLS_SSL_HASH_SHA256:
10768 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10769 break;
10770#endif
10771 default:
10772 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10773 }
10774
10775 return 0;
10776#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10777 (void) ssl;
10778 (void) md;
10779
10780 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10781#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10782}
10783
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010784#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10785 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10786int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10787 unsigned char *output,
10788 unsigned char *data, size_t data_len )
10789{
10790 int ret = 0;
10791 mbedtls_md5_context mbedtls_md5;
10792 mbedtls_sha1_context mbedtls_sha1;
10793
10794 mbedtls_md5_init( &mbedtls_md5 );
10795 mbedtls_sha1_init( &mbedtls_sha1 );
10796
10797 /*
10798 * digitally-signed struct {
10799 * opaque md5_hash[16];
10800 * opaque sha_hash[20];
10801 * };
10802 *
10803 * md5_hash
10804 * MD5(ClientHello.random + ServerHello.random
10805 * + ServerParams);
10806 * sha_hash
10807 * SHA(ClientHello.random + ServerHello.random
10808 * + ServerParams);
10809 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010810 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010811 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010812 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010813 goto exit;
10814 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010815 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010816 ssl->handshake->randbytes, 64 ) ) != 0 )
10817 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010818 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010819 goto exit;
10820 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010821 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010822 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_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_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010827 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010828 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010829 goto exit;
10830 }
10831
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010832 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010833 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010834 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010835 goto exit;
10836 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010837 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010838 ssl->handshake->randbytes, 64 ) ) != 0 )
10839 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010841 goto exit;
10842 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010843 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010844 data_len ) ) != 0 )
10845 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010846 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010847 goto exit;
10848 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010849 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010850 output + 16 ) ) != 0 )
10851 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010010852 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010853 goto exit;
10854 }
10855
10856exit:
10857 mbedtls_md5_free( &mbedtls_md5 );
10858 mbedtls_sha1_free( &mbedtls_sha1 );
10859
10860 if( ret != 0 )
10861 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10862 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10863
10864 return( ret );
10865
10866}
10867#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
10868 MBEDTLS_SSL_PROTO_TLS1_1 */
10869
10870#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10871 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010872
10873#if defined(MBEDTLS_USE_PSA_CRYPTO)
10874int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
10875 unsigned char *hash, size_t *hashlen,
10876 unsigned char *data, size_t data_len,
10877 mbedtls_md_type_t md_alg )
10878{
Andrzej Kurek814feff2019-01-14 04:35:19 -050010879 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000010880 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010881 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
10882
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010883 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010884
10885 if( ( status = psa_hash_setup( &hash_operation,
10886 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010887 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010888 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010889 goto exit;
10890 }
10891
Andrzej Kurek814feff2019-01-14 04:35:19 -050010892 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
10893 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010894 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010895 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010896 goto exit;
10897 }
10898
Andrzej Kurek814feff2019-01-14 04:35:19 -050010899 if( ( status = psa_hash_update( &hash_operation,
10900 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010901 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010902 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010903 goto exit;
10904 }
10905
Andrzej Kurek814feff2019-01-14 04:35:19 -050010906 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
10907 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010908 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050010909 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010910 goto exit;
10911 }
10912
10913exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050010914 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010915 {
10916 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10917 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010918 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010919 {
10920 case PSA_ERROR_NOT_SUPPORTED:
10921 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010922 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010923 case PSA_ERROR_BUFFER_TOO_SMALL:
10924 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
10925 case PSA_ERROR_INSUFFICIENT_MEMORY:
10926 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
10927 default:
10928 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
10929 }
10930 }
10931 return( 0 );
10932}
10933
10934#else
10935
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010936int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020010937 unsigned char *hash, size_t *hashlen,
10938 unsigned char *data, size_t data_len,
10939 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010940{
10941 int ret = 0;
10942 mbedtls_md_context_t ctx;
10943 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020010944 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010945
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010010946 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050010947
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010948 mbedtls_md_init( &ctx );
10949
10950 /*
10951 * digitally-signed struct {
10952 * opaque client_random[32];
10953 * opaque server_random[32];
10954 * ServerDHParams params;
10955 * };
10956 */
10957 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
10958 {
10959 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
10960 goto exit;
10961 }
10962 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
10963 {
10964 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
10965 goto exit;
10966 }
10967 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
10968 {
10969 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10970 goto exit;
10971 }
10972 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
10973 {
10974 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
10975 goto exit;
10976 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020010977 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010978 {
10979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
10980 goto exit;
10981 }
10982
10983exit:
10984 mbedtls_md_free( &ctx );
10985
10986 if( ret != 0 )
10987 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10988 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
10989
10990 return( ret );
10991}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050010992#endif /* MBEDTLS_USE_PSA_CRYPTO */
10993
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010994#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
10995 MBEDTLS_SSL_PROTO_TLS1_2 */
10996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010997#endif /* MBEDTLS_SSL_TLS_C */