blob: 3e23b67595e912a38868240a27e5d10fbe88c57e [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
Hanno Becker2a43f6f2018-08-10 11:12:52 +010062static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010063static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010064
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010065/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020069 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010070 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010071#else
72 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010073#endif
74 return( 0 );
75}
76
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077/*
78 * Start a timer.
79 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020082{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020083 if( ssl->f_set_timer == NULL )
84 return;
85
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
87 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020088}
89
90/*
91 * Return -1 is timer is expired, 0 if it isn't.
92 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020094{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020095 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020096 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020097
98 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020099 {
100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200101 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200102 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200103
104 return( 0 );
105}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200106
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100107static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
108 mbedtls_ssl_transform *transform );
Hanno Becker79594fd2019-05-08 09:38:41 +0100109static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100110
Hanno Beckercfe45792019-07-03 16:13:00 +0100111#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker54229812019-07-12 14:40:00 +0100112static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
113 unsigned char *buf,
114 size_t len,
115 mbedtls_record *rec );
116
Hanno Beckercfe45792019-07-03 16:13:00 +0100117int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
118 unsigned char *buf,
119 size_t buflen )
120{
Hanno Becker54229812019-07-12 14:40:00 +0100121 int ret = 0;
122 mbedtls_record rec;
123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
124 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
125
126 /* We don't support record checking in TLS because
127 * (a) there doesn't seem to be a usecase for it, and
128 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
129 * and we'd need to backup the transform here.
130 */
131 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
132 {
133 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
134 goto exit;
135 }
136#if defined(MBEDTLS_SSL_PROTO_DTLS)
137 else
138 {
139 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
140 if( ret != 0 )
141 {
142 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
143 goto exit;
144 }
145
146 if( ssl->transform_in != NULL )
147 {
148 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
149 if( ret != 0 )
150 {
151 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
152 goto exit;
153 }
154 }
155 }
156#endif /* MBEDTLS_SSL_PROTO_DTLS */
157
158exit:
159 /* On success, we have decrypted the buffer in-place, so make
160 * sure we don't leak any plaintext data. */
161 mbedtls_platform_zeroize( buf, buflen );
162
163 /* For the purpose of this API, treat messages with unexpected CID
164 * as well as such from future epochs as unexpected. */
165 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
166 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
167 {
168 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
169 }
170
171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
172 return( ret );
Hanno Beckercfe45792019-07-03 16:13:00 +0100173}
174#endif /* MBEDTLS_SSL_RECORD_CHECKING */
175
Hanno Becker67bc7c32018-08-06 11:33:50 +0100176#define SSL_DONT_FORCE_FLUSH 0
177#define SSL_FORCE_FLUSH 1
178
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200179#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100180
Hanno Beckera0e20d02019-05-15 14:03:01 +0100181#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100182/* Top-level Connection ID API */
183
Hanno Becker8367ccc2019-05-14 11:30:10 +0100184int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
185 size_t len,
186 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100187{
188 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
189 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
190
Hanno Becker611ac772019-05-14 11:45:26 +0100191 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
192 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
193 {
194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
195 }
196
197 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100198 conf->cid_len = len;
199 return( 0 );
200}
201
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100202int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
203 int enable,
204 unsigned char const *own_cid,
205 size_t own_cid_len )
206{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100207 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
208 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
209
Hanno Beckerca092242019-04-25 16:01:49 +0100210 ssl->negotiate_cid = enable;
211 if( enable == MBEDTLS_SSL_CID_DISABLED )
212 {
213 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
214 return( 0 );
215 }
216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100217 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100218
Hanno Beckerad4a1372019-05-03 13:06:44 +0100219 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100220 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
222 (unsigned) own_cid_len,
223 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
225 }
226
227 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100228 /* Truncation is not an issue here because
229 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
230 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100231
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100232 return( 0 );
233}
234
235int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
236 int *enabled,
237 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
238 size_t *peer_cid_len )
239{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100240 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100241
Hanno Becker76a79ab2019-05-03 14:38:32 +0100242 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
243 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
244 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100245 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100246 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100247
Hanno Beckerc5f24222019-05-03 12:54:52 +0100248 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
249 * were used, but client and server requested the empty CID.
250 * This is indistinguishable from not using the CID extension
251 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100252 if( ssl->transform_in->in_cid_len == 0 &&
253 ssl->transform_in->out_cid_len == 0 )
254 {
255 return( 0 );
256 }
257
Hanno Becker615ef172019-05-22 16:50:35 +0100258 if( peer_cid_len != NULL )
259 {
260 *peer_cid_len = ssl->transform_in->out_cid_len;
261 if( peer_cid != NULL )
262 {
263 memcpy( peer_cid, ssl->transform_in->out_cid,
264 ssl->transform_in->out_cid_len );
265 }
266 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100267
268 *enabled = MBEDTLS_SSL_CID_ENABLED;
269
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100270 return( 0 );
271}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100272#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100273
Hanno Beckerd5847772018-08-28 10:09:23 +0100274/* Forward declarations for functions related to message buffering. */
275static void ssl_buffering_free( mbedtls_ssl_context *ssl );
276static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
277 uint8_t slot );
278static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
279static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
280static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
281static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100282static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
283 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100284static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100285
Hanno Beckera67dee22018-08-22 10:05:20 +0100286static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100287static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100288{
Hanno Becker11682cc2018-08-22 14:41:02 +0100289 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100290
291 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100292 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100293
294 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
295}
296
Hanno Becker67bc7c32018-08-06 11:33:50 +0100297static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
298{
Hanno Becker11682cc2018-08-22 14:41:02 +0100299 size_t const bytes_written = ssl->out_left;
300 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100301
302 /* Double-check that the write-index hasn't gone
303 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100304 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100305 {
306 /* Should never happen... */
307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
308 }
309
310 return( (int) ( mtu - bytes_written ) );
311}
312
313static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
314{
315 int ret;
316 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400317 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100318
319#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
320 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
321
322 if( max_len > mfl )
323 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100324
325 /* By the standard (RFC 6066 Sect. 4), the MFL extension
326 * only limits the maximum record payload size, so in theory
327 * we would be allowed to pack multiple records of payload size
328 * MFL into a single datagram. However, this would mean that there's
329 * no way to explicitly communicate MTU restrictions to the peer.
330 *
331 * The following reduction of max_len makes sure that we never
332 * write datagrams larger than MFL + Record Expansion Overhead.
333 */
334 if( max_len <= ssl->out_left )
335 return( 0 );
336
337 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100338#endif
339
340 ret = ssl_get_remaining_space_in_datagram( ssl );
341 if( ret < 0 )
342 return( ret );
343 remaining = (size_t) ret;
344
345 ret = mbedtls_ssl_get_record_expansion( ssl );
346 if( ret < 0 )
347 return( ret );
348 expansion = (size_t) ret;
349
350 if( remaining <= expansion )
351 return( 0 );
352
353 remaining -= expansion;
354 if( remaining >= max_len )
355 remaining = max_len;
356
357 return( (int) remaining );
358}
359
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200360/*
361 * Double the retransmit timeout value, within the allowed range,
362 * returning -1 if the maximum value has already been reached.
363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200365{
366 uint32_t new_timeout;
367
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200368 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200369 return( -1 );
370
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200371 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
372 * in the following way: after the initial transmission and a first
373 * retransmission, back off to a temporary estimated MTU of 508 bytes.
374 * This value is guaranteed to be deliverable (if not guaranteed to be
375 * delivered) of any compliant IPv4 (and IPv6) network, and should work
376 * on most non-IP stacks too. */
377 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400378 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200379 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
381 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200382
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200383 new_timeout = 2 * ssl->handshake->retransmit_timeout;
384
385 /* Avoid arithmetic overflow and range overflow */
386 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200387 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200388 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200389 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200390 }
391
392 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200394 ssl->handshake->retransmit_timeout ) );
395
396 return( 0 );
397}
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200400{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200401 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200403 ssl->handshake->retransmit_timeout ) );
404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200408/*
409 * Convert max_fragment_length codes to length.
410 * RFC 6066 says:
411 * enum{
412 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
413 * } MaxFragmentLength;
414 * and we add 0 -> extension unused
415 */
Angus Grattond8213d02016-05-25 20:56:48 +1000416static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200417{
Angus Grattond8213d02016-05-25 20:56:48 +1000418 switch( mfl )
419 {
420 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
421 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
422 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
423 return 512;
424 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
425 return 1024;
426 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
427 return 2048;
428 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
429 return 4096;
430 default:
431 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
432 }
433}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200435
Hanno Becker52055ae2019-02-06 14:30:46 +0000436int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
437 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200438{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_ssl_session_free( dst );
440 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000443
444#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200445 if( src->peer_cert != NULL )
446 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200447 int ret;
448
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200449 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200450 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200451 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200456 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200459 dst->peer_cert = NULL;
460 return( ret );
461 }
462 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000463#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000464 if( src->peer_cert_digest != NULL )
465 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000466 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000467 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000468 if( dst->peer_cert_digest == NULL )
469 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
470
471 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
472 src->peer_cert_digest_len );
473 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000474 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000475 }
476#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200479
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200480#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200481 if( src->ticket != NULL )
482 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200483 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200484 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200485 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200486
487 memcpy( dst->ticket, src->ticket, src->ticket_len );
488 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200489#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200490
491 return( 0 );
492}
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
495int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200496 const unsigned char *key_enc, const unsigned char *key_dec,
497 size_t keylen,
498 const unsigned char *iv_enc, const unsigned char *iv_dec,
499 size_t ivlen,
500 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200501 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
503int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
504int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
505int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
506int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
507#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000508
Paul Bakker5121ce52009-01-03 21:22:43 +0000509/*
510 * Key material generation
511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200513static int ssl3_prf( const unsigned char *secret, size_t slen,
514 const char *label,
515 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000516 unsigned char *dstbuf, size_t dlen )
517{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100518 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000519 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200520 mbedtls_md5_context md5;
521 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000522 unsigned char padding[16];
523 unsigned char sha1sum[20];
524 ((void)label);
525
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200526 mbedtls_md5_init( &md5 );
527 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200528
Paul Bakker5f70b252012-09-13 14:23:06 +0000529 /*
530 * SSLv3:
531 * block =
532 * MD5( secret + SHA1( 'A' + secret + random ) ) +
533 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
534 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
535 * ...
536 */
537 for( i = 0; i < dlen / 16; i++ )
538 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200539 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000540
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100541 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100542 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100543 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100544 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100545 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100546 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100547 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100548 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100549 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100550 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000551
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100552 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100553 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100554 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100555 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100556 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100557 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100558 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100559 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000560 }
561
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100562exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200563 mbedtls_md5_free( &md5 );
564 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000565
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500566 mbedtls_platform_zeroize( padding, sizeof( padding ) );
567 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000568
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100569 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000570}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200574static int tls1_prf( const unsigned char *secret, size_t slen,
575 const char *label,
576 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000577 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000578{
Paul Bakker23986e52011-04-24 08:57:21 +0000579 size_t nb, hs;
580 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200581 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300582 unsigned char *tmp;
583 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 const mbedtls_md_info_t *md_info;
586 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100587 int ret;
588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
Ron Eldor3b350852019-05-07 18:31:49 +0300591 tmp_len = 20 + strlen( label ) + rlen;
592 tmp = mbedtls_calloc( 1, tmp_len );
593 if( tmp == NULL )
594 {
595 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
596 goto exit;
597 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599 hs = ( slen + 1 ) / 2;
600 S1 = secret;
601 S2 = secret + slen - hs;
602
603 nb = strlen( label );
604 memcpy( tmp + 20, label, nb );
605 memcpy( tmp + 20 + nb, random, rlen );
606 nb += rlen;
607
608 /*
609 * First compute P_md5(secret,label+random)[0..dlen]
610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300612 {
613 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
614 goto exit;
615 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300618 {
619 goto exit;
620 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
623 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
624 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
626 for( i = 0; i < dlen; i += 16 )
627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_md_hmac_reset ( &md_ctx );
629 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
630 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_md_hmac_reset ( &md_ctx );
633 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
634 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
636 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
637
638 for( j = 0; j < k; j++ )
639 dstbuf[i + j] = h_i[j];
640 }
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100643
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 /*
645 * XOR out with P_sha1(secret,label+random)[0..dlen]
646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300648 {
649 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
650 goto exit;
651 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300654 {
655 goto exit;
656 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
659 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
660 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
662 for( i = 0; i < dlen; i += 20 )
663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_md_hmac_reset ( &md_ctx );
665 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
666 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_md_hmac_reset ( &md_ctx );
669 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
670 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
673
674 for( j = 0; j < k; j++ )
675 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
676 }
677
Ron Eldor3b350852019-05-07 18:31:49 +0300678exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100680
Ron Eldor3b350852019-05-07 18:31:49 +0300681 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500682 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
Ron Eldor3b350852019-05-07 18:31:49 +0300684 mbedtls_free( tmp );
685 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500690#if defined(MBEDTLS_USE_PSA_CRYPTO)
691static int tls_prf_generic( mbedtls_md_type_t md_type,
692 const unsigned char *secret, size_t slen,
693 const char *label,
694 const unsigned char *random, size_t rlen,
695 unsigned char *dstbuf, size_t dlen )
696{
697 psa_status_t status;
698 psa_algorithm_t alg;
Janos Follath53b8ec22019-08-08 10:28:27 +0100699 psa_key_attributes_t key_attributes;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500700 psa_key_handle_t master_slot;
Janos Follathda6ac012019-08-16 13:47:29 +0100701 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +0100702 PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500703
Andrzej Kurekc929a822019-01-14 03:51:11 -0500704 if( md_type == MBEDTLS_MD_SHA384 )
705 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
706 else
707 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
708
Janos Follath53b8ec22019-08-08 10:28:27 +0100709 key_attributes = psa_key_attributes_init();
710 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
711 psa_set_key_algorithm( &key_attributes, alg );
712 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500713
Janos Follath53b8ec22019-08-08 10:28:27 +0100714 status = psa_import_key( &key_attributes, secret, slen, &master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500715 if( status != PSA_SUCCESS )
716 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
717
Janos Follathda6ac012019-08-16 13:47:29 +0100718 status = psa_key_derivation( &derivation,
Andrzej Kurekc929a822019-01-14 03:51:11 -0500719 master_slot, alg,
720 random, rlen,
721 (unsigned char const *) label,
722 (size_t) strlen( label ),
723 dlen );
724 if( status != PSA_SUCCESS )
725 {
Janos Follathda6ac012019-08-16 13:47:29 +0100726 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500727 psa_destroy_key( master_slot );
728 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
729 }
730
Janos Follathda6ac012019-08-16 13:47:29 +0100731 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500732 if( status != PSA_SUCCESS )
733 {
Janos Follathda6ac012019-08-16 13:47:29 +0100734 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500735 psa_destroy_key( master_slot );
736 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
737 }
738
Janos Follathda6ac012019-08-16 13:47:29 +0100739 status = psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500740 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500741 {
742 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500743 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500744 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500745
746 status = psa_destroy_key( master_slot );
747 if( status != PSA_SUCCESS )
748 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
749
Andrzej Kurek33171262019-01-15 03:25:18 -0500750 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500751}
752
753#else /* MBEDTLS_USE_PSA_CRYPTO */
754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100756 const unsigned char *secret, size_t slen,
757 const char *label,
758 const unsigned char *random, size_t rlen,
759 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000760{
761 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100762 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300763 unsigned char *tmp;
764 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
766 const mbedtls_md_info_t *md_info;
767 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100768 int ret;
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100776
Ron Eldor3b350852019-05-07 18:31:49 +0300777 tmp_len = md_len + strlen( label ) + rlen;
778 tmp = mbedtls_calloc( 1, tmp_len );
779 if( tmp == NULL )
780 {
781 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
782 goto exit;
783 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000784
785 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100786 memcpy( tmp + md_len, label, nb );
787 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000788 nb += rlen;
789
790 /*
791 * Compute P_<hash>(secret, label + random)[0..dlen]
792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300794 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
797 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
798 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100799
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100800 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_md_hmac_reset ( &md_ctx );
803 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
804 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_md_hmac_reset ( &md_ctx );
807 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
808 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000809
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100810 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000811
812 for( j = 0; j < k; j++ )
813 dstbuf[i + j] = h_i[j];
814 }
815
Ron Eldor3b350852019-05-07 18:31:49 +0300816exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100818
Ron Eldor3b350852019-05-07 18:31:49 +0300819 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500820 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000821
Ron Eldor3b350852019-05-07 18:31:49 +0300822 mbedtls_free( tmp );
823
824 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000825}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500826#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100828static int tls_prf_sha256( const unsigned char *secret, size_t slen,
829 const char *label,
830 const unsigned char *random, size_t rlen,
831 unsigned char *dstbuf, size_t dlen )
832{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100834 label, random, rlen, dstbuf, dlen ) );
835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200839static int tls_prf_sha384( const unsigned char *secret, size_t slen,
840 const char *label,
841 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000842 unsigned char *dstbuf, size_t dlen )
843{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100845 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000846}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#endif /* MBEDTLS_SHA512_C */
848#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
853 defined(MBEDTLS_SSL_PROTO_TLS1_1)
854static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200855#endif
Paul Bakker380da532012-04-18 16:10:25 +0000856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200858static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200860#endif
861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200863static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200865#endif
866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
868#if defined(MBEDTLS_SHA256_C)
869static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200870static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200872#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874#if defined(MBEDTLS_SHA512_C)
875static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200876static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100878#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000880
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100881#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100882 defined(MBEDTLS_USE_PSA_CRYPTO)
883static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
884{
885 if( ssl->conf->f_psk != NULL )
886 {
887 /* If we've used a callback to select the PSK,
888 * the static configuration is irrelevant. */
889 if( ssl->handshake->psk_opaque != 0 )
890 return( 1 );
891
892 return( 0 );
893 }
894
895 if( ssl->conf->psk_opaque != 0 )
896 return( 1 );
897
898 return( 0 );
899}
900#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100901 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100902
Ron Eldorcf280092019-05-14 20:19:13 +0300903#if defined(MBEDTLS_SSL_EXPORT_KEYS)
904static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
905{
906#if defined(MBEDTLS_SSL_PROTO_SSL3)
907 if( tls_prf == ssl3_prf )
908 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300909 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300910 }
911 else
912#endif
913#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
914 if( tls_prf == tls1_prf )
915 {
916 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
917 }
918 else
919#endif
920#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
921#if defined(MBEDTLS_SHA512_C)
922 if( tls_prf == tls_prf_sha384 )
923 {
924 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
925 }
926 else
927#endif
928#if defined(MBEDTLS_SHA256_C)
929 if( tls_prf == tls_prf_sha256 )
930 {
931 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
932 }
933 else
934#endif
935#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
936 return( MBEDTLS_SSL_TLS_PRF_NONE );
937}
938#endif /* MBEDTLS_SSL_EXPORT_KEYS */
939
Ron Eldor51d3ab52019-05-12 14:54:30 +0300940int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
941 const unsigned char *secret, size_t slen,
942 const char *label,
943 const unsigned char *random, size_t rlen,
944 unsigned char *dstbuf, size_t dlen )
945{
946 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
947
948 switch( prf )
949 {
950#if defined(MBEDTLS_SSL_PROTO_SSL3)
951 case MBEDTLS_SSL_TLS_PRF_SSL3:
952 tls_prf = ssl3_prf;
953 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300954#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300955#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
956 case MBEDTLS_SSL_TLS_PRF_TLS1:
957 tls_prf = tls1_prf;
958 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300959#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
960
961#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300962#if defined(MBEDTLS_SHA512_C)
963 case MBEDTLS_SSL_TLS_PRF_SHA384:
964 tls_prf = tls_prf_sha384;
965 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300966#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300967#if defined(MBEDTLS_SHA256_C)
968 case MBEDTLS_SSL_TLS_PRF_SHA256:
969 tls_prf = tls_prf_sha256;
970 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300971#endif /* MBEDTLS_SHA256_C */
972#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300973 default:
974 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
975 }
976
977 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
978}
979
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200980/*
981 * This function will ultimetaly only be responsible for populating a
982 * transform structure from data passed as explicit parameters.
983 *
984 * For now however it's doing rather more in a rather less explicit way.
985 */
986static int ssl_populate_transform( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000987{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200988 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000989#if defined(MBEDTLS_USE_PSA_CRYPTO)
990 int psa_fallthrough;
991#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 unsigned char keyblk[256];
993 unsigned char *key1;
994 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100995 unsigned char *mac_enc;
996 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000997 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200998 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000999 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001000 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 const mbedtls_cipher_info_t *cipher_info;
1002 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 mbedtls_ssl_session *session = ssl->session_negotiate;
1005 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
1006 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +00001007
Jaeden Amero2de07f12019-06-05 13:32:08 +01001008#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1009 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001010 transform->encrypt_then_mac = session->encrypt_then_mac;
1011#endif
1012 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001013
1014 ciphersuite_info = handshake->ciphersuite_info;
1015 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001016 if( cipher_info == NULL )
1017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001019 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001021 }
1022
Hanno Beckere694c3e2017-12-27 21:34:08 +00001023 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001024 if( md_info == NULL )
1025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001027 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001029 }
1030
Hanno Beckera0e20d02019-05-15 14:03:01 +01001031#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001032 /* Copy own and peer's CID if the use of the CID
1033 * extension has been negotiated. */
1034 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1035 {
1036 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001037
Hanno Becker05154c32019-05-03 15:23:51 +01001038 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001039 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001040 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001041 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001042
1043 transform->out_cid_len = ssl->handshake->peer_cid_len;
1044 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1045 ssl->handshake->peer_cid_len );
1046 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1047 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001048 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001049#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001050
Paul Bakker5121ce52009-01-03 21:22:43 +00001051 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001052 * SSLv3:
1053 * key block =
1054 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1055 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1056 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1057 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1058 * ...
1059 *
1060 * TLSv1:
1061 * key block = PRF( master, "key expansion", randbytes )
1062 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001063 ret = handshake->tls_prf( session->master, 48, "key expansion",
1064 handshake->randbytes, 64, keyblk, 256 );
1065 if( ret != 0 )
1066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001068 return( ret );
1069 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1072 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1073 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1074 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1075 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001076
Paul Bakker5121ce52009-01-03 21:22:43 +00001077 /*
1078 * Determine the appropriate key, IV and MAC length.
1079 */
Paul Bakker68884e32013-01-07 18:20:04 +01001080
Hanno Becker88aaf652017-12-27 08:17:40 +00001081 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001082
Hanno Becker8031d062018-01-03 15:32:31 +00001083#if defined(MBEDTLS_GCM_C) || \
1084 defined(MBEDTLS_CCM_C) || \
1085 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001087 cipher_info->mode == MBEDTLS_MODE_CCM ||
1088 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001089 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001090 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001091
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001092 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001093 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001094 transform->taglen =
1095 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001096
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001097 /* All modes haves 96-bit IVs;
1098 * GCM and CCM has 4 implicit and 8 explicit bytes
1099 * ChachaPoly has all 12 bytes implicit
1100 */
Paul Bakker68884e32013-01-07 18:20:04 +01001101 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001102 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1103 transform->fixed_ivlen = 12;
1104 else
1105 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001106
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001107 /* Minimum length of encrypted record */
1108 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001109 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001110 }
1111 else
Hanno Becker8031d062018-01-03 15:32:31 +00001112#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1113#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1114 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1115 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001116 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001117 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1119 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001122 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001123 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001124
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001125 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001126 mac_key_len = mbedtls_md_get_size( md_info );
1127 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001130 /*
1131 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1132 * (rfc 6066 page 13 or rfc 2104 section 4),
1133 * so we only need to adjust the length here.
1134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001138
1139#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1140 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001141 * HMAC implementation which also truncates the key
1142 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001143 mac_key_len = transform->maclen;
1144#endif
1145 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001147
1148 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001149 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001150
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001151 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001153 transform->minlen = transform->maclen;
1154 else
Paul Bakker68884e32013-01-07 18:20:04 +01001155 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001156 /*
1157 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001158 * 1. if EtM is in use: one block plus MAC
1159 * otherwise: * first multiple of blocklen greater than maclen
1160 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1163 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001164 {
1165 transform->minlen = transform->maclen
1166 + cipher_info->block_size;
1167 }
1168 else
1169#endif
1170 {
1171 transform->minlen = transform->maclen
1172 + cipher_info->block_size
1173 - transform->maclen % cipher_info->block_size;
1174 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1177 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1178 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001179 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001180 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001181#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1183 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1184 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001185 {
1186 transform->minlen += transform->ivlen;
1187 }
1188 else
1189#endif
1190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001192 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1193 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001194 }
Paul Bakker68884e32013-01-07 18:20:04 +01001195 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 }
Hanno Becker8031d062018-01-03 15:32:31 +00001197 else
1198#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1199 {
1200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1202 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001203
Hanno Becker88aaf652017-12-27 08:17:40 +00001204 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1205 (unsigned) keylen,
1206 (unsigned) transform->minlen,
1207 (unsigned) transform->ivlen,
1208 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001209
1210 /*
1211 * Finally setup the cipher contexts, IVs and MAC secrets.
1212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001214 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001216 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001217 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001218
Paul Bakker68884e32013-01-07 18:20:04 +01001219 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001220 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001221
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001222 /*
1223 * This is not used in TLS v1.1.
1224 */
Paul Bakker48916f92012-09-16 19:57:18 +00001225 iv_copy_len = ( transform->fixed_ivlen ) ?
1226 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001227 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1228 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001229 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 }
1231 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232#endif /* MBEDTLS_SSL_CLI_C */
1233#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001234 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001235 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001236 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001237 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001238
Hanno Becker81c7b182017-11-09 18:39:33 +00001239 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001240 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001241
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001242 /*
1243 * This is not used in TLS v1.1.
1244 */
Paul Bakker48916f92012-09-16 19:57:18 +00001245 iv_copy_len = ( transform->fixed_ivlen ) ?
1246 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001247 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1248 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001249 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001251 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001255 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1256 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001257 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001258
Hanno Beckerd56ed242018-01-03 15:32:51 +00001259#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260#if defined(MBEDTLS_SSL_PROTO_SSL3)
1261 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001262 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001263 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001266 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1267 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001268 }
1269
Hanno Becker81c7b182017-11-09 18:39:33 +00001270 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1271 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001272 }
1273 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1275#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1276 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1277 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001278 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001279 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1280 For AEAD-based ciphersuites, there is nothing to do here. */
1281 if( mac_key_len != 0 )
1282 {
1283 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1284 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1285 }
Paul Bakker68884e32013-01-07 18:20:04 +01001286 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001287 else
1288#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001291 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1292 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001293 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001294#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1297 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001298 {
1299 int ret = 0;
1300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001302
Hanno Becker88aaf652017-12-27 08:17:40 +00001303 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001304 transform->iv_enc, transform->iv_dec,
1305 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001306 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001307 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001310 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1311 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001312 }
1313 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001314#else
1315 ((void) mac_dec);
1316 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001318
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001319#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1320 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001321 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001322 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1323 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001324 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001325 iv_copy_len );
1326 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001327
1328 if( ssl->conf->f_export_keys_ext != NULL )
1329 {
1330 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1331 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001332 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001333 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001334 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001335 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001336 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001337 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001338#endif
1339
Hanno Beckerf704bef2018-11-16 15:21:18 +00001340#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001341
1342 /* Only use PSA-based ciphers for TLS-1.2.
1343 * That's relevant at least for TLS-1.0, where
1344 * we assume that mbedtls_cipher_crypt() updates
1345 * the structure field for the IV, which the PSA-based
1346 * implementation currently doesn't. */
1347#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1348 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001349 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001350 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001351 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001352 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1353 {
1354 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001355 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001356 }
1357
1358 if( ret == 0 )
1359 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001360 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001361 psa_fallthrough = 0;
1362 }
1363 else
1364 {
1365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1366 psa_fallthrough = 1;
1367 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001368 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001369 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001370 psa_fallthrough = 1;
1371#else
1372 psa_fallthrough = 1;
1373#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001374
Hanno Beckercb1cc802018-11-17 22:27:38 +00001375 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001376#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001377 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001378 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001380 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001381 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001382 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001383
Hanno Beckerf704bef2018-11-16 15:21:18 +00001384#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001385 /* Only use PSA-based ciphers for TLS-1.2.
1386 * That's relevant at least for TLS-1.0, where
1387 * we assume that mbedtls_cipher_crypt() updates
1388 * the structure field for the IV, which the PSA-based
1389 * implementation currently doesn't. */
1390#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1391 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001392 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001393 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001394 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001395 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1396 {
1397 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001398 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001399 }
1400
1401 if( ret == 0 )
1402 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001403 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001404 psa_fallthrough = 0;
1405 }
1406 else
1407 {
1408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1409 psa_fallthrough = 1;
1410 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001411 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001412 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001413 psa_fallthrough = 1;
1414#else
1415 psa_fallthrough = 1;
1416#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001417
Hanno Beckercb1cc802018-11-17 22:27:38 +00001418 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001419#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001420 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001421 cipher_info ) ) != 0 )
1422 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001424 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001425 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001428 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001432 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001433 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001436 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001440 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001441 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443#if defined(MBEDTLS_CIPHER_MODE_CBC)
1444 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1447 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001450 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001451 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1454 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001457 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001458 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001459 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001461
Paul Bakker5121ce52009-01-03 21:22:43 +00001462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001464 // Initialize compression
1465 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001467 {
Paul Bakker16770332013-10-11 09:59:44 +02001468 if( ssl->compress_buf == NULL )
1469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001471 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001472 if( ssl->compress_buf == NULL )
1473 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001475 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001476 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1477 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001478 }
1479 }
1480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001482
Paul Bakker48916f92012-09-16 19:57:18 +00001483 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1484 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001485
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001486 if( deflateInit( &transform->ctx_deflate,
1487 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001488 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001491 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1492 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001493 }
1494 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001496
Ron Eldore6992702019-05-07 18:27:13 +03001497end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001498 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Ron Eldore6992702019-05-07 18:27:13 +03001499 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001500}
1501
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001502/*
1503 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
1504 *
1505 * Inputs:
1506 * - SSL/TLS minor version
1507 * - hash associated with the ciphersuite (only used by TLS 1.2)
1508 *
1509 * Ouputs:
1510 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1511 */
1512static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1513 int minor_ver,
1514 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001515{
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001516#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1517 (void) hash;
1518#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001519
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001520#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001521 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001522 {
1523 handshake->tls_prf = ssl3_prf;
1524 handshake->calc_verify = ssl_calc_verify_ssl;
1525 handshake->calc_finished = ssl_calc_finished_ssl;
1526 }
1527 else
1528#endif
1529#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001530 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001531 {
1532 handshake->tls_prf = tls1_prf;
1533 handshake->calc_verify = ssl_calc_verify_tls;
1534 handshake->calc_finished = ssl_calc_finished_tls;
1535 }
1536 else
1537#endif
1538#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1539#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001540 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1541 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001542 {
1543 handshake->tls_prf = tls_prf_sha384;
1544 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1545 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1546 }
1547 else
1548#endif
1549#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001550 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001551 {
1552 handshake->tls_prf = tls_prf_sha256;
1553 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1554 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1555 }
1556 else
1557#endif
1558#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1559 {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001560 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1561 }
1562
1563 return( 0 );
1564}
1565
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001566/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001567 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001568 *
1569 * Parameters:
1570 * [in/out] handshake
1571 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001572 * (PSA-PSK) ciphersuite_info, psk_opaque
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001573 * [out] premaster (cleared)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001574 * [out] master
1575 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1576 * debug: conf->f_dbg, conf->p_dbg
1577 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001578 * PSA-PSA: minor_ver, conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001579 */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001580static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001581 unsigned char *master,
Manuel Pégourié-Gonnard0d56aaa2019-05-03 09:58:33 +02001582 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001583{
1584 int ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001585
1586 /* cf. RFC 5246, Section 8.1:
1587 * "The master secret is always exactly 48 bytes in length." */
1588 size_t const master_secret_len = 48;
1589
1590#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1591 unsigned char session_hash[48];
1592#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1593
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001594 /* The label for the KDF used for key expansion.
1595 * This is either "master secret" or "extended master secret"
1596 * depending on whether the Extended Master Secret extension
1597 * is used. */
1598 char const *lbl = "master secret";
1599
1600 /* The salt for the KDF used for key expansion.
1601 * - If the Extended Master Secret extension is not used,
1602 * this is ClientHello.Random + ServerHello.Random
1603 * (see Sect. 8.1 in RFC 5246).
1604 * - If the Extended Master Secret extension is used,
1605 * this is the transcript of the handshake so far.
1606 * (see Sect. 4 in RFC 7627). */
1607 unsigned char const *salt = handshake->randbytes;
1608 size_t salt_len = 64;
1609
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001610#if !defined(MBEDTLS_DEBUG_C) && \
1611 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1612 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
1613 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
1614 (void) ssl;
1615#endif
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001616
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001617 if( handshake->resume != 0 )
1618 {
1619 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001620 return( 0 );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001621 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001622
1623#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001624 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001625 {
1626 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001627
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001628 lbl = "extended master secret";
1629 salt = session_hash;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001630 handshake->calc_verify( ssl, session_hash, &salt_len );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001631
1632 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
1633 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001634#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1635
1636#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001637 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1638 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001639 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001640 ssl_use_opaque_psk( ssl ) == 1 )
1641 {
1642 /* Perform PSK-to-MS expansion in a single step. */
1643 psa_status_t status;
1644 psa_algorithm_t alg;
1645 psa_key_handle_t psk;
1646 psa_key_derivation_operation_t derivation =
1647 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001648 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001649
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001651
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001652 psk = ssl->conf->psk_opaque;
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001653 if( handshake->psk_opaque != 0 )
1654 psk = handshake->psk_opaque;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001655
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001656 if( hash_alg == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001657 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001658 else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001659 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1660
1661 status = psa_key_derivation( &derivation, psk, alg,
1662 salt, salt_len,
1663 (unsigned char const *) lbl,
1664 (size_t) strlen( lbl ),
1665 master_secret_len );
1666 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001667 {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001668 psa_key_derivation_abort( &derivation );
1669 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001670 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001671
1672 status = psa_key_derivation_output_bytes( &derivation,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001673 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001674 master_secret_len );
1675 if( status != PSA_SUCCESS )
1676 {
1677 psa_key_derivation_abort( &derivation );
1678 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1679 }
1680
1681 status = psa_key_derivation_abort( &derivation );
1682 if( status != PSA_SUCCESS )
1683 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1684 }
1685 else
1686#endif
1687 {
1688 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1689 lbl, salt, salt_len,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001690 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001691 master_secret_len );
1692 if( ret != 0 )
1693 {
1694 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1695 return( ret );
1696 }
1697
1698 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1699 handshake->premaster,
1700 handshake->pmslen );
1701
1702 mbedtls_platform_zeroize( handshake->premaster,
1703 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001704 }
1705
1706 return( 0 );
1707}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001708
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001709int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1710{
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001711 int ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001712 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1713 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001714
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1716
1717 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001718 ret = ssl_set_handshake_prfs( ssl->handshake,
1719 ssl->minor_ver,
1720 ciphersuite_info->mac );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001721 if( ret != 0 )
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001722 {
1723 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001724 return( ret );
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001725 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001726
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001727 /* Compute master secret if needed */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001728 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001729 ssl->session_negotiate->master,
1730 ssl );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001731 if( ret != 0 )
1732 {
1733 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1734 return( ret );
1735 }
1736
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001737 /* Swap the client and server random values:
1738 * - MS derivation wanted client+server (RFC 5246 8.1)
1739 * - key derivation wants server+client (RFC 5246 6.3) */
1740 {
1741 unsigned char tmp[64];
1742 memcpy( tmp, ssl->handshake->randbytes, 64 );
1743 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1744 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1745 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1746 }
1747
1748 /* Populate transform structure */
1749 ret = ssl_populate_transform( ssl );
1750 if( ret != 0 )
1751 {
1752 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1753 return( ret );
1754 }
1755
1756 /* We no longer need Server/ClientHello.random values */
1757 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1758 sizeof( ssl->handshake->randbytes ) );
1759
1760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1761
1762 return( 0 );
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001763}
1764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001766void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1767 unsigned char hash[36],
1768 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001769{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001770 mbedtls_md5_context md5;
1771 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001772 unsigned char pad_1[48];
1773 unsigned char pad_2[48];
1774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001776
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001777 mbedtls_md5_init( &md5 );
1778 mbedtls_sha1_init( &sha1 );
1779
1780 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1781 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001782
Paul Bakker380da532012-04-18 16:10:25 +00001783 memset( pad_1, 0x36, 48 );
1784 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001785
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001786 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1787 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1788 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001789
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001790 mbedtls_md5_starts_ret( &md5 );
1791 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1792 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1793 mbedtls_md5_update_ret( &md5, hash, 16 );
1794 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001795
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001796 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1797 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1798 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001799
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001800 mbedtls_sha1_starts_ret( &sha1 );
1801 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1802 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1803 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1804 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001805
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001806 *hlen = 36;
1807
1808 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001810
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001811 mbedtls_md5_free( &md5 );
1812 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001813
Paul Bakker380da532012-04-18 16:10:25 +00001814 return;
1815}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001819void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1820 unsigned char hash[36],
1821 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001822{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001823 mbedtls_md5_context md5;
1824 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001827
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001828 mbedtls_md5_init( &md5 );
1829 mbedtls_sha1_init( &sha1 );
1830
1831 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1832 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001833
Andrzej Kurekeb342242019-01-29 09:14:33 -05001834 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001835 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001836
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001837 *hlen = 36;
1838
1839 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001841
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001842 mbedtls_md5_free( &md5 );
1843 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001844
Paul Bakker380da532012-04-18 16:10:25 +00001845 return;
1846}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1850#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001851void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1852 unsigned char hash[32],
1853 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001854{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001855#if defined(MBEDTLS_USE_PSA_CRYPTO)
1856 size_t hash_size;
1857 psa_status_t status;
1858 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1859
1860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1861 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1862 if( status != PSA_SUCCESS )
1863 {
1864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1865 return;
1866 }
1867
1868 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1869 if( status != PSA_SUCCESS )
1870 {
1871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1872 return;
1873 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001874
1875 *hlen = 32;
1876 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1878#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001879 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001880
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001881 mbedtls_sha256_init( &sha256 );
1882
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001884
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001885 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001886 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001887
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001888 *hlen = 32;
1889
1890 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001892
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001893 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001894#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001895 return;
1896}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001900void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1901 unsigned char hash[48],
1902 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001903{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001904#if defined(MBEDTLS_USE_PSA_CRYPTO)
1905 size_t hash_size;
1906 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001907 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001908
1909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001910 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001911 if( status != PSA_SUCCESS )
1912 {
1913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1914 return;
1915 }
1916
Andrzej Kurek972fba52019-01-30 03:29:12 -05001917 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001918 if( status != PSA_SUCCESS )
1919 {
1920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1921 return;
1922 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001923
1924 *hlen = 48;
1925 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1927#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001928 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001929
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001930 mbedtls_sha512_init( &sha512 );
1931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001933
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001934 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001935 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001936
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001937 *hlen = 48;
1938
1939 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001941
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001942 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001943#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 return;
1945}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946#endif /* MBEDTLS_SHA512_C */
1947#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1950int 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 +02001951{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001952 unsigned char *p = ssl->handshake->premaster;
1953 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001954 const unsigned char *psk = ssl->conf->psk;
1955 size_t psk_len = ssl->conf->psk_len;
1956
1957 /* If the psk callback was called, use its result */
1958 if( ssl->handshake->psk != NULL )
1959 {
1960 psk = ssl->handshake->psk;
1961 psk_len = ssl->handshake->psk_len;
1962 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001963
1964 /*
1965 * PMS = struct {
1966 * opaque other_secret<0..2^16-1>;
1967 * opaque psk<0..2^16-1>;
1968 * };
1969 * with "other_secret" depending on the particular key exchange
1970 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1972 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001973 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001974 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001976
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001977 *(p++) = (unsigned char)( psk_len >> 8 );
1978 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001979
1980 if( end < p || (size_t)( end - p ) < psk_len )
1981 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1982
1983 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001984 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001985 }
1986 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1988#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1989 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001990 {
1991 /*
1992 * other_secret already set by the ClientKeyExchange message,
1993 * and is 48 bytes long
1994 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001995 if( end - p < 2 )
1996 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1997
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001998 *p++ = 0;
1999 *p++ = 48;
2000 p += 48;
2001 }
2002 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2004#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2005 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002006 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002007 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002008 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002009
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002010 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002012 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002013 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002016 return( ret );
2017 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002018 *(p++) = (unsigned char)( len >> 8 );
2019 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002020 p += len;
2021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002023 }
2024 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2026#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2027 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002028 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002029 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002030 size_t zlen;
2031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002033 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002034 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002037 return( ret );
2038 }
2039
2040 *(p++) = (unsigned char)( zlen >> 8 );
2041 *(p++) = (unsigned char)( zlen );
2042 p += zlen;
2043
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002044 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2045 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002046 }
2047 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002048#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2051 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002052 }
2053
2054 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002055 if( end - p < 2 )
2056 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002057
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002058 *(p++) = (unsigned char)( psk_len >> 8 );
2059 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002060
2061 if( end < p || (size_t)( end - p ) < psk_len )
2062 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2063
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002064 memcpy( p, psk, psk_len );
2065 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002066
2067 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2068
2069 return( 0 );
2070}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002074/*
2075 * SSLv3.0 MAC functions
2076 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002077#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002078static void ssl_mac( mbedtls_md_context_t *md_ctx,
2079 const unsigned char *secret,
2080 const unsigned char *buf, size_t len,
2081 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002082 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002083{
2084 unsigned char header[11];
2085 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002086 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2088 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002089
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002090 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002092 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002093 else
Paul Bakker68884e32013-01-07 18:20:04 +01002094 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002095
2096 memcpy( header, ctr, 8 );
2097 header[ 8] = (unsigned char) type;
2098 header[ 9] = (unsigned char)( len >> 8 );
2099 header[10] = (unsigned char)( len );
2100
Paul Bakker68884e32013-01-07 18:20:04 +01002101 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 mbedtls_md_starts( md_ctx );
2103 mbedtls_md_update( md_ctx, secret, md_size );
2104 mbedtls_md_update( md_ctx, padding, padlen );
2105 mbedtls_md_update( md_ctx, header, 11 );
2106 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002107 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002108
Paul Bakker68884e32013-01-07 18:20:04 +01002109 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 mbedtls_md_starts( md_ctx );
2111 mbedtls_md_update( md_ctx, secret, md_size );
2112 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002113 mbedtls_md_update( md_ctx, out, md_size );
2114 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002115}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002117
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002118/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002119 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002120#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002121 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2122 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2123 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2124/* This function makes sure every byte in the memory region is accessed
2125 * (in ascending addresses order) */
2126static void ssl_read_memory( unsigned char *p, size_t len )
2127{
2128 unsigned char acc = 0;
2129 volatile unsigned char force;
2130
2131 for( ; len != 0; p++, len-- )
2132 acc ^= *p;
2133
2134 force = acc;
2135 (void) force;
2136}
2137#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2138
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002139/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002141 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002142
Hanno Beckera0e20d02019-05-15 14:03:01 +01002143#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002144/* This functions transforms a DTLS plaintext fragment and a record content
2145 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002146 *
2147 * struct {
2148 * opaque content[DTLSPlaintext.length];
2149 * ContentType real_type;
2150 * uint8 zeros[length_of_padding];
2151 * } DTLSInnerPlaintext;
2152 *
2153 * Input:
2154 * - `content`: The beginning of the buffer holding the
2155 * plaintext to be wrapped.
2156 * - `*content_size`: The length of the plaintext in Bytes.
2157 * - `max_len`: The number of Bytes available starting from
2158 * `content`. This must be `>= *content_size`.
2159 * - `rec_type`: The desired record content type.
2160 *
2161 * Output:
2162 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2163 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2164 *
2165 * Returns:
2166 * - `0` on success.
2167 * - A negative error code if `max_len` didn't offer enough space
2168 * for the expansion.
2169 */
2170static int ssl_cid_build_inner_plaintext( unsigned char *content,
2171 size_t *content_size,
2172 size_t remaining,
2173 uint8_t rec_type )
2174{
2175 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002176 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2177 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2178 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002179
2180 /* Write real content type */
2181 if( remaining == 0 )
2182 return( -1 );
2183 content[ len ] = rec_type;
2184 len++;
2185 remaining--;
2186
2187 if( remaining < pad )
2188 return( -1 );
2189 memset( content + len, 0, pad );
2190 len += pad;
2191 remaining -= pad;
2192
2193 *content_size = len;
2194 return( 0 );
2195}
2196
Hanno Becker07dc97d2019-05-20 15:08:01 +01002197/* This function parses a DTLSInnerPlaintext structure.
2198 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002199static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2200 size_t *content_size,
2201 uint8_t *rec_type )
2202{
2203 size_t remaining = *content_size;
2204
2205 /* Determine length of padding by skipping zeroes from the back. */
2206 do
2207 {
2208 if( remaining == 0 )
2209 return( -1 );
2210 remaining--;
2211 } while( content[ remaining ] == 0 );
2212
2213 *content_size = remaining;
2214 *rec_type = content[ remaining ];
2215
2216 return( 0 );
2217}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002218#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002219
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002220/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002221 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002222static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002223 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002224 mbedtls_record *rec )
2225{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002226 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002227 *
2228 * additional_data = seq_num + TLSCompressed.type +
2229 * TLSCompressed.version + TLSCompressed.length;
2230 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002231 * For the CID extension, this is extended as follows
2232 * (quoting draft-ietf-tls-dtls-connection-id-05,
2233 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002234 *
2235 * additional_data = seq_num + DTLSPlaintext.type +
2236 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002237 * cid +
2238 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002239 * length_of_DTLSInnerPlaintext;
2240 */
2241
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002242 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2243 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002244 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002245
Hanno Beckera0e20d02019-05-15 14:03:01 +01002246#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002247 if( rec->cid_len != 0 )
2248 {
2249 memcpy( add_data + 11, rec->cid, rec->cid_len );
2250 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2251 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2252 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2253 *add_data_len = 13 + 1 + rec->cid_len;
2254 }
2255 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002256#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002257 {
2258 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2259 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2260 *add_data_len = 13;
2261 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002262}
2263
Hanno Beckera18d1322018-01-03 14:27:32 +00002264int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2265 mbedtls_ssl_transform *transform,
2266 mbedtls_record *rec,
2267 int (*f_rng)(void *, unsigned char *, size_t),
2268 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002269{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002271 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002272 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002273 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002274 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002275 size_t post_avail;
2276
2277 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002278#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002279 ((void) ssl);
2280#endif
2281
2282 /* The PRNG is used for dynamic IV generation that's used
2283 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2284#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2285 ( defined(MBEDTLS_AES_C) || \
2286 defined(MBEDTLS_ARIA_C) || \
2287 defined(MBEDTLS_CAMELLIA_C) ) && \
2288 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2289 ((void) f_rng);
2290 ((void) p_rng);
2291#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002295 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002296 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2298 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2299 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002300 if( rec == NULL
2301 || rec->buf == NULL
2302 || rec->buf_len < rec->data_offset
2303 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002304#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002305 || rec->cid_len != 0
2306#endif
2307 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002308 {
2309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002311 }
2312
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002313 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002314 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002316 data, rec->data_len );
2317
2318 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2319
2320 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2321 {
2322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2323 (unsigned) rec->data_len,
2324 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2325 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2326 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002327
Hanno Beckera0e20d02019-05-15 14:03:01 +01002328#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002329 /*
2330 * Add CID information
2331 */
2332 rec->cid_len = transform->out_cid_len;
2333 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2334 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002335
2336 if( rec->cid_len != 0 )
2337 {
2338 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002339 * Wrap plaintext into DTLSInnerPlaintext structure.
2340 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002341 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002342 * Note that this changes `rec->data_len`, and hence
2343 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002344 */
2345 if( ssl_cid_build_inner_plaintext( data,
2346 &rec->data_len,
2347 post_avail,
2348 rec->type ) != 0 )
2349 {
2350 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2351 }
2352
2353 rec->type = MBEDTLS_SSL_MSG_CID;
2354 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002355#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002356
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002357 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2358
Paul Bakker5121ce52009-01-03 21:22:43 +00002359 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002360 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002361 */
Hanno Becker52344c22018-01-03 15:24:20 +00002362#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 if( mode == MBEDTLS_MODE_STREAM ||
2364 ( mode == MBEDTLS_MODE_CBC
2365#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002366 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002367#endif
2368 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002369 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002370 if( post_avail < transform->maclen )
2371 {
2372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2373 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2374 }
2375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002377 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002378 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002379 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002380 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2381 data, rec->data_len, rec->ctr, rec->type, mac );
2382 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002383 }
2384 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002385#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2387 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002388 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002389 {
Hanno Becker992b6872017-11-09 18:57:39 +00002390 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2391
Hanno Beckercab87e62019-04-29 13:52:53 +01002392 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002393
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002394 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002395 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002396 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2397 data, rec->data_len );
2398 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2399 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2400
2401 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002402 }
2403 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002404#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2407 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002408 }
2409
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002410 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2411 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002412
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002413 rec->data_len += transform->maclen;
2414 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002415 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002416 }
Hanno Becker52344c22018-01-03 15:24:20 +00002417#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002418
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002419 /*
2420 * Encrypt
2421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2423 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002425 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002426 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002428 "including %d bytes of padding",
2429 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002430
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002431 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2432 transform->iv_enc, transform->ivlen,
2433 data, rec->data_len,
2434 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002437 return( ret );
2438 }
2439
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002440 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2443 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002444 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 }
Paul Bakker68884e32013-01-07 18:20:04 +01002446 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002448
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002449#if defined(MBEDTLS_GCM_C) || \
2450 defined(MBEDTLS_CCM_C) || \
2451 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002453 mode == MBEDTLS_MODE_CCM ||
2454 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002455 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002456 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002457 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002458 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002459
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002460 /* Check that there's space for both the authentication tag
2461 * and the explicit IV before and after the record content. */
2462 if( post_avail < transform->taglen ||
2463 rec->data_offset < explicit_iv_len )
2464 {
2465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2466 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2467 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002468
Paul Bakker68884e32013-01-07 18:20:04 +01002469 /*
2470 * Generate IV
2471 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002472 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2473 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002474 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002475 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002476 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2477 explicit_iv_len );
2478 /* Prefix record content with explicit IV. */
2479 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002480 }
2481 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2482 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002483 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002484 unsigned char i;
2485
2486 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2487
2488 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002489 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002490 }
2491 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002492 {
2493 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2495 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002496 }
2497
Hanno Beckercab87e62019-04-29 13:52:53 +01002498 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002499
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002500 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2501 iv, transform->ivlen );
2502 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002503 data - explicit_iv_len, explicit_iv_len );
2504 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002505 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002507 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002508 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002509
Paul Bakker68884e32013-01-07 18:20:04 +01002510 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002511 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002512 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002513
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002514 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002515 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002516 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002517 data, rec->data_len, /* source */
2518 data, &rec->data_len, /* destination */
2519 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002522 return( ret );
2523 }
2524
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002525 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2526 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002527
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002528 rec->data_len += transform->taglen + explicit_iv_len;
2529 rec->data_offset -= explicit_iv_len;
2530 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002531 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002532 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002533 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2535#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002536 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002538 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002539 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002540 size_t padlen, i;
2541 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002542
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002543 /* Currently we're always using minimal padding
2544 * (up to 255 bytes would be allowed). */
2545 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2546 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002547 padlen = 0;
2548
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002549 /* Check there's enough space in the buffer for the padding. */
2550 if( post_avail < padlen + 1 )
2551 {
2552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2553 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2554 }
2555
Paul Bakker5121ce52009-01-03 21:22:43 +00002556 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002557 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002559 rec->data_len += padlen + 1;
2560 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002563 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002564 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2565 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002566 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002567 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002568 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002569 if( f_rng == NULL )
2570 {
2571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2573 }
2574
2575 if( rec->data_offset < transform->ivlen )
2576 {
2577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2578 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2579 }
2580
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002581 /*
2582 * Generate IV
2583 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002584 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002585 if( ret != 0 )
2586 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002587
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002588 memcpy( data - transform->ivlen, transform->iv_enc,
2589 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002590
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002591 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002595 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002596 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002597 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002598
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002599 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2600 transform->iv_enc,
2601 transform->ivlen,
2602 data, rec->data_len,
2603 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002606 return( ret );
2607 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002608
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002609 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2612 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002613 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002616 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002617 {
2618 /*
2619 * Save IV in SSL3 and TLS1
2620 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002621 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2622 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002623 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002624 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002625#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002626 {
2627 data -= transform->ivlen;
2628 rec->data_offset -= transform->ivlen;
2629 rec->data_len += transform->ivlen;
2630 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002633 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002634 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002635 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2636
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002637 /*
2638 * MAC(MAC_write_key, seq_num +
2639 * TLSCipherText.type +
2640 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002641 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002642 * IV + // except for TLS 1.0
2643 * ENC(content + padding + padding_length));
2644 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002645
2646 if( post_avail < transform->maclen)
2647 {
2648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2649 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2650 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002651
Hanno Beckercab87e62019-04-29 13:52:53 +01002652 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002655 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002656 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002657
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002658 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002659 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002660 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2661 data, rec->data_len );
2662 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2663 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002664
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002665 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002666
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002667 rec->data_len += transform->maclen;
2668 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002669 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002670 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002672 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002673 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002675 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2678 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002679 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002681 /* Make extra sure authentication was performed, exactly once */
2682 if( auth_done != 1 )
2683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002686 }
2687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002689
2690 return( 0 );
2691}
2692
Hanno Becker605949f2019-07-12 08:23:59 +01002693int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002694 mbedtls_ssl_transform *transform,
2695 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002696{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002697 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002699 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002700#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002701 size_t padlen = 0, correct = 1;
2702#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002703 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002704 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002705 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002706
Hanno Beckera18d1322018-01-03 14:27:32 +00002707#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002708 ((void) ssl);
2709#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002712 if( rec == NULL ||
2713 rec->buf == NULL ||
2714 rec->buf_len < rec->data_offset ||
2715 rec->buf_len - rec->data_offset < rec->data_len )
2716 {
2717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002719 }
2720
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002721 data = rec->buf + rec->data_offset;
2722 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002723
Hanno Beckera0e20d02019-05-15 14:03:01 +01002724#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002725 /*
2726 * Match record's CID with incoming CID.
2727 */
Hanno Becker938489a2019-05-08 13:02:22 +01002728 if( rec->cid_len != transform->in_cid_len ||
2729 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2730 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002731 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002732 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002733#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2736 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002737 {
2738 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002739 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2740 transform->iv_dec,
2741 transform->ivlen,
2742 data, rec->data_len,
2743 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002746 return( ret );
2747 }
2748
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002749 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2752 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002753 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002754 }
Paul Bakker68884e32013-01-07 18:20:04 +01002755 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002757#if defined(MBEDTLS_GCM_C) || \
2758 defined(MBEDTLS_CCM_C) || \
2759 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002761 mode == MBEDTLS_MODE_CCM ||
2762 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002763 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002764 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002765 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002766
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002767 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002768 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002769 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002770
2771 /* Check that there's enough space for the explicit IV
2772 * (at the beginning of the record) and the MAC (at the
2773 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002774 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002777 "+ taglen (%d)", rec->data_len,
2778 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002780 }
Paul Bakker68884e32013-01-07 18:20:04 +01002781
Hanno Beckerd96a6522019-07-10 13:55:25 +01002782#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002783 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2784 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002785 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002786
Hanno Beckerd96a6522019-07-10 13:55:25 +01002787 /* Fixed */
2788 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2789 /* Explicit */
2790 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002791 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002792 else
2793#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2794#if defined(MBEDTLS_CHACHAPOLY_C)
2795 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002796 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002797 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002798 unsigned char i;
2799
2800 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2801
2802 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002803 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002804 }
2805 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002806#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002807 {
2808 /* Reminder if we ever add an AEAD mode with a different size */
2809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2810 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2811 }
2812
Hanno Beckerd96a6522019-07-10 13:55:25 +01002813 /* Group changes to data, data_len, and add_data, because
2814 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002815 data += explicit_iv_len;
2816 rec->data_offset += explicit_iv_len;
2817 rec->data_len -= explicit_iv_len + transform->taglen;
2818
Hanno Beckercab87e62019-04-29 13:52:53 +01002819 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002820 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002821 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002822
Hanno Beckerd96a6522019-07-10 13:55:25 +01002823 /* Because of the check above, we know that there are
2824 * explicit_iv_len Bytes preceeding data, and taglen
2825 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002826 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002827 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002828
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002829 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002830 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002831 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002832
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002833 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002834 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002835 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002836 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2837 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002838 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002839 data, rec->data_len,
2840 data, &olen,
2841 data + rec->data_len,
2842 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2847 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002848
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002849 return( ret );
2850 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002851 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002852
Hanno Beckerd96a6522019-07-10 13:55:25 +01002853 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002854 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2857 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002858 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002859 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002860 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2862#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002863 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002865 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002866 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002867
Paul Bakker5121ce52009-01-03 21:22:43 +00002868 /*
Paul Bakker45829992013-01-03 14:52:21 +01002869 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002872 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2873 {
2874 /* The ciphertext is prefixed with the CBC IV. */
2875 minlen += transform->ivlen;
2876 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002877#endif
Paul Bakker45829992013-01-03 14:52:21 +01002878
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002879 /* Size considerations:
2880 *
2881 * - The CBC cipher text must not be empty and hence
2882 * at least of size transform->ivlen.
2883 *
2884 * Together with the potential IV-prefix, this explains
2885 * the first of the two checks below.
2886 *
2887 * - The record must contain a MAC, either in plain or
2888 * encrypted, depending on whether Encrypt-then-MAC
2889 * is used or not.
2890 * - If it is, the message contains the IV-prefix,
2891 * the CBC ciphertext, and the MAC.
2892 * - If it is not, the padded plaintext, and hence
2893 * the CBC ciphertext, has at least length maclen + 1
2894 * because there is at least the padding length byte.
2895 *
2896 * As the CBC ciphertext is not empty, both cases give the
2897 * lower bound minlen + maclen + 1 on the record size, which
2898 * we test for in the second check below.
2899 */
2900 if( rec->data_len < minlen + transform->ivlen ||
2901 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002904 "+ 1 ) ( + expl IV )", rec->data_len,
2905 transform->ivlen,
2906 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002908 }
2909
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002910 /*
2911 * Authenticate before decrypt if enabled
2912 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002914 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002915 {
Hanno Becker992b6872017-11-09 18:57:39 +00002916 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002919
Hanno Beckerd96a6522019-07-10 13:55:25 +01002920 /* Update data_len in tandem with add_data.
2921 *
2922 * The subtraction is safe because of the previous check
2923 * data_len >= minlen + maclen + 1.
2924 *
2925 * Afterwards, we know that data + data_len is followed by at
2926 * least maclen Bytes, which justifies the call to
2927 * mbedtls_ssl_safer_memcmp() below.
2928 *
2929 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002930 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002931 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002932
Hanno Beckerd96a6522019-07-10 13:55:25 +01002933 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002934 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2935 add_data_len );
2936 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2937 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002938 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2939 data, rec->data_len );
2940 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2941 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002942
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002943 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2944 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002945 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002946 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002947
Hanno Beckerd96a6522019-07-10 13:55:25 +01002948 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002949 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2950 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002954 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002955 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002956 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002958
2959 /*
2960 * Check length sanity
2961 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002962
2963 /* We know from above that data_len > minlen >= 0,
2964 * so the following check in particular implies that
2965 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002966 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002969 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002971 }
2972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002974 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002975 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002976 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002977 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002978 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002979 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002980 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002981
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002982 data += transform->ivlen;
2983 rec->data_offset += transform->ivlen;
2984 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002985 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002987
Hanno Beckerd96a6522019-07-10 13:55:25 +01002988 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
2989
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002990 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2991 transform->iv_dec, transform->ivlen,
2992 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002995 return( ret );
2996 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002997
Hanno Beckerd96a6522019-07-10 13:55:25 +01002998 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002999 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3002 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003003 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003006 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003007 {
3008 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01003009 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3010 * records is equivalent to CBC decryption of the concatenation
3011 * of the records; in other words, IVs are maintained across
3012 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003013 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003014 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3015 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003016 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003017#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003018
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003019 /* Safe since data_len >= minlen + maclen + 1, so after having
3020 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01003021 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3022 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003023 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003024
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003025 if( auth_done == 1 )
3026 {
3027 correct *= ( rec->data_len >= padlen + 1 );
3028 padlen *= ( rec->data_len >= padlen + 1 );
3029 }
3030 else
Paul Bakker45829992013-01-03 14:52:21 +01003031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003033 if( rec->data_len < transform->maclen + padlen + 1 )
3034 {
3035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3036 rec->data_len,
3037 transform->maclen,
3038 padlen + 1 ) );
3039 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003040#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003041
3042 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3043 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003044 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003045
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003046 padlen++;
3047
3048 /* Regardless of the validity of the padding,
3049 * we have data_len >= padlen here. */
3050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003052 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003053 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003054 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056#if defined(MBEDTLS_SSL_DEBUG_ALL)
3057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003058 "should be no more than %d",
3059 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003060#endif
Paul Bakker45829992013-01-03 14:52:21 +01003061 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003062 }
3063 }
3064 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3066#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3067 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003068 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003069 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003070 /* The padding check involves a series of up to 256
3071 * consecutive memory reads at the end of the record
3072 * plaintext buffer. In order to hide the length and
3073 * validity of the padding, always perform exactly
3074 * `min(256,plaintext_len)` reads (but take into account
3075 * only the last `padlen` bytes for the padding check). */
3076 size_t pad_count = 0;
3077 size_t real_count = 0;
3078 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003079
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003080 /* Index of first padding byte; it has been ensured above
3081 * that the subtraction is safe. */
3082 size_t const padding_idx = rec->data_len - padlen;
3083 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3084 size_t const start_idx = rec->data_len - num_checks;
3085 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003086
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003087 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003088 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003089 real_count |= ( idx >= padding_idx );
3090 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003091 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003092 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003095 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003097#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003098 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003099 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003100 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3102 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3105 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003106 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003107
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003108 /* If the padding was found to be invalid, padlen == 0
3109 * and the subtraction is safe. If the padding was found valid,
3110 * padlen hasn't been changed and the previous assertion
3111 * data_len >= padlen still holds. */
3112 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003113 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003114 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003115#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003116 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3119 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003120 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003121
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003122#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003124 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003125#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003126
3127 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003128 * Authenticate if not done yet.
3129 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003130 */
Hanno Becker52344c22018-01-03 15:24:20 +00003131#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003132 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003133 {
Hanno Becker992b6872017-11-09 18:57:39 +00003134 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003135
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003136 /* If the initial value of padlen was such that
3137 * data_len < maclen + padlen + 1, then padlen
3138 * got reset to 1, and the initial check
3139 * data_len >= minlen + maclen + 1
3140 * guarantees that at this point we still
3141 * have at least data_len >= maclen.
3142 *
3143 * If the initial value of padlen was such that
3144 * data_len >= maclen + padlen + 1, then we have
3145 * subtracted either padlen + 1 (if the padding was correct)
3146 * or 0 (if the padding was incorrect) since then,
3147 * hence data_len >= maclen in any case.
3148 */
3149 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003150 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003153 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003154 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003155 ssl_mac( &transform->md_ctx_dec,
3156 transform->mac_dec,
3157 data, rec->data_len,
3158 rec->ctr, rec->type,
3159 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003160 }
3161 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3163#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3164 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003165 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003166 {
3167 /*
3168 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003169 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003170 *
3171 * Known timing attacks:
3172 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3173 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003174 * To compensate for different timings for the MAC calculation
3175 * depending on how much padding was removed (which is determined
3176 * by padlen), process extra_run more blocks through the hash
3177 * function.
3178 *
3179 * The formula in the paper is
3180 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3181 * where L1 is the size of the header plus the decrypted message
3182 * plus CBC padding and L2 is the size of the header plus the
3183 * decrypted message. This is for an underlying hash function
3184 * with 64-byte blocks.
3185 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3186 * correctly. We round down instead of up, so -56 is the correct
3187 * value for our calculations instead of -55.
3188 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003189 * Repeat the formula rather than defining a block_size variable.
3190 * This avoids requiring division by a variable at runtime
3191 * (which would be marginally less efficient and would require
3192 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003193 */
3194 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003195 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003196
3197 /*
3198 * The next two sizes are the minimum and maximum values of
3199 * in_msglen over all padlen values.
3200 *
3201 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003202 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003203 *
3204 * Note that max_len + maclen is never more than the buffer
3205 * length, as we previously did in_msglen -= maclen too.
3206 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003207 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003208 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3209
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003210 memset( tmp, 0, sizeof( tmp ) );
3211
3212 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003213 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003214#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3215 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003216 case MBEDTLS_MD_MD5:
3217 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003218 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003219 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003220 extra_run =
3221 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3222 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003223 break;
3224#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003225#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003226 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003227 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003228 extra_run =
3229 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3230 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003231 break;
3232#endif
3233 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003235 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3236 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003237
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003238 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003239
Hanno Beckercab87e62019-04-29 13:52:53 +01003240 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3241 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003242 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3243 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003244 /* Make sure we access everything even when padlen > 0. This
3245 * makes the synchronisation requirements for just-in-time
3246 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003247 ssl_read_memory( data + rec->data_len, padlen );
3248 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003249
3250 /* Call mbedtls_md_process at least once due to cache attacks
3251 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003252 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003253 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003254
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003255 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003256
3257 /* Make sure we access all the memory that could contain the MAC,
3258 * before we check it in the next code block. This makes the
3259 * synchronisation requirements for just-in-time Prime+Probe
3260 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003261 ssl_read_memory( data + min_len,
3262 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003263 }
3264 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003265#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3266 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3269 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003270 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003271
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003272#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003273 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3274 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003275#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003276
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003277 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3278 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280#if defined(MBEDTLS_SSL_DEBUG_ALL)
3281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003282#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003283 correct = 0;
3284 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003285 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003286 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003287
3288 /*
3289 * Finally check the correct flag
3290 */
3291 if( correct == 0 )
3292 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003293#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003294
3295 /* Make extra sure authentication was performed, exactly once */
3296 if( auth_done != 1 )
3297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3299 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003300 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003301
Hanno Beckera0e20d02019-05-15 14:03:01 +01003302#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003303 if( rec->cid_len != 0 )
3304 {
3305 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3306 &rec->type );
3307 if( ret != 0 )
3308 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3309 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003310#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003313
3314 return( 0 );
3315}
3316
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003317#undef MAC_NONE
3318#undef MAC_PLAINTEXT
3319#undef MAC_CIPHERTEXT
3320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003322/*
3323 * Compression/decompression functions
3324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003326{
3327 int ret;
3328 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003329 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003330 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003331 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003334
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003335 if( len_pre == 0 )
3336 return( 0 );
3337
Paul Bakker2770fbd2012-07-03 13:30:23 +00003338 memcpy( msg_pre, ssl->out_msg, len_pre );
3339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003341 ssl->out_msglen ) );
3342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003344 ssl->out_msg, ssl->out_msglen );
3345
Paul Bakker48916f92012-09-16 19:57:18 +00003346 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3347 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3348 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003349 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003350
Paul Bakker48916f92012-09-16 19:57:18 +00003351 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003352 if( ret != Z_OK )
3353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3355 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003356 }
3357
Angus Grattond8213d02016-05-25 20:56:48 +10003358 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003359 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003362 ssl->out_msglen ) );
3363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003365 ssl->out_msg, ssl->out_msglen );
3366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003368
3369 return( 0 );
3370}
3371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003373{
3374 int ret;
3375 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003376 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003377 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003378 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003381
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003382 if( len_pre == 0 )
3383 return( 0 );
3384
Paul Bakker2770fbd2012-07-03 13:30:23 +00003385 memcpy( msg_pre, ssl->in_msg, len_pre );
3386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003388 ssl->in_msglen ) );
3389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003391 ssl->in_msg, ssl->in_msglen );
3392
Paul Bakker48916f92012-09-16 19:57:18 +00003393 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3394 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3395 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003396 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003397 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003398
Paul Bakker48916f92012-09-16 19:57:18 +00003399 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003400 if( ret != Z_OK )
3401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3403 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003404 }
3405
Angus Grattond8213d02016-05-25 20:56:48 +10003406 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003407 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003410 ssl->in_msglen ) );
3411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003413 ssl->in_msg, ssl->in_msglen );
3414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003416
3417 return( 0 );
3418}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3422static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424#if defined(MBEDTLS_SSL_PROTO_DTLS)
3425static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003426{
3427 /* If renegotiation is not enforced, retransmit until we would reach max
3428 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003429 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003430 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003431 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003432 unsigned char doublings = 1;
3433
3434 while( ratio != 0 )
3435 {
3436 ++doublings;
3437 ratio >>= 1;
3438 }
3439
3440 if( ++ssl->renego_records_seen > doublings )
3441 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003443 return( 0 );
3444 }
3445 }
3446
3447 return( ssl_write_hello_request( ssl ) );
3448}
3449#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003451
Paul Bakker5121ce52009-01-03 21:22:43 +00003452/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003453 * Fill the input message buffer by appending data to it.
3454 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003455 *
3456 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3457 * available (from this read and/or a previous one). Otherwise, an error code
3458 * is returned (possibly EOF or WANT_READ).
3459 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003460 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3461 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3462 * since we always read a whole datagram at once.
3463 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003464 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003465 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003468{
Paul Bakker23986e52011-04-24 08:57:21 +00003469 int ret;
3470 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003473
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003474 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003477 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003479 }
3480
Angus Grattond8213d02016-05-25 20:56:48 +10003481 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3484 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003485 }
3486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003488 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003489 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003490 uint32_t timeout;
3491
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003492 /* Just to be sure */
3493 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3494 {
3495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3496 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3497 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3498 }
3499
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003500 /*
3501 * The point is, we need to always read a full datagram at once, so we
3502 * sometimes read more then requested, and handle the additional data.
3503 * It could be the rest of the current record (while fetching the
3504 * header) and/or some other records in the same datagram.
3505 */
3506
3507 /*
3508 * Move to the next record in the already read datagram if applicable
3509 */
3510 if( ssl->next_record_offset != 0 )
3511 {
3512 if( ssl->in_left < ssl->next_record_offset )
3513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3515 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003516 }
3517
3518 ssl->in_left -= ssl->next_record_offset;
3519
3520 if( ssl->in_left != 0 )
3521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003523 ssl->next_record_offset ) );
3524 memmove( ssl->in_hdr,
3525 ssl->in_hdr + ssl->next_record_offset,
3526 ssl->in_left );
3527 }
3528
3529 ssl->next_record_offset = 0;
3530 }
3531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003533 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003534
3535 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003536 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003537 */
3538 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003541 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003542 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003543
3544 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003545 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003546 * are not at the beginning of a new record, the caller did something
3547 * wrong.
3548 */
3549 if( ssl->in_left != 0 )
3550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3552 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003553 }
3554
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003555 /*
3556 * Don't even try to read if time's out already.
3557 * This avoids by-passing the timer when repeatedly receiving messages
3558 * that will end up being dropped.
3559 */
3560 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003561 {
3562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003563 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003564 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003565 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003566 {
Angus Grattond8213d02016-05-25 20:56:48 +10003567 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003570 timeout = ssl->handshake->retransmit_timeout;
3571 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003572 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003575
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003576 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003577 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3578 timeout );
3579 else
3580 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003583
3584 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003586 }
3587
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003588 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003591 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003593 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003594 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003595 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003598 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003599 }
3600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003604 return( ret );
3605 }
3606
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003607 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003608 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003609#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003610 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003612 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003613 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003616 return( ret );
3617 }
3618
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003619 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003620 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003622 }
3623
Paul Bakker5121ce52009-01-03 21:22:43 +00003624 if( ret < 0 )
3625 return( ret );
3626
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003627 ssl->in_left = ret;
3628 }
3629 else
3630#endif
3631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003633 ssl->in_left, nb_want ) );
3634
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003635 while( ssl->in_left < nb_want )
3636 {
3637 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003638
3639 if( ssl_check_timer( ssl ) != 0 )
3640 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3641 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003642 {
3643 if( ssl->f_recv_timeout != NULL )
3644 {
3645 ret = ssl->f_recv_timeout( ssl->p_bio,
3646 ssl->in_hdr + ssl->in_left, len,
3647 ssl->conf->read_timeout );
3648 }
3649 else
3650 {
3651 ret = ssl->f_recv( ssl->p_bio,
3652 ssl->in_hdr + ssl->in_left, len );
3653 }
3654 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003657 ssl->in_left, nb_want ) );
3658 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003659
3660 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003662
3663 if( ret < 0 )
3664 return( ret );
3665
mohammad160352aecb92018-03-28 23:41:40 -07003666 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003667 {
Darryl Green11999bb2018-03-13 15:22:58 +00003668 MBEDTLS_SSL_DEBUG_MSG( 1,
3669 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003670 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003671 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3672 }
3673
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003674 ssl->in_left += ret;
3675 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003676 }
3677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003679
3680 return( 0 );
3681}
3682
3683/*
3684 * Flush any data not yet written
3685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003686int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003687{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003688 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003689 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003692
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003693 if( ssl->f_send == NULL )
3694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003696 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003697 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003698 }
3699
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003700 /* Avoid incrementing counter if data is flushed */
3701 if( ssl->out_left == 0 )
3702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003704 return( 0 );
3705 }
3706
Paul Bakker5121ce52009-01-03 21:22:43 +00003707 while( ssl->out_left > 0 )
3708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003710 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003711
Hanno Becker2b1e3542018-08-06 11:19:13 +01003712 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003713 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003716
3717 if( ret <= 0 )
3718 return( ret );
3719
mohammad160352aecb92018-03-28 23:41:40 -07003720 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003721 {
Darryl Green11999bb2018-03-13 15:22:58 +00003722 MBEDTLS_SSL_DEBUG_MSG( 1,
3723 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003724 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3726 }
3727
Paul Bakker5121ce52009-01-03 21:22:43 +00003728 ssl->out_left -= ret;
3729 }
3730
Hanno Becker2b1e3542018-08-06 11:19:13 +01003731#if defined(MBEDTLS_SSL_PROTO_DTLS)
3732 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003733 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003734 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003735 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003736 else
3737#endif
3738 {
3739 ssl->out_hdr = ssl->out_buf + 8;
3740 }
3741 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003744
3745 return( 0 );
3746}
3747
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003748/*
3749 * Functions to handle the DTLS retransmission state machine
3750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003751#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003752/*
3753 * Append current handshake message to current outgoing flight
3754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003756{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003757 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3759 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3760 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003761
3762 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003763 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003764 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003767 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003768 }
3769
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003770 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003771 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003774 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003775 }
3776
3777 /* Copy current handshake message with headers */
3778 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3779 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003780 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003781 msg->next = NULL;
3782
3783 /* Append to the current flight */
3784 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003785 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003786 else
3787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003788 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003789 while( cur->next != NULL )
3790 cur = cur->next;
3791 cur->next = msg;
3792 }
3793
Hanno Becker3b235902018-08-06 09:54:53 +01003794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003795 return( 0 );
3796}
3797
3798/*
3799 * Free the current flight of handshake messages
3800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003802{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003803 mbedtls_ssl_flight_item *cur = flight;
3804 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003805
3806 while( cur != NULL )
3807 {
3808 next = cur->next;
3809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003810 mbedtls_free( cur->p );
3811 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003812
3813 cur = next;
3814 }
3815}
3816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003817#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3818static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003819#endif
3820
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003821/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003822 * Swap transform_out and out_ctr with the alternative ones
3823 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003825{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003826 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003827 unsigned char tmp_out_ctr[8];
3828
3829 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003832 return;
3833 }
3834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003836
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003837 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003838 tmp_transform = ssl->transform_out;
3839 ssl->transform_out = ssl->handshake->alt_transform_out;
3840 ssl->handshake->alt_transform_out = tmp_transform;
3841
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003842 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003843 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3844 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003845 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003846
3847 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003848 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3851 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3856 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003857 }
3858 }
3859#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003860}
3861
3862/*
3863 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003864 */
3865int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3866{
3867 int ret = 0;
3868
3869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3870
3871 ret = mbedtls_ssl_flight_transmit( ssl );
3872
3873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3874
3875 return( ret );
3876}
3877
3878/*
3879 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003880 *
3881 * Need to remember the current message in case flush_output returns
3882 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003883 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003884 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003885int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003886{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003887 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003891 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003893
3894 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003895 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003896 ssl_swap_epochs( ssl );
3897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003899 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003900
3901 while( ssl->handshake->cur_msg != NULL )
3902 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003903 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003904 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003905
Hanno Beckere1dcb032018-08-17 16:47:58 +01003906 int const is_finished =
3907 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3908 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3909
Hanno Becker04da1892018-08-14 13:22:10 +01003910 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3911 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3912
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003913 /* Swap epochs before sending Finished: we can't do it after
3914 * sending ChangeCipherSpec, in case write returns WANT_READ.
3915 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003916 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003917 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003919 ssl_swap_epochs( ssl );
3920 }
3921
Hanno Becker67bc7c32018-08-06 11:33:50 +01003922 ret = ssl_get_remaining_payload_in_datagram( ssl );
3923 if( ret < 0 )
3924 return( ret );
3925 max_frag_len = (size_t) ret;
3926
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003927 /* CCS is copied as is, while HS messages may need fragmentation */
3928 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3929 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003930 if( max_frag_len == 0 )
3931 {
3932 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3933 return( ret );
3934
3935 continue;
3936 }
3937
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003938 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003939 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003940 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003941
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003942 /* Update position inside current message */
3943 ssl->handshake->cur_msg_p += cur->len;
3944 }
3945 else
3946 {
3947 const unsigned char * const p = ssl->handshake->cur_msg_p;
3948 const size_t hs_len = cur->len - 12;
3949 const size_t frag_off = p - ( cur->p + 12 );
3950 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003951 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003952
Hanno Beckere1dcb032018-08-17 16:47:58 +01003953 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003954 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003955 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003956 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003957
Hanno Becker67bc7c32018-08-06 11:33:50 +01003958 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3959 return( ret );
3960
3961 continue;
3962 }
3963 max_hs_frag_len = max_frag_len - 12;
3964
3965 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3966 max_hs_frag_len : rem_len;
3967
3968 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003969 {
3970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003971 (unsigned) cur_hs_frag_len,
3972 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003973 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003974
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003975 /* Messages are stored with handshake headers as if not fragmented,
3976 * copy beginning of headers then fill fragmentation fields.
3977 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3978 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003979
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003980 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3981 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3982 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3983
Hanno Becker67bc7c32018-08-06 11:33:50 +01003984 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3985 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3986 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003987
3988 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3989
Hanno Becker3f7b9732018-08-28 09:53:25 +01003990 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003991 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3992 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003993 ssl->out_msgtype = cur->type;
3994
3995 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003996 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003997 }
3998
3999 /* If done with the current message move to the next one if any */
4000 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4001 {
4002 if( cur->next != NULL )
4003 {
4004 ssl->handshake->cur_msg = cur->next;
4005 ssl->handshake->cur_msg_p = cur->next->p + 12;
4006 }
4007 else
4008 {
4009 ssl->handshake->cur_msg = NULL;
4010 ssl->handshake->cur_msg_p = NULL;
4011 }
4012 }
4013
4014 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004015 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004017 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004018 return( ret );
4019 }
4020 }
4021
Hanno Becker67bc7c32018-08-06 11:33:50 +01004022 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4023 return( ret );
4024
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004025 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004026 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4027 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004028 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004030 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004031 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4032 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004033
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004035
4036 return( 0 );
4037}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004038
4039/*
4040 * To be called when the last message of an incoming flight is received.
4041 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004042void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004043{
4044 /* We won't need to resend that one any more */
4045 ssl_flight_free( ssl->handshake->flight );
4046 ssl->handshake->flight = NULL;
4047 ssl->handshake->cur_msg = NULL;
4048
4049 /* The next incoming flight will start with this msg_seq */
4050 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4051
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004052 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004053 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004054
Hanno Becker0271f962018-08-16 13:23:47 +01004055 /* Clear future message buffering structure. */
4056 ssl_buffering_free( ssl );
4057
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004058 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004059 ssl_set_timer( ssl, 0 );
4060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004061 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4062 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004065 }
4066 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004067 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004068}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004069
4070/*
4071 * To be called when the last message of an outgoing flight is send.
4072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004073void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004074{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004075 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004076 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4079 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004081 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004082 }
4083 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004084 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004085}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004086#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004087
Paul Bakker5121ce52009-01-03 21:22:43 +00004088/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004089 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004090 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004091
4092/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004093 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004094 *
4095 * - fill in handshake headers
4096 * - update handshake checksum
4097 * - DTLS: save message for resending
4098 * - then pass to the record layer
4099 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004100 * DTLS: except for HelloRequest, messages are only queued, and will only be
4101 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004102 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004103 * Inputs:
4104 * - ssl->out_msglen: 4 + actual handshake message len
4105 * (4 is the size of handshake headers for TLS)
4106 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4107 * - ssl->out_msg + 4: the handshake message body
4108 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004109 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004110 * - ssl->out_msglen: the length of the record contents
4111 * (including handshake headers but excluding record headers)
4112 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004113 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004114int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004115{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004116 int ret;
4117 const size_t hs_len = ssl->out_msglen - 4;
4118 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004119
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4121
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004122 /*
4123 * Sanity checks
4124 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004125 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004126 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4127 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004128 /* In SSLv3, the client might send a NoCertificate alert. */
4129#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4130 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4131 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4132 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4133#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4134 {
4135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4137 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004138 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004139
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004140 /* Whenever we send anything different from a
4141 * HelloRequest we should be in a handshake - double check. */
4142 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4143 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004144 ssl->handshake == NULL )
4145 {
4146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4147 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4148 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004150#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004151 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004152 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004153 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004154 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004157 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004158#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004159
Hanno Beckerb50a2532018-08-06 11:52:54 +01004160 /* Double-check that we did not exceed the bounds
4161 * of the outgoing record buffer.
4162 * This should never fail as the various message
4163 * writing functions must obey the bounds of the
4164 * outgoing record buffer, but better be safe.
4165 *
4166 * Note: We deliberately do not check for the MTU or MFL here.
4167 */
4168 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4169 {
4170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4171 "size %u, maximum %u",
4172 (unsigned) ssl->out_msglen,
4173 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4174 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4175 }
4176
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004177 /*
4178 * Fill handshake headers
4179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004180 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004181 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004182 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4183 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4184 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004185
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004186 /*
4187 * DTLS has additional fields in the Handshake layer,
4188 * between the length field and the actual payload:
4189 * uint16 message_seq;
4190 * uint24 fragment_offset;
4191 * uint24 fragment_length;
4192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004193#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004194 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004195 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004196 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004197 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004198 {
4199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4200 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004201 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004202 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004203 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4204 }
4205
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004206 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004207 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004208
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004209 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004210 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004211 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004212 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4213 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4214 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004215 }
4216 else
4217 {
4218 ssl->out_msg[4] = 0;
4219 ssl->out_msg[5] = 0;
4220 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004221
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004222 /* Handshake hashes are computed without fragmentation,
4223 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004224 memset( ssl->out_msg + 6, 0x00, 3 );
4225 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004226 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004227#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004228
Hanno Becker0207e532018-08-28 10:28:28 +01004229 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004230 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4231 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004232 }
4233
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004234 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004235#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004236 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004237 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4238 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004239 {
4240 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004242 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004243 return( ret );
4244 }
4245 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004246 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004247#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004248 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004249 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004250 {
4251 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4252 return( ret );
4253 }
4254 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004255
4256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4257
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004258 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004259}
4260
4261/*
4262 * Record layer functions
4263 */
4264
4265/*
4266 * Write current record.
4267 *
4268 * Uses:
4269 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4270 * - ssl->out_msglen: length of the record content (excl headers)
4271 * - ssl->out_msg: record content
4272 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004273int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004274{
4275 int ret, done = 0;
4276 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004277 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004278
4279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004282 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004283 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004284 {
4285 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004287 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004288 return( ret );
4289 }
4290
4291 len = ssl->out_msglen;
4292 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004293#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004295#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4296 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004300 ret = mbedtls_ssl_hw_record_write( ssl );
4301 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004303 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4304 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004305 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004306
4307 if( ret == 0 )
4308 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004309 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004310#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004311 if( !done )
4312 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004313 unsigned i;
4314 size_t protected_record_size;
4315
Hanno Becker6430faf2019-05-08 11:57:13 +01004316 /* Skip writing the record content type to after the encryption,
4317 * as it may change when using the CID extension. */
4318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004320 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004321
Hanno Becker19859472018-08-06 09:40:20 +01004322 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004323 ssl->out_len[0] = (unsigned char)( len >> 8 );
4324 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004325
Paul Bakker48916f92012-09-16 19:57:18 +00004326 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004327 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004328 mbedtls_record rec;
4329
4330 rec.buf = ssl->out_iv;
4331 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4332 ( ssl->out_iv - ssl->out_buf );
4333 rec.data_len = ssl->out_msglen;
4334 rec.data_offset = ssl->out_msg - rec.buf;
4335
4336 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4337 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4338 ssl->conf->transport, rec.ver );
4339 rec.type = ssl->out_msgtype;
4340
Hanno Beckera0e20d02019-05-15 14:03:01 +01004341#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004342 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004343 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004344#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004345
Hanno Beckera18d1322018-01-03 14:27:32 +00004346 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004347 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004349 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004350 return( ret );
4351 }
4352
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004353 if( rec.data_offset != 0 )
4354 {
4355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4356 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4357 }
4358
Hanno Becker6430faf2019-05-08 11:57:13 +01004359 /* Update the record content type and CID. */
4360 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004361#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004362 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004363#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004364 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004365 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4366 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004367 }
4368
Hanno Becker5903de42019-05-03 14:46:38 +01004369 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004370
4371#if defined(MBEDTLS_SSL_PROTO_DTLS)
4372 /* In case of DTLS, double-check that we don't exceed
4373 * the remaining space in the datagram. */
4374 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4375 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004376 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004377 if( ret < 0 )
4378 return( ret );
4379
4380 if( protected_record_size > (size_t) ret )
4381 {
4382 /* Should never happen */
4383 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4384 }
4385 }
4386#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004387
Hanno Becker6430faf2019-05-08 11:57:13 +01004388 /* Now write the potentially updated record content type. */
4389 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004391 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004392 "version = [%d:%d], msglen = %d",
4393 ssl->out_hdr[0], ssl->out_hdr[1],
4394 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004397 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004398
4399 ssl->out_left += protected_record_size;
4400 ssl->out_hdr += protected_record_size;
4401 ssl_update_out_pointers( ssl, ssl->transform_out );
4402
Hanno Becker04484622018-08-06 09:49:38 +01004403 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4404 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4405 break;
4406
4407 /* The loop goes to its end iff the counter is wrapping */
4408 if( i == ssl_ep_len( ssl ) )
4409 {
4410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4411 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4412 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004413 }
4414
Hanno Becker67bc7c32018-08-06 11:33:50 +01004415#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004416 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4417 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004418 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004419 size_t remaining;
4420 ret = ssl_get_remaining_payload_in_datagram( ssl );
4421 if( ret < 0 )
4422 {
4423 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4424 ret );
4425 return( ret );
4426 }
4427
4428 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004429 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004430 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004431 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004432 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004433 else
4434 {
Hanno Becker513815a2018-08-20 11:56:09 +01004435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004436 }
4437 }
4438#endif /* MBEDTLS_SSL_PROTO_DTLS */
4439
4440 if( ( flush == SSL_FORCE_FLUSH ) &&
4441 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004443 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004444 return( ret );
4445 }
4446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004448
4449 return( 0 );
4450}
4451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004453
4454static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4455{
4456 if( ssl->in_msglen < ssl->in_hslen ||
4457 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4458 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4459 {
4460 return( 1 );
4461 }
4462 return( 0 );
4463}
Hanno Becker44650b72018-08-16 12:51:11 +01004464
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004465static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004466{
4467 return( ( ssl->in_msg[9] << 16 ) |
4468 ( ssl->in_msg[10] << 8 ) |
4469 ssl->in_msg[11] );
4470}
4471
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004472static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004473{
4474 return( ( ssl->in_msg[6] << 16 ) |
4475 ( ssl->in_msg[7] << 8 ) |
4476 ssl->in_msg[8] );
4477}
4478
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004479static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004480{
4481 uint32_t msg_len, frag_off, frag_len;
4482
4483 msg_len = ssl_get_hs_total_len( ssl );
4484 frag_off = ssl_get_hs_frag_off( ssl );
4485 frag_len = ssl_get_hs_frag_len( ssl );
4486
4487 if( frag_off > msg_len )
4488 return( -1 );
4489
4490 if( frag_len > msg_len - frag_off )
4491 return( -1 );
4492
4493 if( frag_len + 12 > ssl->in_msglen )
4494 return( -1 );
4495
4496 return( 0 );
4497}
4498
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004499/*
4500 * Mark bits in bitmask (used for DTLS HS reassembly)
4501 */
4502static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4503{
4504 unsigned int start_bits, end_bits;
4505
4506 start_bits = 8 - ( offset % 8 );
4507 if( start_bits != 8 )
4508 {
4509 size_t first_byte_idx = offset / 8;
4510
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004511 /* Special case */
4512 if( len <= start_bits )
4513 {
4514 for( ; len != 0; len-- )
4515 mask[first_byte_idx] |= 1 << ( start_bits - len );
4516
4517 /* Avoid potential issues with offset or len becoming invalid */
4518 return;
4519 }
4520
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004521 offset += start_bits; /* Now offset % 8 == 0 */
4522 len -= start_bits;
4523
4524 for( ; start_bits != 0; start_bits-- )
4525 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4526 }
4527
4528 end_bits = len % 8;
4529 if( end_bits != 0 )
4530 {
4531 size_t last_byte_idx = ( offset + len ) / 8;
4532
4533 len -= end_bits; /* Now len % 8 == 0 */
4534
4535 for( ; end_bits != 0; end_bits-- )
4536 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4537 }
4538
4539 memset( mask + offset / 8, 0xFF, len / 8 );
4540}
4541
4542/*
4543 * Check that bitmask is full
4544 */
4545static int ssl_bitmask_check( unsigned char *mask, size_t len )
4546{
4547 size_t i;
4548
4549 for( i = 0; i < len / 8; i++ )
4550 if( mask[i] != 0xFF )
4551 return( -1 );
4552
4553 for( i = 0; i < len % 8; i++ )
4554 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4555 return( -1 );
4556
4557 return( 0 );
4558}
4559
Hanno Becker56e205e2018-08-16 09:06:12 +01004560/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004561static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004562 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004563{
Hanno Becker56e205e2018-08-16 09:06:12 +01004564 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004565
Hanno Becker56e205e2018-08-16 09:06:12 +01004566 alloc_len = 12; /* Handshake header */
4567 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004568
Hanno Beckerd07df862018-08-16 09:14:58 +01004569 if( add_bitmap )
4570 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004571
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004572 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004573}
Hanno Becker56e205e2018-08-16 09:06:12 +01004574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004575#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004576
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004577static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004578{
4579 return( ( ssl->in_msg[1] << 16 ) |
4580 ( ssl->in_msg[2] << 8 ) |
4581 ssl->in_msg[3] );
4582}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004583
Simon Butcher99000142016-10-13 17:21:01 +01004584int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004585{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004586 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004589 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004590 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004591 }
4592
Hanno Becker12555c62018-08-16 12:47:53 +01004593 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004595 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004596 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004597 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004599#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004600 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004601 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004602 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004603 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004604
Hanno Becker44650b72018-08-16 12:51:11 +01004605 if( ssl_check_hs_header( ssl ) != 0 )
4606 {
4607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4608 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4609 }
4610
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004611 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004612 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4613 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4614 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4615 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004616 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004617 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4618 {
4619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4620 recv_msg_seq,
4621 ssl->handshake->in_msg_seq ) );
4622 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4623 }
4624
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004625 /* Retransmit only on last message from previous flight, to avoid
4626 * too many retransmissions.
4627 * Besides, No sane server ever retransmits HelloVerifyRequest */
4628 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004629 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004632 "message_seq = %d, start_of_flight = %d",
4633 recv_msg_seq,
4634 ssl->handshake->in_flight_start_seq ) );
4635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004636 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004638 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004639 return( ret );
4640 }
4641 }
4642 else
4643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004645 "message_seq = %d, expected = %d",
4646 recv_msg_seq,
4647 ssl->handshake->in_msg_seq ) );
4648 }
4649
Hanno Becker90333da2017-10-10 11:27:13 +01004650 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004651 }
4652 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004653
Hanno Becker6d97ef52018-08-16 13:09:04 +01004654 /* Message reassembly is handled alongside buffering of future
4655 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004656 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004657 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004658 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004661 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004662 }
4663 }
4664 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004665#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004666 /* With TLS we don't handle fragmentation (for now) */
4667 if( ssl->in_msglen < ssl->in_hslen )
4668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4670 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004671 }
4672
Simon Butcher99000142016-10-13 17:21:01 +01004673 return( 0 );
4674}
4675
4676void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4677{
Hanno Becker0271f962018-08-16 13:23:47 +01004678 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004679
Hanno Becker0271f962018-08-16 13:23:47 +01004680 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004681 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004682 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004683 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004684
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004685 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004686#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004687 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004688 ssl->handshake != NULL )
4689 {
Hanno Becker0271f962018-08-16 13:23:47 +01004690 unsigned offset;
4691 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004692
Hanno Becker0271f962018-08-16 13:23:47 +01004693 /* Increment handshake sequence number */
4694 hs->in_msg_seq++;
4695
4696 /*
4697 * Clear up handshake buffering and reassembly structure.
4698 */
4699
4700 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004701 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004702
4703 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004704 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4705 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004706 offset++, hs_buf++ )
4707 {
4708 *hs_buf = *(hs_buf + 1);
4709 }
4710
4711 /* Create a fresh last entry */
4712 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004713 }
4714#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004715}
4716
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004717/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004718 * DTLS anti-replay: RFC 6347 4.1.2.6
4719 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004720 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4721 * Bit n is set iff record number in_window_top - n has been seen.
4722 *
4723 * Usually, in_window_top is the last record number seen and the lsb of
4724 * in_window is set. The only exception is the initial state (record number 0
4725 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004727#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4728static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004729{
4730 ssl->in_window_top = 0;
4731 ssl->in_window = 0;
4732}
4733
4734static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4735{
4736 return( ( (uint64_t) buf[0] << 40 ) |
4737 ( (uint64_t) buf[1] << 32 ) |
4738 ( (uint64_t) buf[2] << 24 ) |
4739 ( (uint64_t) buf[3] << 16 ) |
4740 ( (uint64_t) buf[4] << 8 ) |
4741 ( (uint64_t) buf[5] ) );
4742}
4743
4744/*
4745 * Return 0 if sequence number is acceptable, -1 otherwise
4746 */
Hanno Becker0183d692019-07-12 08:50:37 +01004747int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004748{
4749 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4750 uint64_t bit;
4751
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004752 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004753 return( 0 );
4754
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004755 if( rec_seqnum > ssl->in_window_top )
4756 return( 0 );
4757
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004758 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004759
4760 if( bit >= 64 )
4761 return( -1 );
4762
4763 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4764 return( -1 );
4765
4766 return( 0 );
4767}
4768
4769/*
4770 * Update replay window on new validated record
4771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004772void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004773{
4774 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4775
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004776 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004777 return;
4778
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004779 if( rec_seqnum > ssl->in_window_top )
4780 {
4781 /* Update window_top and the contents of the window */
4782 uint64_t shift = rec_seqnum - ssl->in_window_top;
4783
4784 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004785 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004786 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004787 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004788 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004789 ssl->in_window |= 1;
4790 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004791
4792 ssl->in_window_top = rec_seqnum;
4793 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004794 else
4795 {
4796 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004797 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004798
4799 if( bit < 64 ) /* Always true, but be extra sure */
4800 ssl->in_window |= (uint64_t) 1 << bit;
4801 }
4802}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004803#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004804
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004805#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004806/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004807static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4808
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004809/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004810 * Without any SSL context, check if a datagram looks like a ClientHello with
4811 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004812 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004813 *
4814 * - if cookie is valid, return 0
4815 * - if ClientHello looks superficially valid but cookie is not,
4816 * fill obuf and set olen, then
4817 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4818 * - otherwise return a specific error code
4819 */
4820static int ssl_check_dtls_clihlo_cookie(
4821 mbedtls_ssl_cookie_write_t *f_cookie_write,
4822 mbedtls_ssl_cookie_check_t *f_cookie_check,
4823 void *p_cookie,
4824 const unsigned char *cli_id, size_t cli_id_len,
4825 const unsigned char *in, size_t in_len,
4826 unsigned char *obuf, size_t buf_len, size_t *olen )
4827{
4828 size_t sid_len, cookie_len;
4829 unsigned char *p;
4830
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004831 /*
4832 * Structure of ClientHello with record and handshake headers,
4833 * and expected values. We don't need to check a lot, more checks will be
4834 * done when actually parsing the ClientHello - skipping those checks
4835 * avoids code duplication and does not make cookie forging any easier.
4836 *
4837 * 0-0 ContentType type; copied, must be handshake
4838 * 1-2 ProtocolVersion version; copied
4839 * 3-4 uint16 epoch; copied, must be 0
4840 * 5-10 uint48 sequence_number; copied
4841 * 11-12 uint16 length; (ignored)
4842 *
4843 * 13-13 HandshakeType msg_type; (ignored)
4844 * 14-16 uint24 length; (ignored)
4845 * 17-18 uint16 message_seq; copied
4846 * 19-21 uint24 fragment_offset; copied, must be 0
4847 * 22-24 uint24 fragment_length; (ignored)
4848 *
4849 * 25-26 ProtocolVersion client_version; (ignored)
4850 * 27-58 Random random; (ignored)
4851 * 59-xx SessionID session_id; 1 byte len + sid_len content
4852 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4853 * ...
4854 *
4855 * Minimum length is 61 bytes.
4856 */
4857 if( in_len < 61 ||
4858 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4859 in[3] != 0 || in[4] != 0 ||
4860 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4861 {
4862 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4863 }
4864
4865 sid_len = in[59];
4866 if( sid_len > in_len - 61 )
4867 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4868
4869 cookie_len = in[60 + sid_len];
4870 if( cookie_len > in_len - 60 )
4871 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4872
4873 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4874 cli_id, cli_id_len ) == 0 )
4875 {
4876 /* Valid cookie */
4877 return( 0 );
4878 }
4879
4880 /*
4881 * If we get here, we've got an invalid cookie, let's prepare HVR.
4882 *
4883 * 0-0 ContentType type; copied
4884 * 1-2 ProtocolVersion version; copied
4885 * 3-4 uint16 epoch; copied
4886 * 5-10 uint48 sequence_number; copied
4887 * 11-12 uint16 length; olen - 13
4888 *
4889 * 13-13 HandshakeType msg_type; hello_verify_request
4890 * 14-16 uint24 length; olen - 25
4891 * 17-18 uint16 message_seq; copied
4892 * 19-21 uint24 fragment_offset; copied
4893 * 22-24 uint24 fragment_length; olen - 25
4894 *
4895 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4896 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4897 *
4898 * Minimum length is 28.
4899 */
4900 if( buf_len < 28 )
4901 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4902
4903 /* Copy most fields and adapt others */
4904 memcpy( obuf, in, 25 );
4905 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4906 obuf[25] = 0xfe;
4907 obuf[26] = 0xff;
4908
4909 /* Generate and write actual cookie */
4910 p = obuf + 28;
4911 if( f_cookie_write( p_cookie,
4912 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4913 {
4914 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4915 }
4916
4917 *olen = p - obuf;
4918
4919 /* Go back and fill length fields */
4920 obuf[27] = (unsigned char)( *olen - 28 );
4921
4922 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4923 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4924 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4925
4926 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4927 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4928
4929 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4930}
4931
4932/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004933 * Handle possible client reconnect with the same UDP quadruplet
4934 * (RFC 6347 Section 4.2.8).
4935 *
4936 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4937 * that looks like a ClientHello.
4938 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004939 * - if the input looks like a ClientHello without cookies,
4940 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004941 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004942 * - if the input looks like a ClientHello with a valid cookie,
4943 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004944 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004945 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004946 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004947 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004948 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4949 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004950 */
4951static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4952{
4953 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004954 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004955
Hanno Becker2fddd372019-07-10 14:37:41 +01004956 if( ssl->conf->f_cookie_write == NULL ||
4957 ssl->conf->f_cookie_check == NULL )
4958 {
4959 /* If we can't use cookies to verify reachability of the peer,
4960 * drop the record. */
4961 return( 0 );
4962 }
4963
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004964 ret = ssl_check_dtls_clihlo_cookie(
4965 ssl->conf->f_cookie_write,
4966 ssl->conf->f_cookie_check,
4967 ssl->conf->p_cookie,
4968 ssl->cli_id, ssl->cli_id_len,
4969 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004970 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004971
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004972 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4973
4974 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004975 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004976 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004977 * If the error is permanent we'll catch it later,
4978 * if it's not, then hopefully it'll work next time. */
4979 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01004980 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004981 }
4982
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004983 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004984 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004985 /* Got a valid cookie, partially reset context */
4986 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4987 {
4988 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4989 return( ret );
4990 }
4991
4992 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004993 }
4994
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004995 return( ret );
4996}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004997#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004998
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004999static int ssl_check_record_type( uint8_t record_type )
5000{
5001 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5002 record_type != MBEDTLS_SSL_MSG_ALERT &&
5003 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5004 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5005 {
5006 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5007 }
5008
5009 return( 0 );
5010}
5011
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005012/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005013 * ContentType type;
5014 * ProtocolVersion version;
5015 * uint16 epoch; // DTLS only
5016 * uint48 sequence_number; // DTLS only
5017 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005018 *
5019 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005020 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005021 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5022 *
5023 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005024 * 1. proceed with the record if this function returns 0
5025 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5026 * 3. return CLIENT_RECONNECT if this function return that value
5027 * 4. drop the whole datagram if this function returns anything else.
5028 * Point 2 is needed when the peer is resending, and we have already received
5029 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005030 */
Hanno Becker331de3d2019-07-12 11:10:16 +01005031static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01005032 unsigned char *buf,
5033 size_t len,
5034 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005035{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005036 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005037
Hanno Beckere5e7e782019-07-11 12:29:35 +01005038 size_t const rec_hdr_type_offset = 0;
5039 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005040
Hanno Beckere5e7e782019-07-11 12:29:35 +01005041 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5042 rec_hdr_type_len;
5043 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005044
Hanno Beckere5e7e782019-07-11 12:29:35 +01005045 size_t const rec_hdr_ctr_len = 8;
5046#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01005047 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005048 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5049 rec_hdr_version_len;
5050
Hanno Beckera0e20d02019-05-15 14:03:01 +01005051#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01005052 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5053 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01005054 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005055#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5056#endif /* MBEDTLS_SSL_PROTO_DTLS */
5057
5058 size_t rec_hdr_len_offset; /* To be determined */
5059 size_t const rec_hdr_len_len = 2;
5060
5061 /*
5062 * Check minimum lengths for record header.
5063 */
5064
5065#if defined(MBEDTLS_SSL_PROTO_DTLS)
5066 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5067 {
5068 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5069 }
5070 else
5071#endif /* MBEDTLS_SSL_PROTO_DTLS */
5072 {
5073 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5074 }
5075
5076 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5077 {
5078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5079 (unsigned) len,
5080 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5081 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5082 }
5083
5084 /*
5085 * Parse and validate record content type
5086 */
5087
5088 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005089
5090 /* Check record content type */
5091#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5092 rec->cid_len = 0;
5093
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005094 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005095 ssl->conf->cid_len != 0 &&
5096 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005097 {
5098 /* Shift pointers to account for record header including CID
5099 * struct {
5100 * ContentType special_type = tls12_cid;
5101 * ProtocolVersion version;
5102 * uint16 epoch;
5103 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005104 * opaque cid[cid_length]; // Additional field compared to
5105 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005106 * uint16 length;
5107 * opaque enc_content[DTLSCiphertext.length];
5108 * } DTLSCiphertext;
5109 */
5110
5111 /* So far, we only support static CID lengths
5112 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005113 rec_hdr_cid_len = ssl->conf->cid_len;
5114 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005115
Hanno Beckere5e7e782019-07-11 12:29:35 +01005116 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005117 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5119 (unsigned) len,
5120 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005121 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005122 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005123
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02005124 /* configured CID len is guaranteed at most 255, see
5125 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5126 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005127 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005128 }
5129 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005130#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005131 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005132 if( ssl_check_record_type( rec->type ) )
5133 {
Hanno Becker54229812019-07-12 14:40:00 +01005134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5135 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005136 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5137 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005138 }
5139
Hanno Beckere5e7e782019-07-11 12:29:35 +01005140 /*
5141 * Parse and validate record version
5142 */
5143
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005144 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5145 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005146 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5147 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005148 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005149
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005150 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5153 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005154 }
5155
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005156 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5159 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005160 }
5161
Hanno Beckere5e7e782019-07-11 12:29:35 +01005162 /*
5163 * Parse/Copy record sequence number.
5164 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005165
Hanno Beckere5e7e782019-07-11 12:29:35 +01005166#if defined(MBEDTLS_SSL_PROTO_DTLS)
5167 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005168 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005169 /* Copy explicit record sequence number from input buffer. */
5170 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5171 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005172 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005173 else
5174#endif /* MBEDTLS_SSL_PROTO_DTLS */
5175 {
5176 /* Copy implicit record sequence number from SSL context structure. */
5177 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5178 }
Paul Bakker40e46942009-01-03 21:51:57 +00005179
Hanno Beckere5e7e782019-07-11 12:29:35 +01005180 /*
5181 * Parse record length.
5182 */
5183
Hanno Beckere5e7e782019-07-11 12:29:35 +01005184 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005185 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5186 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005187 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00005188
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005189 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005190 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005191 rec->type,
5192 major_ver, minor_ver, rec->data_len ) );
5193
5194 rec->buf = buf;
5195 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005196
Hanno Beckerd417cc92019-07-26 08:20:27 +01005197 if( rec->data_len == 0 )
5198 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005199
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005200 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005201 * DTLS-related tests.
5202 * Check epoch before checking length constraint because
5203 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5204 * message gets duplicated before the corresponding Finished message,
5205 * the second ChangeCipherSpec should be discarded because it belongs
5206 * to an old epoch, but not because its length is shorter than
5207 * the minimum record length for packets using the new record transform.
5208 * Note that these two kinds of failures are handled differently,
5209 * as an unexpected record is silently skipped but an invalid
5210 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005211 */
5212#if defined(MBEDTLS_SSL_PROTO_DTLS)
5213 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5214 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005215 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005216
Hanno Becker955a5c92019-07-10 17:12:07 +01005217 /* Check that the datagram is large enough to contain a record
5218 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005219 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005220 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5222 (unsigned) len,
5223 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005224 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5225 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005226
Hanno Becker37cfe732019-07-10 17:20:01 +01005227 /* Records from other, non-matching epochs are silently discarded.
5228 * (The case of same-port Client reconnects must be considered in
5229 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005230 if( rec_epoch != ssl->in_epoch )
5231 {
5232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5233 "expected %d, received %d",
5234 ssl->in_epoch, rec_epoch ) );
5235
Hanno Becker552f7472019-07-19 10:59:12 +01005236 /* Records from the next epoch are considered for buffering
5237 * (concretely: early Finished messages). */
5238 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005239 {
Hanno Becker552f7472019-07-19 10:59:12 +01005240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5241 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005242 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005243
Hanno Becker2fddd372019-07-10 14:37:41 +01005244 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005245 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005246#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005247 /* For records from the correct epoch, check whether their
5248 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005249 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005250 {
5251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5252 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5253 }
5254#endif
5255 }
5256#endif /* MBEDTLS_SSL_PROTO_DTLS */
5257
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005258 return( 0 );
5259}
Paul Bakker5121ce52009-01-03 21:22:43 +00005260
Paul Bakker5121ce52009-01-03 21:22:43 +00005261
Hanno Becker2fddd372019-07-10 14:37:41 +01005262#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5263static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5264{
5265 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5266
5267 /*
5268 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5269 * access the first byte of record content (handshake type), as we
5270 * have an active transform (possibly iv_len != 0), so use the
5271 * fact that the record header len is 13 instead.
5272 */
5273 if( rec_epoch == 0 &&
5274 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5275 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5276 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5277 ssl->in_left > 13 &&
5278 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5279 {
5280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5281 "from the same port" ) );
5282 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005283 }
5284
5285 return( 0 );
5286}
Hanno Becker2fddd372019-07-10 14:37:41 +01005287#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005288
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005289/*
5290 * If applicable, decrypt (and decompress) record content
5291 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005292static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5293 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005294{
5295 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005297 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005298 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005300#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5301 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305 ret = mbedtls_ssl_hw_record_read( ssl );
5306 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005308 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5309 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005310 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005311
5312 if( ret == 0 )
5313 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005314 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005315#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005316 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005317 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005318 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005319
Hanno Beckera18d1322018-01-03 14:27:32 +00005320 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005321 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005323 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005324
Hanno Beckera0e20d02019-05-15 14:03:01 +01005325#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005326 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5327 ssl->conf->ignore_unexpected_cid
5328 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5329 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005330 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005331 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005332 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005333#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005334
Paul Bakker5121ce52009-01-03 21:22:43 +00005335 return( ret );
5336 }
5337
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005338 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005339 {
5340 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005341 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005342 }
5343
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005344 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005345 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005346
Hanno Beckera0e20d02019-05-15 14:03:01 +01005347#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005348 /* We have already checked the record content type
5349 * in ssl_parse_record_header(), failing or silently
5350 * dropping the record in the case of an unknown type.
5351 *
5352 * Since with the use of CIDs, the record content type
5353 * might change during decryption, re-check the record
5354 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005355 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005356 {
5357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5358 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5359 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005360#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005361
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005362 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005363 {
5364#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5365 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005366 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005367 {
5368 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5370 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5371 }
5372#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5373
5374 ssl->nb_zero++;
5375
5376 /*
5377 * Three or more empty messages may be a DoS attack
5378 * (excessive CPU consumption).
5379 */
5380 if( ssl->nb_zero > 3 )
5381 {
5382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005383 "messages, possible DoS attack" ) );
5384 /* Treat the records as if they were not properly authenticated,
5385 * thereby failing the connection if we see more than allowed
5386 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005387 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5388 }
5389 }
5390 else
5391 ssl->nb_zero = 0;
5392
5393#if defined(MBEDTLS_SSL_PROTO_DTLS)
5394 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5395 {
5396 ; /* in_ctr read from peer, not maintained internally */
5397 }
5398 else
5399#endif
5400 {
5401 unsigned i;
5402 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5403 if( ++ssl->in_ctr[i - 1] != 0 )
5404 break;
5405
5406 /* The loop goes to its end iff the counter is wrapping */
5407 if( i == ssl_ep_len( ssl ) )
5408 {
5409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5410 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5411 }
5412 }
5413
Paul Bakker5121ce52009-01-03 21:22:43 +00005414 }
5415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005417 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005418 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005419 {
5420 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005422 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005423 return( ret );
5424 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005425 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005426#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005428#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005429 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005432 }
5433#endif
5434
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005435 /* Check actual (decrypted) record content length against
5436 * configured maximum. */
5437 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5438 {
5439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5440 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5441 }
5442
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005443 return( 0 );
5444}
5445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005446static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005447
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005448/*
5449 * Read a record.
5450 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005451 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5452 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5453 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005454 */
Hanno Becker1097b342018-08-15 14:09:41 +01005455
5456/* Helper functions for mbedtls_ssl_read_record(). */
5457static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005458static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5459static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005460
Hanno Becker327c93b2018-08-15 13:56:18 +01005461int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005462 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005463{
5464 int ret;
5465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005467
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005468 if( ssl->keep_current_message == 0 )
5469 {
5470 do {
Simon Butcher99000142016-10-13 17:21:01 +01005471
Hanno Becker26994592018-08-15 14:14:59 +01005472 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005473 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005474 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005475
Hanno Beckere74d5562018-08-15 14:26:08 +01005476 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005477 {
Hanno Becker40f50842018-08-15 14:48:01 +01005478#if defined(MBEDTLS_SSL_PROTO_DTLS)
5479 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005480
Hanno Becker40f50842018-08-15 14:48:01 +01005481 /* We only check for buffered messages if the
5482 * current datagram is fully consumed. */
5483 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005484 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005485 {
Hanno Becker40f50842018-08-15 14:48:01 +01005486 if( ssl_load_buffered_message( ssl ) == 0 )
5487 have_buffered = 1;
5488 }
5489
5490 if( have_buffered == 0 )
5491#endif /* MBEDTLS_SSL_PROTO_DTLS */
5492 {
5493 ret = ssl_get_next_record( ssl );
5494 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5495 continue;
5496
5497 if( ret != 0 )
5498 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005499 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005500 return( ret );
5501 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005502 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005503 }
5504
5505 ret = mbedtls_ssl_handle_message_type( ssl );
5506
Hanno Becker40f50842018-08-15 14:48:01 +01005507#if defined(MBEDTLS_SSL_PROTO_DTLS)
5508 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5509 {
5510 /* Buffer future message */
5511 ret = ssl_buffer_message( ssl );
5512 if( ret != 0 )
5513 return( ret );
5514
5515 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5516 }
5517#endif /* MBEDTLS_SSL_PROTO_DTLS */
5518
Hanno Becker90333da2017-10-10 11:27:13 +01005519 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5520 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005521
5522 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005523 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005524 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005525 return( ret );
5526 }
5527
Hanno Becker327c93b2018-08-15 13:56:18 +01005528 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005529 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005530 {
5531 mbedtls_ssl_update_handshake_status( ssl );
5532 }
Simon Butcher99000142016-10-13 17:21:01 +01005533 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005534 else
Simon Butcher99000142016-10-13 17:21:01 +01005535 {
Hanno Becker02f59072018-08-15 14:00:24 +01005536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005537 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005538 }
5539
5540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5541
5542 return( 0 );
5543}
5544
Hanno Becker40f50842018-08-15 14:48:01 +01005545#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005546static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005547{
Hanno Becker40f50842018-08-15 14:48:01 +01005548 if( ssl->in_left > ssl->next_record_offset )
5549 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005550
Hanno Becker40f50842018-08-15 14:48:01 +01005551 return( 0 );
5552}
5553
5554static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5555{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005556 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005557 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005558 int ret = 0;
5559
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005560 if( hs == NULL )
5561 return( -1 );
5562
Hanno Beckere00ae372018-08-20 09:39:42 +01005563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5564
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005565 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5566 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5567 {
5568 /* Check if we have seen a ChangeCipherSpec before.
5569 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005570 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005571 {
5572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5573 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005574 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005575 }
5576
Hanno Becker39b8bc92018-08-28 17:17:13 +01005577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005578 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5579 ssl->in_msglen = 1;
5580 ssl->in_msg[0] = 1;
5581
5582 /* As long as they are equal, the exact value doesn't matter. */
5583 ssl->in_left = 0;
5584 ssl->next_record_offset = 0;
5585
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005586 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005587 goto exit;
5588 }
Hanno Becker37f95322018-08-16 13:55:32 +01005589
Hanno Beckerb8f50142018-08-28 10:01:34 +01005590#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005591 /* Debug only */
5592 {
5593 unsigned offset;
5594 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5595 {
5596 hs_buf = &hs->buffering.hs[offset];
5597 if( hs_buf->is_valid == 1 )
5598 {
5599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5600 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005601 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005602 }
5603 }
5604 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005605#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005606
5607 /* Check if we have buffered and/or fully reassembled the
5608 * next handshake message. */
5609 hs_buf = &hs->buffering.hs[0];
5610 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5611 {
5612 /* Synthesize a record containing the buffered HS message. */
5613 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5614 ( hs_buf->data[2] << 8 ) |
5615 hs_buf->data[3];
5616
5617 /* Double-check that we haven't accidentally buffered
5618 * a message that doesn't fit into the input buffer. */
5619 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5620 {
5621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5622 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5623 }
5624
5625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5626 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5627 hs_buf->data, msg_len + 12 );
5628
5629 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5630 ssl->in_hslen = msg_len + 12;
5631 ssl->in_msglen = msg_len + 12;
5632 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5633
5634 ret = 0;
5635 goto exit;
5636 }
5637 else
5638 {
5639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5640 hs->in_msg_seq ) );
5641 }
5642
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005643 ret = -1;
5644
5645exit:
5646
5647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5648 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005649}
5650
Hanno Beckera02b0b42018-08-21 17:20:27 +01005651static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5652 size_t desired )
5653{
5654 int offset;
5655 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5657 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005658
Hanno Becker01315ea2018-08-21 17:22:17 +01005659 /* Get rid of future records epoch first, if such exist. */
5660 ssl_free_buffered_record( ssl );
5661
5662 /* Check if we have enough space available now. */
5663 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5664 hs->buffering.total_bytes_buffered ) )
5665 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005667 return( 0 );
5668 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005669
Hanno Becker4f432ad2018-08-28 10:02:32 +01005670 /* We don't have enough space to buffer the next expected handshake
5671 * message. Remove buffers used for future messages to gain space,
5672 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005673 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5674 offset >= 0; offset-- )
5675 {
5676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5677 offset ) );
5678
Hanno Beckerb309b922018-08-23 13:18:05 +01005679 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005680
5681 /* Check if we have enough space available now. */
5682 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5683 hs->buffering.total_bytes_buffered ) )
5684 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005686 return( 0 );
5687 }
5688 }
5689
5690 return( -1 );
5691}
5692
Hanno Becker40f50842018-08-15 14:48:01 +01005693static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5694{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005695 int ret = 0;
5696 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5697
5698 if( hs == NULL )
5699 return( 0 );
5700
5701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5702
5703 switch( ssl->in_msgtype )
5704 {
5705 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005707
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005708 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005709 break;
5710
5711 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005712 {
5713 unsigned recv_msg_seq_offset;
5714 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5715 mbedtls_ssl_hs_buffer *hs_buf;
5716 size_t msg_len = ssl->in_hslen - 12;
5717
5718 /* We should never receive an old handshake
5719 * message - double-check nonetheless. */
5720 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5721 {
5722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5723 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5724 }
5725
5726 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5727 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5728 {
5729 /* Silently ignore -- message too far in the future */
5730 MBEDTLS_SSL_DEBUG_MSG( 2,
5731 ( "Ignore future HS message with sequence number %u, "
5732 "buffering window %u - %u",
5733 recv_msg_seq, ssl->handshake->in_msg_seq,
5734 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5735
5736 goto exit;
5737 }
5738
5739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5740 recv_msg_seq, recv_msg_seq_offset ) );
5741
5742 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5743
5744 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005745 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005746 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005747 size_t reassembly_buf_sz;
5748
Hanno Becker37f95322018-08-16 13:55:32 +01005749 hs_buf->is_fragmented =
5750 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5751
5752 /* We copy the message back into the input buffer
5753 * after reassembly, so check that it's not too large.
5754 * This is an implementation-specific limitation
5755 * and not one from the standard, hence it is not
5756 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005757 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005758 {
5759 /* Ignore message */
5760 goto exit;
5761 }
5762
Hanno Beckere0b150f2018-08-21 15:51:03 +01005763 /* Check if we have enough space to buffer the message. */
5764 if( hs->buffering.total_bytes_buffered >
5765 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5766 {
5767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5768 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5769 }
5770
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005771 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5772 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005773
5774 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5775 hs->buffering.total_bytes_buffered ) )
5776 {
5777 if( recv_msg_seq_offset > 0 )
5778 {
5779 /* If we can't buffer a future message because
5780 * of space limitations -- ignore. */
5781 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",
5782 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5783 (unsigned) hs->buffering.total_bytes_buffered ) );
5784 goto exit;
5785 }
Hanno Beckere1801392018-08-21 16:51:05 +01005786 else
5787 {
5788 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",
5789 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5790 (unsigned) hs->buffering.total_bytes_buffered ) );
5791 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005792
Hanno Beckera02b0b42018-08-21 17:20:27 +01005793 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005794 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005795 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",
5796 (unsigned) msg_len,
5797 (unsigned) reassembly_buf_sz,
5798 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005799 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005800 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5801 goto exit;
5802 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005803 }
5804
5805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5806 msg_len ) );
5807
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005808 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5809 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005810 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005811 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005812 goto exit;
5813 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005814 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005815
5816 /* Prepare final header: copy msg_type, length and message_seq,
5817 * then add standardised fragment_offset and fragment_length */
5818 memcpy( hs_buf->data, ssl->in_msg, 6 );
5819 memset( hs_buf->data + 6, 0, 3 );
5820 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5821
5822 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005823
5824 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005825 }
5826 else
5827 {
5828 /* Make sure msg_type and length are consistent */
5829 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5830 {
5831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5832 /* Ignore */
5833 goto exit;
5834 }
5835 }
5836
Hanno Becker4422bbb2018-08-20 09:40:19 +01005837 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005838 {
5839 size_t frag_len, frag_off;
5840 unsigned char * const msg = hs_buf->data + 12;
5841
5842 /*
5843 * Check and copy current fragment
5844 */
5845
5846 /* Validation of header fields already done in
5847 * mbedtls_ssl_prepare_handshake_record(). */
5848 frag_off = ssl_get_hs_frag_off( ssl );
5849 frag_len = ssl_get_hs_frag_len( ssl );
5850
5851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5852 frag_off, frag_len ) );
5853 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5854
5855 if( hs_buf->is_fragmented )
5856 {
5857 unsigned char * const bitmask = msg + msg_len;
5858 ssl_bitmask_set( bitmask, frag_off, frag_len );
5859 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5860 msg_len ) == 0 );
5861 }
5862 else
5863 {
5864 hs_buf->is_complete = 1;
5865 }
5866
5867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5868 hs_buf->is_complete ? "" : "not yet " ) );
5869 }
5870
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005871 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005872 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005873
5874 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005875 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005876 break;
5877 }
5878
5879exit:
5880
5881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5882 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005883}
5884#endif /* MBEDTLS_SSL_PROTO_DTLS */
5885
Hanno Becker1097b342018-08-15 14:09:41 +01005886static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005887{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005888 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005889 * Consume last content-layer message and potentially
5890 * update in_msglen which keeps track of the contents'
5891 * consumption state.
5892 *
5893 * (1) Handshake messages:
5894 * Remove last handshake message, move content
5895 * and adapt in_msglen.
5896 *
5897 * (2) Alert messages:
5898 * Consume whole record content, in_msglen = 0.
5899 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005900 * (3) Change cipher spec:
5901 * Consume whole record content, in_msglen = 0.
5902 *
5903 * (4) Application data:
5904 * Don't do anything - the record layer provides
5905 * the application data as a stream transport
5906 * and consumes through mbedtls_ssl_read only.
5907 *
5908 */
5909
5910 /* Case (1): Handshake messages */
5911 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005912 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005913 /* Hard assertion to be sure that no application data
5914 * is in flight, as corrupting ssl->in_msglen during
5915 * ssl->in_offt != NULL is fatal. */
5916 if( ssl->in_offt != NULL )
5917 {
5918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5919 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5920 }
5921
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005922 /*
5923 * Get next Handshake message in the current record
5924 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005925
Hanno Becker4a810fb2017-05-24 16:27:30 +01005926 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005927 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005928 * current handshake content: If DTLS handshake
5929 * fragmentation is used, that's the fragment
5930 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005931 * size here is faulty and should be changed at
5932 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005933 * (2) While it doesn't seem to cause problems, one
5934 * has to be very careful not to assume that in_hslen
5935 * is always <= in_msglen in a sensible communication.
5936 * Again, it's wrong for DTLS handshake fragmentation.
5937 * The following check is therefore mandatory, and
5938 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005939 * Additionally, ssl->in_hslen might be arbitrarily out of
5940 * bounds after handling a DTLS message with an unexpected
5941 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005942 */
5943 if( ssl->in_hslen < ssl->in_msglen )
5944 {
5945 ssl->in_msglen -= ssl->in_hslen;
5946 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5947 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005948
Hanno Becker4a810fb2017-05-24 16:27:30 +01005949 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5950 ssl->in_msg, ssl->in_msglen );
5951 }
5952 else
5953 {
5954 ssl->in_msglen = 0;
5955 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005956
Hanno Becker4a810fb2017-05-24 16:27:30 +01005957 ssl->in_hslen = 0;
5958 }
5959 /* Case (4): Application data */
5960 else if( ssl->in_offt != NULL )
5961 {
5962 return( 0 );
5963 }
5964 /* Everything else (CCS & Alerts) */
5965 else
5966 {
5967 ssl->in_msglen = 0;
5968 }
5969
Hanno Becker1097b342018-08-15 14:09:41 +01005970 return( 0 );
5971}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005972
Hanno Beckere74d5562018-08-15 14:26:08 +01005973static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5974{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005975 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005976 return( 1 );
5977
5978 return( 0 );
5979}
5980
Hanno Becker5f066e72018-08-16 14:56:31 +01005981#if defined(MBEDTLS_SSL_PROTO_DTLS)
5982
5983static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5984{
5985 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5986 if( hs == NULL )
5987 return;
5988
Hanno Becker01315ea2018-08-21 17:22:17 +01005989 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005990 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005991 hs->buffering.total_bytes_buffered -=
5992 hs->buffering.future_record.len;
5993
5994 mbedtls_free( hs->buffering.future_record.data );
5995 hs->buffering.future_record.data = NULL;
5996 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005997}
5998
5999static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6000{
6001 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6002 unsigned char * rec;
6003 size_t rec_len;
6004 unsigned rec_epoch;
6005
6006 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6007 return( 0 );
6008
6009 if( hs == NULL )
6010 return( 0 );
6011
Hanno Becker5f066e72018-08-16 14:56:31 +01006012 rec = hs->buffering.future_record.data;
6013 rec_len = hs->buffering.future_record.len;
6014 rec_epoch = hs->buffering.future_record.epoch;
6015
6016 if( rec == NULL )
6017 return( 0 );
6018
Hanno Becker4cb782d2018-08-20 11:19:05 +01006019 /* Only consider loading future records if the
6020 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006021 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006022 return( 0 );
6023
Hanno Becker5f066e72018-08-16 14:56:31 +01006024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6025
6026 if( rec_epoch != ssl->in_epoch )
6027 {
6028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6029 goto exit;
6030 }
6031
6032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6033
6034 /* Double-check that the record is not too large */
6035 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6036 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6037 {
6038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6039 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6040 }
6041
6042 memcpy( ssl->in_hdr, rec, rec_len );
6043 ssl->in_left = rec_len;
6044 ssl->next_record_offset = 0;
6045
6046 ssl_free_buffered_record( ssl );
6047
6048exit:
6049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6050 return( 0 );
6051}
6052
Hanno Becker519f15d2019-07-11 12:43:20 +01006053static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6054 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006055{
6056 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006057
6058 /* Don't buffer future records outside handshakes. */
6059 if( hs == NULL )
6060 return( 0 );
6061
6062 /* Only buffer handshake records (we are only interested
6063 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01006064 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006065 return( 0 );
6066
6067 /* Don't buffer more than one future epoch record. */
6068 if( hs->buffering.future_record.data != NULL )
6069 return( 0 );
6070
Hanno Becker01315ea2018-08-21 17:22:17 +01006071 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01006072 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006073 hs->buffering.total_bytes_buffered ) )
6074 {
6075 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",
Hanno Becker519f15d2019-07-11 12:43:20 +01006076 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006077 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006078 return( 0 );
6079 }
6080
Hanno Becker5f066e72018-08-16 14:56:31 +01006081 /* Buffer record */
6082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6083 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01006084 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006085
6086 /* ssl_parse_record_header() only considers records
6087 * of the next epoch as candidates for buffering. */
6088 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006089 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006090
6091 hs->buffering.future_record.data =
6092 mbedtls_calloc( 1, hs->buffering.future_record.len );
6093 if( hs->buffering.future_record.data == NULL )
6094 {
6095 /* If we run out of RAM trying to buffer a
6096 * record from the next epoch, just ignore. */
6097 return( 0 );
6098 }
6099
Hanno Becker519f15d2019-07-11 12:43:20 +01006100 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006101
Hanno Becker519f15d2019-07-11 12:43:20 +01006102 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006103 return( 0 );
6104}
6105
6106#endif /* MBEDTLS_SSL_PROTO_DTLS */
6107
Hanno Beckere74d5562018-08-15 14:26:08 +01006108static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006109{
6110 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006111 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006112
Hanno Becker5f066e72018-08-16 14:56:31 +01006113#if defined(MBEDTLS_SSL_PROTO_DTLS)
6114 /* We might have buffered a future record; if so,
6115 * and if the epoch matches now, load it.
6116 * On success, this call will set ssl->in_left to
6117 * the length of the buffered record, so that
6118 * the calls to ssl_fetch_input() below will
6119 * essentially be no-ops. */
6120 ret = ssl_load_buffered_record( ssl );
6121 if( ret != 0 )
6122 return( ret );
6123#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006124
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006125 /* Ensure that we have enough space available for the default form
6126 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6127 * with no space for CIDs counted in). */
6128 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6129 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006131 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006132 return( ret );
6133 }
6134
Hanno Beckere5e7e782019-07-11 12:29:35 +01006135 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6136 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006138#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006139 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006140 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006141 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6142 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006143 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006144 if( ret != 0 )
6145 return( ret );
6146
6147 /* Fall through to handling of unexpected records */
6148 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6149 }
6150
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006151 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6152 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006153#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006154 /* Reset in pointers to default state for TLS/DTLS records,
6155 * assuming no CID and no offset between record content and
6156 * record plaintext. */
6157 ssl_update_in_pointers( ssl );
6158
Hanno Becker7ae20e02019-07-12 08:33:49 +01006159 /* Setup internal message pointers from record structure. */
6160 ssl->in_msgtype = rec.type;
6161#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6162 ssl->in_len = ssl->in_cid + rec.cid_len;
6163#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6164 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6165 ssl->in_msglen = rec.data_len;
6166
Hanno Becker2fddd372019-07-10 14:37:41 +01006167 ret = ssl_check_client_reconnect( ssl );
6168 if( ret != 0 )
6169 return( ret );
6170#endif
6171
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006172 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006173 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006174
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6176 "(header)" ) );
6177 }
6178 else
6179 {
6180 /* Skip invalid record and the rest of the datagram */
6181 ssl->next_record_offset = 0;
6182 ssl->in_left = 0;
6183
6184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6185 "(header)" ) );
6186 }
6187
6188 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006189 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006190 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006191 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006192#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006193 {
6194 return( ret );
6195 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006196 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006198#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006199 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006200 {
Hanno Beckera8814792019-07-10 15:01:45 +01006201 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006202 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006203 if( ssl->next_record_offset < ssl->in_left )
6204 {
6205 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6206 }
6207 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006208 else
6209#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006210 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006211 /*
6212 * Fetch record contents from underlying transport.
6213 */
Hanno Beckera3175662019-07-11 12:50:29 +01006214 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006215 if( ret != 0 )
6216 {
6217 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6218 return( ret );
6219 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006220
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006221 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006222 }
6223
6224 /*
6225 * Decrypt record contents.
6226 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006227
Hanno Beckerfdf66042019-07-11 13:07:45 +01006228 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006231 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006232 {
6233 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006234 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006235 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006236 /* Except when waiting for Finished as a bad mac here
6237 * probably means something went wrong in the handshake
6238 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6239 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6240 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6241 {
6242#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6243 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6244 {
6245 mbedtls_ssl_send_alert_message( ssl,
6246 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6247 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6248 }
6249#endif
6250 return( ret );
6251 }
6252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006253#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006254 if( ssl->conf->badmac_limit != 0 &&
6255 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6258 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006259 }
6260#endif
6261
Hanno Becker4a810fb2017-05-24 16:27:30 +01006262 /* As above, invalid records cause
6263 * dismissal of the whole datagram. */
6264
6265 ssl->next_record_offset = 0;
6266 ssl->in_left = 0;
6267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006269 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006270 }
6271
6272 return( ret );
6273 }
6274 else
6275#endif
6276 {
6277 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006278#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6279 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006281 mbedtls_ssl_send_alert_message( ssl,
6282 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6283 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006284 }
6285#endif
6286 return( ret );
6287 }
6288 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006289
Hanno Becker44d89b22019-07-12 09:40:44 +01006290
6291 /* Reset in pointers to default state for TLS/DTLS records,
6292 * assuming no CID and no offset between record content and
6293 * record plaintext. */
6294 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006295#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6296 ssl->in_len = ssl->in_cid + rec.cid_len;
6297#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6298 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006299
Hanno Becker8685c822019-07-12 09:37:30 +01006300 /* The record content type may change during decryption,
6301 * so re-read it. */
6302 ssl->in_msgtype = rec.type;
6303 /* Also update the input buffer, because unfortunately
6304 * the server-side ssl_parse_client_hello() reparses the
6305 * record header when receiving a ClientHello initiating
6306 * a renegotiation. */
6307 ssl->in_hdr[0] = rec.type;
6308 ssl->in_msg = rec.buf + rec.data_offset;
6309 ssl->in_msglen = rec.data_len;
6310 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6311 ssl->in_len[1] = (unsigned char)( rec.data_len );
6312
Simon Butcher99000142016-10-13 17:21:01 +01006313 return( 0 );
6314}
6315
6316int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6317{
6318 int ret;
6319
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006320 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006321 * Handle particular types of records
6322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006324 {
Simon Butcher99000142016-10-13 17:21:01 +01006325 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6326 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006327 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006328 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006329 }
6330
Hanno Beckere678eaa2018-08-21 14:57:46 +01006331 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006332 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006333 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006334 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6336 ssl->in_msglen ) );
6337 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006338 }
6339
Hanno Beckere678eaa2018-08-21 14:57:46 +01006340 if( ssl->in_msg[0] != 1 )
6341 {
6342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6343 ssl->in_msg[0] ) );
6344 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6345 }
6346
6347#if defined(MBEDTLS_SSL_PROTO_DTLS)
6348 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6349 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6350 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6351 {
6352 if( ssl->handshake == NULL )
6353 {
6354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6355 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6356 }
6357
6358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6359 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6360 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006361#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006362 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006365 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006366 if( ssl->in_msglen != 2 )
6367 {
6368 /* Note: Standard allows for more than one 2 byte alert
6369 to be packed in a single message, but Mbed TLS doesn't
6370 currently support this. */
6371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6372 ssl->in_msglen ) );
6373 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6374 }
6375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006377 ssl->in_msg[0], ssl->in_msg[1] ) );
6378
6379 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006380 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006382 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006385 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006387 }
6388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006389 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6390 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6393 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006394 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006395
6396#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6397 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6398 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6399 {
Hanno Becker90333da2017-10-10 11:27:13 +01006400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006401 /* Will be handled when trying to parse ServerHello */
6402 return( 0 );
6403 }
6404#endif
6405
6406#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6407 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6408 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6409 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6410 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6411 {
6412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6413 /* Will be handled in mbedtls_ssl_parse_certificate() */
6414 return( 0 );
6415 }
6416#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6417
6418 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006419 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006420 }
6421
Hanno Beckerc76c6192017-06-06 10:03:17 +01006422#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006423 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006424 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006425 /* Drop unexpected ApplicationData records,
6426 * except at the beginning of renegotiations */
6427 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6428 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6429#if defined(MBEDTLS_SSL_RENEGOTIATION)
6430 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6431 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006432#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006433 )
6434 {
6435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6436 return( MBEDTLS_ERR_SSL_NON_FATAL );
6437 }
6438
6439 if( ssl->handshake != NULL &&
6440 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6441 {
6442 ssl_handshake_wrapup_free_hs_transform( ssl );
6443 }
6444 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006445#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006446
Paul Bakker5121ce52009-01-03 21:22:43 +00006447 return( 0 );
6448}
6449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006450int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006451{
6452 int ret;
6453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006454 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6455 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6456 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006457 {
6458 return( ret );
6459 }
6460
6461 return( 0 );
6462}
6463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006465 unsigned char level,
6466 unsigned char message )
6467{
6468 int ret;
6469
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006470 if( ssl == NULL || ssl->conf == NULL )
6471 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006474 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006476 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006477 ssl->out_msglen = 2;
6478 ssl->out_msg[0] = level;
6479 ssl->out_msg[1] = message;
6480
Hanno Becker67bc7c32018-08-06 11:33:50 +01006481 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006484 return( ret );
6485 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006487
6488 return( 0 );
6489}
6490
Hanno Beckerb9d44792019-02-08 07:19:04 +00006491#if defined(MBEDTLS_X509_CRT_PARSE_C)
6492static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6493{
6494#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6495 if( session->peer_cert != NULL )
6496 {
6497 mbedtls_x509_crt_free( session->peer_cert );
6498 mbedtls_free( session->peer_cert );
6499 session->peer_cert = NULL;
6500 }
6501#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6502 if( session->peer_cert_digest != NULL )
6503 {
6504 /* Zeroization is not necessary. */
6505 mbedtls_free( session->peer_cert_digest );
6506 session->peer_cert_digest = NULL;
6507 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6508 session->peer_cert_digest_len = 0;
6509 }
6510#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6511}
6512#endif /* MBEDTLS_X509_CRT_PARSE_C */
6513
Paul Bakker5121ce52009-01-03 21:22:43 +00006514/*
6515 * Handshake functions
6516 */
Hanno Becker21489932019-02-05 13:20:55 +00006517#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006518/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006520{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006521 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6522 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006525
Hanno Becker7177a882019-02-05 13:36:46 +00006526 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006529 ssl->state++;
6530 return( 0 );
6531 }
6532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6534 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006535}
6536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006537int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006538{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006539 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6540 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006543
Hanno Becker7177a882019-02-05 13:36:46 +00006544 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006547 ssl->state++;
6548 return( 0 );
6549 }
6550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6552 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006553}
Gilles Peskinef9828522017-05-03 12:28:43 +02006554
Hanno Becker21489932019-02-05 13:20:55 +00006555#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006556/* Some certificate support -> implement write and parse */
6557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006558int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006559{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006561 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006562 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006563 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6564 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006567
Hanno Becker7177a882019-02-05 13:36:46 +00006568 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006571 ssl->state++;
6572 return( 0 );
6573 }
6574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006575#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006576 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006577 {
6578 if( ssl->client_auth == 0 )
6579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006581 ssl->state++;
6582 return( 0 );
6583 }
6584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006585#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006586 /*
6587 * If using SSLv3 and got no cert, send an Alert message
6588 * (otherwise an empty Certificate message will be sent).
6589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006590 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6591 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006592 {
6593 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6595 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6596 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006599 goto write_msg;
6600 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006601#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006602 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006603#endif /* MBEDTLS_SSL_CLI_C */
6604#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006605 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006607 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6610 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006611 }
6612 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006613#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006616
6617 /*
6618 * 0 . 0 handshake type
6619 * 1 . 3 handshake length
6620 * 4 . 6 length of all certs
6621 * 7 . 9 length of cert. 1
6622 * 10 . n-1 peer certificate
6623 * n . n+2 length of cert. 2
6624 * n+3 . ... upper level cert, etc.
6625 */
6626 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006627 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006628
Paul Bakker29087132010-03-21 21:03:34 +00006629 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006630 {
6631 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006632 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006635 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006636 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006637 }
6638
6639 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6640 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6641 ssl->out_msg[i + 2] = (unsigned char)( n );
6642
6643 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6644 i += n; crt = crt->next;
6645 }
6646
6647 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6648 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6649 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6650
6651 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6653 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006654
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006655#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006656write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006657#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006658
6659 ssl->state++;
6660
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006661 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006662 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006663 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006664 return( ret );
6665 }
6666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006668
Paul Bakkered27a042013-04-18 22:46:23 +02006669 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006670}
6671
Hanno Becker84879e32019-01-31 07:44:03 +00006672#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006673
6674#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006675static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6676 unsigned char *crt_buf,
6677 size_t crt_buf_len )
6678{
6679 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6680
6681 if( peer_crt == NULL )
6682 return( -1 );
6683
6684 if( peer_crt->raw.len != crt_buf_len )
6685 return( -1 );
6686
Hanno Becker46f34d02019-02-08 14:00:04 +00006687 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006688}
Hanno Becker177475a2019-02-05 17:02:46 +00006689#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6690static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6691 unsigned char *crt_buf,
6692 size_t crt_buf_len )
6693{
6694 int ret;
6695 unsigned char const * const peer_cert_digest =
6696 ssl->session->peer_cert_digest;
6697 mbedtls_md_type_t const peer_cert_digest_type =
6698 ssl->session->peer_cert_digest_type;
6699 mbedtls_md_info_t const * const digest_info =
6700 mbedtls_md_info_from_type( peer_cert_digest_type );
6701 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6702 size_t digest_len;
6703
6704 if( peer_cert_digest == NULL || digest_info == NULL )
6705 return( -1 );
6706
6707 digest_len = mbedtls_md_get_size( digest_info );
6708 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6709 return( -1 );
6710
6711 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6712 if( ret != 0 )
6713 return( -1 );
6714
6715 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6716}
6717#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006718#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006719
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006720/*
6721 * Once the certificate message is read, parse it into a cert chain and
6722 * perform basic checks, but leave actual verification to the caller
6723 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006724static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6725 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006726{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006727 int ret;
6728#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6729 int crt_cnt=0;
6730#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006731 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006732 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006734 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006737 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6738 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006740 }
6741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006742 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6743 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006746 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6747 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006749 }
6750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006751 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006752
Paul Bakker5121ce52009-01-03 21:22:43 +00006753 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006755 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006756 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006757
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006758 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006762 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6763 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006765 }
6766
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006767 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6768 i += 3;
6769
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006770 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006771 while( i < ssl->in_hslen )
6772 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006773 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006774 if ( i + 3 > ssl->in_hslen ) {
6775 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006776 mbedtls_ssl_send_alert_message( ssl,
6777 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6778 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006779 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6780 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006781 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6782 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006783 if( ssl->in_msg[i] != 0 )
6784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006786 mbedtls_ssl_send_alert_message( ssl,
6787 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6788 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006789 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006790 }
6791
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006792 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006793 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6794 | (unsigned int) ssl->in_msg[i + 2];
6795 i += 3;
6796
6797 if( n < 128 || i + n > ssl->in_hslen )
6798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006800 mbedtls_ssl_send_alert_message( ssl,
6801 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6802 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006804 }
6805
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006806 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006807#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6808 if( crt_cnt++ == 0 &&
6809 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6810 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006811 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006812 /* During client-side renegotiation, check that the server's
6813 * end-CRTs hasn't changed compared to the initial handshake,
6814 * mitigating the triple handshake attack. On success, reuse
6815 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006816 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6817 if( ssl_check_peer_crt_unchanged( ssl,
6818 &ssl->in_msg[i],
6819 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006820 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6822 mbedtls_ssl_send_alert_message( ssl,
6823 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6824 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6825 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006826 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006827
6828 /* Now we can safely free the original chain. */
6829 ssl_clear_peer_cert( ssl->session );
6830 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006831#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6832
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006833 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006834#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006835 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006836#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006837 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006838 * it in-place from the input buffer instead of making a copy. */
6839 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6840#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006841 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006842 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006843 case 0: /*ok*/
6844 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6845 /* Ignore certificate with an unknown algorithm: maybe a
6846 prior certificate was already trusted. */
6847 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006848
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006849 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6850 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6851 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006852
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006853 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6854 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6855 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006856
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006857 default:
6858 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6859 crt_parse_der_failed:
6860 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6861 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6862 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006863 }
6864
6865 i += n;
6866 }
6867
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006868 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006869 return( 0 );
6870}
6871
Hanno Becker4a55f632019-02-05 12:49:06 +00006872#if defined(MBEDTLS_SSL_SRV_C)
6873static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6874{
6875 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6876 return( -1 );
6877
6878#if defined(MBEDTLS_SSL_PROTO_SSL3)
6879 /*
6880 * Check if the client sent an empty certificate
6881 */
6882 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6883 {
6884 if( ssl->in_msglen == 2 &&
6885 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6886 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6887 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6888 {
6889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6890 return( 0 );
6891 }
6892
6893 return( -1 );
6894 }
6895#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6896
6897#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6898 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6899 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6900 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6901 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6902 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6903 {
6904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6905 return( 0 );
6906 }
6907
6908 return( -1 );
6909#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6910 MBEDTLS_SSL_PROTO_TLS1_2 */
6911}
6912#endif /* MBEDTLS_SSL_SRV_C */
6913
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006914/* Check if a certificate message is expected.
6915 * Return either
6916 * - SSL_CERTIFICATE_EXPECTED, or
6917 * - SSL_CERTIFICATE_SKIP
6918 * indicating whether a Certificate message is expected or not.
6919 */
6920#define SSL_CERTIFICATE_EXPECTED 0
6921#define SSL_CERTIFICATE_SKIP 1
6922static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6923 int authmode )
6924{
6925 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006926 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006927
6928 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6929 return( SSL_CERTIFICATE_SKIP );
6930
6931#if defined(MBEDTLS_SSL_SRV_C)
6932 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6933 {
6934 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6935 return( SSL_CERTIFICATE_SKIP );
6936
6937 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6938 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006939 ssl->session_negotiate->verify_result =
6940 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6941 return( SSL_CERTIFICATE_SKIP );
6942 }
6943 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006944#else
6945 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006946#endif /* MBEDTLS_SSL_SRV_C */
6947
6948 return( SSL_CERTIFICATE_EXPECTED );
6949}
6950
Hanno Becker68636192019-02-05 14:36:34 +00006951static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6952 int authmode,
6953 mbedtls_x509_crt *chain,
6954 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006955{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006956 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006957 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006958 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006959 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006960
Hanno Becker8927c832019-04-03 12:52:50 +01006961 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6962 void *p_vrfy;
6963
Hanno Becker68636192019-02-05 14:36:34 +00006964 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6965 return( 0 );
6966
Hanno Becker8927c832019-04-03 12:52:50 +01006967 if( ssl->f_vrfy != NULL )
6968 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006969 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006970 f_vrfy = ssl->f_vrfy;
6971 p_vrfy = ssl->p_vrfy;
6972 }
6973 else
6974 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006975 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006976 f_vrfy = ssl->conf->f_vrfy;
6977 p_vrfy = ssl->conf->p_vrfy;
6978 }
6979
Hanno Becker68636192019-02-05 14:36:34 +00006980 /*
6981 * Main check: verify certificate
6982 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006983#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6984 if( ssl->conf->f_ca_cb != NULL )
6985 {
6986 ((void) rs_ctx);
6987 have_ca_chain = 1;
6988
6989 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006990 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006991 chain,
6992 ssl->conf->f_ca_cb,
6993 ssl->conf->p_ca_cb,
6994 ssl->conf->cert_profile,
6995 ssl->hostname,
6996 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006997 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006998 }
6999 else
7000#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7001 {
7002 mbedtls_x509_crt *ca_chain;
7003 mbedtls_x509_crl *ca_crl;
7004
7005#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7006 if( ssl->handshake->sni_ca_chain != NULL )
7007 {
7008 ca_chain = ssl->handshake->sni_ca_chain;
7009 ca_crl = ssl->handshake->sni_ca_crl;
7010 }
7011 else
7012#endif
7013 {
7014 ca_chain = ssl->conf->ca_chain;
7015 ca_crl = ssl->conf->ca_crl;
7016 }
7017
7018 if( ca_chain != NULL )
7019 have_ca_chain = 1;
7020
7021 ret = mbedtls_x509_crt_verify_restartable(
7022 chain,
7023 ca_chain, ca_crl,
7024 ssl->conf->cert_profile,
7025 ssl->hostname,
7026 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007027 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007028 }
Hanno Becker68636192019-02-05 14:36:34 +00007029
7030 if( ret != 0 )
7031 {
7032 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7033 }
7034
7035#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7036 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7037 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7038#endif
7039
7040 /*
7041 * Secondary checks: always done, but change 'ret' only if it was 0
7042 */
7043
7044#if defined(MBEDTLS_ECP_C)
7045 {
7046 const mbedtls_pk_context *pk = &chain->pk;
7047
7048 /* If certificate uses an EC key, make sure the curve is OK */
7049 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
7050 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
7051 {
7052 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7053
7054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7055 if( ret == 0 )
7056 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7057 }
7058 }
7059#endif /* MBEDTLS_ECP_C */
7060
7061 if( mbedtls_ssl_check_cert_usage( chain,
7062 ciphersuite_info,
7063 ! ssl->conf->endpoint,
7064 &ssl->session_negotiate->verify_result ) != 0 )
7065 {
7066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7067 if( ret == 0 )
7068 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7069 }
7070
7071 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7072 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7073 * with details encoded in the verification flags. All other kinds
7074 * of error codes, including those from the user provided f_vrfy
7075 * functions, are treated as fatal and lead to a failure of
7076 * ssl_parse_certificate even if verification was optional. */
7077 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7078 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7079 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7080 {
7081 ret = 0;
7082 }
7083
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007084 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007085 {
7086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7087 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7088 }
7089
7090 if( ret != 0 )
7091 {
7092 uint8_t alert;
7093
7094 /* The certificate may have been rejected for several reasons.
7095 Pick one and send the corresponding alert. Which alert to send
7096 may be a subject of debate in some cases. */
7097 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7098 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7099 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7100 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7101 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7102 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7103 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7104 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7105 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7106 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7107 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7108 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7109 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7110 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7111 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7112 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7113 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7114 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7115 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7116 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7117 else
7118 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7119 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7120 alert );
7121 }
7122
7123#if defined(MBEDTLS_DEBUG_C)
7124 if( ssl->session_negotiate->verify_result != 0 )
7125 {
7126 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7127 ssl->session_negotiate->verify_result ) );
7128 }
7129 else
7130 {
7131 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7132 }
7133#endif /* MBEDTLS_DEBUG_C */
7134
7135 return( ret );
7136}
7137
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007138#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7139static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7140 unsigned char *start, size_t len )
7141{
7142 int ret;
7143 /* Remember digest of the peer's end-CRT. */
7144 ssl->session_negotiate->peer_cert_digest =
7145 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7146 if( ssl->session_negotiate->peer_cert_digest == NULL )
7147 {
7148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7149 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7150 mbedtls_ssl_send_alert_message( ssl,
7151 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7152 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7153
7154 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7155 }
7156
7157 ret = mbedtls_md( mbedtls_md_info_from_type(
7158 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7159 start, len,
7160 ssl->session_negotiate->peer_cert_digest );
7161
7162 ssl->session_negotiate->peer_cert_digest_type =
7163 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7164 ssl->session_negotiate->peer_cert_digest_len =
7165 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7166
7167 return( ret );
7168}
7169
7170static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7171 unsigned char *start, size_t len )
7172{
7173 unsigned char *end = start + len;
7174 int ret;
7175
7176 /* Make a copy of the peer's raw public key. */
7177 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7178 ret = mbedtls_pk_parse_subpubkey( &start, end,
7179 &ssl->handshake->peer_pubkey );
7180 if( ret != 0 )
7181 {
7182 /* We should have parsed the public key before. */
7183 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7184 }
7185
7186 return( 0 );
7187}
7188#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7189
Hanno Becker68636192019-02-05 14:36:34 +00007190int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7191{
7192 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007193 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007194#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7195 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7196 ? ssl->handshake->sni_authmode
7197 : ssl->conf->authmode;
7198#else
7199 const int authmode = ssl->conf->authmode;
7200#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007201 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007202 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007203
7204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7205
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007206 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7207 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007208 {
7209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007210 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007211 }
7212
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007213#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7214 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007215 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007216 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007217 chain = ssl->handshake->ecrs_peer_cert;
7218 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007219 goto crt_verify;
7220 }
7221#endif
7222
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007223 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007224 {
7225 /* mbedtls_ssl_read_record may have sent an alert already. We
7226 let it decide whether to alert. */
7227 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007228 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007229 }
7230
Hanno Becker4a55f632019-02-05 12:49:06 +00007231#if defined(MBEDTLS_SSL_SRV_C)
7232 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7233 {
7234 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007235
7236 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007237 ret = 0;
7238 else
7239 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007240
Hanno Becker6bdfab22019-02-05 13:11:17 +00007241 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007242 }
7243#endif /* MBEDTLS_SSL_SRV_C */
7244
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007245 /* Clear existing peer CRT structure in case we tried to
7246 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007247 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007248
7249 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7250 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007251 {
7252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7253 sizeof( mbedtls_x509_crt ) ) );
7254 mbedtls_ssl_send_alert_message( ssl,
7255 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7256 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007257
Hanno Becker3dad3112019-02-05 17:19:52 +00007258 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7259 goto exit;
7260 }
7261 mbedtls_x509_crt_init( chain );
7262
7263 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007264 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007265 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007266
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007267#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7268 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007269 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007270
7271crt_verify:
7272 if( ssl->handshake->ecrs_enabled)
7273 rs_ctx = &ssl->handshake->ecrs_ctx;
7274#endif
7275
Hanno Becker68636192019-02-05 14:36:34 +00007276 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007277 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007278 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007279 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007280
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007281#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007282 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007283 unsigned char *crt_start, *pk_start;
7284 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007285
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007286 /* We parse the CRT chain without copying, so
7287 * these pointers point into the input buffer,
7288 * and are hence still valid after freeing the
7289 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007290
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007291 crt_start = chain->raw.p;
7292 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007293
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007294 pk_start = chain->pk_raw.p;
7295 pk_len = chain->pk_raw.len;
7296
7297 /* Free the CRT structures before computing
7298 * digest and copying the peer's public key. */
7299 mbedtls_x509_crt_free( chain );
7300 mbedtls_free( chain );
7301 chain = NULL;
7302
7303 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007304 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007305 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007306
7307 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7308 if( ret != 0 )
7309 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007310 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007311#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7312 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007313 ssl->session_negotiate->peer_cert = chain;
7314 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007315#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007318
Hanno Becker6bdfab22019-02-05 13:11:17 +00007319exit:
7320
Hanno Becker3dad3112019-02-05 17:19:52 +00007321 if( ret == 0 )
7322 ssl->state++;
7323
7324#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7325 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7326 {
7327 ssl->handshake->ecrs_peer_cert = chain;
7328 chain = NULL;
7329 }
7330#endif
7331
7332 if( chain != NULL )
7333 {
7334 mbedtls_x509_crt_free( chain );
7335 mbedtls_free( chain );
7336 }
7337
Paul Bakker5121ce52009-01-03 21:22:43 +00007338 return( ret );
7339}
Hanno Becker21489932019-02-05 13:20:55 +00007340#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007343{
7344 int ret;
7345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007349 ssl->out_msglen = 1;
7350 ssl->out_msg[0] = 1;
7351
Paul Bakker5121ce52009-01-03 21:22:43 +00007352 ssl->state++;
7353
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007354 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007355 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007356 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007357 return( ret );
7358 }
7359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007361
7362 return( 0 );
7363}
7364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007365int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007366{
7367 int ret;
7368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007370
Hanno Becker327c93b2018-08-15 13:56:18 +01007371 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007374 return( ret );
7375 }
7376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007380 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7381 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007383 }
7384
Hanno Beckere678eaa2018-08-21 14:57:46 +01007385 /* CCS records are only accepted if they have length 1 and content '1',
7386 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007387
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007388 /*
7389 * Switch to our negotiated transform and session parameters for inbound
7390 * data.
7391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007393 ssl->transform_in = ssl->transform_negotiate;
7394 ssl->session_in = ssl->session_negotiate;
7395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007396#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007397 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007400 ssl_dtls_replay_reset( ssl );
7401#endif
7402
7403 /* Increment epoch */
7404 if( ++ssl->in_epoch == 0 )
7405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007407 /* This is highly unlikely to happen for legitimate reasons, so
7408 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007410 }
7411 }
7412 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007413#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007414 memset( ssl->in_ctr, 0, 8 );
7415
Hanno Becker79594fd2019-05-08 09:38:41 +01007416 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7419 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007421 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007424 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7425 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007427 }
7428 }
7429#endif
7430
Paul Bakker5121ce52009-01-03 21:22:43 +00007431 ssl->state++;
7432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007434
7435 return( 0 );
7436}
7437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007438void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7439 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007440{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007441 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7444 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7445 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007446 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007447 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007448#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7450#if defined(MBEDTLS_SHA512_C)
7451 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007452 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7453 else
7454#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007455#if defined(MBEDTLS_SHA256_C)
7456 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007457 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007458 else
7459#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007460#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007463 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007464 }
Paul Bakker380da532012-04-18 16:10:25 +00007465}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007467void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007468{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007469#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7470 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007471 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7472 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007473#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7475#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007476#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007477 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007478 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7479#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007480 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007481#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007482#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007484#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007485 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007486 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007487#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007488 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007489#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007490#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007492}
7493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007494static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007495 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007496{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007497#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7498 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007499 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7500 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007501#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7503#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007504#if defined(MBEDTLS_USE_PSA_CRYPTO)
7505 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7506#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007507 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007508#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007509#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007511#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007512 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007513#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007514 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007515#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007516#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007518}
7519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7521 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7522static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007523 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007524{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007525 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7526 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007527}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007528#endif
Paul Bakker380da532012-04-18 16:10:25 +00007529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7531#if defined(MBEDTLS_SHA256_C)
7532static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007533 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007534{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007535#if defined(MBEDTLS_USE_PSA_CRYPTO)
7536 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7537#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007538 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007539#endif
Paul Bakker380da532012-04-18 16:10:25 +00007540}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007541#endif
Paul Bakker380da532012-04-18 16:10:25 +00007542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007543#if defined(MBEDTLS_SHA512_C)
7544static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007545 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007546{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007547#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007548 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007549#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007550 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007551#endif
Paul Bakker380da532012-04-18 16:10:25 +00007552}
Paul Bakker769075d2012-11-24 11:26:46 +01007553#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007557static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007559{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007560 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007561 mbedtls_md5_context md5;
7562 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007563
Paul Bakker5121ce52009-01-03 21:22:43 +00007564 unsigned char padbuf[48];
7565 unsigned char md5sum[16];
7566 unsigned char sha1sum[20];
7567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007568 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007569 if( !session )
7570 session = ssl->session;
7571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007573
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007574 mbedtls_md5_init( &md5 );
7575 mbedtls_sha1_init( &sha1 );
7576
7577 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7578 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007579
7580 /*
7581 * SSLv3:
7582 * hash =
7583 * MD5( master + pad2 +
7584 * MD5( handshake + sender + master + pad1 ) )
7585 * + SHA1( master + pad2 +
7586 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007587 */
7588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007590 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7591 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007592#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007595 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7596 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007597#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007600 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007601
Paul Bakker1ef83d62012-04-11 12:09:53 +00007602 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007603
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007604 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7605 mbedtls_md5_update_ret( &md5, session->master, 48 );
7606 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7607 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007608
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007609 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7610 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7611 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7612 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007613
Paul Bakker1ef83d62012-04-11 12:09:53 +00007614 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007615
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007616 mbedtls_md5_starts_ret( &md5 );
7617 mbedtls_md5_update_ret( &md5, session->master, 48 );
7618 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7619 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7620 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007621
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007622 mbedtls_sha1_starts_ret( &sha1 );
7623 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7624 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7625 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7626 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007629
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007630 mbedtls_md5_free( &md5 );
7631 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007632
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007633 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7634 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7635 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007638}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007642static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007643 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007644{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007645 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007646 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007647 mbedtls_md5_context md5;
7648 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007649 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007651 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007652 if( !session )
7653 session = ssl->session;
7654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007656
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007657 mbedtls_md5_init( &md5 );
7658 mbedtls_sha1_init( &sha1 );
7659
7660 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7661 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007662
Paul Bakker1ef83d62012-04-11 12:09:53 +00007663 /*
7664 * TLSv1:
7665 * hash = PRF( master, finished_label,
7666 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7667 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007669#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007670 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7671 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007672#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007674#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007675 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7676 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007677#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007679 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007680 ? "client finished"
7681 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007682
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007683 mbedtls_md5_finish_ret( &md5, padbuf );
7684 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007685
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007686 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007687 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007690
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007691 mbedtls_md5_free( &md5 );
7692 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007693
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007694 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007698#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7701#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007702static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007703 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007704{
7705 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007706 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007707 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007708#if defined(MBEDTLS_USE_PSA_CRYPTO)
7709 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007710 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007711 psa_status_t status;
7712#else
7713 mbedtls_sha256_context sha256;
7714#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007717 if( !session )
7718 session = ssl->session;
7719
Andrzej Kurekeb342242019-01-29 09:14:33 -05007720 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7721 ? "client finished"
7722 : "server finished";
7723
7724#if defined(MBEDTLS_USE_PSA_CRYPTO)
7725 sha256_psa = psa_hash_operation_init();
7726
7727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7728
7729 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7730 if( status != PSA_SUCCESS )
7731 {
7732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7733 return;
7734 }
7735
7736 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7737 if( status != PSA_SUCCESS )
7738 {
7739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7740 return;
7741 }
7742 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7743#else
7744
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007745 mbedtls_sha256_init( &sha256 );
7746
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007748
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007749 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007750
7751 /*
7752 * TLSv1.2:
7753 * hash = PRF( master, finished_label,
7754 * Hash( handshake ) )[0.11]
7755 */
7756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757#if !defined(MBEDTLS_SHA256_ALT)
7758 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007759 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007760#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007761
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007762 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007763 mbedtls_sha256_free( &sha256 );
7764#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007765
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007766 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007767 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007770
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007771 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007774}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007775#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007778static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007779 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007780{
7781 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007782 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007783 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007784#if defined(MBEDTLS_USE_PSA_CRYPTO)
7785 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007786 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007787 psa_status_t status;
7788#else
7789 mbedtls_sha512_context sha512;
7790#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007792 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007793 if( !session )
7794 session = ssl->session;
7795
Andrzej Kurekeb342242019-01-29 09:14:33 -05007796 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7797 ? "client finished"
7798 : "server finished";
7799
7800#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007801 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007802
7803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7804
Andrzej Kurek972fba52019-01-30 03:29:12 -05007805 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007806 if( status != PSA_SUCCESS )
7807 {
7808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7809 return;
7810 }
7811
Andrzej Kurek972fba52019-01-30 03:29:12 -05007812 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007813 if( status != PSA_SUCCESS )
7814 {
7815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7816 return;
7817 }
7818 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7819#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007820 mbedtls_sha512_init( &sha512 );
7821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007823
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007824 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007825
7826 /*
7827 * TLSv1.2:
7828 * hash = PRF( master, finished_label,
7829 * Hash( handshake ) )[0.11]
7830 */
7831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007833 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7834 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007835#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007836
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007837 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007838 mbedtls_sha512_free( &sha512 );
7839#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007840
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007841 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007842 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007845
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007846 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007849}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007850#endif /* MBEDTLS_SHA512_C */
7851#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007854{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007856
7857 /*
7858 * Free our handshake params
7859 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007860 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007862 ssl->handshake = NULL;
7863
7864 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007865 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007866 */
7867 if( ssl->transform )
7868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869 mbedtls_ssl_transform_free( ssl->transform );
7870 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007871 }
7872 ssl->transform = ssl->transform_negotiate;
7873 ssl->transform_negotiate = NULL;
7874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007876}
7877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007879{
7880 int resume = ssl->handshake->resume;
7881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884#if defined(MBEDTLS_SSL_RENEGOTIATION)
7885 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007888 ssl->renego_records_seen = 0;
7889 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007890#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007891
7892 /*
7893 * Free the previous session and switch in the current one
7894 */
Paul Bakker0a597072012-09-25 21:55:46 +00007895 if( ssl->session )
7896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007897#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007898 /* RFC 7366 3.1: keep the EtM state */
7899 ssl->session_negotiate->encrypt_then_mac =
7900 ssl->session->encrypt_then_mac;
7901#endif
7902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903 mbedtls_ssl_session_free( ssl->session );
7904 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007905 }
7906 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007907 ssl->session_negotiate = NULL;
7908
Paul Bakker0a597072012-09-25 21:55:46 +00007909 /*
7910 * Add cache entry
7911 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007912 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007913 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007914 resume == 0 )
7915 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007916 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007918 }
Paul Bakker0a597072012-09-25 21:55:46 +00007919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007921 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007922 ssl->handshake->flight != NULL )
7923 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007924 /* Cancel handshake timer */
7925 ssl_set_timer( ssl, 0 );
7926
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007927 /* Keep last flight around in case we need to resend it:
7928 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007930 }
7931 else
7932#endif
7933 ssl_handshake_wrapup_free_hs_transform( ssl );
7934
Paul Bakker48916f92012-09-16 19:57:18 +00007935 ssl->state++;
7936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007938}
7939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007941{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007942 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007945
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007946 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007947
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007948 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007949
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007950 /*
7951 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7952 * may define some other value. Currently (early 2016), no defined
7953 * ciphersuite does this (and this is unlikely to change as activity has
7954 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007959 ssl->verify_data_len = hash_len;
7960 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007961#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007962
Paul Bakker5121ce52009-01-03 21:22:43 +00007963 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7965 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007966
7967 /*
7968 * In case of session resuming, invert the client and server
7969 * ChangeCipherSpec messages order.
7970 */
Paul Bakker0a597072012-09-25 21:55:46 +00007971 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007974 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007976#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007978 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007980#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007981 }
7982 else
7983 ssl->state++;
7984
Paul Bakker48916f92012-09-16 19:57:18 +00007985 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007986 * Switch to our negotiated transform and session parameters for outbound
7987 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007989 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007991#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007992 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007993 {
7994 unsigned char i;
7995
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007996 /* Remember current epoch settings for resending */
7997 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007998 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007999
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008000 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008001 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008002
8003 /* Increment epoch */
8004 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008005 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008006 break;
8007
8008 /* The loop goes to its end iff the counter is wrapping */
8009 if( i == 0 )
8010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8012 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008013 }
8014 }
8015 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01008017 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008018
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008019 ssl->transform_out = ssl->transform_negotiate;
8020 ssl->session_out = ssl->session_negotiate;
8021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8023 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8028 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008029 }
8030 }
8031#endif
8032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008033#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008034 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008035 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008036#endif
8037
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008038 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008039 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008040 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008041 return( ret );
8042 }
8043
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008044#if defined(MBEDTLS_SSL_PROTO_DTLS)
8045 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8046 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8047 {
8048 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8049 return( ret );
8050 }
8051#endif
8052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008054
8055 return( 0 );
8056}
8057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008058#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008059#define SSL_MAX_HASH_LEN 36
8060#else
8061#define SSL_MAX_HASH_LEN 12
8062#endif
8063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008064int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008065{
Paul Bakker23986e52011-04-24 08:57:21 +00008066 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008067 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008068 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008071
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008072 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008073
Hanno Becker327c93b2018-08-15 13:56:18 +01008074 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008076 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008077 return( ret );
8078 }
8079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008083 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8084 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008086 }
8087
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008088 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008089#if defined(MBEDTLS_SSL_PROTO_SSL3)
8090 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008091 hash_len = 36;
8092 else
8093#endif
8094 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008096 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8097 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008100 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8101 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008102 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008103 }
8104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008105 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008106 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008109 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8110 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008111 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008112 }
8113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008114#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008115 ssl->verify_data_len = hash_len;
8116 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008117#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008118
Paul Bakker0a597072012-09-25 21:55:46 +00008119 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008121#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008122 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008124#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008125#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008126 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008127 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008128#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008129 }
8130 else
8131 ssl->state++;
8132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008133#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008134 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008136#endif
8137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008139
8140 return( 0 );
8141}
8142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008143static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008144{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008147#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8148 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8149 mbedtls_md5_init( &handshake->fin_md5 );
8150 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008151 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8152 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008153#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8155#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008156#if defined(MBEDTLS_USE_PSA_CRYPTO)
8157 handshake->fin_sha256_psa = psa_hash_operation_init();
8158 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8159#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008160 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008161 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008162#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008163#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008165#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008166 handshake->fin_sha384_psa = psa_hash_operation_init();
8167 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008168#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008169 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008170 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008171#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008172#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008174
8175 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008176
8177#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8178 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8179 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8180#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008182#if defined(MBEDTLS_DHM_C)
8183 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008184#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185#if defined(MBEDTLS_ECDH_C)
8186 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008187#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008188#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008189 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008190#if defined(MBEDTLS_SSL_CLI_C)
8191 handshake->ecjpake_cache = NULL;
8192 handshake->ecjpake_cache_len = 0;
8193#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008194#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008195
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008196#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008197 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008198#endif
8199
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008200#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8201 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8202#endif
Hanno Becker75173122019-02-06 16:18:31 +00008203
8204#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8205 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8206 mbedtls_pk_init( &handshake->peer_pubkey );
8207#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008208}
8209
Hanno Beckera18d1322018-01-03 14:27:32 +00008210void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008211{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8215 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008216
Hanno Beckerd56ed242018-01-03 15:32:51 +00008217#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008218 mbedtls_md_init( &transform->md_ctx_enc );
8219 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008220#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008221}
8222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008223void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008224{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008225 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008226}
8227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008228static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008229{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008230 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008231 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008232 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008233 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008234 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008235 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008236 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008237
8238 /*
8239 * Either the pointers are now NULL or cleared properly and can be freed.
8240 * Now allocate missing structures.
8241 */
8242 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008243 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008244 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008245 }
Paul Bakker48916f92012-09-16 19:57:18 +00008246
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008247 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008248 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008249 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008250 }
Paul Bakker48916f92012-09-16 19:57:18 +00008251
Paul Bakker82788fb2014-10-20 13:59:19 +02008252 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008253 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008254 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008255 }
Paul Bakker48916f92012-09-16 19:57:18 +00008256
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008257 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008258 if( ssl->handshake == NULL ||
8259 ssl->transform_negotiate == NULL ||
8260 ssl->session_negotiate == NULL )
8261 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008264 mbedtls_free( ssl->handshake );
8265 mbedtls_free( ssl->transform_negotiate );
8266 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008267
8268 ssl->handshake = NULL;
8269 ssl->transform_negotiate = NULL;
8270 ssl->session_negotiate = NULL;
8271
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008272 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008273 }
8274
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008275 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008277 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008278 ssl_handshake_params_init( ssl->handshake );
8279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008281 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8282 {
8283 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008284
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008285 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8286 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8287 else
8288 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008289
8290 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008291 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008292#endif
8293
Paul Bakker48916f92012-09-16 19:57:18 +00008294 return( 0 );
8295}
8296
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008297#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008298/* Dummy cookie callbacks for defaults */
8299static int ssl_cookie_write_dummy( void *ctx,
8300 unsigned char **p, unsigned char *end,
8301 const unsigned char *cli_id, size_t cli_id_len )
8302{
8303 ((void) ctx);
8304 ((void) p);
8305 ((void) end);
8306 ((void) cli_id);
8307 ((void) cli_id_len);
8308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008309 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008310}
8311
8312static int ssl_cookie_check_dummy( void *ctx,
8313 const unsigned char *cookie, size_t cookie_len,
8314 const unsigned char *cli_id, size_t cli_id_len )
8315{
8316 ((void) ctx);
8317 ((void) cookie);
8318 ((void) cookie_len);
8319 ((void) cli_id);
8320 ((void) cli_id_len);
8321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008323}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008324#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008325
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008326/* Once ssl->out_hdr as the address of the beginning of the
8327 * next outgoing record is set, deduce the other pointers.
8328 *
8329 * Note: For TLS, we save the implicit record sequence number
8330 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8331 * and the caller has to make sure there's space for this.
8332 */
8333
8334static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8335 mbedtls_ssl_transform *transform )
8336{
8337#if defined(MBEDTLS_SSL_PROTO_DTLS)
8338 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8339 {
8340 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008341#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008342 ssl->out_cid = ssl->out_ctr + 8;
8343 ssl->out_len = ssl->out_cid;
8344 if( transform != NULL )
8345 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008346#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008347 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008348#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008349 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008350 }
8351 else
8352#endif
8353 {
8354 ssl->out_ctr = ssl->out_hdr - 8;
8355 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008356#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008357 ssl->out_cid = ssl->out_len;
8358#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008359 ssl->out_iv = ssl->out_hdr + 5;
8360 }
8361
8362 /* Adjust out_msg to make space for explicit IV, if used. */
8363 if( transform != NULL &&
8364 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8365 {
8366 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8367 }
8368 else
8369 ssl->out_msg = ssl->out_iv;
8370}
8371
8372/* Once ssl->in_hdr as the address of the beginning of the
8373 * next incoming record is set, deduce the other pointers.
8374 *
8375 * Note: For TLS, we save the implicit record sequence number
8376 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8377 * and the caller has to make sure there's space for this.
8378 */
8379
Hanno Becker79594fd2019-05-08 09:38:41 +01008380static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008381{
Hanno Becker79594fd2019-05-08 09:38:41 +01008382 /* This function sets the pointers to match the case
8383 * of unprotected TLS/DTLS records, with both ssl->in_iv
8384 * and ssl->in_msg pointing to the beginning of the record
8385 * content.
8386 *
8387 * When decrypting a protected record, ssl->in_msg
8388 * will be shifted to point to the beginning of the
8389 * record plaintext.
8390 */
8391
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008392#if defined(MBEDTLS_SSL_PROTO_DTLS)
8393 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8394 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008395 /* This sets the header pointers to match records
8396 * without CID. When we receive a record containing
8397 * a CID, the fields are shifted accordingly in
8398 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008399 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008400#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008401 ssl->in_cid = ssl->in_ctr + 8;
8402 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008403#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008404 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008405#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008406 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008407 }
8408 else
8409#endif
8410 {
8411 ssl->in_ctr = ssl->in_hdr - 8;
8412 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008413#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008414 ssl->in_cid = ssl->in_len;
8415#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008416 ssl->in_iv = ssl->in_hdr + 5;
8417 }
8418
Hanno Becker79594fd2019-05-08 09:38:41 +01008419 /* This will be adjusted at record decryption time. */
8420 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008421}
8422
Paul Bakker5121ce52009-01-03 21:22:43 +00008423/*
8424 * Initialize an SSL context
8425 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008426void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8427{
8428 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8429}
8430
8431/*
8432 * Setup an SSL context
8433 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008434
8435static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8436{
8437 /* Set the incoming and outgoing record pointers. */
8438#if defined(MBEDTLS_SSL_PROTO_DTLS)
8439 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8440 {
8441 ssl->out_hdr = ssl->out_buf;
8442 ssl->in_hdr = ssl->in_buf;
8443 }
8444 else
8445#endif /* MBEDTLS_SSL_PROTO_DTLS */
8446 {
8447 ssl->out_hdr = ssl->out_buf + 8;
8448 ssl->in_hdr = ssl->in_buf + 8;
8449 }
8450
8451 /* Derive other internal pointers. */
8452 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008453 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008454}
8455
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008456int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008457 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008458{
Paul Bakker48916f92012-09-16 19:57:18 +00008459 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008460
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008461 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008462
8463 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008464 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008465 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008466
8467 /* Set to NULL in case of an error condition */
8468 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008469
Angus Grattond8213d02016-05-25 20:56:48 +10008470 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8471 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008472 {
Angus Grattond8213d02016-05-25 20:56:48 +10008473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008474 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008475 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008476 }
8477
8478 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8479 if( ssl->out_buf == NULL )
8480 {
8481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008482 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008483 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008484 }
8485
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008486 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008487
Paul Bakker48916f92012-09-16 19:57:18 +00008488 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008489 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008490
8491 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008492
8493error:
8494 mbedtls_free( ssl->in_buf );
8495 mbedtls_free( ssl->out_buf );
8496
8497 ssl->conf = NULL;
8498
8499 ssl->in_buf = NULL;
8500 ssl->out_buf = NULL;
8501
8502 ssl->in_hdr = NULL;
8503 ssl->in_ctr = NULL;
8504 ssl->in_len = NULL;
8505 ssl->in_iv = NULL;
8506 ssl->in_msg = NULL;
8507
8508 ssl->out_hdr = NULL;
8509 ssl->out_ctr = NULL;
8510 ssl->out_len = NULL;
8511 ssl->out_iv = NULL;
8512 ssl->out_msg = NULL;
8513
k-stachowiak9f7798e2018-07-31 16:52:32 +02008514 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008515}
8516
8517/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008518 * Reset an initialized and used SSL context for re-use while retaining
8519 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008520 *
8521 * If partial is non-zero, keep data in the input buffer and client ID.
8522 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008523 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008524static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008525{
Paul Bakker48916f92012-09-16 19:57:18 +00008526 int ret;
8527
Hanno Becker7e772132018-08-10 12:38:21 +01008528#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8529 !defined(MBEDTLS_SSL_SRV_C)
8530 ((void) partial);
8531#endif
8532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008533 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008534
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008535 /* Cancel any possibly running timer */
8536 ssl_set_timer( ssl, 0 );
8537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008538#if defined(MBEDTLS_SSL_RENEGOTIATION)
8539 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008540 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008541
8542 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008543 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8544 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008545#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008546 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008547
Paul Bakker7eb013f2011-10-06 12:37:39 +00008548 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008549 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008550
8551 ssl->in_msgtype = 0;
8552 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008553#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008554 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008555 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008556#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008557#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008558 ssl_dtls_replay_reset( ssl );
8559#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008560
8561 ssl->in_hslen = 0;
8562 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008563
8564 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008565
8566 ssl->out_msgtype = 0;
8567 ssl->out_msglen = 0;
8568 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008569#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8570 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008571 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008572#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008573
Hanno Becker19859472018-08-06 09:40:20 +01008574 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8575
Paul Bakker48916f92012-09-16 19:57:18 +00008576 ssl->transform_in = NULL;
8577 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008578
Hanno Becker78640902018-08-13 16:35:15 +01008579 ssl->session_in = NULL;
8580 ssl->session_out = NULL;
8581
Angus Grattond8213d02016-05-25 20:56:48 +10008582 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008583
8584#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008585 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008586#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8587 {
8588 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008589 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008590 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008592#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8593 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8596 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008598 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8599 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008600 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008601 }
8602#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008603
Paul Bakker48916f92012-09-16 19:57:18 +00008604 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008606 mbedtls_ssl_transform_free( ssl->transform );
8607 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008608 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008609 }
Paul Bakker48916f92012-09-16 19:57:18 +00008610
Paul Bakkerc0463502013-02-14 11:19:38 +01008611 if( ssl->session )
8612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613 mbedtls_ssl_session_free( ssl->session );
8614 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008615 ssl->session = NULL;
8616 }
8617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008618#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008619 ssl->alpn_chosen = NULL;
8620#endif
8621
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008622#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008623#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008624 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008625#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008626 {
8627 mbedtls_free( ssl->cli_id );
8628 ssl->cli_id = NULL;
8629 ssl->cli_id_len = 0;
8630 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008631#endif
8632
Paul Bakker48916f92012-09-16 19:57:18 +00008633 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8634 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008635
8636 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008637}
8638
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008639/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008640 * Reset an initialized and used SSL context for re-use while retaining
8641 * all application-set variables, function pointers and data.
8642 */
8643int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8644{
8645 return( ssl_session_reset_int( ssl, 0 ) );
8646}
8647
8648/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008649 * SSL set accessors
8650 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008651void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008652{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008653 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008654}
8655
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008656void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008657{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008658 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008659}
8660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008661#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008662void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008663{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008664 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008665}
8666#endif
8667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008668#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008669void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008670{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008671 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008672}
8673#endif
8674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008675#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008676
Hanno Becker1841b0a2018-08-24 11:13:57 +01008677void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8678 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008679{
8680 ssl->disable_datagram_packing = !allow_packing;
8681}
8682
8683void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8684 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008685{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008686 conf->hs_timeout_min = min;
8687 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008688}
8689#endif
8690
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008691void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008692{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008693 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008694}
8695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008696#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008697void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008698 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008699 void *p_vrfy )
8700{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008701 conf->f_vrfy = f_vrfy;
8702 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008703}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008704#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008705
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008706void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008707 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008708 void *p_rng )
8709{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008710 conf->f_rng = f_rng;
8711 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008712}
8713
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008714void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008715 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008716 void *p_dbg )
8717{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008718 conf->f_dbg = f_dbg;
8719 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008720}
8721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008722void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008723 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008724 mbedtls_ssl_send_t *f_send,
8725 mbedtls_ssl_recv_t *f_recv,
8726 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008727{
8728 ssl->p_bio = p_bio;
8729 ssl->f_send = f_send;
8730 ssl->f_recv = f_recv;
8731 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008732}
8733
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008734#if defined(MBEDTLS_SSL_PROTO_DTLS)
8735void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8736{
8737 ssl->mtu = mtu;
8738}
8739#endif
8740
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008741void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008742{
8743 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008744}
8745
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008746void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8747 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008748 mbedtls_ssl_set_timer_t *f_set_timer,
8749 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008750{
8751 ssl->p_timer = p_timer;
8752 ssl->f_set_timer = f_set_timer;
8753 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008754
8755 /* Make sure we start with no timer running */
8756 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008757}
8758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008759#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008760void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008761 void *p_cache,
8762 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8763 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008764{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008765 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008766 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008767 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008768}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008771#if defined(MBEDTLS_SSL_CLI_C)
8772int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008773{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008774 int ret;
8775
8776 if( ssl == NULL ||
8777 session == NULL ||
8778 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008779 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008782 }
8783
Hanno Becker52055ae2019-02-06 14:30:46 +00008784 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8785 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008786 return( ret );
8787
Paul Bakker0a597072012-09-25 21:55:46 +00008788 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008789
8790 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008791}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008792#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008793
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008794void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008795 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008796{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008797 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8798 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8799 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8800 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008801}
8802
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008803void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008804 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008805 int major, int minor )
8806{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008807 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008808 return;
8809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008810 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008811 return;
8812
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008813 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008814}
8815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008816#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008817void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008818 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008819{
8820 conf->cert_profile = profile;
8821}
8822
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008823/* Append a new keycert entry to a (possibly empty) list */
8824static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8825 mbedtls_x509_crt *cert,
8826 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008827{
niisato8ee24222018-06-25 19:05:48 +09008828 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008829
niisato8ee24222018-06-25 19:05:48 +09008830 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8831 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008832 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008833
niisato8ee24222018-06-25 19:05:48 +09008834 new_cert->cert = cert;
8835 new_cert->key = key;
8836 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008837
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008838 /* Update head is the list was null, else add to the end */
8839 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008840 {
niisato8ee24222018-06-25 19:05:48 +09008841 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008842 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008843 else
8844 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008845 mbedtls_ssl_key_cert *cur = *head;
8846 while( cur->next != NULL )
8847 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008848 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008849 }
8850
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008851 return( 0 );
8852}
8853
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008854int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008855 mbedtls_x509_crt *own_cert,
8856 mbedtls_pk_context *pk_key )
8857{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008858 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008859}
8860
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008861void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008862 mbedtls_x509_crt *ca_chain,
8863 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008864{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008865 conf->ca_chain = ca_chain;
8866 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008867
8868#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8869 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8870 * cannot be used together. */
8871 conf->f_ca_cb = NULL;
8872 conf->p_ca_cb = NULL;
8873#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008874}
Hanno Becker5adaad92019-03-27 16:54:37 +00008875
8876#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8877void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008878 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008879 void *p_ca_cb )
8880{
8881 conf->f_ca_cb = f_ca_cb;
8882 conf->p_ca_cb = p_ca_cb;
8883
8884 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8885 * cannot be used together. */
8886 conf->ca_chain = NULL;
8887 conf->ca_crl = NULL;
8888}
8889#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008890#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008891
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008892#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8893int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8894 mbedtls_x509_crt *own_cert,
8895 mbedtls_pk_context *pk_key )
8896{
8897 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8898 own_cert, pk_key ) );
8899}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008900
8901void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8902 mbedtls_x509_crt *ca_chain,
8903 mbedtls_x509_crl *ca_crl )
8904{
8905 ssl->handshake->sni_ca_chain = ca_chain;
8906 ssl->handshake->sni_ca_crl = ca_crl;
8907}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008908
8909void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8910 int authmode )
8911{
8912 ssl->handshake->sni_authmode = authmode;
8913}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008914#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8915
Hanno Becker8927c832019-04-03 12:52:50 +01008916#if defined(MBEDTLS_X509_CRT_PARSE_C)
8917void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8918 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8919 void *p_vrfy )
8920{
8921 ssl->f_vrfy = f_vrfy;
8922 ssl->p_vrfy = p_vrfy;
8923}
8924#endif
8925
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008926#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008927/*
8928 * Set EC J-PAKE password for current handshake
8929 */
8930int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8931 const unsigned char *pw,
8932 size_t pw_len )
8933{
8934 mbedtls_ecjpake_role role;
8935
Janos Follath8eb64132016-06-03 15:40:57 +01008936 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008937 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8938
8939 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8940 role = MBEDTLS_ECJPAKE_SERVER;
8941 else
8942 role = MBEDTLS_ECJPAKE_CLIENT;
8943
8944 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8945 role,
8946 MBEDTLS_MD_SHA256,
8947 MBEDTLS_ECP_DP_SECP256R1,
8948 pw, pw_len ) );
8949}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008950#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008952#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008953
8954static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8955{
8956 /* Remove reference to existing PSK, if any. */
8957#if defined(MBEDTLS_USE_PSA_CRYPTO)
8958 if( conf->psk_opaque != 0 )
8959 {
8960 /* The maintenance of the PSK key slot is the
8961 * user's responsibility. */
8962 conf->psk_opaque = 0;
8963 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008964 /* This and the following branch should never
8965 * be taken simultaenously as we maintain the
8966 * invariant that raw and opaque PSKs are never
8967 * configured simultaneously. As a safeguard,
8968 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008969#endif /* MBEDTLS_USE_PSA_CRYPTO */
8970 if( conf->psk != NULL )
8971 {
8972 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8973
8974 mbedtls_free( conf->psk );
8975 conf->psk = NULL;
8976 conf->psk_len = 0;
8977 }
8978
8979 /* Remove reference to PSK identity, if any. */
8980 if( conf->psk_identity != NULL )
8981 {
8982 mbedtls_free( conf->psk_identity );
8983 conf->psk_identity = NULL;
8984 conf->psk_identity_len = 0;
8985 }
8986}
8987
Hanno Becker7390c712018-11-15 13:33:04 +00008988/* This function assumes that PSK identity in the SSL config is unset.
8989 * It checks that the provided identity is well-formed and attempts
8990 * to make a copy of it in the SSL config.
8991 * On failure, the PSK identity in the config remains unset. */
8992static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8993 unsigned char const *psk_identity,
8994 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008995{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008996 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008997 if( psk_identity == NULL ||
8998 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008999 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009000 {
9001 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9002 }
9003
Hanno Becker7390c712018-11-15 13:33:04 +00009004 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
9005 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009006 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02009007
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009008 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009009 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02009010
9011 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02009012}
9013
Hanno Becker7390c712018-11-15 13:33:04 +00009014int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
9015 const unsigned char *psk, size_t psk_len,
9016 const unsigned char *psk_identity, size_t psk_identity_len )
9017{
9018 int ret;
9019 /* Remove opaque/raw PSK + PSK Identity */
9020 ssl_conf_remove_psk( conf );
9021
9022 /* Check and set raw PSK */
9023 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
9024 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9025 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
9026 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9027 conf->psk_len = psk_len;
9028 memcpy( conf->psk, psk, conf->psk_len );
9029
9030 /* Check and set PSK Identity */
9031 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
9032 if( ret != 0 )
9033 ssl_conf_remove_psk( conf );
9034
9035 return( ret );
9036}
9037
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009038static void ssl_remove_psk( mbedtls_ssl_context *ssl )
9039{
9040#if defined(MBEDTLS_USE_PSA_CRYPTO)
9041 if( ssl->handshake->psk_opaque != 0 )
9042 {
9043 ssl->handshake->psk_opaque = 0;
9044 }
9045 else
9046#endif /* MBEDTLS_USE_PSA_CRYPTO */
9047 if( ssl->handshake->psk != NULL )
9048 {
9049 mbedtls_platform_zeroize( ssl->handshake->psk,
9050 ssl->handshake->psk_len );
9051 mbedtls_free( ssl->handshake->psk );
9052 ssl->handshake->psk_len = 0;
9053 }
9054}
9055
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009056int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9057 const unsigned char *psk, size_t psk_len )
9058{
9059 if( psk == NULL || ssl->handshake == NULL )
9060 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9061
9062 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9064
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009065 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009066
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009067 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009068 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009069
9070 ssl->handshake->psk_len = psk_len;
9071 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
9072
9073 return( 0 );
9074}
9075
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009076#if defined(MBEDTLS_USE_PSA_CRYPTO)
9077int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009078 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009079 const unsigned char *psk_identity,
9080 size_t psk_identity_len )
9081{
Hanno Becker7390c712018-11-15 13:33:04 +00009082 int ret;
9083 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009084 ssl_conf_remove_psk( conf );
9085
Hanno Becker7390c712018-11-15 13:33:04 +00009086 /* Check and set opaque PSK */
9087 if( psk_slot == 0 )
9088 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009089 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009090
9091 /* Check and set PSK Identity */
9092 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9093 psk_identity_len );
9094 if( ret != 0 )
9095 ssl_conf_remove_psk( conf );
9096
9097 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009098}
9099
9100int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009101 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009102{
9103 if( psk_slot == 0 || ssl->handshake == NULL )
9104 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9105
9106 ssl_remove_psk( ssl );
9107 ssl->handshake->psk_opaque = psk_slot;
9108 return( 0 );
9109}
9110#endif /* MBEDTLS_USE_PSA_CRYPTO */
9111
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009112void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009113 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009114 size_t),
9115 void *p_psk )
9116{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009117 conf->f_psk = f_psk;
9118 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009119}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009120#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009121
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009122#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009123
9124#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009125int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009126{
9127 int ret;
9128
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009129 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9130 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9131 {
9132 mbedtls_mpi_free( &conf->dhm_P );
9133 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009134 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009135 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009136
9137 return( 0 );
9138}
Hanno Becker470a8c42017-10-04 15:28:46 +01009139#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009140
Hanno Beckera90658f2017-10-04 15:29:08 +01009141int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9142 const unsigned char *dhm_P, size_t P_len,
9143 const unsigned char *dhm_G, size_t G_len )
9144{
9145 int ret;
9146
9147 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9148 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9149 {
9150 mbedtls_mpi_free( &conf->dhm_P );
9151 mbedtls_mpi_free( &conf->dhm_G );
9152 return( ret );
9153 }
9154
9155 return( 0 );
9156}
Paul Bakker5121ce52009-01-03 21:22:43 +00009157
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009158int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009159{
9160 int ret;
9161
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009162 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9163 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9164 {
9165 mbedtls_mpi_free( &conf->dhm_P );
9166 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009167 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009168 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009169
9170 return( 0 );
9171}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009172#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009173
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009174#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9175/*
9176 * Set the minimum length for Diffie-Hellman parameters
9177 */
9178void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9179 unsigned int bitlen )
9180{
9181 conf->dhm_min_bitlen = bitlen;
9182}
9183#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9184
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009185#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009186/*
9187 * Set allowed/preferred hashes for handshake signatures
9188 */
9189void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9190 const int *hashes )
9191{
9192 conf->sig_hashes = hashes;
9193}
Hanno Becker947194e2017-04-07 13:25:49 +01009194#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009195
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009196#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009197/*
9198 * Set the allowed elliptic curves
9199 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009200void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009201 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009202{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009203 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009204}
Hanno Becker947194e2017-04-07 13:25:49 +01009205#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009206
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009207#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009208int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009209{
Hanno Becker947194e2017-04-07 13:25:49 +01009210 /* Initialize to suppress unnecessary compiler warning */
9211 size_t hostname_len = 0;
9212
9213 /* Check if new hostname is valid before
9214 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009215 if( hostname != NULL )
9216 {
9217 hostname_len = strlen( hostname );
9218
9219 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9220 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9221 }
9222
9223 /* Now it's clear that we will overwrite the old hostname,
9224 * so we can free it safely */
9225
9226 if( ssl->hostname != NULL )
9227 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009228 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009229 mbedtls_free( ssl->hostname );
9230 }
9231
9232 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009233
Paul Bakker5121ce52009-01-03 21:22:43 +00009234 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009235 {
9236 ssl->hostname = NULL;
9237 }
9238 else
9239 {
9240 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009241 if( ssl->hostname == NULL )
9242 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009243
Hanno Becker947194e2017-04-07 13:25:49 +01009244 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009245
Hanno Becker947194e2017-04-07 13:25:49 +01009246 ssl->hostname[hostname_len] = '\0';
9247 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009248
9249 return( 0 );
9250}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009251#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009252
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009253#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009254void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009255 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009256 const unsigned char *, size_t),
9257 void *p_sni )
9258{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009259 conf->f_sni = f_sni;
9260 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009261}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009262#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009264#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009265int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009266{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009267 size_t cur_len, tot_len;
9268 const char **p;
9269
9270 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009271 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9272 * MUST NOT be truncated."
9273 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009274 */
9275 tot_len = 0;
9276 for( p = protos; *p != NULL; p++ )
9277 {
9278 cur_len = strlen( *p );
9279 tot_len += cur_len;
9280
9281 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009283 }
9284
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009285 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009286
9287 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009288}
9289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009290const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009291{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009292 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009293}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009294#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009295
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009296void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009297{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009298 conf->max_major_ver = major;
9299 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009300}
9301
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009302void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009303{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009304 conf->min_major_ver = major;
9305 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009306}
9307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009309void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009310{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009311 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009312}
9313#endif
9314
Janos Follath088ce432017-04-10 12:42:31 +01009315#if defined(MBEDTLS_SSL_SRV_C)
9316void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9317 char cert_req_ca_list )
9318{
9319 conf->cert_req_ca_list = cert_req_ca_list;
9320}
9321#endif
9322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009324void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009325{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009326 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009327}
9328#endif
9329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009331void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009332{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009333 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009334}
9335#endif
9336
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009337#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009338void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009339{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009340 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009341}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009342#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009345int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009346{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009347 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009348 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009351 }
9352
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009353 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009354
9355 return( 0 );
9356}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009360void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009361{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009362 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009363}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009364#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009366#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009367void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009368{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009369 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009370}
9371#endif
9372
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009373void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009374{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009375 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009376}
9377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009378#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009379void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009380{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009381 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009382}
9383
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009384void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009385{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009386 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009387}
9388
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009389void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009390 const unsigned char period[8] )
9391{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009392 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009393}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009394#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009396#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009397#if defined(MBEDTLS_SSL_CLI_C)
9398void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009399{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009400 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009401}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009402#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009403
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009404#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009405void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9406 mbedtls_ssl_ticket_write_t *f_ticket_write,
9407 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9408 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009409{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009410 conf->f_ticket_write = f_ticket_write;
9411 conf->f_ticket_parse = f_ticket_parse;
9412 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009413}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009414#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009415#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009416
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009417#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9418void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9419 mbedtls_ssl_export_keys_t *f_export_keys,
9420 void *p_export_keys )
9421{
9422 conf->f_export_keys = f_export_keys;
9423 conf->p_export_keys = p_export_keys;
9424}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009425
9426void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9427 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9428 void *p_export_keys )
9429{
9430 conf->f_export_keys_ext = f_export_keys_ext;
9431 conf->p_export_keys = p_export_keys;
9432}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009433#endif
9434
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009435#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009436void mbedtls_ssl_conf_async_private_cb(
9437 mbedtls_ssl_config *conf,
9438 mbedtls_ssl_async_sign_t *f_async_sign,
9439 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9440 mbedtls_ssl_async_resume_t *f_async_resume,
9441 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009442 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009443{
9444 conf->f_async_sign_start = f_async_sign;
9445 conf->f_async_decrypt_start = f_async_decrypt;
9446 conf->f_async_resume = f_async_resume;
9447 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009448 conf->p_async_config_data = async_config_data;
9449}
9450
Gilles Peskine8f97af72018-04-26 11:46:10 +02009451void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9452{
9453 return( conf->p_async_config_data );
9454}
9455
Gilles Peskine1febfef2018-04-30 11:54:39 +02009456void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009457{
9458 if( ssl->handshake == NULL )
9459 return( NULL );
9460 else
9461 return( ssl->handshake->user_async_ctx );
9462}
9463
Gilles Peskine1febfef2018-04-30 11:54:39 +02009464void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009465 void *ctx )
9466{
9467 if( ssl->handshake != NULL )
9468 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009469}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009470#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009471
Paul Bakker5121ce52009-01-03 21:22:43 +00009472/*
9473 * SSL get accessors
9474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009475size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009476{
9477 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9478}
9479
Hanno Becker8b170a02017-10-10 11:51:19 +01009480int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9481{
9482 /*
9483 * Case A: We're currently holding back
9484 * a message for further processing.
9485 */
9486
9487 if( ssl->keep_current_message == 1 )
9488 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009489 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009490 return( 1 );
9491 }
9492
9493 /*
9494 * Case B: Further records are pending in the current datagram.
9495 */
9496
9497#if defined(MBEDTLS_SSL_PROTO_DTLS)
9498 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9499 ssl->in_left > ssl->next_record_offset )
9500 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009501 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009502 return( 1 );
9503 }
9504#endif /* MBEDTLS_SSL_PROTO_DTLS */
9505
9506 /*
9507 * Case C: A handshake message is being processed.
9508 */
9509
Hanno Becker8b170a02017-10-10 11:51:19 +01009510 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9511 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009512 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009513 return( 1 );
9514 }
9515
9516 /*
9517 * Case D: An application data message is being processed
9518 */
9519 if( ssl->in_offt != NULL )
9520 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009521 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009522 return( 1 );
9523 }
9524
9525 /*
9526 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009527 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009528 * we implement support for multiple alerts in single records.
9529 */
9530
9531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9532 return( 0 );
9533}
9534
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009535uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009536{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009537 if( ssl->session != NULL )
9538 return( ssl->session->verify_result );
9539
9540 if( ssl->session_negotiate != NULL )
9541 return( ssl->session_negotiate->verify_result );
9542
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009543 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009544}
9545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009547{
Paul Bakker926c8e42013-03-06 10:23:34 +01009548 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009549 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009552}
9553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009554const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009555{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009557 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009558 {
9559 switch( ssl->minor_ver )
9560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009562 return( "DTLSv1.0" );
9563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009564 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009565 return( "DTLSv1.2" );
9566
9567 default:
9568 return( "unknown (DTLS)" );
9569 }
9570 }
9571#endif
9572
Paul Bakker43ca69c2011-01-15 17:35:19 +00009573 switch( ssl->minor_ver )
9574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009575 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009576 return( "SSLv3.0" );
9577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009578 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009579 return( "TLSv1.0" );
9580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009581 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009582 return( "TLSv1.1" );
9583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009584 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009585 return( "TLSv1.2" );
9586
Paul Bakker43ca69c2011-01-15 17:35:19 +00009587 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009588 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009589 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009590}
9591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009592int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009593{
Hanno Becker3136ede2018-08-17 15:28:19 +01009594 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009595 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009596 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009597
Hanno Becker5903de42019-05-03 14:46:38 +01009598 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9599
Hanno Becker78640902018-08-13 16:35:15 +01009600 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009601 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603#if defined(MBEDTLS_ZLIB_SUPPORT)
9604 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9605 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009606#endif
9607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009610 case MBEDTLS_MODE_GCM:
9611 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009612 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009613 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009614 transform_expansion = transform->minlen;
9615 break;
9616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009617 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009618
9619 block_size = mbedtls_cipher_get_block_size(
9620 &transform->cipher_ctx_enc );
9621
Hanno Becker3136ede2018-08-17 15:28:19 +01009622 /* Expansion due to the addition of the MAC. */
9623 transform_expansion += transform->maclen;
9624
9625 /* Expansion due to the addition of CBC padding;
9626 * Theoretically up to 256 bytes, but we never use
9627 * more than the block size of the underlying cipher. */
9628 transform_expansion += block_size;
9629
9630 /* For TLS 1.1 or higher, an explicit IV is added
9631 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009632#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9633 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009634 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009635#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009636
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009637 break;
9638
9639 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009641 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009642 }
9643
Hanno Beckera0e20d02019-05-15 14:03:01 +01009644#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009645 if( transform->out_cid_len != 0 )
9646 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009647#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009648
Hanno Becker5903de42019-05-03 14:46:38 +01009649 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009650}
9651
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009652#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9653size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9654{
9655 size_t max_len;
9656
9657 /*
9658 * Assume mfl_code is correct since it was checked when set
9659 */
Angus Grattond8213d02016-05-25 20:56:48 +10009660 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009661
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009662 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009663 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009664 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009665 {
Angus Grattond8213d02016-05-25 20:56:48 +10009666 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009667 }
9668
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009669 /* During a handshake, use the value being negotiated */
9670 if( ssl->session_negotiate != NULL &&
9671 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9672 {
9673 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9674 }
9675
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009676 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009677}
9678#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9679
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009680#if defined(MBEDTLS_SSL_PROTO_DTLS)
9681static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9682{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009683 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9684 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9685 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9686 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9687 return ( 0 );
9688
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009689 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9690 return( ssl->mtu );
9691
9692 if( ssl->mtu == 0 )
9693 return( ssl->handshake->mtu );
9694
9695 return( ssl->mtu < ssl->handshake->mtu ?
9696 ssl->mtu : ssl->handshake->mtu );
9697}
9698#endif /* MBEDTLS_SSL_PROTO_DTLS */
9699
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009700int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9701{
9702 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9703
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009704#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9705 !defined(MBEDTLS_SSL_PROTO_DTLS)
9706 (void) ssl;
9707#endif
9708
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009709#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9710 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9711
9712 if( max_len > mfl )
9713 max_len = mfl;
9714#endif
9715
9716#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009717 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009718 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009719 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009720 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9721 const size_t overhead = (size_t) ret;
9722
9723 if( ret < 0 )
9724 return( ret );
9725
9726 if( mtu <= overhead )
9727 {
9728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9729 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9730 }
9731
9732 if( max_len > mtu - overhead )
9733 max_len = mtu - overhead;
9734 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009735#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009736
Hanno Becker0defedb2018-08-10 12:35:02 +01009737#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9738 !defined(MBEDTLS_SSL_PROTO_DTLS)
9739 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009740#endif
9741
9742 return( (int) max_len );
9743}
9744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745#if defined(MBEDTLS_X509_CRT_PARSE_C)
9746const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009747{
9748 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009749 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009750
Hanno Beckere6824572019-02-07 13:18:46 +00009751#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009752 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009753#else
9754 return( NULL );
9755#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009756}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009757#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009759#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009760int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9761 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009762{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009763 if( ssl == NULL ||
9764 dst == NULL ||
9765 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009766 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009768 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009769 }
9770
Hanno Becker52055ae2019-02-06 14:30:46 +00009771 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009772}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009774
Paul Bakker5121ce52009-01-03 21:22:43 +00009775/*
Paul Bakker1961b702013-01-25 14:49:24 +01009776 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009777 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009778int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009779{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009780 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009781
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009782 if( ssl == NULL || ssl->conf == NULL )
9783 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009785#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009786 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009787 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009788#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009789#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009790 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009791 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009792#endif
9793
Paul Bakker1961b702013-01-25 14:49:24 +01009794 return( ret );
9795}
9796
9797/*
9798 * Perform the SSL handshake
9799 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009800int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009801{
9802 int ret = 0;
9803
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009804 if( ssl == NULL || ssl->conf == NULL )
9805 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009809 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009811 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009812
9813 if( ret != 0 )
9814 break;
9815 }
9816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009818
9819 return( ret );
9820}
9821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822#if defined(MBEDTLS_SSL_RENEGOTIATION)
9823#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009824/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009825 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009828{
9829 int ret;
9830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009832
9833 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009834 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9835 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009836
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009837 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009838 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009839 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009840 return( ret );
9841 }
9842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009844
9845 return( 0 );
9846}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009848
9849/*
9850 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009851 * - any side: calling mbedtls_ssl_renegotiate(),
9852 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9853 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009854 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009855 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009856 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009857 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009858static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009859{
9860 int ret;
9861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009863
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009864 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9865 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009866
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009867 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9868 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009869#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009870 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009871 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009872 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009873 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009874 ssl->handshake->out_msg_seq = 1;
9875 else
9876 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009877 }
9878#endif
9879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009880 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9881 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009886 return( ret );
9887 }
9888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009890
9891 return( 0 );
9892}
9893
9894/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009895 * Renegotiate current connection on client,
9896 * or request renegotiation on server
9897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009898int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009899{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009901
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009902 if( ssl == NULL || ssl->conf == NULL )
9903 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009905#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009906 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009907 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009909 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9910 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009912 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009913
9914 /* Did we already try/start sending HelloRequest? */
9915 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009916 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009917
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009918 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009919 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009920#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009922#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009923 /*
9924 * On client, either start the renegotiation process or,
9925 * if already in progress, continue the handshake
9926 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009927 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009929 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9930 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009931
9932 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009934 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009935 return( ret );
9936 }
9937 }
9938 else
9939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009940 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009942 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009943 return( ret );
9944 }
9945 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009946#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009947
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009948 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009949}
9950
9951/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009952 * Check record counters and renegotiate if they're above the limit.
9953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009954static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009955{
Andres AG2196c7f2016-12-15 17:01:16 +00009956 size_t ep_len = ssl_ep_len( ssl );
9957 int in_ctr_cmp;
9958 int out_ctr_cmp;
9959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009960 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9961 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009962 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009963 {
9964 return( 0 );
9965 }
9966
Andres AG2196c7f2016-12-15 17:01:16 +00009967 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9968 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009969 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009970 ssl->conf->renego_period + ep_len, 8 - ep_len );
9971
9972 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009973 {
9974 return( 0 );
9975 }
9976
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009977 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009978 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009979}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009980#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009981
9982/*
9983 * Receive application data decrypted from the SSL layer
9984 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009985int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009986{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009987 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009988 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009989
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009990 if( ssl == NULL || ssl->conf == NULL )
9991 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009995#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009996 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009998 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009999 return( ret );
10000
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010001 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010002 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010003 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010004 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010005 return( ret );
10006 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010007 }
10008#endif
10009
Hanno Becker4a810fb2017-05-24 16:27:30 +010010010 /*
10011 * Check if renegotiation is necessary and/or handshake is
10012 * in process. If yes, perform/continue, and fall through
10013 * if an unexpected packet is received while the client
10014 * is waiting for the ServerHello.
10015 *
10016 * (There is no equivalent to the last condition on
10017 * the server-side as it is not treated as within
10018 * a handshake while waiting for the ClientHello
10019 * after a renegotiation request.)
10020 */
10021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010022#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010023 ret = ssl_check_ctr_renegotiate( ssl );
10024 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10025 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010027 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010028 return( ret );
10029 }
10030#endif
10031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010032 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010034 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010035 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10036 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010038 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010039 return( ret );
10040 }
10041 }
10042
Hanno Beckere41158b2017-10-23 13:30:32 +010010043 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010044 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010045 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010046 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010047 if( ssl->f_get_timer != NULL &&
10048 ssl->f_get_timer( ssl->p_timer ) == -1 )
10049 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010050 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010051 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010052
Hanno Becker327c93b2018-08-15 13:56:18 +010010053 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010054 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010055 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10056 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010057
Hanno Becker4a810fb2017-05-24 16:27:30 +010010058 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10059 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010060 }
10061
10062 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010063 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010064 {
10065 /*
10066 * OpenSSL sends empty messages to randomize the IV
10067 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010068 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010070 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010071 return( 0 );
10072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010073 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010074 return( ret );
10075 }
10076 }
10077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010078 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010081
Hanno Becker4a810fb2017-05-24 16:27:30 +010010082 /*
10083 * - For client-side, expect SERVER_HELLO_REQUEST.
10084 * - For server-side, expect CLIENT_HELLO.
10085 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10086 */
10087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010088#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010089 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010090 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010091 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010094
10095 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010096#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010097 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010098 {
10099 continue;
10100 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010101#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010102 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010103 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010104#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010105
Hanno Becker4a810fb2017-05-24 16:27:30 +010010106#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010107 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010108 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010111
10112 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010113#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010114 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010115 {
10116 continue;
10117 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010118#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010119 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010120 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010121#endif /* MBEDTLS_SSL_SRV_C */
10122
Hanno Becker21df7f92017-10-17 11:03:26 +010010123#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010124 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010125 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10126 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10127 ssl->conf->allow_legacy_renegotiation ==
10128 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10129 {
10130 /*
10131 * Accept renegotiation request
10132 */
Paul Bakker48916f92012-09-16 19:57:18 +000010133
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010134 /* DTLS clients need to know renego is server-initiated */
10135#if defined(MBEDTLS_SSL_PROTO_DTLS)
10136 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10137 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10138 {
10139 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10140 }
10141#endif
10142 ret = ssl_start_renegotiation( ssl );
10143 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10144 ret != 0 )
10145 {
10146 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10147 return( ret );
10148 }
10149 }
10150 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010151#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010152 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010153 /*
10154 * Refuse renegotiation
10155 */
10156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010159#if defined(MBEDTLS_SSL_PROTO_SSL3)
10160 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010161 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010162 /* SSLv3 does not have a "no_renegotiation" warning, so
10163 we send a fatal alert and abort the connection. */
10164 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10165 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10166 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010167 }
10168 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010169#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10170#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10171 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10172 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010174 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10175 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10176 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010177 {
10178 return( ret );
10179 }
Paul Bakker48916f92012-09-16 19:57:18 +000010180 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010181 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010182#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10183 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10186 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010187 }
Paul Bakker48916f92012-09-16 19:57:18 +000010188 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010189
Hanno Becker90333da2017-10-10 11:27:13 +010010190 /* At this point, we don't know whether the renegotiation has been
10191 * completed or not. The cases to consider are the following:
10192 * 1) The renegotiation is complete. In this case, no new record
10193 * has been read yet.
10194 * 2) The renegotiation is incomplete because the client received
10195 * an application data record while awaiting the ServerHello.
10196 * 3) The renegotiation is incomplete because the client received
10197 * a non-handshake, non-application data message while awaiting
10198 * the ServerHello.
10199 * In each of these case, looping will be the proper action:
10200 * - For 1), the next iteration will read a new record and check
10201 * if it's application data.
10202 * - For 2), the loop condition isn't satisfied as application data
10203 * is present, hence continue is the same as break
10204 * - For 3), the loop condition is satisfied and read_record
10205 * will re-deliver the message that was held back by the client
10206 * when expecting the ServerHello.
10207 */
10208 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010209 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010210#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010211 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010212 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010213 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010214 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010215 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010218 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010219 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010220 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010221 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010222 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010223#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010225 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10226 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010229 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010230 }
10231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010232 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10235 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010236 }
10237
10238 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010239
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010240 /* We're going to return something now, cancel timer,
10241 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010242 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010243 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010244
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010245#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010246 /* If we requested renego but received AppData, resend HelloRequest.
10247 * Do it now, after setting in_offt, to avoid taking this branch
10248 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010249#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010250 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010251 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010252 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010253 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010255 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010256 return( ret );
10257 }
10258 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010259#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010260#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010261 }
10262
10263 n = ( len < ssl->in_msglen )
10264 ? len : ssl->in_msglen;
10265
10266 memcpy( buf, ssl->in_offt, n );
10267 ssl->in_msglen -= n;
10268
10269 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010270 {
10271 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010272 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010273 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010274 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010275 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010276 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010277 /* more data available */
10278 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010279 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010282
Paul Bakker23986e52011-04-24 08:57:21 +000010283 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010284}
10285
10286/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010287 * Send application data to be encrypted by the SSL layer, taking care of max
10288 * fragment length and buffer size.
10289 *
10290 * According to RFC 5246 Section 6.2.1:
10291 *
10292 * Zero-length fragments of Application data MAY be sent as they are
10293 * potentially useful as a traffic analysis countermeasure.
10294 *
10295 * Therefore, it is possible that the input message length is 0 and the
10296 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010297 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010298static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010299 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010300{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010301 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10302 const size_t max_len = (size_t) ret;
10303
10304 if( ret < 0 )
10305 {
10306 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10307 return( ret );
10308 }
10309
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010310 if( len > max_len )
10311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010312#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010313 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010316 "maximum fragment length: %d > %d",
10317 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010318 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010319 }
10320 else
10321#endif
10322 len = max_len;
10323 }
Paul Bakker887bd502011-06-08 13:10:54 +000010324
Paul Bakker5121ce52009-01-03 21:22:43 +000010325 if( ssl->out_left != 0 )
10326 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010327 /*
10328 * The user has previously tried to send the data and
10329 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10330 * written. In this case, we expect the high-level write function
10331 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010333 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010336 return( ret );
10337 }
10338 }
Paul Bakker887bd502011-06-08 13:10:54 +000010339 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010340 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010341 /*
10342 * The user is trying to send a message the first time, so we need to
10343 * copy the data into the internal buffers and setup the data structure
10344 * to keep track of partial writes
10345 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010346 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010347 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010348 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010349
Hanno Becker67bc7c32018-08-06 11:33:50 +010010350 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010352 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010353 return( ret );
10354 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010355 }
10356
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010357 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010358}
10359
10360/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010361 * Write application data, doing 1/n-1 splitting if necessary.
10362 *
10363 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010364 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010365 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010366 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010367#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010368static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010369 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010370{
10371 int ret;
10372
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010373 if( ssl->conf->cbc_record_splitting ==
10374 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010375 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010376 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10377 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10378 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010379 {
10380 return( ssl_write_real( ssl, buf, len ) );
10381 }
10382
10383 if( ssl->split_done == 0 )
10384 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010385 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010386 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010387 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010388 }
10389
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010390 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10391 return( ret );
10392 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010393
10394 return( ret + 1 );
10395}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010396#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010397
10398/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010399 * Write application data (public-facing wrapper)
10400 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010401int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010402{
10403 int ret;
10404
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010406
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010407 if( ssl == NULL || ssl->conf == NULL )
10408 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10409
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010410#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010411 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10412 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010413 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010414 return( ret );
10415 }
10416#endif
10417
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010418 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010419 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010420 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010421 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010422 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010423 return( ret );
10424 }
10425 }
10426
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010427#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010428 ret = ssl_write_split( ssl, buf, len );
10429#else
10430 ret = ssl_write_real( ssl, buf, len );
10431#endif
10432
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010434
10435 return( ret );
10436}
10437
10438/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010439 * Notify the peer that the connection is being closed
10440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010441int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010442{
10443 int ret;
10444
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010445 if( ssl == NULL || ssl->conf == NULL )
10446 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010449
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010450 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010451 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010453 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010455 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10456 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10457 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010459 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010460 return( ret );
10461 }
10462 }
10463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010465
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010466 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010467}
10468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010469void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010470{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010471 if( transform == NULL )
10472 return;
10473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010474#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010475 deflateEnd( &transform->ctx_deflate );
10476 inflateEnd( &transform->ctx_inflate );
10477#endif
10478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010479 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10480 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010481
Hanno Beckerd56ed242018-01-03 15:32:51 +000010482#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010483 mbedtls_md_free( &transform->md_ctx_enc );
10484 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010485#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010486
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010487 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010488}
10489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010490#if defined(MBEDTLS_X509_CRT_PARSE_C)
10491static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010492{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010493 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010494
10495 while( cur != NULL )
10496 {
10497 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010498 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010499 cur = next;
10500 }
10501}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010502#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010503
Hanno Becker0271f962018-08-16 13:23:47 +010010504#if defined(MBEDTLS_SSL_PROTO_DTLS)
10505
10506static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10507{
10508 unsigned offset;
10509 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10510
10511 if( hs == NULL )
10512 return;
10513
Hanno Becker283f5ef2018-08-24 09:34:47 +010010514 ssl_free_buffered_record( ssl );
10515
Hanno Becker0271f962018-08-16 13:23:47 +010010516 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010517 ssl_buffering_free_slot( ssl, offset );
10518}
10519
10520static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10521 uint8_t slot )
10522{
10523 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10524 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010525
10526 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10527 return;
10528
Hanno Beckere605b192018-08-21 15:59:07 +010010529 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010530 {
Hanno Beckere605b192018-08-21 15:59:07 +010010531 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010532 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010533 mbedtls_free( hs_buf->data );
10534 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010535 }
10536}
10537
10538#endif /* MBEDTLS_SSL_PROTO_DTLS */
10539
Gilles Peskine9b562d52018-04-25 20:32:43 +020010540void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010541{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010542 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10543
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010544 if( handshake == NULL )
10545 return;
10546
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010547#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10548 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10549 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010550 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010551 handshake->async_in_progress = 0;
10552 }
10553#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10554
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010555#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10556 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10557 mbedtls_md5_free( &handshake->fin_md5 );
10558 mbedtls_sha1_free( &handshake->fin_sha1 );
10559#endif
10560#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10561#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010562#if defined(MBEDTLS_USE_PSA_CRYPTO)
10563 psa_hash_abort( &handshake->fin_sha256_psa );
10564#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010565 mbedtls_sha256_free( &handshake->fin_sha256 );
10566#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010567#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010568#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010569#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010570 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010571#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010572 mbedtls_sha512_free( &handshake->fin_sha512 );
10573#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010574#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010575#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010577#if defined(MBEDTLS_DHM_C)
10578 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010579#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010580#if defined(MBEDTLS_ECDH_C)
10581 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010582#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010583#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010584 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010585#if defined(MBEDTLS_SSL_CLI_C)
10586 mbedtls_free( handshake->ecjpake_cache );
10587 handshake->ecjpake_cache = NULL;
10588 handshake->ecjpake_cache_len = 0;
10589#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010590#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010591
Janos Follath4ae5c292016-02-10 11:27:43 +000010592#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10593 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010594 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010595 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010596#endif
10597
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010598#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10599 if( handshake->psk != NULL )
10600 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010601 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010602 mbedtls_free( handshake->psk );
10603 }
10604#endif
10605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010606#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10607 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010608 /*
10609 * Free only the linked list wrapper, not the keys themselves
10610 * since the belong to the SNI callback
10611 */
10612 if( handshake->sni_key_cert != NULL )
10613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010614 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010615
10616 while( cur != NULL )
10617 {
10618 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010619 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010620 cur = next;
10621 }
10622 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010623#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010624
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010625#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010626 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010627 if( handshake->ecrs_peer_cert != NULL )
10628 {
10629 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10630 mbedtls_free( handshake->ecrs_peer_cert );
10631 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010632#endif
10633
Hanno Becker75173122019-02-06 16:18:31 +000010634#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10635 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10636 mbedtls_pk_free( &handshake->peer_pubkey );
10637#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010639#if defined(MBEDTLS_SSL_PROTO_DTLS)
10640 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010641 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010642 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010643#endif
10644
Hanno Becker4a63ed42019-01-08 11:39:35 +000010645#if defined(MBEDTLS_ECDH_C) && \
10646 defined(MBEDTLS_USE_PSA_CRYPTO)
10647 psa_destroy_key( handshake->ecdh_psa_privkey );
10648#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10649
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010650 mbedtls_platform_zeroize( handshake,
10651 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010652}
10653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010654void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010655{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010656 if( session == NULL )
10657 return;
10658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010659#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010660 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010661#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010662
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010663#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010664 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010665#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010666
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010667 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010668}
10669
Paul Bakker5121ce52009-01-03 21:22:43 +000010670/*
10671 * Free an SSL context
10672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010673void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010674{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010675 if( ssl == NULL )
10676 return;
10677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010679
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010680 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010681 {
Angus Grattond8213d02016-05-25 20:56:48 +100010682 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010683 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010684 }
10685
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010686 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010687 {
Angus Grattond8213d02016-05-25 20:56:48 +100010688 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010689 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010690 }
10691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010692#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010693 if( ssl->compress_buf != NULL )
10694 {
Angus Grattond8213d02016-05-25 20:56:48 +100010695 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010696 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010697 }
10698#endif
10699
Paul Bakker48916f92012-09-16 19:57:18 +000010700 if( ssl->transform )
10701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010702 mbedtls_ssl_transform_free( ssl->transform );
10703 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010704 }
10705
10706 if( ssl->handshake )
10707 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010708 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010709 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10710 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010712 mbedtls_free( ssl->handshake );
10713 mbedtls_free( ssl->transform_negotiate );
10714 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010715 }
10716
Paul Bakkerc0463502013-02-14 11:19:38 +010010717 if( ssl->session )
10718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010719 mbedtls_ssl_session_free( ssl->session );
10720 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010721 }
10722
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010723#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010724 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010725 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010726 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010727 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010728 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010729#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010731#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10732 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10735 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010736 }
10737#endif
10738
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010739#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010740 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010741#endif
10742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010744
Paul Bakker86f04f42013-02-14 11:20:09 +010010745 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010746 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010747}
10748
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010749/*
10750 * Initialze mbedtls_ssl_config
10751 */
10752void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10753{
10754 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10755}
10756
Simon Butcherc97b6972015-12-27 23:48:17 +000010757#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010758static int ssl_preset_default_hashes[] = {
10759#if defined(MBEDTLS_SHA512_C)
10760 MBEDTLS_MD_SHA512,
10761 MBEDTLS_MD_SHA384,
10762#endif
10763#if defined(MBEDTLS_SHA256_C)
10764 MBEDTLS_MD_SHA256,
10765 MBEDTLS_MD_SHA224,
10766#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010767#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010768 MBEDTLS_MD_SHA1,
10769#endif
10770 MBEDTLS_MD_NONE
10771};
Simon Butcherc97b6972015-12-27 23:48:17 +000010772#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010773
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010774static int ssl_preset_suiteb_ciphersuites[] = {
10775 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10776 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10777 0
10778};
10779
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010780#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010781static int ssl_preset_suiteb_hashes[] = {
10782 MBEDTLS_MD_SHA256,
10783 MBEDTLS_MD_SHA384,
10784 MBEDTLS_MD_NONE
10785};
10786#endif
10787
10788#if defined(MBEDTLS_ECP_C)
10789static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010790#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010791 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010792#endif
10793#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010794 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010795#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010796 MBEDTLS_ECP_DP_NONE
10797};
10798#endif
10799
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010800/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010801 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010802 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010803int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010804 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010805{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010806#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010807 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010808#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010809
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010810 /* Use the functions here so that they are covered in tests,
10811 * but otherwise access member directly for efficiency */
10812 mbedtls_ssl_conf_endpoint( conf, endpoint );
10813 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010814
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010815 /*
10816 * Things that are common to all presets
10817 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010818#if defined(MBEDTLS_SSL_CLI_C)
10819 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10820 {
10821 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10822#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10823 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10824#endif
10825 }
10826#endif
10827
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010828#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010829 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010830#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010831
10832#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10833 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10834#endif
10835
10836#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10837 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10838#endif
10839
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010840#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10841 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10842#endif
10843
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010844#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010845 conf->f_cookie_write = ssl_cookie_write_dummy;
10846 conf->f_cookie_check = ssl_cookie_check_dummy;
10847#endif
10848
10849#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10850 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10851#endif
10852
Janos Follath088ce432017-04-10 12:42:31 +010010853#if defined(MBEDTLS_SSL_SRV_C)
10854 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10855#endif
10856
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010857#if defined(MBEDTLS_SSL_PROTO_DTLS)
10858 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10859 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10860#endif
10861
10862#if defined(MBEDTLS_SSL_RENEGOTIATION)
10863 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010864 memset( conf->renego_period, 0x00, 2 );
10865 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010866#endif
10867
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010868#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10869 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10870 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010871 const unsigned char dhm_p[] =
10872 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10873 const unsigned char dhm_g[] =
10874 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10875
Hanno Beckera90658f2017-10-04 15:29:08 +010010876 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10877 dhm_p, sizeof( dhm_p ),
10878 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010879 {
10880 return( ret );
10881 }
10882 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010883#endif
10884
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010885 /*
10886 * Preset-specific defaults
10887 */
10888 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010889 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010890 /*
10891 * NSA Suite B
10892 */
10893 case MBEDTLS_SSL_PRESET_SUITEB:
10894 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10895 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10896 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10897 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10898
10899 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10900 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10901 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10902 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10903 ssl_preset_suiteb_ciphersuites;
10904
10905#if defined(MBEDTLS_X509_CRT_PARSE_C)
10906 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010907#endif
10908
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010909#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010910 conf->sig_hashes = ssl_preset_suiteb_hashes;
10911#endif
10912
10913#if defined(MBEDTLS_ECP_C)
10914 conf->curve_list = ssl_preset_suiteb_curves;
10915#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010916 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010917
10918 /*
10919 * Default
10920 */
10921 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010922 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10923 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10924 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10925 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10926 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10927 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10928 MBEDTLS_SSL_MIN_MINOR_VERSION :
10929 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010930 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10931 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10932
10933#if defined(MBEDTLS_SSL_PROTO_DTLS)
10934 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10935 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10936#endif
10937
10938 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10939 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10940 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10941 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10942 mbedtls_ssl_list_ciphersuites();
10943
10944#if defined(MBEDTLS_X509_CRT_PARSE_C)
10945 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10946#endif
10947
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010948#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010949 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010950#endif
10951
10952#if defined(MBEDTLS_ECP_C)
10953 conf->curve_list = mbedtls_ecp_grp_id_list();
10954#endif
10955
10956#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10957 conf->dhm_min_bitlen = 1024;
10958#endif
10959 }
10960
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010961 return( 0 );
10962}
10963
10964/*
10965 * Free mbedtls_ssl_config
10966 */
10967void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10968{
10969#if defined(MBEDTLS_DHM_C)
10970 mbedtls_mpi_free( &conf->dhm_P );
10971 mbedtls_mpi_free( &conf->dhm_G );
10972#endif
10973
10974#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10975 if( conf->psk != NULL )
10976 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010977 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010978 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010979 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010980 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010981 }
10982
10983 if( conf->psk_identity != NULL )
10984 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010985 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010986 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010987 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010988 conf->psk_identity_len = 0;
10989 }
10990#endif
10991
10992#if defined(MBEDTLS_X509_CRT_PARSE_C)
10993 ssl_key_cert_free( conf->key_cert );
10994#endif
10995
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010996 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010997}
10998
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010999#if defined(MBEDTLS_PK_C) && \
11000 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011001/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011002 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011003 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011004unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011005{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011006#if defined(MBEDTLS_RSA_C)
11007 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
11008 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011009#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011010#if defined(MBEDTLS_ECDSA_C)
11011 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
11012 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011013#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011014 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011015}
11016
Hanno Becker7e5437a2017-04-28 17:15:26 +010011017unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
11018{
11019 switch( type ) {
11020 case MBEDTLS_PK_RSA:
11021 return( MBEDTLS_SSL_SIG_RSA );
11022 case MBEDTLS_PK_ECDSA:
11023 case MBEDTLS_PK_ECKEY:
11024 return( MBEDTLS_SSL_SIG_ECDSA );
11025 default:
11026 return( MBEDTLS_SSL_SIG_ANON );
11027 }
11028}
11029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011030mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011031{
11032 switch( sig )
11033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011034#if defined(MBEDTLS_RSA_C)
11035 case MBEDTLS_SSL_SIG_RSA:
11036 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011038#if defined(MBEDTLS_ECDSA_C)
11039 case MBEDTLS_SSL_SIG_ECDSA:
11040 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011041#endif
11042 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011043 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011044 }
11045}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011046#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011047
Hanno Becker7e5437a2017-04-28 17:15:26 +010011048#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
11049 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
11050
11051/* Find an entry in a signature-hash set matching a given hash algorithm. */
11052mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
11053 mbedtls_pk_type_t sig_alg )
11054{
11055 switch( sig_alg )
11056 {
11057 case MBEDTLS_PK_RSA:
11058 return( set->rsa );
11059 case MBEDTLS_PK_ECDSA:
11060 return( set->ecdsa );
11061 default:
11062 return( MBEDTLS_MD_NONE );
11063 }
11064}
11065
11066/* Add a signature-hash-pair to a signature-hash set */
11067void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
11068 mbedtls_pk_type_t sig_alg,
11069 mbedtls_md_type_t md_alg )
11070{
11071 switch( sig_alg )
11072 {
11073 case MBEDTLS_PK_RSA:
11074 if( set->rsa == MBEDTLS_MD_NONE )
11075 set->rsa = md_alg;
11076 break;
11077
11078 case MBEDTLS_PK_ECDSA:
11079 if( set->ecdsa == MBEDTLS_MD_NONE )
11080 set->ecdsa = md_alg;
11081 break;
11082
11083 default:
11084 break;
11085 }
11086}
11087
11088/* Allow exactly one hash algorithm for each signature. */
11089void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11090 mbedtls_md_type_t md_alg )
11091{
11092 set->rsa = md_alg;
11093 set->ecdsa = md_alg;
11094}
11095
11096#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11097 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11098
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011099/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011100 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011102mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011103{
11104 switch( hash )
11105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011106#if defined(MBEDTLS_MD5_C)
11107 case MBEDTLS_SSL_HASH_MD5:
11108 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011109#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011110#if defined(MBEDTLS_SHA1_C)
11111 case MBEDTLS_SSL_HASH_SHA1:
11112 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011113#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011114#if defined(MBEDTLS_SHA256_C)
11115 case MBEDTLS_SSL_HASH_SHA224:
11116 return( MBEDTLS_MD_SHA224 );
11117 case MBEDTLS_SSL_HASH_SHA256:
11118 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011119#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011120#if defined(MBEDTLS_SHA512_C)
11121 case MBEDTLS_SSL_HASH_SHA384:
11122 return( MBEDTLS_MD_SHA384 );
11123 case MBEDTLS_SSL_HASH_SHA512:
11124 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011125#endif
11126 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011127 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011128 }
11129}
11130
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011131/*
11132 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11133 */
11134unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11135{
11136 switch( md )
11137 {
11138#if defined(MBEDTLS_MD5_C)
11139 case MBEDTLS_MD_MD5:
11140 return( MBEDTLS_SSL_HASH_MD5 );
11141#endif
11142#if defined(MBEDTLS_SHA1_C)
11143 case MBEDTLS_MD_SHA1:
11144 return( MBEDTLS_SSL_HASH_SHA1 );
11145#endif
11146#if defined(MBEDTLS_SHA256_C)
11147 case MBEDTLS_MD_SHA224:
11148 return( MBEDTLS_SSL_HASH_SHA224 );
11149 case MBEDTLS_MD_SHA256:
11150 return( MBEDTLS_SSL_HASH_SHA256 );
11151#endif
11152#if defined(MBEDTLS_SHA512_C)
11153 case MBEDTLS_MD_SHA384:
11154 return( MBEDTLS_SSL_HASH_SHA384 );
11155 case MBEDTLS_MD_SHA512:
11156 return( MBEDTLS_SSL_HASH_SHA512 );
11157#endif
11158 default:
11159 return( MBEDTLS_SSL_HASH_NONE );
11160 }
11161}
11162
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011163#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011164/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011165 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011166 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011167 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011168int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011169{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011170 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011171
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011172 if( ssl->conf->curve_list == NULL )
11173 return( -1 );
11174
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011175 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011176 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011177 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011178
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011179 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011180}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011181#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011182
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011183#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011184/*
11185 * Check if a hash proposed by the peer is in our list.
11186 * Return 0 if we're willing to use it, -1 otherwise.
11187 */
11188int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11189 mbedtls_md_type_t md )
11190{
11191 const int *cur;
11192
11193 if( ssl->conf->sig_hashes == NULL )
11194 return( -1 );
11195
11196 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11197 if( *cur == (int) md )
11198 return( 0 );
11199
11200 return( -1 );
11201}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011202#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011204#if defined(MBEDTLS_X509_CRT_PARSE_C)
11205int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11206 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011207 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011208 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011209{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011210 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011211#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011212 int usage = 0;
11213#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011214#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011215 const char *ext_oid;
11216 size_t ext_len;
11217#endif
11218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011219#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11220 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011221 ((void) cert);
11222 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011223 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011224#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011226#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11227 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011228 {
11229 /* Server part of the key exchange */
11230 switch( ciphersuite->key_exchange )
11231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011232 case MBEDTLS_KEY_EXCHANGE_RSA:
11233 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011234 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011235 break;
11236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011237 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11238 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11239 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11240 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011241 break;
11242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011243 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11244 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011245 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011246 break;
11247
11248 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011249 case MBEDTLS_KEY_EXCHANGE_NONE:
11250 case MBEDTLS_KEY_EXCHANGE_PSK:
11251 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11252 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011253 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011254 usage = 0;
11255 }
11256 }
11257 else
11258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011259 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11260 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011261 }
11262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011263 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011264 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011265 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011266 ret = -1;
11267 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011268#else
11269 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011270#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011272#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11273 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011275 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11276 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011277 }
11278 else
11279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011280 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11281 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011282 }
11283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011284 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011285 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011286 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011287 ret = -1;
11288 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011289#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011290
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011291 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011292}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011293#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011294
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011295/*
11296 * Convert version numbers to/from wire format
11297 * and, for DTLS, to/from TLS equivalent.
11298 *
11299 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011300 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011301 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11302 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011304void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011305 unsigned char ver[2] )
11306{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011307#if defined(MBEDTLS_SSL_PROTO_DTLS)
11308 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011310 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011311 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11312
11313 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11314 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11315 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011316 else
11317#else
11318 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011319#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011320 {
11321 ver[0] = (unsigned char) major;
11322 ver[1] = (unsigned char) minor;
11323 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011324}
11325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011326void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011327 const unsigned char ver[2] )
11328{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011329#if defined(MBEDTLS_SSL_PROTO_DTLS)
11330 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011331 {
11332 *major = 255 - ver[0] + 2;
11333 *minor = 255 - ver[1] + 1;
11334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011335 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011336 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11337 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011338 else
11339#else
11340 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011341#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011342 {
11343 *major = ver[0];
11344 *minor = ver[1];
11345 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011346}
11347
Simon Butcher99000142016-10-13 17:21:01 +010011348int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11349{
11350#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11351 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11352 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11353
11354 switch( md )
11355 {
11356#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11357#if defined(MBEDTLS_MD5_C)
11358 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011359 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011360#endif
11361#if defined(MBEDTLS_SHA1_C)
11362 case MBEDTLS_SSL_HASH_SHA1:
11363 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11364 break;
11365#endif
11366#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11367#if defined(MBEDTLS_SHA512_C)
11368 case MBEDTLS_SSL_HASH_SHA384:
11369 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11370 break;
11371#endif
11372#if defined(MBEDTLS_SHA256_C)
11373 case MBEDTLS_SSL_HASH_SHA256:
11374 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11375 break;
11376#endif
11377 default:
11378 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11379 }
11380
11381 return 0;
11382#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11383 (void) ssl;
11384 (void) md;
11385
11386 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11387#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11388}
11389
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011390#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11391 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11392int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11393 unsigned char *output,
11394 unsigned char *data, size_t data_len )
11395{
11396 int ret = 0;
11397 mbedtls_md5_context mbedtls_md5;
11398 mbedtls_sha1_context mbedtls_sha1;
11399
11400 mbedtls_md5_init( &mbedtls_md5 );
11401 mbedtls_sha1_init( &mbedtls_sha1 );
11402
11403 /*
11404 * digitally-signed struct {
11405 * opaque md5_hash[16];
11406 * opaque sha_hash[20];
11407 * };
11408 *
11409 * md5_hash
11410 * MD5(ClientHello.random + ServerHello.random
11411 * + ServerParams);
11412 * sha_hash
11413 * SHA(ClientHello.random + ServerHello.random
11414 * + ServerParams);
11415 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011416 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011417 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011418 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011419 goto exit;
11420 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011421 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011422 ssl->handshake->randbytes, 64 ) ) != 0 )
11423 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011424 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011425 goto exit;
11426 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011427 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011428 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011429 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011430 goto exit;
11431 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011432 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011433 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011435 goto exit;
11436 }
11437
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011438 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011439 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011441 goto exit;
11442 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011443 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011444 ssl->handshake->randbytes, 64 ) ) != 0 )
11445 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011446 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011447 goto exit;
11448 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011449 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011450 data_len ) ) != 0 )
11451 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011452 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011453 goto exit;
11454 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011455 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011456 output + 16 ) ) != 0 )
11457 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011459 goto exit;
11460 }
11461
11462exit:
11463 mbedtls_md5_free( &mbedtls_md5 );
11464 mbedtls_sha1_free( &mbedtls_sha1 );
11465
11466 if( ret != 0 )
11467 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11468 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11469
11470 return( ret );
11471
11472}
11473#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11474 MBEDTLS_SSL_PROTO_TLS1_1 */
11475
11476#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11477 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011478
11479#if defined(MBEDTLS_USE_PSA_CRYPTO)
11480int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11481 unsigned char *hash, size_t *hashlen,
11482 unsigned char *data, size_t data_len,
11483 mbedtls_md_type_t md_alg )
11484{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011485 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011486 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011487 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11488
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011489 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011490
11491 if( ( status = psa_hash_setup( &hash_operation,
11492 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011493 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011494 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011495 goto exit;
11496 }
11497
Andrzej Kurek814feff2019-01-14 04:35:19 -050011498 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11499 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011500 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011501 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011502 goto exit;
11503 }
11504
Andrzej Kurek814feff2019-01-14 04:35:19 -050011505 if( ( status = psa_hash_update( &hash_operation,
11506 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011507 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011508 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011509 goto exit;
11510 }
11511
Andrzej Kurek814feff2019-01-14 04:35:19 -050011512 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11513 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011514 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011515 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011516 goto exit;
11517 }
11518
11519exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011520 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011521 {
11522 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11523 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011524 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011525 {
11526 case PSA_ERROR_NOT_SUPPORTED:
11527 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011528 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011529 case PSA_ERROR_BUFFER_TOO_SMALL:
11530 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11531 case PSA_ERROR_INSUFFICIENT_MEMORY:
11532 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11533 default:
11534 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11535 }
11536 }
11537 return( 0 );
11538}
11539
11540#else
11541
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011542int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011543 unsigned char *hash, size_t *hashlen,
11544 unsigned char *data, size_t data_len,
11545 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011546{
11547 int ret = 0;
11548 mbedtls_md_context_t ctx;
11549 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011550 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011551
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011552 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011553
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011554 mbedtls_md_init( &ctx );
11555
11556 /*
11557 * digitally-signed struct {
11558 * opaque client_random[32];
11559 * opaque server_random[32];
11560 * ServerDHParams params;
11561 * };
11562 */
11563 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11564 {
11565 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11566 goto exit;
11567 }
11568 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11569 {
11570 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11571 goto exit;
11572 }
11573 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11574 {
11575 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11576 goto exit;
11577 }
11578 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11579 {
11580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11581 goto exit;
11582 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011583 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011584 {
11585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11586 goto exit;
11587 }
11588
11589exit:
11590 mbedtls_md_free( &ctx );
11591
11592 if( ret != 0 )
11593 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11594 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11595
11596 return( ret );
11597}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011598#endif /* MBEDTLS_USE_PSA_CRYPTO */
11599
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011600#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11601 MBEDTLS_SSL_PROTO_TLS1_2 */
11602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011603#endif /* MBEDTLS_SSL_TLS_C */