blob: 7cfdbf213851fc5dd6e865350355869bd07d8dd0 [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/*
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +0200981 * Populate a transform structure with session keys and all the other
982 * necessary information.
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200983 *
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +0200984 * Parameters:
985 * - [in/out]: transform: structure to populate
986 * [in] must be just initialised with mbedtls_ssl_transform_init()
987 * [out] fully populate, ready for use by mbedtls_ssl_{en,de}crypt_buf()
988 * - [in] session: used members: encrypt_then_max, master, compression
989 * - [in] handshake: used members: prf, ciphersuite_info, randbytes
990 * - [in]: ssl: used members: minor_ver, conf->endpoint
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200991 */
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +0200992static int ssl_populate_transform( mbedtls_ssl_transform *transform,
993 const mbedtls_ssl_session *session,
994 const mbedtls_ssl_handshake_params *handshake,
995 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000996{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200997 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000998#if defined(MBEDTLS_USE_PSA_CRYPTO)
999 int psa_fallthrough;
1000#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001001 unsigned char keyblk[256];
1002 unsigned char *key1;
1003 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001004 unsigned char *mac_enc;
1005 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001006 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001007 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001008 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001009 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 const mbedtls_cipher_info_t *cipher_info;
1011 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001012
Jaeden Amero2de07f12019-06-05 13:32:08 +01001013#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1014 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001015 transform->encrypt_then_mac = session->encrypt_then_mac;
1016#endif
1017 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001018
1019 ciphersuite_info = handshake->ciphersuite_info;
1020 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001021 if( cipher_info == NULL )
1022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001024 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001026 }
1027
Hanno Beckere694c3e2017-12-27 21:34:08 +00001028 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001029 if( md_info == NULL )
1030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001032 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001034 }
1035
Hanno Beckera0e20d02019-05-15 14:03:01 +01001036#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001037 /* Copy own and peer's CID if the use of the CID
1038 * extension has been negotiated. */
1039 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1040 {
1041 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001042
Hanno Becker05154c32019-05-03 15:23:51 +01001043 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001044 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001045 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001046 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001047
1048 transform->out_cid_len = ssl->handshake->peer_cid_len;
1049 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1050 ssl->handshake->peer_cid_len );
1051 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1052 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001053 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001054#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001055
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001057 * SSLv3:
1058 * key block =
1059 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1060 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1061 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1062 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1063 * ...
1064 *
1065 * TLSv1:
1066 * key block = PRF( master, "key expansion", randbytes )
1067 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001068 ret = handshake->tls_prf( session->master, 48, "key expansion",
1069 handshake->randbytes, 64, keyblk, 256 );
1070 if( ret != 0 )
1071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001073 return( ret );
1074 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1077 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1078 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1079 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1080 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
Paul Bakker5121ce52009-01-03 21:22:43 +00001082 /*
1083 * Determine the appropriate key, IV and MAC length.
1084 */
Paul Bakker68884e32013-01-07 18:20:04 +01001085
Hanno Becker88aaf652017-12-27 08:17:40 +00001086 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001087
Hanno Becker8031d062018-01-03 15:32:31 +00001088#if defined(MBEDTLS_GCM_C) || \
1089 defined(MBEDTLS_CCM_C) || \
1090 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001092 cipher_info->mode == MBEDTLS_MODE_CCM ||
1093 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001094 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001095 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001096
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001097 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001098 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001099 transform->taglen =
1100 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001101
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001102 /* All modes haves 96-bit IVs;
1103 * GCM and CCM has 4 implicit and 8 explicit bytes
1104 * ChachaPoly has all 12 bytes implicit
1105 */
Paul Bakker68884e32013-01-07 18:20:04 +01001106 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001107 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1108 transform->fixed_ivlen = 12;
1109 else
1110 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001111
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001112 /* Minimum length of encrypted record */
1113 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001114 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001115 }
1116 else
Hanno Becker8031d062018-01-03 15:32:31 +00001117#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1118#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1119 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1120 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001121 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001122 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1124 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001127 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001128 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001129
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001130 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001131 mac_key_len = mbedtls_md_get_size( md_info );
1132 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001135 /*
1136 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1137 * (rfc 6066 page 13 or rfc 2104 section 4),
1138 * so we only need to adjust the length here.
1139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001143
1144#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1145 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001146 * HMAC implementation which also truncates the key
1147 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001148 mac_key_len = transform->maclen;
1149#endif
1150 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001152
1153 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001154 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001156 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001158 transform->minlen = transform->maclen;
1159 else
Paul Bakker68884e32013-01-07 18:20:04 +01001160 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001161 /*
1162 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001163 * 1. if EtM is in use: one block plus MAC
1164 * otherwise: * first multiple of blocklen greater than maclen
1165 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1168 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001169 {
1170 transform->minlen = transform->maclen
1171 + cipher_info->block_size;
1172 }
1173 else
1174#endif
1175 {
1176 transform->minlen = transform->maclen
1177 + cipher_info->block_size
1178 - transform->maclen % cipher_info->block_size;
1179 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1182 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1183 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001184 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001185 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001186#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1188 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1189 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001190 {
1191 transform->minlen += transform->ivlen;
1192 }
1193 else
1194#endif
1195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001197 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1198 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001199 }
Paul Bakker68884e32013-01-07 18:20:04 +01001200 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 }
Hanno Becker8031d062018-01-03 15:32:31 +00001202 else
1203#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1204 {
1205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1206 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1207 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001208
Hanno Becker88aaf652017-12-27 08:17:40 +00001209 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1210 (unsigned) keylen,
1211 (unsigned) transform->minlen,
1212 (unsigned) transform->ivlen,
1213 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001214
1215 /*
1216 * Finally setup the cipher contexts, IVs and MAC secrets.
1217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001219 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001221 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001222 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001223
Paul Bakker68884e32013-01-07 18:20:04 +01001224 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001225 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001226
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001227 /*
1228 * This is not used in TLS v1.1.
1229 */
Paul Bakker48916f92012-09-16 19:57:18 +00001230 iv_copy_len = ( transform->fixed_ivlen ) ?
1231 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001232 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1233 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001234 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001235 }
1236 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237#endif /* MBEDTLS_SSL_CLI_C */
1238#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001239 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001240 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001241 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001242 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001243
Hanno Becker81c7b182017-11-09 18:39:33 +00001244 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001245 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001246
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001247 /*
1248 * This is not used in TLS v1.1.
1249 */
Paul Bakker48916f92012-09-16 19:57:18 +00001250 iv_copy_len = ( transform->fixed_ivlen ) ?
1251 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001252 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1253 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001254 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001255 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001256 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001260 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1261 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001262 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
Hanno Beckerd56ed242018-01-03 15:32:51 +00001264#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#if defined(MBEDTLS_SSL_PROTO_SSL3)
1266 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001267 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001268 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001271 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1272 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001273 }
1274
Hanno Becker81c7b182017-11-09 18:39:33 +00001275 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1276 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001277 }
1278 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1280#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1281 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1282 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001283 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001284 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1285 For AEAD-based ciphersuites, there is nothing to do here. */
1286 if( mac_key_len != 0 )
1287 {
1288 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1289 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1290 }
Paul Bakker68884e32013-01-07 18:20:04 +01001291 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001292 else
1293#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001296 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1297 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001298 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001299#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1302 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001303 {
1304 int ret = 0;
1305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001307
Hanno Becker88aaf652017-12-27 08:17:40 +00001308 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001309 transform->iv_enc, transform->iv_dec,
1310 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001311 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001312 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001315 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1316 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001317 }
1318 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001319#else
1320 ((void) mac_dec);
1321 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001323
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001324#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1325 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001326 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001327 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1328 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001329 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001330 iv_copy_len );
1331 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001332
1333 if( ssl->conf->f_export_keys_ext != NULL )
1334 {
1335 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1336 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001337 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001338 iv_copy_len,
Manuel Pégourié-Gonnard344460c2019-07-25 13:17:38 +02001339 /* work around bug in exporter type */
1340 (unsigned char *) handshake->randbytes + 32,
1341 (unsigned char *) handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001342 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001343 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001344#endif
1345
Hanno Beckerf704bef2018-11-16 15:21:18 +00001346#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001347
1348 /* Only use PSA-based ciphers for TLS-1.2.
1349 * That's relevant at least for TLS-1.0, where
1350 * we assume that mbedtls_cipher_crypt() updates
1351 * the structure field for the IV, which the PSA-based
1352 * implementation currently doesn't. */
1353#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1354 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001355 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001356 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001357 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001358 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1359 {
1360 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001361 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001362 }
1363
1364 if( ret == 0 )
1365 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001367 psa_fallthrough = 0;
1368 }
1369 else
1370 {
1371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1372 psa_fallthrough = 1;
1373 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001374 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001375 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001376 psa_fallthrough = 1;
1377#else
1378 psa_fallthrough = 1;
1379#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001380
Hanno Beckercb1cc802018-11-17 22:27:38 +00001381 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001382#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001383 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001384 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001387 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001388 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001389
Hanno Beckerf704bef2018-11-16 15:21:18 +00001390#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001391 /* Only use PSA-based ciphers for TLS-1.2.
1392 * That's relevant at least for TLS-1.0, where
1393 * we assume that mbedtls_cipher_crypt() updates
1394 * the structure field for the IV, which the PSA-based
1395 * implementation currently doesn't. */
1396#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1397 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001398 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001399 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001400 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001401 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1402 {
1403 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001404 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001405 }
1406
1407 if( ret == 0 )
1408 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001409 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001410 psa_fallthrough = 0;
1411 }
1412 else
1413 {
1414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1415 psa_fallthrough = 1;
1416 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001417 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001418 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001419 psa_fallthrough = 1;
1420#else
1421 psa_fallthrough = 1;
1422#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001423
Hanno Beckercb1cc802018-11-17 22:27:38 +00001424 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001425#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001426 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001427 cipher_info ) ) != 0 )
1428 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001429 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001430 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001431 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001434 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001438 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001439 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001442 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001446 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001447 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_CIPHER_MODE_CBC)
1450 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1453 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001456 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001457 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1460 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001463 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001464 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001465 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001467
Paul Bakker5121ce52009-01-03 21:22:43 +00001468
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001469 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001474
Paul Bakker48916f92012-09-16 19:57:18 +00001475 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1476 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001477
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001478 if( deflateInit( &transform->ctx_deflate,
1479 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001480 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001483 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1484 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001485 }
1486 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001488
Ron Eldore6992702019-05-07 18:27:13 +03001489end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001490 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Ron Eldore6992702019-05-07 18:27:13 +03001491 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001492}
1493
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001494/*
1495 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
1496 *
1497 * Inputs:
1498 * - SSL/TLS minor version
1499 * - hash associated with the ciphersuite (only used by TLS 1.2)
1500 *
1501 * Ouputs:
1502 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1503 */
1504static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1505 int minor_ver,
1506 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001507{
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001508#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1509 (void) hash;
1510#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001511
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001512#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001513 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001514 {
1515 handshake->tls_prf = ssl3_prf;
1516 handshake->calc_verify = ssl_calc_verify_ssl;
1517 handshake->calc_finished = ssl_calc_finished_ssl;
1518 }
1519 else
1520#endif
1521#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001522 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001523 {
1524 handshake->tls_prf = tls1_prf;
1525 handshake->calc_verify = ssl_calc_verify_tls;
1526 handshake->calc_finished = ssl_calc_finished_tls;
1527 }
1528 else
1529#endif
1530#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1531#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001532 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1533 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001534 {
1535 handshake->tls_prf = tls_prf_sha384;
1536 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1537 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1538 }
1539 else
1540#endif
1541#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001542 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001543 {
1544 handshake->tls_prf = tls_prf_sha256;
1545 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1546 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1547 }
1548 else
1549#endif
1550#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1551 {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001552 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1553 }
1554
1555 return( 0 );
1556}
1557
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001558/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001559 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001560 *
1561 * Parameters:
1562 * [in/out] handshake
1563 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001564 * (PSA-PSK) ciphersuite_info, psk_opaque
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001565 * [out] premaster (cleared)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001566 * [out] master
1567 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1568 * debug: conf->f_dbg, conf->p_dbg
1569 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001570 * PSA-PSA: minor_ver, conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001571 */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001572static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001573 unsigned char *master,
Manuel Pégourié-Gonnard0d56aaa2019-05-03 09:58:33 +02001574 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001575{
1576 int ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001577
1578 /* cf. RFC 5246, Section 8.1:
1579 * "The master secret is always exactly 48 bytes in length." */
1580 size_t const master_secret_len = 48;
1581
1582#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1583 unsigned char session_hash[48];
1584#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1585
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001586 /* The label for the KDF used for key expansion.
1587 * This is either "master secret" or "extended master secret"
1588 * depending on whether the Extended Master Secret extension
1589 * is used. */
1590 char const *lbl = "master secret";
1591
1592 /* The salt for the KDF used for key expansion.
1593 * - If the Extended Master Secret extension is not used,
1594 * this is ClientHello.Random + ServerHello.Random
1595 * (see Sect. 8.1 in RFC 5246).
1596 * - If the Extended Master Secret extension is used,
1597 * this is the transcript of the handshake so far.
1598 * (see Sect. 4 in RFC 7627). */
1599 unsigned char const *salt = handshake->randbytes;
1600 size_t salt_len = 64;
1601
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001602#if !defined(MBEDTLS_DEBUG_C) && \
1603 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1604 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
1605 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
1606 (void) ssl;
1607#endif
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001608
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001609 if( handshake->resume != 0 )
1610 {
1611 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001612 return( 0 );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001613 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001614
1615#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001616 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001617 {
1618 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001619
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001620 lbl = "extended master secret";
1621 salt = session_hash;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001622 handshake->calc_verify( ssl, session_hash, &salt_len );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001623
1624 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
1625 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001626#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1627
1628#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001629 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1630 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001631 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001632 ssl_use_opaque_psk( ssl ) == 1 )
1633 {
1634 /* Perform PSK-to-MS expansion in a single step. */
1635 psa_status_t status;
1636 psa_algorithm_t alg;
1637 psa_key_handle_t psk;
1638 psa_key_derivation_operation_t derivation =
1639 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001640 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001641
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001643
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001644 psk = ssl->conf->psk_opaque;
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001645 if( handshake->psk_opaque != 0 )
1646 psk = handshake->psk_opaque;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001647
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001648 if( hash_alg == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001649 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001650 else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001651 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1652
1653 status = psa_key_derivation( &derivation, psk, alg,
1654 salt, salt_len,
1655 (unsigned char const *) lbl,
1656 (size_t) strlen( lbl ),
1657 master_secret_len );
1658 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001659 {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001660 psa_key_derivation_abort( &derivation );
1661 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001662 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001663
1664 status = psa_key_derivation_output_bytes( &derivation,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001665 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001666 master_secret_len );
1667 if( status != PSA_SUCCESS )
1668 {
1669 psa_key_derivation_abort( &derivation );
1670 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1671 }
1672
1673 status = psa_key_derivation_abort( &derivation );
1674 if( status != PSA_SUCCESS )
1675 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1676 }
1677 else
1678#endif
1679 {
1680 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1681 lbl, salt, salt_len,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001682 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001683 master_secret_len );
1684 if( ret != 0 )
1685 {
1686 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1687 return( ret );
1688 }
1689
1690 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1691 handshake->premaster,
1692 handshake->pmslen );
1693
1694 mbedtls_platform_zeroize( handshake->premaster,
1695 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001696 }
1697
1698 return( 0 );
1699}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001700
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001701int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1702{
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001703 int ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001704 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1705 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001706
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1708
1709 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001710 ret = ssl_set_handshake_prfs( ssl->handshake,
1711 ssl->minor_ver,
1712 ciphersuite_info->mac );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001713 if( ret != 0 )
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001714 {
1715 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001716 return( ret );
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001717 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001718
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001719 /* Compute master secret if needed */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001720 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001721 ssl->session_negotiate->master,
1722 ssl );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001723 if( ret != 0 )
1724 {
1725 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1726 return( ret );
1727 }
1728
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001729 /* Swap the client and server random values:
1730 * - MS derivation wanted client+server (RFC 5246 8.1)
1731 * - key derivation wants server+client (RFC 5246 6.3) */
1732 {
1733 unsigned char tmp[64];
1734 memcpy( tmp, ssl->handshake->randbytes, 64 );
1735 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1736 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1737 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1738 }
1739
1740 /* Populate transform structure */
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001741 ret = ssl_populate_transform( ssl->transform_negotiate,
1742 ssl->session_negotiate,
1743 ssl->handshake,
1744 ssl );
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001745 if( ret != 0 )
1746 {
1747 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1748 return( ret );
1749 }
1750
1751 /* We no longer need Server/ClientHello.random values */
1752 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1753 sizeof( ssl->handshake->randbytes ) );
1754
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001755 /* Allocate compression buffer */
1756#if defined(MBEDTLS_ZLIB_SUPPORT)
1757 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1758 ssl->compress_buf == NULL )
1759 {
1760 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1761 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1762 if( ssl->compress_buf == NULL )
1763 {
1764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
1765 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
1766 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1767 }
1768 }
1769#endif
1770
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1772
1773 return( 0 );
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001774}
1775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001777void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1778 unsigned char hash[36],
1779 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001780{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001781 mbedtls_md5_context md5;
1782 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001783 unsigned char pad_1[48];
1784 unsigned char pad_2[48];
1785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001787
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001788 mbedtls_md5_init( &md5 );
1789 mbedtls_sha1_init( &sha1 );
1790
1791 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1792 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001793
Paul Bakker380da532012-04-18 16:10:25 +00001794 memset( pad_1, 0x36, 48 );
1795 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001796
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001797 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1798 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1799 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001800
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001801 mbedtls_md5_starts_ret( &md5 );
1802 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1803 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1804 mbedtls_md5_update_ret( &md5, hash, 16 );
1805 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001806
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001807 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1808 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1809 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001810
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001811 mbedtls_sha1_starts_ret( &sha1 );
1812 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1813 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1814 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1815 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001816
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001817 *hlen = 36;
1818
1819 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001821
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001822 mbedtls_md5_free( &md5 );
1823 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001824
Paul Bakker380da532012-04-18 16:10:25 +00001825 return;
1826}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001830void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1831 unsigned char hash[36],
1832 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001833{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001834 mbedtls_md5_context md5;
1835 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001838
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001839 mbedtls_md5_init( &md5 );
1840 mbedtls_sha1_init( &sha1 );
1841
1842 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1843 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001844
Andrzej Kurekeb342242019-01-29 09:14:33 -05001845 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001846 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001847
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001848 *hlen = 36;
1849
1850 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001852
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001853 mbedtls_md5_free( &md5 );
1854 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001855
Paul Bakker380da532012-04-18 16:10:25 +00001856 return;
1857}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1861#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001862void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1863 unsigned char hash[32],
1864 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001865{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001866#if defined(MBEDTLS_USE_PSA_CRYPTO)
1867 size_t hash_size;
1868 psa_status_t status;
1869 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1870
1871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1872 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1873 if( status != PSA_SUCCESS )
1874 {
1875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1876 return;
1877 }
1878
1879 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1880 if( status != PSA_SUCCESS )
1881 {
1882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1883 return;
1884 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001885
1886 *hlen = 32;
1887 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1889#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001890 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001891
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001892 mbedtls_sha256_init( &sha256 );
1893
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001895
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001896 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001897 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001898
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001899 *hlen = 32;
1900
1901 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001903
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001904 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001905#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001906 return;
1907}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001911void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1912 unsigned char hash[48],
1913 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001914{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001915#if defined(MBEDTLS_USE_PSA_CRYPTO)
1916 size_t hash_size;
1917 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001918 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001919
1920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001921 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001922 if( status != PSA_SUCCESS )
1923 {
1924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1925 return;
1926 }
1927
Andrzej Kurek972fba52019-01-30 03:29:12 -05001928 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001929 if( status != PSA_SUCCESS )
1930 {
1931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1932 return;
1933 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001934
1935 *hlen = 48;
1936 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1938#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001939 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001940
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001941 mbedtls_sha512_init( &sha512 );
1942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001944
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001945 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001946 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001947
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001948 *hlen = 48;
1949
1950 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001952
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001953 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001954#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 return;
1956}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957#endif /* MBEDTLS_SHA512_C */
1958#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1961int 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 +02001962{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001963 unsigned char *p = ssl->handshake->premaster;
1964 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001965 const unsigned char *psk = ssl->conf->psk;
1966 size_t psk_len = ssl->conf->psk_len;
1967
1968 /* If the psk callback was called, use its result */
1969 if( ssl->handshake->psk != NULL )
1970 {
1971 psk = ssl->handshake->psk;
1972 psk_len = ssl->handshake->psk_len;
1973 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001974
1975 /*
1976 * PMS = struct {
1977 * opaque other_secret<0..2^16-1>;
1978 * opaque psk<0..2^16-1>;
1979 * };
1980 * with "other_secret" depending on the particular key exchange
1981 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1983 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001984 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001985 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001987
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001988 *(p++) = (unsigned char)( psk_len >> 8 );
1989 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001990
1991 if( end < p || (size_t)( end - p ) < psk_len )
1992 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1993
1994 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001995 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001996 }
1997 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1999#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2000 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002001 {
2002 /*
2003 * other_secret already set by the ClientKeyExchange message,
2004 * and is 48 bytes long
2005 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002006 if( end - p < 2 )
2007 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2008
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002009 *p++ = 0;
2010 *p++ = 48;
2011 p += 48;
2012 }
2013 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2015#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2016 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002017 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002018 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002019 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002020
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002021 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002023 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002024 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002027 return( ret );
2028 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002029 *(p++) = (unsigned char)( len >> 8 );
2030 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002031 p += len;
2032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002034 }
2035 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2037#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2038 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002039 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002040 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002041 size_t zlen;
2042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002044 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002045 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002048 return( ret );
2049 }
2050
2051 *(p++) = (unsigned char)( zlen >> 8 );
2052 *(p++) = (unsigned char)( zlen );
2053 p += zlen;
2054
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002055 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2056 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002057 }
2058 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2062 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002063 }
2064
2065 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002066 if( end - p < 2 )
2067 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002068
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002069 *(p++) = (unsigned char)( psk_len >> 8 );
2070 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002071
2072 if( end < p || (size_t)( end - p ) < psk_len )
2073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2074
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002075 memcpy( p, psk, psk_len );
2076 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002077
2078 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2079
2080 return( 0 );
2081}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002085/*
2086 * SSLv3.0 MAC functions
2087 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002088#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002089static void ssl_mac( mbedtls_md_context_t *md_ctx,
2090 const unsigned char *secret,
2091 const unsigned char *buf, size_t len,
2092 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002093 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002094{
2095 unsigned char header[11];
2096 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002097 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2099 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002100
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002101 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002103 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002104 else
Paul Bakker68884e32013-01-07 18:20:04 +01002105 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002106
2107 memcpy( header, ctr, 8 );
2108 header[ 8] = (unsigned char) type;
2109 header[ 9] = (unsigned char)( len >> 8 );
2110 header[10] = (unsigned char)( len );
2111
Paul Bakker68884e32013-01-07 18:20:04 +01002112 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 mbedtls_md_starts( md_ctx );
2114 mbedtls_md_update( md_ctx, secret, md_size );
2115 mbedtls_md_update( md_ctx, padding, padlen );
2116 mbedtls_md_update( md_ctx, header, 11 );
2117 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002118 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002119
Paul Bakker68884e32013-01-07 18:20:04 +01002120 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 mbedtls_md_starts( md_ctx );
2122 mbedtls_md_update( md_ctx, secret, md_size );
2123 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002124 mbedtls_md_update( md_ctx, out, md_size );
2125 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002126}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002128
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002129/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002130 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002131#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002132 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2133 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2134 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2135/* This function makes sure every byte in the memory region is accessed
2136 * (in ascending addresses order) */
2137static void ssl_read_memory( unsigned char *p, size_t len )
2138{
2139 unsigned char acc = 0;
2140 volatile unsigned char force;
2141
2142 for( ; len != 0; p++, len-- )
2143 acc ^= *p;
2144
2145 force = acc;
2146 (void) force;
2147}
2148#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2149
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002150/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002151 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002152 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002153
Hanno Beckera0e20d02019-05-15 14:03:01 +01002154#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002155/* This functions transforms a DTLS plaintext fragment and a record content
2156 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002157 *
2158 * struct {
2159 * opaque content[DTLSPlaintext.length];
2160 * ContentType real_type;
2161 * uint8 zeros[length_of_padding];
2162 * } DTLSInnerPlaintext;
2163 *
2164 * Input:
2165 * - `content`: The beginning of the buffer holding the
2166 * plaintext to be wrapped.
2167 * - `*content_size`: The length of the plaintext in Bytes.
2168 * - `max_len`: The number of Bytes available starting from
2169 * `content`. This must be `>= *content_size`.
2170 * - `rec_type`: The desired record content type.
2171 *
2172 * Output:
2173 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2174 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2175 *
2176 * Returns:
2177 * - `0` on success.
2178 * - A negative error code if `max_len` didn't offer enough space
2179 * for the expansion.
2180 */
2181static int ssl_cid_build_inner_plaintext( unsigned char *content,
2182 size_t *content_size,
2183 size_t remaining,
2184 uint8_t rec_type )
2185{
2186 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002187 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2188 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2189 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002190
2191 /* Write real content type */
2192 if( remaining == 0 )
2193 return( -1 );
2194 content[ len ] = rec_type;
2195 len++;
2196 remaining--;
2197
2198 if( remaining < pad )
2199 return( -1 );
2200 memset( content + len, 0, pad );
2201 len += pad;
2202 remaining -= pad;
2203
2204 *content_size = len;
2205 return( 0 );
2206}
2207
Hanno Becker07dc97d2019-05-20 15:08:01 +01002208/* This function parses a DTLSInnerPlaintext structure.
2209 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002210static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2211 size_t *content_size,
2212 uint8_t *rec_type )
2213{
2214 size_t remaining = *content_size;
2215
2216 /* Determine length of padding by skipping zeroes from the back. */
2217 do
2218 {
2219 if( remaining == 0 )
2220 return( -1 );
2221 remaining--;
2222 } while( content[ remaining ] == 0 );
2223
2224 *content_size = remaining;
2225 *rec_type = content[ remaining ];
2226
2227 return( 0 );
2228}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002229#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002230
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002231/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002232 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002233static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002234 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002235 mbedtls_record *rec )
2236{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002237 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002238 *
2239 * additional_data = seq_num + TLSCompressed.type +
2240 * TLSCompressed.version + TLSCompressed.length;
2241 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002242 * For the CID extension, this is extended as follows
2243 * (quoting draft-ietf-tls-dtls-connection-id-05,
2244 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002245 *
2246 * additional_data = seq_num + DTLSPlaintext.type +
2247 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002248 * cid +
2249 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002250 * length_of_DTLSInnerPlaintext;
2251 */
2252
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002253 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2254 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002255 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002256
Hanno Beckera0e20d02019-05-15 14:03:01 +01002257#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002258 if( rec->cid_len != 0 )
2259 {
2260 memcpy( add_data + 11, rec->cid, rec->cid_len );
2261 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2262 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2263 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2264 *add_data_len = 13 + 1 + rec->cid_len;
2265 }
2266 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002267#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002268 {
2269 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2270 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2271 *add_data_len = 13;
2272 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002273}
2274
Hanno Beckera18d1322018-01-03 14:27:32 +00002275int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2276 mbedtls_ssl_transform *transform,
2277 mbedtls_record *rec,
2278 int (*f_rng)(void *, unsigned char *, size_t),
2279 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002280{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002282 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002283 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002284 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002285 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002286 size_t post_avail;
2287
2288 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002289#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002290 ((void) ssl);
2291#endif
2292
2293 /* The PRNG is used for dynamic IV generation that's used
2294 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2295#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2296 ( defined(MBEDTLS_AES_C) || \
2297 defined(MBEDTLS_ARIA_C) || \
2298 defined(MBEDTLS_CAMELLIA_C) ) && \
2299 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2300 ((void) f_rng);
2301 ((void) p_rng);
2302#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002305
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002306 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002307 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2309 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2310 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002311 if( rec == NULL
2312 || rec->buf == NULL
2313 || rec->buf_len < rec->data_offset
2314 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002315#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002316 || rec->cid_len != 0
2317#endif
2318 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002319 {
2320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002322 }
2323
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002324 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002325 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002327 data, rec->data_len );
2328
2329 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2330
2331 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2332 {
2333 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2334 (unsigned) rec->data_len,
2335 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2336 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2337 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002338
Hanno Beckera0e20d02019-05-15 14:03:01 +01002339#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002340 /*
2341 * Add CID information
2342 */
2343 rec->cid_len = transform->out_cid_len;
2344 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2345 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002346
2347 if( rec->cid_len != 0 )
2348 {
2349 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002350 * Wrap plaintext into DTLSInnerPlaintext structure.
2351 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002352 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002353 * Note that this changes `rec->data_len`, and hence
2354 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002355 */
2356 if( ssl_cid_build_inner_plaintext( data,
2357 &rec->data_len,
2358 post_avail,
2359 rec->type ) != 0 )
2360 {
2361 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2362 }
2363
2364 rec->type = MBEDTLS_SSL_MSG_CID;
2365 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002366#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002367
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002368 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2369
Paul Bakker5121ce52009-01-03 21:22:43 +00002370 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002371 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002372 */
Hanno Becker52344c22018-01-03 15:24:20 +00002373#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 if( mode == MBEDTLS_MODE_STREAM ||
2375 ( mode == MBEDTLS_MODE_CBC
2376#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002377 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002378#endif
2379 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002381 if( post_avail < transform->maclen )
2382 {
2383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2384 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2385 }
2386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002388 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002389 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002390 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002391 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2392 data, rec->data_len, rec->ctr, rec->type, mac );
2393 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002394 }
2395 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002396#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2398 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002399 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002400 {
Hanno Becker992b6872017-11-09 18:57:39 +00002401 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2402
Hanno Beckercab87e62019-04-29 13:52:53 +01002403 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002404
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002405 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002406 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002407 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2408 data, rec->data_len );
2409 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2410 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2411
2412 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002413 }
2414 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002415#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2418 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002419 }
2420
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002421 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2422 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002423
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002424 rec->data_len += transform->maclen;
2425 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002426 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002427 }
Hanno Becker52344c22018-01-03 15:24:20 +00002428#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002430 /*
2431 * Encrypt
2432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2434 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002435 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002436 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002437 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002439 "including %d bytes of padding",
2440 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002441
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002442 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2443 transform->iv_enc, transform->ivlen,
2444 data, rec->data_len,
2445 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002448 return( ret );
2449 }
2450
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002451 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2454 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002455 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 }
Paul Bakker68884e32013-01-07 18:20:04 +01002457 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002459
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002460#if defined(MBEDTLS_GCM_C) || \
2461 defined(MBEDTLS_CCM_C) || \
2462 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002464 mode == MBEDTLS_MODE_CCM ||
2465 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002466 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002467 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002468 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002469 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002470
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002471 /* Check that there's space for both the authentication tag
2472 * and the explicit IV before and after the record content. */
2473 if( post_avail < transform->taglen ||
2474 rec->data_offset < explicit_iv_len )
2475 {
2476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2477 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2478 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002479
Paul Bakker68884e32013-01-07 18:20:04 +01002480 /*
2481 * Generate IV
2482 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002483 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2484 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002485 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002486 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002487 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2488 explicit_iv_len );
2489 /* Prefix record content with explicit IV. */
2490 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002491 }
2492 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2493 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002494 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002495 unsigned char i;
2496
2497 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2498
2499 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002500 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002501 }
2502 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002503 {
2504 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2506 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002507 }
2508
Hanno Beckercab87e62019-04-29 13:52:53 +01002509 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002510
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002511 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2512 iv, transform->ivlen );
2513 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002514 data - explicit_iv_len, explicit_iv_len );
2515 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002516 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002518 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002519 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002520
Paul Bakker68884e32013-01-07 18:20:04 +01002521 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002522 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002523 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002524
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002525 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002526 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002527 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002528 data, rec->data_len, /* source */
2529 data, &rec->data_len, /* destination */
2530 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002533 return( ret );
2534 }
2535
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002536 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2537 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002538
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002539 rec->data_len += transform->taglen + explicit_iv_len;
2540 rec->data_offset -= explicit_iv_len;
2541 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002542 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002543 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002544 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2546#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002547 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002549 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002550 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002551 size_t padlen, i;
2552 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002553
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002554 /* Currently we're always using minimal padding
2555 * (up to 255 bytes would be allowed). */
2556 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2557 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 padlen = 0;
2559
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002560 /* Check there's enough space in the buffer for the padding. */
2561 if( post_avail < padlen + 1 )
2562 {
2563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2564 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2565 }
2566
Paul Bakker5121ce52009-01-03 21:22:43 +00002567 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002568 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002570 rec->data_len += padlen + 1;
2571 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002574 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002575 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2576 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002577 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002578 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002579 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002580 if( f_rng == NULL )
2581 {
2582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2583 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2584 }
2585
2586 if( rec->data_offset < transform->ivlen )
2587 {
2588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2589 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2590 }
2591
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002592 /*
2593 * Generate IV
2594 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002595 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002596 if( ret != 0 )
2597 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002598
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002599 memcpy( data - transform->ivlen, transform->iv_enc,
2600 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002601
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002602 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002606 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002607 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002608 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002609
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002610 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2611 transform->iv_enc,
2612 transform->ivlen,
2613 data, rec->data_len,
2614 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002617 return( ret );
2618 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002619
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002620 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2623 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002624 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002627 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002628 {
2629 /*
2630 * Save IV in SSL3 and TLS1
2631 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002632 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2633 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002634 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002635 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002636#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002637 {
2638 data -= transform->ivlen;
2639 rec->data_offset -= transform->ivlen;
2640 rec->data_len += transform->ivlen;
2641 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002644 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002645 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002646 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2647
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002648 /*
2649 * MAC(MAC_write_key, seq_num +
2650 * TLSCipherText.type +
2651 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002652 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002653 * IV + // except for TLS 1.0
2654 * ENC(content + padding + padding_length));
2655 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002656
2657 if( post_avail < transform->maclen)
2658 {
2659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2660 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2661 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002662
Hanno Beckercab87e62019-04-29 13:52:53 +01002663 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002666 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002667 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002668
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002669 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002670 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002671 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2672 data, rec->data_len );
2673 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2674 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002675
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002676 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002677
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002678 rec->data_len += transform->maclen;
2679 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002680 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002681 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002683 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002684 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002686 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2689 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002690 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002692 /* Make extra sure authentication was performed, exactly once */
2693 if( auth_done != 1 )
2694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2696 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002697 }
2698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
2701 return( 0 );
2702}
2703
Hanno Becker605949f2019-07-12 08:23:59 +01002704int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002705 mbedtls_ssl_transform *transform,
2706 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002707{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002708 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002710 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002711#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002712 size_t padlen = 0, correct = 1;
2713#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002714 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002715 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002716 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002717
Hanno Beckera18d1322018-01-03 14:27:32 +00002718#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002719 ((void) ssl);
2720#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002723 if( rec == NULL ||
2724 rec->buf == NULL ||
2725 rec->buf_len < rec->data_offset ||
2726 rec->buf_len - rec->data_offset < rec->data_len )
2727 {
2728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002730 }
2731
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002732 data = rec->buf + rec->data_offset;
2733 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002734
Hanno Beckera0e20d02019-05-15 14:03:01 +01002735#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002736 /*
2737 * Match record's CID with incoming CID.
2738 */
Hanno Becker938489a2019-05-08 13:02:22 +01002739 if( rec->cid_len != transform->in_cid_len ||
2740 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2741 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002742 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002743 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002744#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2747 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002748 {
2749 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002750 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2751 transform->iv_dec,
2752 transform->ivlen,
2753 data, rec->data_len,
2754 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002757 return( ret );
2758 }
2759
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002760 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2763 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002764 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002765 }
Paul Bakker68884e32013-01-07 18:20:04 +01002766 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002768#if defined(MBEDTLS_GCM_C) || \
2769 defined(MBEDTLS_CCM_C) || \
2770 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002772 mode == MBEDTLS_MODE_CCM ||
2773 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002774 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002775 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002776 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002777
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002778 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002779 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002780 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002781
2782 /* Check that there's enough space for the explicit IV
2783 * (at the beginning of the record) and the MAC (at the
2784 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002785 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002788 "+ taglen (%d)", rec->data_len,
2789 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002791 }
Paul Bakker68884e32013-01-07 18:20:04 +01002792
Hanno Beckerd96a6522019-07-10 13:55:25 +01002793#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002794 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2795 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002796 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002797
Hanno Beckerd96a6522019-07-10 13:55:25 +01002798 /* Fixed */
2799 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2800 /* Explicit */
2801 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002802 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002803 else
2804#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2805#if defined(MBEDTLS_CHACHAPOLY_C)
2806 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002807 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002808 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002809 unsigned char i;
2810
2811 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2812
2813 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002814 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002815 }
2816 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002817#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002818 {
2819 /* Reminder if we ever add an AEAD mode with a different size */
2820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2821 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2822 }
2823
Hanno Beckerd96a6522019-07-10 13:55:25 +01002824 /* Group changes to data, data_len, and add_data, because
2825 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002826 data += explicit_iv_len;
2827 rec->data_offset += explicit_iv_len;
2828 rec->data_len -= explicit_iv_len + transform->taglen;
2829
Hanno Beckercab87e62019-04-29 13:52:53 +01002830 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002831 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002832 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002833
Hanno Beckerd96a6522019-07-10 13:55:25 +01002834 /* Because of the check above, we know that there are
2835 * explicit_iv_len Bytes preceeding data, and taglen
2836 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002837 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002838 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002839
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002840 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002841 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002842 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002843
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002844 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002845 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002846 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002847 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2848 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002849 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002850 data, rec->data_len,
2851 data, &olen,
2852 data + rec->data_len,
2853 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2858 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002859
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002860 return( ret );
2861 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002862 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002863
Hanno Beckerd96a6522019-07-10 13:55:25 +01002864 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002865 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2868 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002869 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002870 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002871 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2873#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002874 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002876 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002877 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002878
Paul Bakker5121ce52009-01-03 21:22:43 +00002879 /*
Paul Bakker45829992013-01-03 14:52:21 +01002880 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002883 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2884 {
2885 /* The ciphertext is prefixed with the CBC IV. */
2886 minlen += transform->ivlen;
2887 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002888#endif
Paul Bakker45829992013-01-03 14:52:21 +01002889
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002890 /* Size considerations:
2891 *
2892 * - The CBC cipher text must not be empty and hence
2893 * at least of size transform->ivlen.
2894 *
2895 * Together with the potential IV-prefix, this explains
2896 * the first of the two checks below.
2897 *
2898 * - The record must contain a MAC, either in plain or
2899 * encrypted, depending on whether Encrypt-then-MAC
2900 * is used or not.
2901 * - If it is, the message contains the IV-prefix,
2902 * the CBC ciphertext, and the MAC.
2903 * - If it is not, the padded plaintext, and hence
2904 * the CBC ciphertext, has at least length maclen + 1
2905 * because there is at least the padding length byte.
2906 *
2907 * As the CBC ciphertext is not empty, both cases give the
2908 * lower bound minlen + maclen + 1 on the record size, which
2909 * we test for in the second check below.
2910 */
2911 if( rec->data_len < minlen + transform->ivlen ||
2912 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002915 "+ 1 ) ( + expl IV )", rec->data_len,
2916 transform->ivlen,
2917 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002919 }
2920
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002921 /*
2922 * Authenticate before decrypt if enabled
2923 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002925 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002926 {
Hanno Becker992b6872017-11-09 18:57:39 +00002927 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002930
Hanno Beckerd96a6522019-07-10 13:55:25 +01002931 /* Update data_len in tandem with add_data.
2932 *
2933 * The subtraction is safe because of the previous check
2934 * data_len >= minlen + maclen + 1.
2935 *
2936 * Afterwards, we know that data + data_len is followed by at
2937 * least maclen Bytes, which justifies the call to
2938 * mbedtls_ssl_safer_memcmp() below.
2939 *
2940 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002941 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002942 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002943
Hanno Beckerd96a6522019-07-10 13:55:25 +01002944 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002945 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2946 add_data_len );
2947 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2948 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002949 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2950 data, rec->data_len );
2951 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2952 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002953
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002954 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2955 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002956 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002957 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002958
Hanno Beckerd96a6522019-07-10 13:55:25 +01002959 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002960 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2961 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002965 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002966 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002967 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002969
2970 /*
2971 * Check length sanity
2972 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002973
2974 /* We know from above that data_len > minlen >= 0,
2975 * so the following check in particular implies that
2976 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002977 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002980 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002982 }
2983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002985 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002986 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002987 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002988 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002989 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002990 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002991 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002992
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002993 data += transform->ivlen;
2994 rec->data_offset += transform->ivlen;
2995 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002996 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002998
Hanno Beckerd96a6522019-07-10 13:55:25 +01002999 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3000
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003001 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3002 transform->iv_dec, transform->ivlen,
3003 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003006 return( ret );
3007 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003008
Hanno Beckerd96a6522019-07-10 13:55:25 +01003009 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003010 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3013 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003014 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003017 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003018 {
3019 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01003020 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3021 * records is equivalent to CBC decryption of the concatenation
3022 * of the records; in other words, IVs are maintained across
3023 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003024 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003025 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3026 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003027 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003028#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003029
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003030 /* Safe since data_len >= minlen + maclen + 1, so after having
3031 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01003032 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3033 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003034 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003035
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003036 if( auth_done == 1 )
3037 {
3038 correct *= ( rec->data_len >= padlen + 1 );
3039 padlen *= ( rec->data_len >= padlen + 1 );
3040 }
3041 else
Paul Bakker45829992013-01-03 14:52:21 +01003042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003044 if( rec->data_len < transform->maclen + padlen + 1 )
3045 {
3046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3047 rec->data_len,
3048 transform->maclen,
3049 padlen + 1 ) );
3050 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003051#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003052
3053 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3054 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003055 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003056
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003057 padlen++;
3058
3059 /* Regardless of the validity of the padding,
3060 * we have data_len >= padlen here. */
3061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003063 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003064 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003065 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067#if defined(MBEDTLS_SSL_DEBUG_ALL)
3068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003069 "should be no more than %d",
3070 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003071#endif
Paul Bakker45829992013-01-03 14:52:21 +01003072 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003073 }
3074 }
3075 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3077#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3078 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003079 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003080 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003081 /* The padding check involves a series of up to 256
3082 * consecutive memory reads at the end of the record
3083 * plaintext buffer. In order to hide the length and
3084 * validity of the padding, always perform exactly
3085 * `min(256,plaintext_len)` reads (but take into account
3086 * only the last `padlen` bytes for the padding check). */
3087 size_t pad_count = 0;
3088 size_t real_count = 0;
3089 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003090
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003091 /* Index of first padding byte; it has been ensured above
3092 * that the subtraction is safe. */
3093 size_t const padding_idx = rec->data_len - padlen;
3094 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3095 size_t const start_idx = rec->data_len - num_checks;
3096 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003097
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003098 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003099 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003100 real_count |= ( idx >= padding_idx );
3101 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003102 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003103 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003106 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003108#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003109 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003110 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003111 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3113 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3116 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003117 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003118
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003119 /* If the padding was found to be invalid, padlen == 0
3120 * and the subtraction is safe. If the padding was found valid,
3121 * padlen hasn't been changed and the previous assertion
3122 * data_len >= padlen still holds. */
3123 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003124 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003125 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003127 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3130 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003131 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003132
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003133#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003135 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003136#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003137
3138 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003139 * Authenticate if not done yet.
3140 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003141 */
Hanno Becker52344c22018-01-03 15:24:20 +00003142#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003143 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003144 {
Hanno Becker992b6872017-11-09 18:57:39 +00003145 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003146
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003147 /* If the initial value of padlen was such that
3148 * data_len < maclen + padlen + 1, then padlen
3149 * got reset to 1, and the initial check
3150 * data_len >= minlen + maclen + 1
3151 * guarantees that at this point we still
3152 * have at least data_len >= maclen.
3153 *
3154 * If the initial value of padlen was such that
3155 * data_len >= maclen + padlen + 1, then we have
3156 * subtracted either padlen + 1 (if the padding was correct)
3157 * or 0 (if the padding was incorrect) since then,
3158 * hence data_len >= maclen in any case.
3159 */
3160 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003161 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003164 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003165 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003166 ssl_mac( &transform->md_ctx_dec,
3167 transform->mac_dec,
3168 data, rec->data_len,
3169 rec->ctr, rec->type,
3170 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003171 }
3172 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3174#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3175 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003176 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003177 {
3178 /*
3179 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003180 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003181 *
3182 * Known timing attacks:
3183 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3184 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003185 * To compensate for different timings for the MAC calculation
3186 * depending on how much padding was removed (which is determined
3187 * by padlen), process extra_run more blocks through the hash
3188 * function.
3189 *
3190 * The formula in the paper is
3191 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3192 * where L1 is the size of the header plus the decrypted message
3193 * plus CBC padding and L2 is the size of the header plus the
3194 * decrypted message. This is for an underlying hash function
3195 * with 64-byte blocks.
3196 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3197 * correctly. We round down instead of up, so -56 is the correct
3198 * value for our calculations instead of -55.
3199 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003200 * Repeat the formula rather than defining a block_size variable.
3201 * This avoids requiring division by a variable at runtime
3202 * (which would be marginally less efficient and would require
3203 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003204 */
3205 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003206 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003207
3208 /*
3209 * The next two sizes are the minimum and maximum values of
3210 * in_msglen over all padlen values.
3211 *
3212 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003213 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003214 *
3215 * Note that max_len + maclen is never more than the buffer
3216 * length, as we previously did in_msglen -= maclen too.
3217 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003218 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003219 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3220
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003221 memset( tmp, 0, sizeof( tmp ) );
3222
3223 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003224 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003225#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3226 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003227 case MBEDTLS_MD_MD5:
3228 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003229 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003230 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003231 extra_run =
3232 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3233 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003234 break;
3235#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003236#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003237 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003238 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003239 extra_run =
3240 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3241 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003242 break;
3243#endif
3244 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003246 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3247 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003248
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003249 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003250
Hanno Beckercab87e62019-04-29 13:52:53 +01003251 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3252 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003253 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3254 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003255 /* Make sure we access everything even when padlen > 0. This
3256 * makes the synchronisation requirements for just-in-time
3257 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003258 ssl_read_memory( data + rec->data_len, padlen );
3259 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003260
3261 /* Call mbedtls_md_process at least once due to cache attacks
3262 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003263 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003264 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003265
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003266 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003267
3268 /* Make sure we access all the memory that could contain the MAC,
3269 * before we check it in the next code block. This makes the
3270 * synchronisation requirements for just-in-time Prime+Probe
3271 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003272 ssl_read_memory( data + min_len,
3273 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003274 }
3275 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3277 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3280 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003281 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003282
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003283#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003284 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3285 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003286#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003287
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003288 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3289 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291#if defined(MBEDTLS_SSL_DEBUG_ALL)
3292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003293#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003294 correct = 0;
3295 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003296 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003297 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003298
3299 /*
3300 * Finally check the correct flag
3301 */
3302 if( correct == 0 )
3303 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003304#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003305
3306 /* Make extra sure authentication was performed, exactly once */
3307 if( auth_done != 1 )
3308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3310 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003311 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003312
Hanno Beckera0e20d02019-05-15 14:03:01 +01003313#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003314 if( rec->cid_len != 0 )
3315 {
3316 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3317 &rec->type );
3318 if( ret != 0 )
3319 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3320 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003321#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003324
3325 return( 0 );
3326}
3327
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003328#undef MAC_NONE
3329#undef MAC_PLAINTEXT
3330#undef MAC_CIPHERTEXT
3331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003333/*
3334 * Compression/decompression functions
3335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003337{
3338 int ret;
3339 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003340 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003341 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003342 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003345
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003346 if( len_pre == 0 )
3347 return( 0 );
3348
Paul Bakker2770fbd2012-07-03 13:30:23 +00003349 memcpy( msg_pre, ssl->out_msg, len_pre );
3350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003352 ssl->out_msglen ) );
3353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003355 ssl->out_msg, ssl->out_msglen );
3356
Paul Bakker48916f92012-09-16 19:57:18 +00003357 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3358 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3359 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003360 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003361
Paul Bakker48916f92012-09-16 19:57:18 +00003362 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003363 if( ret != Z_OK )
3364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3366 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003367 }
3368
Angus Grattond8213d02016-05-25 20:56:48 +10003369 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003370 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003373 ssl->out_msglen ) );
3374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003376 ssl->out_msg, ssl->out_msglen );
3377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003379
3380 return( 0 );
3381}
3382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003384{
3385 int ret;
3386 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003387 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003388 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003389 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003392
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003393 if( len_pre == 0 )
3394 return( 0 );
3395
Paul Bakker2770fbd2012-07-03 13:30:23 +00003396 memcpy( msg_pre, ssl->in_msg, len_pre );
3397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003399 ssl->in_msglen ) );
3400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003402 ssl->in_msg, ssl->in_msglen );
3403
Paul Bakker48916f92012-09-16 19:57:18 +00003404 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3405 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3406 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003407 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003408 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003409
Paul Bakker48916f92012-09-16 19:57:18 +00003410 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003411 if( ret != Z_OK )
3412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3414 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003415 }
3416
Angus Grattond8213d02016-05-25 20:56:48 +10003417 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003418 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003421 ssl->in_msglen ) );
3422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003424 ssl->in_msg, ssl->in_msglen );
3425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003427
3428 return( 0 );
3429}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3433static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435#if defined(MBEDTLS_SSL_PROTO_DTLS)
3436static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003437{
3438 /* If renegotiation is not enforced, retransmit until we would reach max
3439 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003440 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003441 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003442 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003443 unsigned char doublings = 1;
3444
3445 while( ratio != 0 )
3446 {
3447 ++doublings;
3448 ratio >>= 1;
3449 }
3450
3451 if( ++ssl->renego_records_seen > doublings )
3452 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003454 return( 0 );
3455 }
3456 }
3457
3458 return( ssl_write_hello_request( ssl ) );
3459}
3460#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003462
Paul Bakker5121ce52009-01-03 21:22:43 +00003463/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003464 * Fill the input message buffer by appending data to it.
3465 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003466 *
3467 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3468 * available (from this read and/or a previous one). Otherwise, an error code
3469 * is returned (possibly EOF or WANT_READ).
3470 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003471 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3472 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3473 * since we always read a whole datagram at once.
3474 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003475 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003476 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003479{
Paul Bakker23986e52011-04-24 08:57:21 +00003480 int ret;
3481 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003484
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003485 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003488 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003490 }
3491
Angus Grattond8213d02016-05-25 20:56:48 +10003492 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3495 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003496 }
3497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003499 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003500 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003501 uint32_t timeout;
3502
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003503 /* Just to be sure */
3504 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3505 {
3506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3507 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3508 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3509 }
3510
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003511 /*
3512 * The point is, we need to always read a full datagram at once, so we
3513 * sometimes read more then requested, and handle the additional data.
3514 * It could be the rest of the current record (while fetching the
3515 * header) and/or some other records in the same datagram.
3516 */
3517
3518 /*
3519 * Move to the next record in the already read datagram if applicable
3520 */
3521 if( ssl->next_record_offset != 0 )
3522 {
3523 if( ssl->in_left < ssl->next_record_offset )
3524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3526 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003527 }
3528
3529 ssl->in_left -= ssl->next_record_offset;
3530
3531 if( ssl->in_left != 0 )
3532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003534 ssl->next_record_offset ) );
3535 memmove( ssl->in_hdr,
3536 ssl->in_hdr + ssl->next_record_offset,
3537 ssl->in_left );
3538 }
3539
3540 ssl->next_record_offset = 0;
3541 }
3542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003544 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003545
3546 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003547 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003548 */
3549 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003552 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003553 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003554
3555 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003556 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003557 * are not at the beginning of a new record, the caller did something
3558 * wrong.
3559 */
3560 if( ssl->in_left != 0 )
3561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003564 }
3565
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003566 /*
3567 * Don't even try to read if time's out already.
3568 * This avoids by-passing the timer when repeatedly receiving messages
3569 * that will end up being dropped.
3570 */
3571 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003572 {
3573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003574 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003575 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003576 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003577 {
Angus Grattond8213d02016-05-25 20:56:48 +10003578 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003581 timeout = ssl->handshake->retransmit_timeout;
3582 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003583 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003586
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003587 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003588 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3589 timeout );
3590 else
3591 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003593 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003594
3595 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003597 }
3598
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003599 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003602 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003605 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003606 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003609 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003610 }
3611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003615 return( ret );
3616 }
3617
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003618 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003619 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003621 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003623 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003624 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003627 return( ret );
3628 }
3629
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003630 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003631 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003633 }
3634
Paul Bakker5121ce52009-01-03 21:22:43 +00003635 if( ret < 0 )
3636 return( ret );
3637
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003638 ssl->in_left = ret;
3639 }
3640 else
3641#endif
3642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003644 ssl->in_left, nb_want ) );
3645
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003646 while( ssl->in_left < nb_want )
3647 {
3648 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003649
3650 if( ssl_check_timer( ssl ) != 0 )
3651 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3652 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003653 {
3654 if( ssl->f_recv_timeout != NULL )
3655 {
3656 ret = ssl->f_recv_timeout( ssl->p_bio,
3657 ssl->in_hdr + ssl->in_left, len,
3658 ssl->conf->read_timeout );
3659 }
3660 else
3661 {
3662 ret = ssl->f_recv( ssl->p_bio,
3663 ssl->in_hdr + ssl->in_left, len );
3664 }
3665 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003668 ssl->in_left, nb_want ) );
3669 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003670
3671 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003673
3674 if( ret < 0 )
3675 return( ret );
3676
mohammad160352aecb92018-03-28 23:41:40 -07003677 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003678 {
Darryl Green11999bb2018-03-13 15:22:58 +00003679 MBEDTLS_SSL_DEBUG_MSG( 1,
3680 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003681 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003682 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3683 }
3684
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003685 ssl->in_left += ret;
3686 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003687 }
3688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003690
3691 return( 0 );
3692}
3693
3694/*
3695 * Flush any data not yet written
3696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003697int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003698{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003699 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003700 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003703
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003704 if( ssl->f_send == NULL )
3705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003707 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003709 }
3710
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003711 /* Avoid incrementing counter if data is flushed */
3712 if( ssl->out_left == 0 )
3713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003715 return( 0 );
3716 }
3717
Paul Bakker5121ce52009-01-03 21:22:43 +00003718 while( ssl->out_left > 0 )
3719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003721 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003722
Hanno Becker2b1e3542018-08-06 11:19:13 +01003723 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003724 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003727
3728 if( ret <= 0 )
3729 return( ret );
3730
mohammad160352aecb92018-03-28 23:41:40 -07003731 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003732 {
Darryl Green11999bb2018-03-13 15:22:58 +00003733 MBEDTLS_SSL_DEBUG_MSG( 1,
3734 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003735 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003736 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3737 }
3738
Paul Bakker5121ce52009-01-03 21:22:43 +00003739 ssl->out_left -= ret;
3740 }
3741
Hanno Becker2b1e3542018-08-06 11:19:13 +01003742#if defined(MBEDTLS_SSL_PROTO_DTLS)
3743 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003744 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003745 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003746 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003747 else
3748#endif
3749 {
3750 ssl->out_hdr = ssl->out_buf + 8;
3751 }
3752 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003755
3756 return( 0 );
3757}
3758
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003759/*
3760 * Functions to handle the DTLS retransmission state machine
3761 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003762#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003763/*
3764 * Append current handshake message to current outgoing flight
3765 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003767{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003768 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3770 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3771 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003772
3773 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003774 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003775 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003778 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003779 }
3780
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003781 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003782 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003784 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003785 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003786 }
3787
3788 /* Copy current handshake message with headers */
3789 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3790 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003791 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003792 msg->next = NULL;
3793
3794 /* Append to the current flight */
3795 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003796 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003797 else
3798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003799 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003800 while( cur->next != NULL )
3801 cur = cur->next;
3802 cur->next = msg;
3803 }
3804
Hanno Becker3b235902018-08-06 09:54:53 +01003805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003806 return( 0 );
3807}
3808
3809/*
3810 * Free the current flight of handshake messages
3811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003812static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003813{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003814 mbedtls_ssl_flight_item *cur = flight;
3815 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003816
3817 while( cur != NULL )
3818 {
3819 next = cur->next;
3820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003821 mbedtls_free( cur->p );
3822 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003823
3824 cur = next;
3825 }
3826}
3827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3829static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003830#endif
3831
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003832/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003833 * Swap transform_out and out_ctr with the alternative ones
3834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003836{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003838 unsigned char tmp_out_ctr[8];
3839
3840 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003843 return;
3844 }
3845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003847
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003848 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003849 tmp_transform = ssl->transform_out;
3850 ssl->transform_out = ssl->handshake->alt_transform_out;
3851 ssl->handshake->alt_transform_out = tmp_transform;
3852
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003853 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003854 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3855 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003856 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003857
3858 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003859 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3862 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003864 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3867 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003868 }
3869 }
3870#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003871}
3872
3873/*
3874 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003875 */
3876int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3877{
3878 int ret = 0;
3879
3880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3881
3882 ret = mbedtls_ssl_flight_transmit( ssl );
3883
3884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3885
3886 return( ret );
3887}
3888
3889/*
3890 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003891 *
3892 * Need to remember the current message in case flush_output returns
3893 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003894 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003895 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003896int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003897{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003898 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003901 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003902 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003904
3905 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003906 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003907 ssl_swap_epochs( ssl );
3908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003909 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003910 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003911
3912 while( ssl->handshake->cur_msg != NULL )
3913 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003914 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003915 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003916
Hanno Beckere1dcb032018-08-17 16:47:58 +01003917 int const is_finished =
3918 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3919 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3920
Hanno Becker04da1892018-08-14 13:22:10 +01003921 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3922 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3923
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003924 /* Swap epochs before sending Finished: we can't do it after
3925 * sending ChangeCipherSpec, in case write returns WANT_READ.
3926 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003927 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003928 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003930 ssl_swap_epochs( ssl );
3931 }
3932
Hanno Becker67bc7c32018-08-06 11:33:50 +01003933 ret = ssl_get_remaining_payload_in_datagram( ssl );
3934 if( ret < 0 )
3935 return( ret );
3936 max_frag_len = (size_t) ret;
3937
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003938 /* CCS is copied as is, while HS messages may need fragmentation */
3939 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3940 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003941 if( max_frag_len == 0 )
3942 {
3943 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3944 return( ret );
3945
3946 continue;
3947 }
3948
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003949 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003950 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003951 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003952
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003953 /* Update position inside current message */
3954 ssl->handshake->cur_msg_p += cur->len;
3955 }
3956 else
3957 {
3958 const unsigned char * const p = ssl->handshake->cur_msg_p;
3959 const size_t hs_len = cur->len - 12;
3960 const size_t frag_off = p - ( cur->p + 12 );
3961 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003962 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003963
Hanno Beckere1dcb032018-08-17 16:47:58 +01003964 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003965 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003966 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003967 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003968
Hanno Becker67bc7c32018-08-06 11:33:50 +01003969 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3970 return( ret );
3971
3972 continue;
3973 }
3974 max_hs_frag_len = max_frag_len - 12;
3975
3976 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3977 max_hs_frag_len : rem_len;
3978
3979 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003980 {
3981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003982 (unsigned) cur_hs_frag_len,
3983 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003984 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003985
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003986 /* Messages are stored with handshake headers as if not fragmented,
3987 * copy beginning of headers then fill fragmentation fields.
3988 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3989 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003990
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003991 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3992 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3993 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3994
Hanno Becker67bc7c32018-08-06 11:33:50 +01003995 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3996 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3997 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003998
3999 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4000
Hanno Becker3f7b9732018-08-28 09:53:25 +01004001 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004002 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4003 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004004 ssl->out_msgtype = cur->type;
4005
4006 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004007 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004008 }
4009
4010 /* If done with the current message move to the next one if any */
4011 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4012 {
4013 if( cur->next != NULL )
4014 {
4015 ssl->handshake->cur_msg = cur->next;
4016 ssl->handshake->cur_msg_p = cur->next->p + 12;
4017 }
4018 else
4019 {
4020 ssl->handshake->cur_msg = NULL;
4021 ssl->handshake->cur_msg_p = NULL;
4022 }
4023 }
4024
4025 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004026 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004029 return( ret );
4030 }
4031 }
4032
Hanno Becker67bc7c32018-08-06 11:33:50 +01004033 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4034 return( ret );
4035
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004036 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4038 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004039 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004041 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004042 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4043 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004044
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004046
4047 return( 0 );
4048}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004049
4050/*
4051 * To be called when the last message of an incoming flight is received.
4052 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004053void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004054{
4055 /* We won't need to resend that one any more */
4056 ssl_flight_free( ssl->handshake->flight );
4057 ssl->handshake->flight = NULL;
4058 ssl->handshake->cur_msg = NULL;
4059
4060 /* The next incoming flight will start with this msg_seq */
4061 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4062
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004063 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004064 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004065
Hanno Becker0271f962018-08-16 13:23:47 +01004066 /* Clear future message buffering structure. */
4067 ssl_buffering_free( ssl );
4068
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004069 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004070 ssl_set_timer( ssl, 0 );
4071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004072 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4073 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004075 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004076 }
4077 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004079}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004080
4081/*
4082 * To be called when the last message of an outgoing flight is send.
4083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004084void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004085{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004086 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004087 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4090 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004092 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004093 }
4094 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004096}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004097#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004098
Paul Bakker5121ce52009-01-03 21:22:43 +00004099/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004100 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004101 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004102
4103/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004104 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004105 *
4106 * - fill in handshake headers
4107 * - update handshake checksum
4108 * - DTLS: save message for resending
4109 * - then pass to the record layer
4110 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004111 * DTLS: except for HelloRequest, messages are only queued, and will only be
4112 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004113 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004114 * Inputs:
4115 * - ssl->out_msglen: 4 + actual handshake message len
4116 * (4 is the size of handshake headers for TLS)
4117 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4118 * - ssl->out_msg + 4: the handshake message body
4119 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004120 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004121 * - ssl->out_msglen: the length of the record contents
4122 * (including handshake headers but excluding record headers)
4123 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004124 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004125int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004126{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004127 int ret;
4128 const size_t hs_len = ssl->out_msglen - 4;
4129 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004130
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4132
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004133 /*
4134 * Sanity checks
4135 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004136 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004137 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4138 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004139 /* In SSLv3, the client might send a NoCertificate alert. */
4140#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4141 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4142 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4143 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4144#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4145 {
4146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4147 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4148 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004149 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004150
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004151 /* Whenever we send anything different from a
4152 * HelloRequest we should be in a handshake - double check. */
4153 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4154 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004155 ssl->handshake == NULL )
4156 {
4157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4158 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4159 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004161#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004162 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004163 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004165 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4167 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004168 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004169#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004170
Hanno Beckerb50a2532018-08-06 11:52:54 +01004171 /* Double-check that we did not exceed the bounds
4172 * of the outgoing record buffer.
4173 * This should never fail as the various message
4174 * writing functions must obey the bounds of the
4175 * outgoing record buffer, but better be safe.
4176 *
4177 * Note: We deliberately do not check for the MTU or MFL here.
4178 */
4179 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4180 {
4181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4182 "size %u, maximum %u",
4183 (unsigned) ssl->out_msglen,
4184 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4185 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4186 }
4187
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004188 /*
4189 * Fill handshake headers
4190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004192 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004193 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4194 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4195 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004196
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004197 /*
4198 * DTLS has additional fields in the Handshake layer,
4199 * between the length field and the actual payload:
4200 * uint16 message_seq;
4201 * uint24 fragment_offset;
4202 * uint24 fragment_length;
4203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004205 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004206 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004207 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004208 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004209 {
4210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4211 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004212 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004213 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004214 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4215 }
4216
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004217 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004218 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004219
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004220 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004221 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004222 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004223 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4224 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4225 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004226 }
4227 else
4228 {
4229 ssl->out_msg[4] = 0;
4230 ssl->out_msg[5] = 0;
4231 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004232
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004233 /* Handshake hashes are computed without fragmentation,
4234 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004235 memset( ssl->out_msg + 6, 0x00, 3 );
4236 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004237 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004238#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004239
Hanno Becker0207e532018-08-28 10:28:28 +01004240 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004241 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4242 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004243 }
4244
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004245 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004246#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004247 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004248 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4249 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004250 {
4251 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004254 return( ret );
4255 }
4256 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004257 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004258#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004259 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004260 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004261 {
4262 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4263 return( ret );
4264 }
4265 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004266
4267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4268
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004269 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004270}
4271
4272/*
4273 * Record layer functions
4274 */
4275
4276/*
4277 * Write current record.
4278 *
4279 * Uses:
4280 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4281 * - ssl->out_msglen: length of the record content (excl headers)
4282 * - ssl->out_msg: record content
4283 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004284int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004285{
4286 int ret, done = 0;
4287 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004288 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004289
4290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004292#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004293 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004294 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004295 {
4296 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004299 return( ret );
4300 }
4301
4302 len = ssl->out_msglen;
4303 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004304#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004306#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4307 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311 ret = mbedtls_ssl_hw_record_write( ssl );
4312 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4315 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004316 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004317
4318 if( ret == 0 )
4319 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004320 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004321#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004322 if( !done )
4323 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004324 unsigned i;
4325 size_t protected_record_size;
4326
Hanno Becker6430faf2019-05-08 11:57:13 +01004327 /* Skip writing the record content type to after the encryption,
4328 * as it may change when using the CID extension. */
4329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004330 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004331 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004332
Hanno Becker19859472018-08-06 09:40:20 +01004333 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004334 ssl->out_len[0] = (unsigned char)( len >> 8 );
4335 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004336
Paul Bakker48916f92012-09-16 19:57:18 +00004337 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004338 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004339 mbedtls_record rec;
4340
4341 rec.buf = ssl->out_iv;
4342 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4343 ( ssl->out_iv - ssl->out_buf );
4344 rec.data_len = ssl->out_msglen;
4345 rec.data_offset = ssl->out_msg - rec.buf;
4346
4347 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4348 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4349 ssl->conf->transport, rec.ver );
4350 rec.type = ssl->out_msgtype;
4351
Hanno Beckera0e20d02019-05-15 14:03:01 +01004352#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004353 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004354 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004355#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004356
Hanno Beckera18d1322018-01-03 14:27:32 +00004357 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004358 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004360 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004361 return( ret );
4362 }
4363
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004364 if( rec.data_offset != 0 )
4365 {
4366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4367 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4368 }
4369
Hanno Becker6430faf2019-05-08 11:57:13 +01004370 /* Update the record content type and CID. */
4371 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004372#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004373 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004374#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004375 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004376 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4377 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004378 }
4379
Hanno Becker5903de42019-05-03 14:46:38 +01004380 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004381
4382#if defined(MBEDTLS_SSL_PROTO_DTLS)
4383 /* In case of DTLS, double-check that we don't exceed
4384 * the remaining space in the datagram. */
4385 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4386 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004387 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004388 if( ret < 0 )
4389 return( ret );
4390
4391 if( protected_record_size > (size_t) ret )
4392 {
4393 /* Should never happen */
4394 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4395 }
4396 }
4397#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004398
Hanno Becker6430faf2019-05-08 11:57:13 +01004399 /* Now write the potentially updated record content type. */
4400 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004402 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004403 "version = [%d:%d], msglen = %d",
4404 ssl->out_hdr[0], ssl->out_hdr[1],
4405 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004407 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004408 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004409
4410 ssl->out_left += protected_record_size;
4411 ssl->out_hdr += protected_record_size;
4412 ssl_update_out_pointers( ssl, ssl->transform_out );
4413
Hanno Becker04484622018-08-06 09:49:38 +01004414 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4415 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4416 break;
4417
4418 /* The loop goes to its end iff the counter is wrapping */
4419 if( i == ssl_ep_len( ssl ) )
4420 {
4421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4422 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4423 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004424 }
4425
Hanno Becker67bc7c32018-08-06 11:33:50 +01004426#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004427 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4428 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004429 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004430 size_t remaining;
4431 ret = ssl_get_remaining_payload_in_datagram( ssl );
4432 if( ret < 0 )
4433 {
4434 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4435 ret );
4436 return( ret );
4437 }
4438
4439 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004440 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004441 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004442 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004443 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004444 else
4445 {
Hanno Becker513815a2018-08-20 11:56:09 +01004446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004447 }
4448 }
4449#endif /* MBEDTLS_SSL_PROTO_DTLS */
4450
4451 if( ( flush == SSL_FORCE_FLUSH ) &&
4452 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004455 return( ret );
4456 }
4457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004459
4460 return( 0 );
4461}
4462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004463#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004464
4465static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4466{
4467 if( ssl->in_msglen < ssl->in_hslen ||
4468 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4469 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4470 {
4471 return( 1 );
4472 }
4473 return( 0 );
4474}
Hanno Becker44650b72018-08-16 12:51:11 +01004475
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004476static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004477{
4478 return( ( ssl->in_msg[9] << 16 ) |
4479 ( ssl->in_msg[10] << 8 ) |
4480 ssl->in_msg[11] );
4481}
4482
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004483static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004484{
4485 return( ( ssl->in_msg[6] << 16 ) |
4486 ( ssl->in_msg[7] << 8 ) |
4487 ssl->in_msg[8] );
4488}
4489
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004490static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004491{
4492 uint32_t msg_len, frag_off, frag_len;
4493
4494 msg_len = ssl_get_hs_total_len( ssl );
4495 frag_off = ssl_get_hs_frag_off( ssl );
4496 frag_len = ssl_get_hs_frag_len( ssl );
4497
4498 if( frag_off > msg_len )
4499 return( -1 );
4500
4501 if( frag_len > msg_len - frag_off )
4502 return( -1 );
4503
4504 if( frag_len + 12 > ssl->in_msglen )
4505 return( -1 );
4506
4507 return( 0 );
4508}
4509
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004510/*
4511 * Mark bits in bitmask (used for DTLS HS reassembly)
4512 */
4513static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4514{
4515 unsigned int start_bits, end_bits;
4516
4517 start_bits = 8 - ( offset % 8 );
4518 if( start_bits != 8 )
4519 {
4520 size_t first_byte_idx = offset / 8;
4521
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004522 /* Special case */
4523 if( len <= start_bits )
4524 {
4525 for( ; len != 0; len-- )
4526 mask[first_byte_idx] |= 1 << ( start_bits - len );
4527
4528 /* Avoid potential issues with offset or len becoming invalid */
4529 return;
4530 }
4531
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004532 offset += start_bits; /* Now offset % 8 == 0 */
4533 len -= start_bits;
4534
4535 for( ; start_bits != 0; start_bits-- )
4536 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4537 }
4538
4539 end_bits = len % 8;
4540 if( end_bits != 0 )
4541 {
4542 size_t last_byte_idx = ( offset + len ) / 8;
4543
4544 len -= end_bits; /* Now len % 8 == 0 */
4545
4546 for( ; end_bits != 0; end_bits-- )
4547 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4548 }
4549
4550 memset( mask + offset / 8, 0xFF, len / 8 );
4551}
4552
4553/*
4554 * Check that bitmask is full
4555 */
4556static int ssl_bitmask_check( unsigned char *mask, size_t len )
4557{
4558 size_t i;
4559
4560 for( i = 0; i < len / 8; i++ )
4561 if( mask[i] != 0xFF )
4562 return( -1 );
4563
4564 for( i = 0; i < len % 8; i++ )
4565 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4566 return( -1 );
4567
4568 return( 0 );
4569}
4570
Hanno Becker56e205e2018-08-16 09:06:12 +01004571/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004572static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004573 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004574{
Hanno Becker56e205e2018-08-16 09:06:12 +01004575 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004576
Hanno Becker56e205e2018-08-16 09:06:12 +01004577 alloc_len = 12; /* Handshake header */
4578 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004579
Hanno Beckerd07df862018-08-16 09:14:58 +01004580 if( add_bitmap )
4581 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004582
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004583 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004584}
Hanno Becker56e205e2018-08-16 09:06:12 +01004585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004586#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004587
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004588static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004589{
4590 return( ( ssl->in_msg[1] << 16 ) |
4591 ( ssl->in_msg[2] << 8 ) |
4592 ssl->in_msg[3] );
4593}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004594
Simon Butcher99000142016-10-13 17:21:01 +01004595int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004596{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004597 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004600 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004601 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004602 }
4603
Hanno Becker12555c62018-08-16 12:47:53 +01004604 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004606 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004607 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004608 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004610#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004611 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004612 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004613 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004614 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004615
Hanno Becker44650b72018-08-16 12:51:11 +01004616 if( ssl_check_hs_header( ssl ) != 0 )
4617 {
4618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4619 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4620 }
4621
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004622 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004623 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4624 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4625 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4626 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004627 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004628 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4629 {
4630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4631 recv_msg_seq,
4632 ssl->handshake->in_msg_seq ) );
4633 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4634 }
4635
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004636 /* Retransmit only on last message from previous flight, to avoid
4637 * too many retransmissions.
4638 * Besides, No sane server ever retransmits HelloVerifyRequest */
4639 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004640 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004643 "message_seq = %d, start_of_flight = %d",
4644 recv_msg_seq,
4645 ssl->handshake->in_flight_start_seq ) );
4646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004647 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004650 return( ret );
4651 }
4652 }
4653 else
4654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004656 "message_seq = %d, expected = %d",
4657 recv_msg_seq,
4658 ssl->handshake->in_msg_seq ) );
4659 }
4660
Hanno Becker90333da2017-10-10 11:27:13 +01004661 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004662 }
4663 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004664
Hanno Becker6d97ef52018-08-16 13:09:04 +01004665 /* Message reassembly is handled alongside buffering of future
4666 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004667 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004668 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004669 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004672 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004673 }
4674 }
4675 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004676#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004677 /* With TLS we don't handle fragmentation (for now) */
4678 if( ssl->in_msglen < ssl->in_hslen )
4679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4681 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004682 }
4683
Simon Butcher99000142016-10-13 17:21:01 +01004684 return( 0 );
4685}
4686
4687void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4688{
Hanno Becker0271f962018-08-16 13:23:47 +01004689 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004690
Hanno Becker0271f962018-08-16 13:23:47 +01004691 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004692 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004693 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004694 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004695
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004696 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004697#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004698 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004699 ssl->handshake != NULL )
4700 {
Hanno Becker0271f962018-08-16 13:23:47 +01004701 unsigned offset;
4702 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004703
Hanno Becker0271f962018-08-16 13:23:47 +01004704 /* Increment handshake sequence number */
4705 hs->in_msg_seq++;
4706
4707 /*
4708 * Clear up handshake buffering and reassembly structure.
4709 */
4710
4711 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004712 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004713
4714 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004715 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4716 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004717 offset++, hs_buf++ )
4718 {
4719 *hs_buf = *(hs_buf + 1);
4720 }
4721
4722 /* Create a fresh last entry */
4723 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004724 }
4725#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004726}
4727
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004728/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004729 * DTLS anti-replay: RFC 6347 4.1.2.6
4730 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004731 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4732 * Bit n is set iff record number in_window_top - n has been seen.
4733 *
4734 * Usually, in_window_top is the last record number seen and the lsb of
4735 * in_window is set. The only exception is the initial state (record number 0
4736 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004738#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4739static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004740{
4741 ssl->in_window_top = 0;
4742 ssl->in_window = 0;
4743}
4744
4745static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4746{
4747 return( ( (uint64_t) buf[0] << 40 ) |
4748 ( (uint64_t) buf[1] << 32 ) |
4749 ( (uint64_t) buf[2] << 24 ) |
4750 ( (uint64_t) buf[3] << 16 ) |
4751 ( (uint64_t) buf[4] << 8 ) |
4752 ( (uint64_t) buf[5] ) );
4753}
4754
4755/*
4756 * Return 0 if sequence number is acceptable, -1 otherwise
4757 */
Hanno Becker0183d692019-07-12 08:50:37 +01004758int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004759{
4760 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4761 uint64_t bit;
4762
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004763 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004764 return( 0 );
4765
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004766 if( rec_seqnum > ssl->in_window_top )
4767 return( 0 );
4768
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004769 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004770
4771 if( bit >= 64 )
4772 return( -1 );
4773
4774 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4775 return( -1 );
4776
4777 return( 0 );
4778}
4779
4780/*
4781 * Update replay window on new validated record
4782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004783void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004784{
4785 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4786
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004787 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004788 return;
4789
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004790 if( rec_seqnum > ssl->in_window_top )
4791 {
4792 /* Update window_top and the contents of the window */
4793 uint64_t shift = rec_seqnum - ssl->in_window_top;
4794
4795 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004796 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004797 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004798 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004799 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004800 ssl->in_window |= 1;
4801 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004802
4803 ssl->in_window_top = rec_seqnum;
4804 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004805 else
4806 {
4807 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004808 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004809
4810 if( bit < 64 ) /* Always true, but be extra sure */
4811 ssl->in_window |= (uint64_t) 1 << bit;
4812 }
4813}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004815
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004816#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004817/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004818static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4819
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004820/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004821 * Without any SSL context, check if a datagram looks like a ClientHello with
4822 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004823 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004824 *
4825 * - if cookie is valid, return 0
4826 * - if ClientHello looks superficially valid but cookie is not,
4827 * fill obuf and set olen, then
4828 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4829 * - otherwise return a specific error code
4830 */
4831static int ssl_check_dtls_clihlo_cookie(
4832 mbedtls_ssl_cookie_write_t *f_cookie_write,
4833 mbedtls_ssl_cookie_check_t *f_cookie_check,
4834 void *p_cookie,
4835 const unsigned char *cli_id, size_t cli_id_len,
4836 const unsigned char *in, size_t in_len,
4837 unsigned char *obuf, size_t buf_len, size_t *olen )
4838{
4839 size_t sid_len, cookie_len;
4840 unsigned char *p;
4841
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004842 /*
4843 * Structure of ClientHello with record and handshake headers,
4844 * and expected values. We don't need to check a lot, more checks will be
4845 * done when actually parsing the ClientHello - skipping those checks
4846 * avoids code duplication and does not make cookie forging any easier.
4847 *
4848 * 0-0 ContentType type; copied, must be handshake
4849 * 1-2 ProtocolVersion version; copied
4850 * 3-4 uint16 epoch; copied, must be 0
4851 * 5-10 uint48 sequence_number; copied
4852 * 11-12 uint16 length; (ignored)
4853 *
4854 * 13-13 HandshakeType msg_type; (ignored)
4855 * 14-16 uint24 length; (ignored)
4856 * 17-18 uint16 message_seq; copied
4857 * 19-21 uint24 fragment_offset; copied, must be 0
4858 * 22-24 uint24 fragment_length; (ignored)
4859 *
4860 * 25-26 ProtocolVersion client_version; (ignored)
4861 * 27-58 Random random; (ignored)
4862 * 59-xx SessionID session_id; 1 byte len + sid_len content
4863 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4864 * ...
4865 *
4866 * Minimum length is 61 bytes.
4867 */
4868 if( in_len < 61 ||
4869 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4870 in[3] != 0 || in[4] != 0 ||
4871 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4872 {
4873 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4874 }
4875
4876 sid_len = in[59];
4877 if( sid_len > in_len - 61 )
4878 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4879
4880 cookie_len = in[60 + sid_len];
4881 if( cookie_len > in_len - 60 )
4882 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4883
4884 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4885 cli_id, cli_id_len ) == 0 )
4886 {
4887 /* Valid cookie */
4888 return( 0 );
4889 }
4890
4891 /*
4892 * If we get here, we've got an invalid cookie, let's prepare HVR.
4893 *
4894 * 0-0 ContentType type; copied
4895 * 1-2 ProtocolVersion version; copied
4896 * 3-4 uint16 epoch; copied
4897 * 5-10 uint48 sequence_number; copied
4898 * 11-12 uint16 length; olen - 13
4899 *
4900 * 13-13 HandshakeType msg_type; hello_verify_request
4901 * 14-16 uint24 length; olen - 25
4902 * 17-18 uint16 message_seq; copied
4903 * 19-21 uint24 fragment_offset; copied
4904 * 22-24 uint24 fragment_length; olen - 25
4905 *
4906 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4907 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4908 *
4909 * Minimum length is 28.
4910 */
4911 if( buf_len < 28 )
4912 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4913
4914 /* Copy most fields and adapt others */
4915 memcpy( obuf, in, 25 );
4916 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4917 obuf[25] = 0xfe;
4918 obuf[26] = 0xff;
4919
4920 /* Generate and write actual cookie */
4921 p = obuf + 28;
4922 if( f_cookie_write( p_cookie,
4923 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4924 {
4925 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4926 }
4927
4928 *olen = p - obuf;
4929
4930 /* Go back and fill length fields */
4931 obuf[27] = (unsigned char)( *olen - 28 );
4932
4933 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4934 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4935 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4936
4937 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4938 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4939
4940 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4941}
4942
4943/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004944 * Handle possible client reconnect with the same UDP quadruplet
4945 * (RFC 6347 Section 4.2.8).
4946 *
4947 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4948 * that looks like a ClientHello.
4949 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004950 * - if the input looks like a ClientHello without cookies,
4951 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004952 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004953 * - if the input looks like a ClientHello with a valid cookie,
4954 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004955 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004956 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004957 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004958 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004959 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4960 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004961 */
4962static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4963{
4964 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004965 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004966
Hanno Becker2fddd372019-07-10 14:37:41 +01004967 if( ssl->conf->f_cookie_write == NULL ||
4968 ssl->conf->f_cookie_check == NULL )
4969 {
4970 /* If we can't use cookies to verify reachability of the peer,
4971 * drop the record. */
4972 return( 0 );
4973 }
4974
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004975 ret = ssl_check_dtls_clihlo_cookie(
4976 ssl->conf->f_cookie_write,
4977 ssl->conf->f_cookie_check,
4978 ssl->conf->p_cookie,
4979 ssl->cli_id, ssl->cli_id_len,
4980 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004981 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004982
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004983 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4984
4985 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004986 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004987 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004988 * If the error is permanent we'll catch it later,
4989 * if it's not, then hopefully it'll work next time. */
4990 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01004991 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004992 }
4993
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004994 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004995 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004996 /* Got a valid cookie, partially reset context */
4997 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4998 {
4999 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5000 return( ret );
5001 }
5002
5003 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005004 }
5005
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005006 return( ret );
5007}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005008#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005009
Hanno Beckerf661c9c2019-05-03 13:25:54 +01005010static int ssl_check_record_type( uint8_t record_type )
5011{
5012 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5013 record_type != MBEDTLS_SSL_MSG_ALERT &&
5014 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5015 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5016 {
5017 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5018 }
5019
5020 return( 0 );
5021}
5022
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005023/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005024 * ContentType type;
5025 * ProtocolVersion version;
5026 * uint16 epoch; // DTLS only
5027 * uint48 sequence_number; // DTLS only
5028 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005029 *
5030 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005031 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005032 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5033 *
5034 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005035 * 1. proceed with the record if this function returns 0
5036 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5037 * 3. return CLIENT_RECONNECT if this function return that value
5038 * 4. drop the whole datagram if this function returns anything else.
5039 * Point 2 is needed when the peer is resending, and we have already received
5040 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005041 */
Hanno Becker331de3d2019-07-12 11:10:16 +01005042static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01005043 unsigned char *buf,
5044 size_t len,
5045 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005046{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005047 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005048
Hanno Beckere5e7e782019-07-11 12:29:35 +01005049 size_t const rec_hdr_type_offset = 0;
5050 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005051
Hanno Beckere5e7e782019-07-11 12:29:35 +01005052 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5053 rec_hdr_type_len;
5054 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005055
Hanno Beckere5e7e782019-07-11 12:29:35 +01005056 size_t const rec_hdr_ctr_len = 8;
5057#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01005058 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005059 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5060 rec_hdr_version_len;
5061
Hanno Beckera0e20d02019-05-15 14:03:01 +01005062#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01005063 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5064 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01005065 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005066#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5067#endif /* MBEDTLS_SSL_PROTO_DTLS */
5068
5069 size_t rec_hdr_len_offset; /* To be determined */
5070 size_t const rec_hdr_len_len = 2;
5071
5072 /*
5073 * Check minimum lengths for record header.
5074 */
5075
5076#if defined(MBEDTLS_SSL_PROTO_DTLS)
5077 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5078 {
5079 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5080 }
5081 else
5082#endif /* MBEDTLS_SSL_PROTO_DTLS */
5083 {
5084 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5085 }
5086
5087 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5088 {
5089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5090 (unsigned) len,
5091 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5092 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5093 }
5094
5095 /*
5096 * Parse and validate record content type
5097 */
5098
5099 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005100
5101 /* Check record content type */
5102#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5103 rec->cid_len = 0;
5104
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005105 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005106 ssl->conf->cid_len != 0 &&
5107 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005108 {
5109 /* Shift pointers to account for record header including CID
5110 * struct {
5111 * ContentType special_type = tls12_cid;
5112 * ProtocolVersion version;
5113 * uint16 epoch;
5114 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005115 * opaque cid[cid_length]; // Additional field compared to
5116 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005117 * uint16 length;
5118 * opaque enc_content[DTLSCiphertext.length];
5119 * } DTLSCiphertext;
5120 */
5121
5122 /* So far, we only support static CID lengths
5123 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005124 rec_hdr_cid_len = ssl->conf->cid_len;
5125 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005126
Hanno Beckere5e7e782019-07-11 12:29:35 +01005127 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005128 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5130 (unsigned) len,
5131 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005132 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005133 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005134
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02005135 /* configured CID len is guaranteed at most 255, see
5136 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5137 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005138 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005139 }
5140 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005141#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005142 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005143 if( ssl_check_record_type( rec->type ) )
5144 {
Hanno Becker54229812019-07-12 14:40:00 +01005145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5146 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005147 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5148 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005149 }
5150
Hanno Beckere5e7e782019-07-11 12:29:35 +01005151 /*
5152 * Parse and validate record version
5153 */
5154
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005155 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5156 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005157 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5158 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005159 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005160
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005161 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5164 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005165 }
5166
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005167 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5170 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005171 }
5172
Hanno Beckere5e7e782019-07-11 12:29:35 +01005173 /*
5174 * Parse/Copy record sequence number.
5175 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005176
Hanno Beckere5e7e782019-07-11 12:29:35 +01005177#if defined(MBEDTLS_SSL_PROTO_DTLS)
5178 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005179 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005180 /* Copy explicit record sequence number from input buffer. */
5181 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5182 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005183 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005184 else
5185#endif /* MBEDTLS_SSL_PROTO_DTLS */
5186 {
5187 /* Copy implicit record sequence number from SSL context structure. */
5188 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5189 }
Paul Bakker40e46942009-01-03 21:51:57 +00005190
Hanno Beckere5e7e782019-07-11 12:29:35 +01005191 /*
5192 * Parse record length.
5193 */
5194
Hanno Beckere5e7e782019-07-11 12:29:35 +01005195 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005196 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5197 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005198 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00005199
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005200 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005201 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005202 rec->type,
5203 major_ver, minor_ver, rec->data_len ) );
5204
5205 rec->buf = buf;
5206 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005207
Hanno Beckerd417cc92019-07-26 08:20:27 +01005208 if( rec->data_len == 0 )
5209 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005210
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005211 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005212 * DTLS-related tests.
5213 * Check epoch before checking length constraint because
5214 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5215 * message gets duplicated before the corresponding Finished message,
5216 * the second ChangeCipherSpec should be discarded because it belongs
5217 * to an old epoch, but not because its length is shorter than
5218 * the minimum record length for packets using the new record transform.
5219 * Note that these two kinds of failures are handled differently,
5220 * as an unexpected record is silently skipped but an invalid
5221 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005222 */
5223#if defined(MBEDTLS_SSL_PROTO_DTLS)
5224 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5225 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005226 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005227
Hanno Becker955a5c92019-07-10 17:12:07 +01005228 /* Check that the datagram is large enough to contain a record
5229 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005230 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005231 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5233 (unsigned) len,
5234 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005235 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5236 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005237
Hanno Becker37cfe732019-07-10 17:20:01 +01005238 /* Records from other, non-matching epochs are silently discarded.
5239 * (The case of same-port Client reconnects must be considered in
5240 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005241 if( rec_epoch != ssl->in_epoch )
5242 {
5243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5244 "expected %d, received %d",
5245 ssl->in_epoch, rec_epoch ) );
5246
Hanno Becker552f7472019-07-19 10:59:12 +01005247 /* Records from the next epoch are considered for buffering
5248 * (concretely: early Finished messages). */
5249 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005250 {
Hanno Becker552f7472019-07-19 10:59:12 +01005251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5252 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005253 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005254
Hanno Becker2fddd372019-07-10 14:37:41 +01005255 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005256 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005257#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005258 /* For records from the correct epoch, check whether their
5259 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005260 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005261 {
5262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5263 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5264 }
5265#endif
5266 }
5267#endif /* MBEDTLS_SSL_PROTO_DTLS */
5268
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005269 return( 0 );
5270}
Paul Bakker5121ce52009-01-03 21:22:43 +00005271
Paul Bakker5121ce52009-01-03 21:22:43 +00005272
Hanno Becker2fddd372019-07-10 14:37:41 +01005273#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5274static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5275{
5276 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5277
5278 /*
5279 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5280 * access the first byte of record content (handshake type), as we
5281 * have an active transform (possibly iv_len != 0), so use the
5282 * fact that the record header len is 13 instead.
5283 */
5284 if( rec_epoch == 0 &&
5285 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5286 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5287 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5288 ssl->in_left > 13 &&
5289 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5290 {
5291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5292 "from the same port" ) );
5293 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005294 }
5295
5296 return( 0 );
5297}
Hanno Becker2fddd372019-07-10 14:37:41 +01005298#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005299
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005300/*
5301 * If applicable, decrypt (and decompress) record content
5302 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005303static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5304 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005305{
5306 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005308 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005309 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005311#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5312 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005316 ret = mbedtls_ssl_hw_record_read( ssl );
5317 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5320 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005321 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005322
5323 if( ret == 0 )
5324 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005325 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005326#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005327 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005328 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005329 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005330
Hanno Beckera18d1322018-01-03 14:27:32 +00005331 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005332 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005334 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005335
Hanno Beckera0e20d02019-05-15 14:03:01 +01005336#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005337 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5338 ssl->conf->ignore_unexpected_cid
5339 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5340 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005341 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005342 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005343 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005344#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005345
Paul Bakker5121ce52009-01-03 21:22:43 +00005346 return( ret );
5347 }
5348
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005349 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005350 {
5351 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005352 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005353 }
5354
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005355 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005356 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005357
Hanno Beckera0e20d02019-05-15 14:03:01 +01005358#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005359 /* We have already checked the record content type
5360 * in ssl_parse_record_header(), failing or silently
5361 * dropping the record in the case of an unknown type.
5362 *
5363 * Since with the use of CIDs, the record content type
5364 * might change during decryption, re-check the record
5365 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005366 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005367 {
5368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5369 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5370 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005371#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005372
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005373 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005374 {
5375#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5376 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005377 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005378 {
5379 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5381 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5382 }
5383#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5384
5385 ssl->nb_zero++;
5386
5387 /*
5388 * Three or more empty messages may be a DoS attack
5389 * (excessive CPU consumption).
5390 */
5391 if( ssl->nb_zero > 3 )
5392 {
5393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005394 "messages, possible DoS attack" ) );
5395 /* Treat the records as if they were not properly authenticated,
5396 * thereby failing the connection if we see more than allowed
5397 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005398 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5399 }
5400 }
5401 else
5402 ssl->nb_zero = 0;
5403
5404#if defined(MBEDTLS_SSL_PROTO_DTLS)
5405 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5406 {
5407 ; /* in_ctr read from peer, not maintained internally */
5408 }
5409 else
5410#endif
5411 {
5412 unsigned i;
5413 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5414 if( ++ssl->in_ctr[i - 1] != 0 )
5415 break;
5416
5417 /* The loop goes to its end iff the counter is wrapping */
5418 if( i == ssl_ep_len( ssl ) )
5419 {
5420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5421 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5422 }
5423 }
5424
Paul Bakker5121ce52009-01-03 21:22:43 +00005425 }
5426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005427#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005428 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005429 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005430 {
5431 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005433 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005434 return( ret );
5435 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005436 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005437#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005439#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005440 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005442 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005443 }
5444#endif
5445
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005446 /* Check actual (decrypted) record content length against
5447 * configured maximum. */
5448 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5449 {
5450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5451 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5452 }
5453
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005454 return( 0 );
5455}
5456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005457static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005458
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005459/*
5460 * Read a record.
5461 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005462 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5463 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5464 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005465 */
Hanno Becker1097b342018-08-15 14:09:41 +01005466
5467/* Helper functions for mbedtls_ssl_read_record(). */
5468static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005469static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5470static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005471
Hanno Becker327c93b2018-08-15 13:56:18 +01005472int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005473 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005474{
5475 int ret;
5476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005478
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005479 if( ssl->keep_current_message == 0 )
5480 {
5481 do {
Simon Butcher99000142016-10-13 17:21:01 +01005482
Hanno Becker26994592018-08-15 14:14:59 +01005483 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005484 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005485 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005486
Hanno Beckere74d5562018-08-15 14:26:08 +01005487 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005488 {
Hanno Becker40f50842018-08-15 14:48:01 +01005489#if defined(MBEDTLS_SSL_PROTO_DTLS)
5490 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005491
Hanno Becker40f50842018-08-15 14:48:01 +01005492 /* We only check for buffered messages if the
5493 * current datagram is fully consumed. */
5494 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005495 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005496 {
Hanno Becker40f50842018-08-15 14:48:01 +01005497 if( ssl_load_buffered_message( ssl ) == 0 )
5498 have_buffered = 1;
5499 }
5500
5501 if( have_buffered == 0 )
5502#endif /* MBEDTLS_SSL_PROTO_DTLS */
5503 {
5504 ret = ssl_get_next_record( ssl );
5505 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5506 continue;
5507
5508 if( ret != 0 )
5509 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005510 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005511 return( ret );
5512 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005513 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005514 }
5515
5516 ret = mbedtls_ssl_handle_message_type( ssl );
5517
Hanno Becker40f50842018-08-15 14:48:01 +01005518#if defined(MBEDTLS_SSL_PROTO_DTLS)
5519 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5520 {
5521 /* Buffer future message */
5522 ret = ssl_buffer_message( ssl );
5523 if( ret != 0 )
5524 return( ret );
5525
5526 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5527 }
5528#endif /* MBEDTLS_SSL_PROTO_DTLS */
5529
Hanno Becker90333da2017-10-10 11:27:13 +01005530 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5531 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005532
5533 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005534 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005535 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005536 return( ret );
5537 }
5538
Hanno Becker327c93b2018-08-15 13:56:18 +01005539 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005540 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005541 {
5542 mbedtls_ssl_update_handshake_status( ssl );
5543 }
Simon Butcher99000142016-10-13 17:21:01 +01005544 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005545 else
Simon Butcher99000142016-10-13 17:21:01 +01005546 {
Hanno Becker02f59072018-08-15 14:00:24 +01005547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005548 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005549 }
5550
5551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5552
5553 return( 0 );
5554}
5555
Hanno Becker40f50842018-08-15 14:48:01 +01005556#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005557static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005558{
Hanno Becker40f50842018-08-15 14:48:01 +01005559 if( ssl->in_left > ssl->next_record_offset )
5560 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005561
Hanno Becker40f50842018-08-15 14:48:01 +01005562 return( 0 );
5563}
5564
5565static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5566{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005567 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005568 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005569 int ret = 0;
5570
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005571 if( hs == NULL )
5572 return( -1 );
5573
Hanno Beckere00ae372018-08-20 09:39:42 +01005574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5575
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005576 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5577 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5578 {
5579 /* Check if we have seen a ChangeCipherSpec before.
5580 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005581 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005582 {
5583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5584 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005585 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005586 }
5587
Hanno Becker39b8bc92018-08-28 17:17:13 +01005588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005589 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5590 ssl->in_msglen = 1;
5591 ssl->in_msg[0] = 1;
5592
5593 /* As long as they are equal, the exact value doesn't matter. */
5594 ssl->in_left = 0;
5595 ssl->next_record_offset = 0;
5596
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005597 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005598 goto exit;
5599 }
Hanno Becker37f95322018-08-16 13:55:32 +01005600
Hanno Beckerb8f50142018-08-28 10:01:34 +01005601#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005602 /* Debug only */
5603 {
5604 unsigned offset;
5605 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5606 {
5607 hs_buf = &hs->buffering.hs[offset];
5608 if( hs_buf->is_valid == 1 )
5609 {
5610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5611 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005612 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005613 }
5614 }
5615 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005616#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005617
5618 /* Check if we have buffered and/or fully reassembled the
5619 * next handshake message. */
5620 hs_buf = &hs->buffering.hs[0];
5621 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5622 {
5623 /* Synthesize a record containing the buffered HS message. */
5624 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5625 ( hs_buf->data[2] << 8 ) |
5626 hs_buf->data[3];
5627
5628 /* Double-check that we haven't accidentally buffered
5629 * a message that doesn't fit into the input buffer. */
5630 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5631 {
5632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5633 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5634 }
5635
5636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5637 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5638 hs_buf->data, msg_len + 12 );
5639
5640 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5641 ssl->in_hslen = msg_len + 12;
5642 ssl->in_msglen = msg_len + 12;
5643 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5644
5645 ret = 0;
5646 goto exit;
5647 }
5648 else
5649 {
5650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5651 hs->in_msg_seq ) );
5652 }
5653
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005654 ret = -1;
5655
5656exit:
5657
5658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5659 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005660}
5661
Hanno Beckera02b0b42018-08-21 17:20:27 +01005662static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5663 size_t desired )
5664{
5665 int offset;
5666 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5668 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005669
Hanno Becker01315ea2018-08-21 17:22:17 +01005670 /* Get rid of future records epoch first, if such exist. */
5671 ssl_free_buffered_record( ssl );
5672
5673 /* Check if we have enough space available now. */
5674 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5675 hs->buffering.total_bytes_buffered ) )
5676 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005678 return( 0 );
5679 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005680
Hanno Becker4f432ad2018-08-28 10:02:32 +01005681 /* We don't have enough space to buffer the next expected handshake
5682 * message. Remove buffers used for future messages to gain space,
5683 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005684 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5685 offset >= 0; offset-- )
5686 {
5687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5688 offset ) );
5689
Hanno Beckerb309b922018-08-23 13:18:05 +01005690 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005691
5692 /* Check if we have enough space available now. */
5693 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5694 hs->buffering.total_bytes_buffered ) )
5695 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005697 return( 0 );
5698 }
5699 }
5700
5701 return( -1 );
5702}
5703
Hanno Becker40f50842018-08-15 14:48:01 +01005704static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5705{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005706 int ret = 0;
5707 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5708
5709 if( hs == NULL )
5710 return( 0 );
5711
5712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5713
5714 switch( ssl->in_msgtype )
5715 {
5716 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005718
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005719 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005720 break;
5721
5722 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005723 {
5724 unsigned recv_msg_seq_offset;
5725 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5726 mbedtls_ssl_hs_buffer *hs_buf;
5727 size_t msg_len = ssl->in_hslen - 12;
5728
5729 /* We should never receive an old handshake
5730 * message - double-check nonetheless. */
5731 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5732 {
5733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5734 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5735 }
5736
5737 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5738 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5739 {
5740 /* Silently ignore -- message too far in the future */
5741 MBEDTLS_SSL_DEBUG_MSG( 2,
5742 ( "Ignore future HS message with sequence number %u, "
5743 "buffering window %u - %u",
5744 recv_msg_seq, ssl->handshake->in_msg_seq,
5745 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5746
5747 goto exit;
5748 }
5749
5750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5751 recv_msg_seq, recv_msg_seq_offset ) );
5752
5753 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5754
5755 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005756 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005757 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005758 size_t reassembly_buf_sz;
5759
Hanno Becker37f95322018-08-16 13:55:32 +01005760 hs_buf->is_fragmented =
5761 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5762
5763 /* We copy the message back into the input buffer
5764 * after reassembly, so check that it's not too large.
5765 * This is an implementation-specific limitation
5766 * and not one from the standard, hence it is not
5767 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005768 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005769 {
5770 /* Ignore message */
5771 goto exit;
5772 }
5773
Hanno Beckere0b150f2018-08-21 15:51:03 +01005774 /* Check if we have enough space to buffer the message. */
5775 if( hs->buffering.total_bytes_buffered >
5776 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5777 {
5778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5779 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5780 }
5781
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005782 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5783 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005784
5785 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5786 hs->buffering.total_bytes_buffered ) )
5787 {
5788 if( recv_msg_seq_offset > 0 )
5789 {
5790 /* If we can't buffer a future message because
5791 * of space limitations -- ignore. */
5792 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",
5793 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5794 (unsigned) hs->buffering.total_bytes_buffered ) );
5795 goto exit;
5796 }
Hanno Beckere1801392018-08-21 16:51:05 +01005797 else
5798 {
5799 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",
5800 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5801 (unsigned) hs->buffering.total_bytes_buffered ) );
5802 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005803
Hanno Beckera02b0b42018-08-21 17:20:27 +01005804 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005805 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005806 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",
5807 (unsigned) msg_len,
5808 (unsigned) reassembly_buf_sz,
5809 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005810 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005811 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5812 goto exit;
5813 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005814 }
5815
5816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5817 msg_len ) );
5818
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005819 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5820 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005821 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005822 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005823 goto exit;
5824 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005825 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005826
5827 /* Prepare final header: copy msg_type, length and message_seq,
5828 * then add standardised fragment_offset and fragment_length */
5829 memcpy( hs_buf->data, ssl->in_msg, 6 );
5830 memset( hs_buf->data + 6, 0, 3 );
5831 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5832
5833 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005834
5835 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005836 }
5837 else
5838 {
5839 /* Make sure msg_type and length are consistent */
5840 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5841 {
5842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5843 /* Ignore */
5844 goto exit;
5845 }
5846 }
5847
Hanno Becker4422bbb2018-08-20 09:40:19 +01005848 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005849 {
5850 size_t frag_len, frag_off;
5851 unsigned char * const msg = hs_buf->data + 12;
5852
5853 /*
5854 * Check and copy current fragment
5855 */
5856
5857 /* Validation of header fields already done in
5858 * mbedtls_ssl_prepare_handshake_record(). */
5859 frag_off = ssl_get_hs_frag_off( ssl );
5860 frag_len = ssl_get_hs_frag_len( ssl );
5861
5862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5863 frag_off, frag_len ) );
5864 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5865
5866 if( hs_buf->is_fragmented )
5867 {
5868 unsigned char * const bitmask = msg + msg_len;
5869 ssl_bitmask_set( bitmask, frag_off, frag_len );
5870 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5871 msg_len ) == 0 );
5872 }
5873 else
5874 {
5875 hs_buf->is_complete = 1;
5876 }
5877
5878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5879 hs_buf->is_complete ? "" : "not yet " ) );
5880 }
5881
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005882 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005883 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005884
5885 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005886 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005887 break;
5888 }
5889
5890exit:
5891
5892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5893 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005894}
5895#endif /* MBEDTLS_SSL_PROTO_DTLS */
5896
Hanno Becker1097b342018-08-15 14:09:41 +01005897static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005898{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005899 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005900 * Consume last content-layer message and potentially
5901 * update in_msglen which keeps track of the contents'
5902 * consumption state.
5903 *
5904 * (1) Handshake messages:
5905 * Remove last handshake message, move content
5906 * and adapt in_msglen.
5907 *
5908 * (2) Alert messages:
5909 * Consume whole record content, in_msglen = 0.
5910 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005911 * (3) Change cipher spec:
5912 * Consume whole record content, in_msglen = 0.
5913 *
5914 * (4) Application data:
5915 * Don't do anything - the record layer provides
5916 * the application data as a stream transport
5917 * and consumes through mbedtls_ssl_read only.
5918 *
5919 */
5920
5921 /* Case (1): Handshake messages */
5922 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005923 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005924 /* Hard assertion to be sure that no application data
5925 * is in flight, as corrupting ssl->in_msglen during
5926 * ssl->in_offt != NULL is fatal. */
5927 if( ssl->in_offt != NULL )
5928 {
5929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5930 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5931 }
5932
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005933 /*
5934 * Get next Handshake message in the current record
5935 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005936
Hanno Becker4a810fb2017-05-24 16:27:30 +01005937 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005938 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005939 * current handshake content: If DTLS handshake
5940 * fragmentation is used, that's the fragment
5941 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005942 * size here is faulty and should be changed at
5943 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005944 * (2) While it doesn't seem to cause problems, one
5945 * has to be very careful not to assume that in_hslen
5946 * is always <= in_msglen in a sensible communication.
5947 * Again, it's wrong for DTLS handshake fragmentation.
5948 * The following check is therefore mandatory, and
5949 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005950 * Additionally, ssl->in_hslen might be arbitrarily out of
5951 * bounds after handling a DTLS message with an unexpected
5952 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005953 */
5954 if( ssl->in_hslen < ssl->in_msglen )
5955 {
5956 ssl->in_msglen -= ssl->in_hslen;
5957 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5958 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005959
Hanno Becker4a810fb2017-05-24 16:27:30 +01005960 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5961 ssl->in_msg, ssl->in_msglen );
5962 }
5963 else
5964 {
5965 ssl->in_msglen = 0;
5966 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005967
Hanno Becker4a810fb2017-05-24 16:27:30 +01005968 ssl->in_hslen = 0;
5969 }
5970 /* Case (4): Application data */
5971 else if( ssl->in_offt != NULL )
5972 {
5973 return( 0 );
5974 }
5975 /* Everything else (CCS & Alerts) */
5976 else
5977 {
5978 ssl->in_msglen = 0;
5979 }
5980
Hanno Becker1097b342018-08-15 14:09:41 +01005981 return( 0 );
5982}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005983
Hanno Beckere74d5562018-08-15 14:26:08 +01005984static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5985{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005986 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005987 return( 1 );
5988
5989 return( 0 );
5990}
5991
Hanno Becker5f066e72018-08-16 14:56:31 +01005992#if defined(MBEDTLS_SSL_PROTO_DTLS)
5993
5994static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5995{
5996 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5997 if( hs == NULL )
5998 return;
5999
Hanno Becker01315ea2018-08-21 17:22:17 +01006000 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006001 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006002 hs->buffering.total_bytes_buffered -=
6003 hs->buffering.future_record.len;
6004
6005 mbedtls_free( hs->buffering.future_record.data );
6006 hs->buffering.future_record.data = NULL;
6007 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006008}
6009
6010static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6011{
6012 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6013 unsigned char * rec;
6014 size_t rec_len;
6015 unsigned rec_epoch;
6016
6017 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6018 return( 0 );
6019
6020 if( hs == NULL )
6021 return( 0 );
6022
Hanno Becker5f066e72018-08-16 14:56:31 +01006023 rec = hs->buffering.future_record.data;
6024 rec_len = hs->buffering.future_record.len;
6025 rec_epoch = hs->buffering.future_record.epoch;
6026
6027 if( rec == NULL )
6028 return( 0 );
6029
Hanno Becker4cb782d2018-08-20 11:19:05 +01006030 /* Only consider loading future records if the
6031 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006032 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006033 return( 0 );
6034
Hanno Becker5f066e72018-08-16 14:56:31 +01006035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6036
6037 if( rec_epoch != ssl->in_epoch )
6038 {
6039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6040 goto exit;
6041 }
6042
6043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6044
6045 /* Double-check that the record is not too large */
6046 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6047 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6048 {
6049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6050 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6051 }
6052
6053 memcpy( ssl->in_hdr, rec, rec_len );
6054 ssl->in_left = rec_len;
6055 ssl->next_record_offset = 0;
6056
6057 ssl_free_buffered_record( ssl );
6058
6059exit:
6060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6061 return( 0 );
6062}
6063
Hanno Becker519f15d2019-07-11 12:43:20 +01006064static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6065 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006066{
6067 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006068
6069 /* Don't buffer future records outside handshakes. */
6070 if( hs == NULL )
6071 return( 0 );
6072
6073 /* Only buffer handshake records (we are only interested
6074 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01006075 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006076 return( 0 );
6077
6078 /* Don't buffer more than one future epoch record. */
6079 if( hs->buffering.future_record.data != NULL )
6080 return( 0 );
6081
Hanno Becker01315ea2018-08-21 17:22:17 +01006082 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01006083 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006084 hs->buffering.total_bytes_buffered ) )
6085 {
6086 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 +01006087 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006088 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006089 return( 0 );
6090 }
6091
Hanno Becker5f066e72018-08-16 14:56:31 +01006092 /* Buffer record */
6093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6094 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01006095 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006096
6097 /* ssl_parse_record_header() only considers records
6098 * of the next epoch as candidates for buffering. */
6099 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006100 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006101
6102 hs->buffering.future_record.data =
6103 mbedtls_calloc( 1, hs->buffering.future_record.len );
6104 if( hs->buffering.future_record.data == NULL )
6105 {
6106 /* If we run out of RAM trying to buffer a
6107 * record from the next epoch, just ignore. */
6108 return( 0 );
6109 }
6110
Hanno Becker519f15d2019-07-11 12:43:20 +01006111 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006112
Hanno Becker519f15d2019-07-11 12:43:20 +01006113 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006114 return( 0 );
6115}
6116
6117#endif /* MBEDTLS_SSL_PROTO_DTLS */
6118
Hanno Beckere74d5562018-08-15 14:26:08 +01006119static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006120{
6121 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006122 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006123
Hanno Becker5f066e72018-08-16 14:56:31 +01006124#if defined(MBEDTLS_SSL_PROTO_DTLS)
6125 /* We might have buffered a future record; if so,
6126 * and if the epoch matches now, load it.
6127 * On success, this call will set ssl->in_left to
6128 * the length of the buffered record, so that
6129 * the calls to ssl_fetch_input() below will
6130 * essentially be no-ops. */
6131 ret = ssl_load_buffered_record( ssl );
6132 if( ret != 0 )
6133 return( ret );
6134#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006135
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006136 /* Ensure that we have enough space available for the default form
6137 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6138 * with no space for CIDs counted in). */
6139 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6140 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006143 return( ret );
6144 }
6145
Hanno Beckere5e7e782019-07-11 12:29:35 +01006146 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6147 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006150 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006151 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006152 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6153 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006154 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006155 if( ret != 0 )
6156 return( ret );
6157
6158 /* Fall through to handling of unexpected records */
6159 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6160 }
6161
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006162 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6163 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006164#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006165 /* Reset in pointers to default state for TLS/DTLS records,
6166 * assuming no CID and no offset between record content and
6167 * record plaintext. */
6168 ssl_update_in_pointers( ssl );
6169
Hanno Becker7ae20e02019-07-12 08:33:49 +01006170 /* Setup internal message pointers from record structure. */
6171 ssl->in_msgtype = rec.type;
6172#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6173 ssl->in_len = ssl->in_cid + rec.cid_len;
6174#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6175 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6176 ssl->in_msglen = rec.data_len;
6177
Hanno Becker2fddd372019-07-10 14:37:41 +01006178 ret = ssl_check_client_reconnect( ssl );
6179 if( ret != 0 )
6180 return( ret );
6181#endif
6182
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006183 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006184 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006185
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6187 "(header)" ) );
6188 }
6189 else
6190 {
6191 /* Skip invalid record and the rest of the datagram */
6192 ssl->next_record_offset = 0;
6193 ssl->in_left = 0;
6194
6195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6196 "(header)" ) );
6197 }
6198
6199 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006200 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006201 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006202 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006203#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006204 {
6205 return( ret );
6206 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006207 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006210 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006211 {
Hanno Beckera8814792019-07-10 15:01:45 +01006212 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006213 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006214 if( ssl->next_record_offset < ssl->in_left )
6215 {
6216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6217 }
6218 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006219 else
6220#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006221 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006222 /*
6223 * Fetch record contents from underlying transport.
6224 */
Hanno Beckera3175662019-07-11 12:50:29 +01006225 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006226 if( ret != 0 )
6227 {
6228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6229 return( ret );
6230 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006231
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006232 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006233 }
6234
6235 /*
6236 * Decrypt record contents.
6237 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006238
Hanno Beckerfdf66042019-07-11 13:07:45 +01006239 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006241#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006242 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006243 {
6244 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006245 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006246 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006247 /* Except when waiting for Finished as a bad mac here
6248 * probably means something went wrong in the handshake
6249 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6250 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6251 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6252 {
6253#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6254 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6255 {
6256 mbedtls_ssl_send_alert_message( ssl,
6257 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6258 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6259 }
6260#endif
6261 return( ret );
6262 }
6263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006265 if( ssl->conf->badmac_limit != 0 &&
6266 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6269 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006270 }
6271#endif
6272
Hanno Becker4a810fb2017-05-24 16:27:30 +01006273 /* As above, invalid records cause
6274 * dismissal of the whole datagram. */
6275
6276 ssl->next_record_offset = 0;
6277 ssl->in_left = 0;
6278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006280 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006281 }
6282
6283 return( ret );
6284 }
6285 else
6286#endif
6287 {
6288 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6290 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006292 mbedtls_ssl_send_alert_message( ssl,
6293 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6294 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006295 }
6296#endif
6297 return( ret );
6298 }
6299 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006300
Hanno Becker44d89b22019-07-12 09:40:44 +01006301
6302 /* Reset in pointers to default state for TLS/DTLS records,
6303 * assuming no CID and no offset between record content and
6304 * record plaintext. */
6305 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006306#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6307 ssl->in_len = ssl->in_cid + rec.cid_len;
6308#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6309 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006310
Hanno Becker8685c822019-07-12 09:37:30 +01006311 /* The record content type may change during decryption,
6312 * so re-read it. */
6313 ssl->in_msgtype = rec.type;
6314 /* Also update the input buffer, because unfortunately
6315 * the server-side ssl_parse_client_hello() reparses the
6316 * record header when receiving a ClientHello initiating
6317 * a renegotiation. */
6318 ssl->in_hdr[0] = rec.type;
6319 ssl->in_msg = rec.buf + rec.data_offset;
6320 ssl->in_msglen = rec.data_len;
6321 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6322 ssl->in_len[1] = (unsigned char)( rec.data_len );
6323
Simon Butcher99000142016-10-13 17:21:01 +01006324 return( 0 );
6325}
6326
6327int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6328{
6329 int ret;
6330
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006331 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006332 * Handle particular types of records
6333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006334 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006335 {
Simon Butcher99000142016-10-13 17:21:01 +01006336 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6337 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006338 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006339 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006340 }
6341
Hanno Beckere678eaa2018-08-21 14:57:46 +01006342 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006343 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006344 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006345 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6347 ssl->in_msglen ) );
6348 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006349 }
6350
Hanno Beckere678eaa2018-08-21 14:57:46 +01006351 if( ssl->in_msg[0] != 1 )
6352 {
6353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6354 ssl->in_msg[0] ) );
6355 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6356 }
6357
6358#if defined(MBEDTLS_SSL_PROTO_DTLS)
6359 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6360 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6361 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6362 {
6363 if( ssl->handshake == NULL )
6364 {
6365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6366 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6367 }
6368
6369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6370 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6371 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006372#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006373 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006375 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006376 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006377 if( ssl->in_msglen != 2 )
6378 {
6379 /* Note: Standard allows for more than one 2 byte alert
6380 to be packed in a single message, but Mbed TLS doesn't
6381 currently support this. */
6382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6383 ssl->in_msglen ) );
6384 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6385 }
6386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006388 ssl->in_msg[0], ssl->in_msg[1] ) );
6389
6390 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006391 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006393 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006396 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006397 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006398 }
6399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6401 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6404 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006405 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006406
6407#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6408 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6409 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6410 {
Hanno Becker90333da2017-10-10 11:27:13 +01006411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006412 /* Will be handled when trying to parse ServerHello */
6413 return( 0 );
6414 }
6415#endif
6416
6417#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6418 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6419 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6420 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6421 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6422 {
6423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6424 /* Will be handled in mbedtls_ssl_parse_certificate() */
6425 return( 0 );
6426 }
6427#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6428
6429 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006430 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006431 }
6432
Hanno Beckerc76c6192017-06-06 10:03:17 +01006433#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006434 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006435 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006436 /* Drop unexpected ApplicationData records,
6437 * except at the beginning of renegotiations */
6438 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6439 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6440#if defined(MBEDTLS_SSL_RENEGOTIATION)
6441 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6442 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006443#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006444 )
6445 {
6446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6447 return( MBEDTLS_ERR_SSL_NON_FATAL );
6448 }
6449
6450 if( ssl->handshake != NULL &&
6451 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6452 {
6453 ssl_handshake_wrapup_free_hs_transform( ssl );
6454 }
6455 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006456#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006457
Paul Bakker5121ce52009-01-03 21:22:43 +00006458 return( 0 );
6459}
6460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006461int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006462{
6463 int ret;
6464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006465 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6466 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6467 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006468 {
6469 return( ret );
6470 }
6471
6472 return( 0 );
6473}
6474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006475int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006476 unsigned char level,
6477 unsigned char message )
6478{
6479 int ret;
6480
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006481 if( ssl == NULL || ssl->conf == NULL )
6482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006485 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006488 ssl->out_msglen = 2;
6489 ssl->out_msg[0] = level;
6490 ssl->out_msg[1] = message;
6491
Hanno Becker67bc7c32018-08-06 11:33:50 +01006492 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006495 return( ret );
6496 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006498
6499 return( 0 );
6500}
6501
Hanno Beckerb9d44792019-02-08 07:19:04 +00006502#if defined(MBEDTLS_X509_CRT_PARSE_C)
6503static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6504{
6505#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6506 if( session->peer_cert != NULL )
6507 {
6508 mbedtls_x509_crt_free( session->peer_cert );
6509 mbedtls_free( session->peer_cert );
6510 session->peer_cert = NULL;
6511 }
6512#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6513 if( session->peer_cert_digest != NULL )
6514 {
6515 /* Zeroization is not necessary. */
6516 mbedtls_free( session->peer_cert_digest );
6517 session->peer_cert_digest = NULL;
6518 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6519 session->peer_cert_digest_len = 0;
6520 }
6521#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6522}
6523#endif /* MBEDTLS_X509_CRT_PARSE_C */
6524
Paul Bakker5121ce52009-01-03 21:22:43 +00006525/*
6526 * Handshake functions
6527 */
Hanno Becker21489932019-02-05 13:20:55 +00006528#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006529/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006530int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006531{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006532 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6533 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006536
Hanno Becker7177a882019-02-05 13:36:46 +00006537 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006540 ssl->state++;
6541 return( 0 );
6542 }
6543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6545 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006546}
6547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006549{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006550 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6551 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006554
Hanno Becker7177a882019-02-05 13:36:46 +00006555 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006558 ssl->state++;
6559 return( 0 );
6560 }
6561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006564}
Gilles Peskinef9828522017-05-03 12:28:43 +02006565
Hanno Becker21489932019-02-05 13:20:55 +00006566#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006567/* Some certificate support -> implement write and parse */
6568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006570{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006571 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006572 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006574 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6575 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006578
Hanno Becker7177a882019-02-05 13:36:46 +00006579 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006582 ssl->state++;
6583 return( 0 );
6584 }
6585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006587 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006588 {
6589 if( ssl->client_auth == 0 )
6590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006592 ssl->state++;
6593 return( 0 );
6594 }
6595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006597 /*
6598 * If using SSLv3 and got no cert, send an Alert message
6599 * (otherwise an empty Certificate message will be sent).
6600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006601 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6602 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006603 {
6604 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006605 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6606 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6607 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006610 goto write_msg;
6611 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006612#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006613 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006614#endif /* MBEDTLS_SSL_CLI_C */
6615#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006616 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006618 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6621 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006622 }
6623 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006624#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006626 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006627
6628 /*
6629 * 0 . 0 handshake type
6630 * 1 . 3 handshake length
6631 * 4 . 6 length of all certs
6632 * 7 . 9 length of cert. 1
6633 * 10 . n-1 peer certificate
6634 * n . n+2 length of cert. 2
6635 * n+3 . ... upper level cert, etc.
6636 */
6637 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006639
Paul Bakker29087132010-03-21 21:03:34 +00006640 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006641 {
6642 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006643 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006646 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006647 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006648 }
6649
6650 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6651 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6652 ssl->out_msg[i + 2] = (unsigned char)( n );
6653
6654 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6655 i += n; crt = crt->next;
6656 }
6657
6658 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6659 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6660 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6661
6662 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6664 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006665
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006666#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006667write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006668#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006669
6670 ssl->state++;
6671
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006672 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006673 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006674 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006675 return( ret );
6676 }
6677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006679
Paul Bakkered27a042013-04-18 22:46:23 +02006680 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006681}
6682
Hanno Becker84879e32019-01-31 07:44:03 +00006683#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006684
6685#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006686static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6687 unsigned char *crt_buf,
6688 size_t crt_buf_len )
6689{
6690 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6691
6692 if( peer_crt == NULL )
6693 return( -1 );
6694
6695 if( peer_crt->raw.len != crt_buf_len )
6696 return( -1 );
6697
Hanno Becker46f34d02019-02-08 14:00:04 +00006698 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006699}
Hanno Becker177475a2019-02-05 17:02:46 +00006700#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6701static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6702 unsigned char *crt_buf,
6703 size_t crt_buf_len )
6704{
6705 int ret;
6706 unsigned char const * const peer_cert_digest =
6707 ssl->session->peer_cert_digest;
6708 mbedtls_md_type_t const peer_cert_digest_type =
6709 ssl->session->peer_cert_digest_type;
6710 mbedtls_md_info_t const * const digest_info =
6711 mbedtls_md_info_from_type( peer_cert_digest_type );
6712 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6713 size_t digest_len;
6714
6715 if( peer_cert_digest == NULL || digest_info == NULL )
6716 return( -1 );
6717
6718 digest_len = mbedtls_md_get_size( digest_info );
6719 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6720 return( -1 );
6721
6722 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6723 if( ret != 0 )
6724 return( -1 );
6725
6726 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6727}
6728#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006729#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006730
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006731/*
6732 * Once the certificate message is read, parse it into a cert chain and
6733 * perform basic checks, but leave actual verification to the caller
6734 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006735static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6736 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006737{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006738 int ret;
6739#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6740 int crt_cnt=0;
6741#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006742 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006743 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006747 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006748 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6749 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006750 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006751 }
6752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6754 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006757 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6758 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006760 }
6761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006762 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006763
Paul Bakker5121ce52009-01-03 21:22:43 +00006764 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006766 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006767 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006768
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006769 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006770 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006773 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6774 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006776 }
6777
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006778 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6779 i += 3;
6780
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006781 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006782 while( i < ssl->in_hslen )
6783 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006784 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006785 if ( i + 3 > ssl->in_hslen ) {
6786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006787 mbedtls_ssl_send_alert_message( ssl,
6788 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6789 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006790 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6791 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006792 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6793 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006794 if( ssl->in_msg[i] != 0 )
6795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006797 mbedtls_ssl_send_alert_message( ssl,
6798 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6799 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006800 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006801 }
6802
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006803 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006804 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6805 | (unsigned int) ssl->in_msg[i + 2];
6806 i += 3;
6807
6808 if( n < 128 || i + n > ssl->in_hslen )
6809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006811 mbedtls_ssl_send_alert_message( ssl,
6812 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6813 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006815 }
6816
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006817 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006818#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6819 if( crt_cnt++ == 0 &&
6820 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6821 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006822 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006823 /* During client-side renegotiation, check that the server's
6824 * end-CRTs hasn't changed compared to the initial handshake,
6825 * mitigating the triple handshake attack. On success, reuse
6826 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006827 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6828 if( ssl_check_peer_crt_unchanged( ssl,
6829 &ssl->in_msg[i],
6830 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006831 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6833 mbedtls_ssl_send_alert_message( ssl,
6834 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6835 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6836 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006837 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006838
6839 /* Now we can safely free the original chain. */
6840 ssl_clear_peer_cert( ssl->session );
6841 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006842#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6843
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006844 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006845#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006846 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006847#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006848 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006849 * it in-place from the input buffer instead of making a copy. */
6850 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6851#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006852 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006853 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006854 case 0: /*ok*/
6855 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6856 /* Ignore certificate with an unknown algorithm: maybe a
6857 prior certificate was already trusted. */
6858 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006859
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006860 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6861 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6862 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006863
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006864 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6865 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6866 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006867
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006868 default:
6869 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6870 crt_parse_der_failed:
6871 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6872 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6873 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006874 }
6875
6876 i += n;
6877 }
6878
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006879 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006880 return( 0 );
6881}
6882
Hanno Becker4a55f632019-02-05 12:49:06 +00006883#if defined(MBEDTLS_SSL_SRV_C)
6884static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6885{
6886 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6887 return( -1 );
6888
6889#if defined(MBEDTLS_SSL_PROTO_SSL3)
6890 /*
6891 * Check if the client sent an empty certificate
6892 */
6893 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6894 {
6895 if( ssl->in_msglen == 2 &&
6896 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6897 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6898 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6899 {
6900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6901 return( 0 );
6902 }
6903
6904 return( -1 );
6905 }
6906#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6907
6908#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6909 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6910 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6911 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6912 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6913 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6914 {
6915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6916 return( 0 );
6917 }
6918
6919 return( -1 );
6920#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6921 MBEDTLS_SSL_PROTO_TLS1_2 */
6922}
6923#endif /* MBEDTLS_SSL_SRV_C */
6924
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006925/* Check if a certificate message is expected.
6926 * Return either
6927 * - SSL_CERTIFICATE_EXPECTED, or
6928 * - SSL_CERTIFICATE_SKIP
6929 * indicating whether a Certificate message is expected or not.
6930 */
6931#define SSL_CERTIFICATE_EXPECTED 0
6932#define SSL_CERTIFICATE_SKIP 1
6933static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6934 int authmode )
6935{
6936 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006937 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006938
6939 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6940 return( SSL_CERTIFICATE_SKIP );
6941
6942#if defined(MBEDTLS_SSL_SRV_C)
6943 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6944 {
6945 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6946 return( SSL_CERTIFICATE_SKIP );
6947
6948 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6949 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006950 ssl->session_negotiate->verify_result =
6951 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6952 return( SSL_CERTIFICATE_SKIP );
6953 }
6954 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006955#else
6956 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006957#endif /* MBEDTLS_SSL_SRV_C */
6958
6959 return( SSL_CERTIFICATE_EXPECTED );
6960}
6961
Hanno Becker68636192019-02-05 14:36:34 +00006962static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6963 int authmode,
6964 mbedtls_x509_crt *chain,
6965 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006966{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006967 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006968 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006969 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006970 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006971
Hanno Becker8927c832019-04-03 12:52:50 +01006972 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6973 void *p_vrfy;
6974
Hanno Becker68636192019-02-05 14:36:34 +00006975 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6976 return( 0 );
6977
Hanno Becker8927c832019-04-03 12:52:50 +01006978 if( ssl->f_vrfy != NULL )
6979 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006980 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006981 f_vrfy = ssl->f_vrfy;
6982 p_vrfy = ssl->p_vrfy;
6983 }
6984 else
6985 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006986 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006987 f_vrfy = ssl->conf->f_vrfy;
6988 p_vrfy = ssl->conf->p_vrfy;
6989 }
6990
Hanno Becker68636192019-02-05 14:36:34 +00006991 /*
6992 * Main check: verify certificate
6993 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006994#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6995 if( ssl->conf->f_ca_cb != NULL )
6996 {
6997 ((void) rs_ctx);
6998 have_ca_chain = 1;
6999
7000 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03007001 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007002 chain,
7003 ssl->conf->f_ca_cb,
7004 ssl->conf->p_ca_cb,
7005 ssl->conf->cert_profile,
7006 ssl->hostname,
7007 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007008 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007009 }
7010 else
7011#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7012 {
7013 mbedtls_x509_crt *ca_chain;
7014 mbedtls_x509_crl *ca_crl;
7015
7016#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7017 if( ssl->handshake->sni_ca_chain != NULL )
7018 {
7019 ca_chain = ssl->handshake->sni_ca_chain;
7020 ca_crl = ssl->handshake->sni_ca_crl;
7021 }
7022 else
7023#endif
7024 {
7025 ca_chain = ssl->conf->ca_chain;
7026 ca_crl = ssl->conf->ca_crl;
7027 }
7028
7029 if( ca_chain != NULL )
7030 have_ca_chain = 1;
7031
7032 ret = mbedtls_x509_crt_verify_restartable(
7033 chain,
7034 ca_chain, ca_crl,
7035 ssl->conf->cert_profile,
7036 ssl->hostname,
7037 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007038 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007039 }
Hanno Becker68636192019-02-05 14:36:34 +00007040
7041 if( ret != 0 )
7042 {
7043 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7044 }
7045
7046#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7047 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7048 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7049#endif
7050
7051 /*
7052 * Secondary checks: always done, but change 'ret' only if it was 0
7053 */
7054
7055#if defined(MBEDTLS_ECP_C)
7056 {
7057 const mbedtls_pk_context *pk = &chain->pk;
7058
7059 /* If certificate uses an EC key, make sure the curve is OK */
7060 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
7061 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
7062 {
7063 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7064
7065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7066 if( ret == 0 )
7067 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7068 }
7069 }
7070#endif /* MBEDTLS_ECP_C */
7071
7072 if( mbedtls_ssl_check_cert_usage( chain,
7073 ciphersuite_info,
7074 ! ssl->conf->endpoint,
7075 &ssl->session_negotiate->verify_result ) != 0 )
7076 {
7077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7078 if( ret == 0 )
7079 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7080 }
7081
7082 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7083 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7084 * with details encoded in the verification flags. All other kinds
7085 * of error codes, including those from the user provided f_vrfy
7086 * functions, are treated as fatal and lead to a failure of
7087 * ssl_parse_certificate even if verification was optional. */
7088 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7089 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7090 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7091 {
7092 ret = 0;
7093 }
7094
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007095 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007096 {
7097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7098 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7099 }
7100
7101 if( ret != 0 )
7102 {
7103 uint8_t alert;
7104
7105 /* The certificate may have been rejected for several reasons.
7106 Pick one and send the corresponding alert. Which alert to send
7107 may be a subject of debate in some cases. */
7108 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7109 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7110 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7111 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7112 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7113 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7114 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7115 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7116 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7117 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7118 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7119 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7120 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7121 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7122 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7123 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7124 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7125 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7126 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7127 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7128 else
7129 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7130 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7131 alert );
7132 }
7133
7134#if defined(MBEDTLS_DEBUG_C)
7135 if( ssl->session_negotiate->verify_result != 0 )
7136 {
7137 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7138 ssl->session_negotiate->verify_result ) );
7139 }
7140 else
7141 {
7142 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7143 }
7144#endif /* MBEDTLS_DEBUG_C */
7145
7146 return( ret );
7147}
7148
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007149#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7150static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7151 unsigned char *start, size_t len )
7152{
7153 int ret;
7154 /* Remember digest of the peer's end-CRT. */
7155 ssl->session_negotiate->peer_cert_digest =
7156 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7157 if( ssl->session_negotiate->peer_cert_digest == NULL )
7158 {
7159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7160 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7161 mbedtls_ssl_send_alert_message( ssl,
7162 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7163 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7164
7165 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7166 }
7167
7168 ret = mbedtls_md( mbedtls_md_info_from_type(
7169 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7170 start, len,
7171 ssl->session_negotiate->peer_cert_digest );
7172
7173 ssl->session_negotiate->peer_cert_digest_type =
7174 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7175 ssl->session_negotiate->peer_cert_digest_len =
7176 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7177
7178 return( ret );
7179}
7180
7181static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7182 unsigned char *start, size_t len )
7183{
7184 unsigned char *end = start + len;
7185 int ret;
7186
7187 /* Make a copy of the peer's raw public key. */
7188 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7189 ret = mbedtls_pk_parse_subpubkey( &start, end,
7190 &ssl->handshake->peer_pubkey );
7191 if( ret != 0 )
7192 {
7193 /* We should have parsed the public key before. */
7194 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7195 }
7196
7197 return( 0 );
7198}
7199#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7200
Hanno Becker68636192019-02-05 14:36:34 +00007201int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7202{
7203 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007204 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007205#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7206 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7207 ? ssl->handshake->sni_authmode
7208 : ssl->conf->authmode;
7209#else
7210 const int authmode = ssl->conf->authmode;
7211#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007212 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007213 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007214
7215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7216
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007217 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7218 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007219 {
7220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007221 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007222 }
7223
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007224#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7225 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007226 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007227 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007228 chain = ssl->handshake->ecrs_peer_cert;
7229 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007230 goto crt_verify;
7231 }
7232#endif
7233
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007234 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007235 {
7236 /* mbedtls_ssl_read_record may have sent an alert already. We
7237 let it decide whether to alert. */
7238 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007239 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007240 }
7241
Hanno Becker4a55f632019-02-05 12:49:06 +00007242#if defined(MBEDTLS_SSL_SRV_C)
7243 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7244 {
7245 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007246
7247 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007248 ret = 0;
7249 else
7250 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007251
Hanno Becker6bdfab22019-02-05 13:11:17 +00007252 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007253 }
7254#endif /* MBEDTLS_SSL_SRV_C */
7255
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007256 /* Clear existing peer CRT structure in case we tried to
7257 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007258 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007259
7260 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7261 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007262 {
7263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7264 sizeof( mbedtls_x509_crt ) ) );
7265 mbedtls_ssl_send_alert_message( ssl,
7266 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7267 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007268
Hanno Becker3dad3112019-02-05 17:19:52 +00007269 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7270 goto exit;
7271 }
7272 mbedtls_x509_crt_init( chain );
7273
7274 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007275 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007276 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007277
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007278#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7279 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007280 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007281
7282crt_verify:
7283 if( ssl->handshake->ecrs_enabled)
7284 rs_ctx = &ssl->handshake->ecrs_ctx;
7285#endif
7286
Hanno Becker68636192019-02-05 14:36:34 +00007287 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007288 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007289 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007290 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007291
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007292#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007293 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007294 unsigned char *crt_start, *pk_start;
7295 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007296
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007297 /* We parse the CRT chain without copying, so
7298 * these pointers point into the input buffer,
7299 * and are hence still valid after freeing the
7300 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007301
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007302 crt_start = chain->raw.p;
7303 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007304
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007305 pk_start = chain->pk_raw.p;
7306 pk_len = chain->pk_raw.len;
7307
7308 /* Free the CRT structures before computing
7309 * digest and copying the peer's public key. */
7310 mbedtls_x509_crt_free( chain );
7311 mbedtls_free( chain );
7312 chain = NULL;
7313
7314 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007315 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007316 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007317
7318 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7319 if( ret != 0 )
7320 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007321 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007322#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7323 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007324 ssl->session_negotiate->peer_cert = chain;
7325 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007326#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007329
Hanno Becker6bdfab22019-02-05 13:11:17 +00007330exit:
7331
Hanno Becker3dad3112019-02-05 17:19:52 +00007332 if( ret == 0 )
7333 ssl->state++;
7334
7335#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7336 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7337 {
7338 ssl->handshake->ecrs_peer_cert = chain;
7339 chain = NULL;
7340 }
7341#endif
7342
7343 if( chain != NULL )
7344 {
7345 mbedtls_x509_crt_free( chain );
7346 mbedtls_free( chain );
7347 }
7348
Paul Bakker5121ce52009-01-03 21:22:43 +00007349 return( ret );
7350}
Hanno Becker21489932019-02-05 13:20:55 +00007351#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007353int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007354{
7355 int ret;
7356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007360 ssl->out_msglen = 1;
7361 ssl->out_msg[0] = 1;
7362
Paul Bakker5121ce52009-01-03 21:22:43 +00007363 ssl->state++;
7364
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007365 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007366 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007367 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007368 return( ret );
7369 }
7370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007372
7373 return( 0 );
7374}
7375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007377{
7378 int ret;
7379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007381
Hanno Becker327c93b2018-08-15 13:56:18 +01007382 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007385 return( ret );
7386 }
7387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007391 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7392 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007394 }
7395
Hanno Beckere678eaa2018-08-21 14:57:46 +01007396 /* CCS records are only accepted if they have length 1 and content '1',
7397 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007398
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007399 /*
7400 * Switch to our negotiated transform and session parameters for inbound
7401 * data.
7402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007404 ssl->transform_in = ssl->transform_negotiate;
7405 ssl->session_in = ssl->session_negotiate;
7406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007408 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007410#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007411 ssl_dtls_replay_reset( ssl );
7412#endif
7413
7414 /* Increment epoch */
7415 if( ++ssl->in_epoch == 0 )
7416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007418 /* This is highly unlikely to happen for legitimate reasons, so
7419 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007421 }
7422 }
7423 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007424#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007425 memset( ssl->in_ctr, 0, 8 );
7426
Hanno Becker79594fd2019-05-08 09:38:41 +01007427 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007429#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7430 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007435 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7436 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007437 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007438 }
7439 }
7440#endif
7441
Paul Bakker5121ce52009-01-03 21:22:43 +00007442 ssl->state++;
7443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007445
7446 return( 0 );
7447}
7448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7450 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007451{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007452 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7455 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7456 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007457 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007458 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007459#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007460#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7461#if defined(MBEDTLS_SHA512_C)
7462 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007463 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7464 else
7465#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466#if defined(MBEDTLS_SHA256_C)
7467 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007468 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007469 else
7470#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007471#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007474 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007475 }
Paul Bakker380da532012-04-18 16:10:25 +00007476}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007478void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007479{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007480#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7481 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007482 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7483 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007484#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007485#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7486#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007487#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007488 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007489 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7490#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007491 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007492#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007493#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007494#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007495#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007496 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007497 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007498#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007499 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007500#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007501#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007503}
7504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007506 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007507{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7509 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007510 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7511 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007512#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007513#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7514#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007515#if defined(MBEDTLS_USE_PSA_CRYPTO)
7516 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7517#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007518 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007519#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007520#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007522#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007523 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007524#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007525 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007526#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007527#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007528#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007529}
7530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7532 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7533static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007534 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007535{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007536 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7537 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007538}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007539#endif
Paul Bakker380da532012-04-18 16:10:25 +00007540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007541#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7542#if defined(MBEDTLS_SHA256_C)
7543static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007544 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007545{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007546#if defined(MBEDTLS_USE_PSA_CRYPTO)
7547 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7548#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007549 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007550#endif
Paul Bakker380da532012-04-18 16:10:25 +00007551}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007552#endif
Paul Bakker380da532012-04-18 16:10:25 +00007553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554#if defined(MBEDTLS_SHA512_C)
7555static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007556 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007557{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007558#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007559 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007560#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007561 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007562#endif
Paul Bakker380da532012-04-18 16:10:25 +00007563}
Paul Bakker769075d2012-11-24 11:26:46 +01007564#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007567#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007568static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007570{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007571 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007572 mbedtls_md5_context md5;
7573 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007574
Paul Bakker5121ce52009-01-03 21:22:43 +00007575 unsigned char padbuf[48];
7576 unsigned char md5sum[16];
7577 unsigned char sha1sum[20];
7578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007580 if( !session )
7581 session = ssl->session;
7582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007584
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007585 mbedtls_md5_init( &md5 );
7586 mbedtls_sha1_init( &sha1 );
7587
7588 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7589 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007590
7591 /*
7592 * SSLv3:
7593 * hash =
7594 * MD5( master + pad2 +
7595 * MD5( handshake + sender + master + pad1 ) )
7596 * + SHA1( master + pad2 +
7597 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007598 */
7599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007601 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7602 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007603#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007606 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7607 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007608#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007611 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007612
Paul Bakker1ef83d62012-04-11 12:09:53 +00007613 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007614
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007615 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7616 mbedtls_md5_update_ret( &md5, session->master, 48 );
7617 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7618 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007619
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007620 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7621 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7622 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7623 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007624
Paul Bakker1ef83d62012-04-11 12:09:53 +00007625 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007626
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007627 mbedtls_md5_starts_ret( &md5 );
7628 mbedtls_md5_update_ret( &md5, session->master, 48 );
7629 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7630 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7631 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007632
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007633 mbedtls_sha1_starts_ret( &sha1 );
7634 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7635 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7636 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7637 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007640
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007641 mbedtls_md5_free( &md5 );
7642 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007643
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007644 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7645 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7646 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007650#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007652#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007653static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007654 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007655{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007656 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007657 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007658 mbedtls_md5_context md5;
7659 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007660 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007663 if( !session )
7664 session = ssl->session;
7665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007667
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007668 mbedtls_md5_init( &md5 );
7669 mbedtls_sha1_init( &sha1 );
7670
7671 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7672 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007673
Paul Bakker1ef83d62012-04-11 12:09:53 +00007674 /*
7675 * TLSv1:
7676 * hash = PRF( master, finished_label,
7677 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7678 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007680#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007681 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7682 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007683#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007685#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007686 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7687 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007688#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007690 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007691 ? "client finished"
7692 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007693
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007694 mbedtls_md5_finish_ret( &md5, padbuf );
7695 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007696
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007697 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007698 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007701
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007702 mbedtls_md5_free( &md5 );
7703 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007704
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007705 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007708}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007711#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7712#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007713static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007714 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007715{
7716 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007717 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007718 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007719#if defined(MBEDTLS_USE_PSA_CRYPTO)
7720 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007721 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007722 psa_status_t status;
7723#else
7724 mbedtls_sha256_context sha256;
7725#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007728 if( !session )
7729 session = ssl->session;
7730
Andrzej Kurekeb342242019-01-29 09:14:33 -05007731 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7732 ? "client finished"
7733 : "server finished";
7734
7735#if defined(MBEDTLS_USE_PSA_CRYPTO)
7736 sha256_psa = psa_hash_operation_init();
7737
7738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7739
7740 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7741 if( status != PSA_SUCCESS )
7742 {
7743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7744 return;
7745 }
7746
7747 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7748 if( status != PSA_SUCCESS )
7749 {
7750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7751 return;
7752 }
7753 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7754#else
7755
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007756 mbedtls_sha256_init( &sha256 );
7757
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007759
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007760 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007761
7762 /*
7763 * TLSv1.2:
7764 * hash = PRF( master, finished_label,
7765 * Hash( handshake ) )[0.11]
7766 */
7767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007768#if !defined(MBEDTLS_SHA256_ALT)
7769 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007770 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007771#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007772
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007773 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007774 mbedtls_sha256_free( &sha256 );
7775#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007776
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007777 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007778 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007780 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007781
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007782 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007785}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007789static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007790 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007791{
7792 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007793 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007794 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007795#if defined(MBEDTLS_USE_PSA_CRYPTO)
7796 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007797 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007798 psa_status_t status;
7799#else
7800 mbedtls_sha512_context sha512;
7801#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007804 if( !session )
7805 session = ssl->session;
7806
Andrzej Kurekeb342242019-01-29 09:14:33 -05007807 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7808 ? "client finished"
7809 : "server finished";
7810
7811#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007812 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007813
7814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7815
Andrzej Kurek972fba52019-01-30 03:29:12 -05007816 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007817 if( status != PSA_SUCCESS )
7818 {
7819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7820 return;
7821 }
7822
Andrzej Kurek972fba52019-01-30 03:29:12 -05007823 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007824 if( status != PSA_SUCCESS )
7825 {
7826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7827 return;
7828 }
7829 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7830#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007831 mbedtls_sha512_init( &sha512 );
7832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007834
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007835 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007836
7837 /*
7838 * TLSv1.2:
7839 * hash = PRF( master, finished_label,
7840 * Hash( handshake ) )[0.11]
7841 */
7842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007844 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7845 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007846#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007847
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007848 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007849 mbedtls_sha512_free( &sha512 );
7850#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007851
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007852 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007853 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007856
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007857 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007860}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861#endif /* MBEDTLS_SHA512_C */
7862#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007865{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007866 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007867
7868 /*
7869 * Free our handshake params
7870 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007871 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007873 ssl->handshake = NULL;
7874
7875 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007876 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007877 */
7878 if( ssl->transform )
7879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880 mbedtls_ssl_transform_free( ssl->transform );
7881 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007882 }
7883 ssl->transform = ssl->transform_negotiate;
7884 ssl->transform_negotiate = NULL;
7885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007887}
7888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007890{
7891 int resume = ssl->handshake->resume;
7892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007895#if defined(MBEDTLS_SSL_RENEGOTIATION)
7896 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007899 ssl->renego_records_seen = 0;
7900 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007901#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007902
7903 /*
7904 * Free the previous session and switch in the current one
7905 */
Paul Bakker0a597072012-09-25 21:55:46 +00007906 if( ssl->session )
7907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007909 /* RFC 7366 3.1: keep the EtM state */
7910 ssl->session_negotiate->encrypt_then_mac =
7911 ssl->session->encrypt_then_mac;
7912#endif
7913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007914 mbedtls_ssl_session_free( ssl->session );
7915 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007916 }
7917 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007918 ssl->session_negotiate = NULL;
7919
Paul Bakker0a597072012-09-25 21:55:46 +00007920 /*
7921 * Add cache entry
7922 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007923 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007924 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007925 resume == 0 )
7926 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007927 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007929 }
Paul Bakker0a597072012-09-25 21:55:46 +00007930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007931#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007932 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007933 ssl->handshake->flight != NULL )
7934 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007935 /* Cancel handshake timer */
7936 ssl_set_timer( ssl, 0 );
7937
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007938 /* Keep last flight around in case we need to resend it:
7939 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007941 }
7942 else
7943#endif
7944 ssl_handshake_wrapup_free_hs_transform( ssl );
7945
Paul Bakker48916f92012-09-16 19:57:18 +00007946 ssl->state++;
7947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007949}
7950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007952{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007953 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007956
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007957 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007958
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007959 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007960
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007961 /*
7962 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7963 * may define some other value. Currently (early 2016), no defined
7964 * ciphersuite does this (and this is unlikely to change as activity has
7965 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7966 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007969#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007970 ssl->verify_data_len = hash_len;
7971 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007972#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007973
Paul Bakker5121ce52009-01-03 21:22:43 +00007974 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7976 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007977
7978 /*
7979 * In case of session resuming, invert the client and server
7980 * ChangeCipherSpec messages order.
7981 */
Paul Bakker0a597072012-09-25 21:55:46 +00007982 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007984#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007985 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007987#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007988#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007989 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007990 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007991#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007992 }
7993 else
7994 ssl->state++;
7995
Paul Bakker48916f92012-09-16 19:57:18 +00007996 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007997 * Switch to our negotiated transform and session parameters for outbound
7998 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007999 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008000 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008002#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008003 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008004 {
8005 unsigned char i;
8006
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008007 /* Remember current epoch settings for resending */
8008 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01008009 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008010
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008011 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008012 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008013
8014 /* Increment epoch */
8015 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008016 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008017 break;
8018
8019 /* The loop goes to its end iff the counter is wrapping */
8020 if( i == 0 )
8021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8023 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008024 }
8025 }
8026 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008027#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01008028 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008029
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008030 ssl->transform_out = ssl->transform_negotiate;
8031 ssl->session_out = ssl->session_negotiate;
8032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008033#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8034 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008036 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008038 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8039 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008040 }
8041 }
8042#endif
8043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008044#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008045 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008046 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008047#endif
8048
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008049 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008050 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008052 return( ret );
8053 }
8054
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008055#if defined(MBEDTLS_SSL_PROTO_DTLS)
8056 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8057 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8058 {
8059 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8060 return( ret );
8061 }
8062#endif
8063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008065
8066 return( 0 );
8067}
8068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008069#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008070#define SSL_MAX_HASH_LEN 36
8071#else
8072#define SSL_MAX_HASH_LEN 12
8073#endif
8074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008075int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008076{
Paul Bakker23986e52011-04-24 08:57:21 +00008077 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008078 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008079 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008082
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008083 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008084
Hanno Becker327c93b2018-08-15 13:56:18 +01008085 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008087 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008088 return( ret );
8089 }
8090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008091 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008094 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8095 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008096 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008097 }
8098
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008099 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100#if defined(MBEDTLS_SSL_PROTO_SSL3)
8101 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008102 hash_len = 36;
8103 else
8104#endif
8105 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008107 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8108 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008111 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8112 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008113 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008114 }
8115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008116 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008117 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008120 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8121 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008122 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008123 }
8124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008125#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008126 ssl->verify_data_len = hash_len;
8127 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008128#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008129
Paul Bakker0a597072012-09-25 21:55:46 +00008130 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008132#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008133 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008135#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008136#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008137 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008139#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008140 }
8141 else
8142 ssl->state++;
8143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008144#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008145 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008147#endif
8148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008150
8151 return( 0 );
8152}
8153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008155{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008158#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8159 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8160 mbedtls_md5_init( &handshake->fin_md5 );
8161 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008162 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8163 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008164#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8166#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008167#if defined(MBEDTLS_USE_PSA_CRYPTO)
8168 handshake->fin_sha256_psa = psa_hash_operation_init();
8169 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8170#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008171 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008172 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008173#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008174#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008175#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008176#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008177 handshake->fin_sha384_psa = psa_hash_operation_init();
8178 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008179#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008180 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008181 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008182#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008183#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008184#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008185
8186 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008187
8188#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8189 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8190 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8191#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008193#if defined(MBEDTLS_DHM_C)
8194 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008195#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196#if defined(MBEDTLS_ECDH_C)
8197 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008198#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008199#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008200 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008201#if defined(MBEDTLS_SSL_CLI_C)
8202 handshake->ecjpake_cache = NULL;
8203 handshake->ecjpake_cache_len = 0;
8204#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008205#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008206
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008207#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008208 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008209#endif
8210
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008211#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8212 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8213#endif
Hanno Becker75173122019-02-06 16:18:31 +00008214
8215#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8216 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8217 mbedtls_pk_init( &handshake->peer_pubkey );
8218#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008219}
8220
Hanno Beckera18d1322018-01-03 14:27:32 +00008221void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008222{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008223 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008225 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8226 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008227
Hanno Beckerd56ed242018-01-03 15:32:51 +00008228#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008229 mbedtls_md_init( &transform->md_ctx_enc );
8230 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008231#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008232}
8233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008234void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008236 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008237}
8238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008239static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008240{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008241 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008242 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008243 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008244 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008245 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008246 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008247 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008248
8249 /*
8250 * Either the pointers are now NULL or cleared properly and can be freed.
8251 * Now allocate missing structures.
8252 */
8253 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008254 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008255 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008256 }
Paul Bakker48916f92012-09-16 19:57:18 +00008257
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008258 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008259 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008260 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008261 }
Paul Bakker48916f92012-09-16 19:57:18 +00008262
Paul Bakker82788fb2014-10-20 13:59:19 +02008263 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008264 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008265 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008266 }
Paul Bakker48916f92012-09-16 19:57:18 +00008267
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008268 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008269 if( ssl->handshake == NULL ||
8270 ssl->transform_negotiate == NULL ||
8271 ssl->session_negotiate == NULL )
8272 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275 mbedtls_free( ssl->handshake );
8276 mbedtls_free( ssl->transform_negotiate );
8277 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008278
8279 ssl->handshake = NULL;
8280 ssl->transform_negotiate = NULL;
8281 ssl->session_negotiate = NULL;
8282
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008283 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008284 }
8285
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008286 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008288 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008289 ssl_handshake_params_init( ssl->handshake );
8290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008291#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008292 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8293 {
8294 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008295
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008296 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8297 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8298 else
8299 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008300
8301 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008302 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008303#endif
8304
Paul Bakker48916f92012-09-16 19:57:18 +00008305 return( 0 );
8306}
8307
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008308#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008309/* Dummy cookie callbacks for defaults */
8310static int ssl_cookie_write_dummy( void *ctx,
8311 unsigned char **p, unsigned char *end,
8312 const unsigned char *cli_id, size_t cli_id_len )
8313{
8314 ((void) ctx);
8315 ((void) p);
8316 ((void) end);
8317 ((void) cli_id);
8318 ((void) cli_id_len);
8319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008320 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008321}
8322
8323static int ssl_cookie_check_dummy( void *ctx,
8324 const unsigned char *cookie, size_t cookie_len,
8325 const unsigned char *cli_id, size_t cli_id_len )
8326{
8327 ((void) ctx);
8328 ((void) cookie);
8329 ((void) cookie_len);
8330 ((void) cli_id);
8331 ((void) cli_id_len);
8332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008334}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008335#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008336
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008337/* Once ssl->out_hdr as the address of the beginning of the
8338 * next outgoing record is set, deduce the other pointers.
8339 *
8340 * Note: For TLS, we save the implicit record sequence number
8341 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8342 * and the caller has to make sure there's space for this.
8343 */
8344
8345static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8346 mbedtls_ssl_transform *transform )
8347{
8348#if defined(MBEDTLS_SSL_PROTO_DTLS)
8349 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8350 {
8351 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008352#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008353 ssl->out_cid = ssl->out_ctr + 8;
8354 ssl->out_len = ssl->out_cid;
8355 if( transform != NULL )
8356 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008357#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008358 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008359#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008360 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008361 }
8362 else
8363#endif
8364 {
8365 ssl->out_ctr = ssl->out_hdr - 8;
8366 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008367#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008368 ssl->out_cid = ssl->out_len;
8369#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008370 ssl->out_iv = ssl->out_hdr + 5;
8371 }
8372
8373 /* Adjust out_msg to make space for explicit IV, if used. */
8374 if( transform != NULL &&
8375 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8376 {
8377 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8378 }
8379 else
8380 ssl->out_msg = ssl->out_iv;
8381}
8382
8383/* Once ssl->in_hdr as the address of the beginning of the
8384 * next incoming record is set, deduce the other pointers.
8385 *
8386 * Note: For TLS, we save the implicit record sequence number
8387 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8388 * and the caller has to make sure there's space for this.
8389 */
8390
Hanno Becker79594fd2019-05-08 09:38:41 +01008391static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008392{
Hanno Becker79594fd2019-05-08 09:38:41 +01008393 /* This function sets the pointers to match the case
8394 * of unprotected TLS/DTLS records, with both ssl->in_iv
8395 * and ssl->in_msg pointing to the beginning of the record
8396 * content.
8397 *
8398 * When decrypting a protected record, ssl->in_msg
8399 * will be shifted to point to the beginning of the
8400 * record plaintext.
8401 */
8402
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008403#if defined(MBEDTLS_SSL_PROTO_DTLS)
8404 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8405 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008406 /* This sets the header pointers to match records
8407 * without CID. When we receive a record containing
8408 * a CID, the fields are shifted accordingly in
8409 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008410 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008411#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008412 ssl->in_cid = ssl->in_ctr + 8;
8413 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008414#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008415 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008416#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008417 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008418 }
8419 else
8420#endif
8421 {
8422 ssl->in_ctr = ssl->in_hdr - 8;
8423 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008424#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008425 ssl->in_cid = ssl->in_len;
8426#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008427 ssl->in_iv = ssl->in_hdr + 5;
8428 }
8429
Hanno Becker79594fd2019-05-08 09:38:41 +01008430 /* This will be adjusted at record decryption time. */
8431 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008432}
8433
Paul Bakker5121ce52009-01-03 21:22:43 +00008434/*
8435 * Initialize an SSL context
8436 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008437void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8438{
8439 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8440}
8441
8442/*
8443 * Setup an SSL context
8444 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008445
8446static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8447{
8448 /* Set the incoming and outgoing record pointers. */
8449#if defined(MBEDTLS_SSL_PROTO_DTLS)
8450 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8451 {
8452 ssl->out_hdr = ssl->out_buf;
8453 ssl->in_hdr = ssl->in_buf;
8454 }
8455 else
8456#endif /* MBEDTLS_SSL_PROTO_DTLS */
8457 {
8458 ssl->out_hdr = ssl->out_buf + 8;
8459 ssl->in_hdr = ssl->in_buf + 8;
8460 }
8461
8462 /* Derive other internal pointers. */
8463 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008464 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008465}
8466
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008467int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008468 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008469{
Paul Bakker48916f92012-09-16 19:57:18 +00008470 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008471
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008472 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008473
8474 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008475 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008476 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008477
8478 /* Set to NULL in case of an error condition */
8479 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008480
Angus Grattond8213d02016-05-25 20:56:48 +10008481 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8482 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008483 {
Angus Grattond8213d02016-05-25 20:56:48 +10008484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008485 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008486 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008487 }
8488
8489 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8490 if( ssl->out_buf == NULL )
8491 {
8492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008493 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008494 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008495 }
8496
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008497 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008498
Paul Bakker48916f92012-09-16 19:57:18 +00008499 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008500 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008501
8502 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008503
8504error:
8505 mbedtls_free( ssl->in_buf );
8506 mbedtls_free( ssl->out_buf );
8507
8508 ssl->conf = NULL;
8509
8510 ssl->in_buf = NULL;
8511 ssl->out_buf = NULL;
8512
8513 ssl->in_hdr = NULL;
8514 ssl->in_ctr = NULL;
8515 ssl->in_len = NULL;
8516 ssl->in_iv = NULL;
8517 ssl->in_msg = NULL;
8518
8519 ssl->out_hdr = NULL;
8520 ssl->out_ctr = NULL;
8521 ssl->out_len = NULL;
8522 ssl->out_iv = NULL;
8523 ssl->out_msg = NULL;
8524
k-stachowiak9f7798e2018-07-31 16:52:32 +02008525 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008526}
8527
8528/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008529 * Reset an initialized and used SSL context for re-use while retaining
8530 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008531 *
8532 * If partial is non-zero, keep data in the input buffer and client ID.
8533 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008534 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008535static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008536{
Paul Bakker48916f92012-09-16 19:57:18 +00008537 int ret;
8538
Hanno Becker7e772132018-08-10 12:38:21 +01008539#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8540 !defined(MBEDTLS_SSL_SRV_C)
8541 ((void) partial);
8542#endif
8543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008544 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008545
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008546 /* Cancel any possibly running timer */
8547 ssl_set_timer( ssl, 0 );
8548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008549#if defined(MBEDTLS_SSL_RENEGOTIATION)
8550 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008551 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008552
8553 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008554 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8555 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008556#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008557 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008558
Paul Bakker7eb013f2011-10-06 12:37:39 +00008559 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008560 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008561
8562 ssl->in_msgtype = 0;
8563 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008565 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008566 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008567#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008568#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008569 ssl_dtls_replay_reset( ssl );
8570#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008571
8572 ssl->in_hslen = 0;
8573 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008574
8575 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008576
8577 ssl->out_msgtype = 0;
8578 ssl->out_msglen = 0;
8579 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008580#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8581 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008582 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008583#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008584
Hanno Becker19859472018-08-06 09:40:20 +01008585 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8586
Paul Bakker48916f92012-09-16 19:57:18 +00008587 ssl->transform_in = NULL;
8588 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008589
Hanno Becker78640902018-08-13 16:35:15 +01008590 ssl->session_in = NULL;
8591 ssl->session_out = NULL;
8592
Angus Grattond8213d02016-05-25 20:56:48 +10008593 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008594
8595#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008596 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008597#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8598 {
8599 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008600 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008601 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008603#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8604 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8607 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8610 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008611 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008612 }
8613#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008614
Paul Bakker48916f92012-09-16 19:57:18 +00008615 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008617 mbedtls_ssl_transform_free( ssl->transform );
8618 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008619 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008620 }
Paul Bakker48916f92012-09-16 19:57:18 +00008621
Paul Bakkerc0463502013-02-14 11:19:38 +01008622 if( ssl->session )
8623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008624 mbedtls_ssl_session_free( ssl->session );
8625 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008626 ssl->session = NULL;
8627 }
8628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008629#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008630 ssl->alpn_chosen = NULL;
8631#endif
8632
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008633#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008634#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008635 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008636#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008637 {
8638 mbedtls_free( ssl->cli_id );
8639 ssl->cli_id = NULL;
8640 ssl->cli_id_len = 0;
8641 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008642#endif
8643
Paul Bakker48916f92012-09-16 19:57:18 +00008644 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8645 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008646
8647 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008648}
8649
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008650/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008651 * Reset an initialized and used SSL context for re-use while retaining
8652 * all application-set variables, function pointers and data.
8653 */
8654int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8655{
8656 return( ssl_session_reset_int( ssl, 0 ) );
8657}
8658
8659/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008660 * SSL set accessors
8661 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008662void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008663{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008664 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008665}
8666
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008667void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008668{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008669 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008670}
8671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008672#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008673void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008674{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008675 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008676}
8677#endif
8678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008679#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008680void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008681{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008682 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008683}
8684#endif
8685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008687
Hanno Becker1841b0a2018-08-24 11:13:57 +01008688void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8689 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008690{
8691 ssl->disable_datagram_packing = !allow_packing;
8692}
8693
8694void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8695 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008696{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008697 conf->hs_timeout_min = min;
8698 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008699}
8700#endif
8701
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008702void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008703{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008704 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008705}
8706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008707#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008708void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008709 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008710 void *p_vrfy )
8711{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008712 conf->f_vrfy = f_vrfy;
8713 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008714}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008715#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008716
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008717void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008718 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008719 void *p_rng )
8720{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008721 conf->f_rng = f_rng;
8722 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008723}
8724
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008725void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008726 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008727 void *p_dbg )
8728{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008729 conf->f_dbg = f_dbg;
8730 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008731}
8732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008733void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008734 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008735 mbedtls_ssl_send_t *f_send,
8736 mbedtls_ssl_recv_t *f_recv,
8737 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008738{
8739 ssl->p_bio = p_bio;
8740 ssl->f_send = f_send;
8741 ssl->f_recv = f_recv;
8742 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008743}
8744
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008745#if defined(MBEDTLS_SSL_PROTO_DTLS)
8746void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8747{
8748 ssl->mtu = mtu;
8749}
8750#endif
8751
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008752void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008753{
8754 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008755}
8756
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008757void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8758 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008759 mbedtls_ssl_set_timer_t *f_set_timer,
8760 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008761{
8762 ssl->p_timer = p_timer;
8763 ssl->f_set_timer = f_set_timer;
8764 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008765
8766 /* Make sure we start with no timer running */
8767 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008768}
8769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008770#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008771void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008772 void *p_cache,
8773 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8774 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008775{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008776 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008777 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008778 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008779}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008780#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008782#if defined(MBEDTLS_SSL_CLI_C)
8783int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008784{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008785 int ret;
8786
8787 if( ssl == NULL ||
8788 session == NULL ||
8789 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008790 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008792 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008793 }
8794
Hanno Becker52055ae2019-02-06 14:30:46 +00008795 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8796 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008797 return( ret );
8798
Paul Bakker0a597072012-09-25 21:55:46 +00008799 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008800
8801 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008802}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008803#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008804
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008805void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008806 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008807{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008808 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8809 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8810 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8811 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008812}
8813
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008814void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008815 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008816 int major, int minor )
8817{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008818 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008819 return;
8820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008821 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008822 return;
8823
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008824 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008825}
8826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008827#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008828void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008829 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008830{
8831 conf->cert_profile = profile;
8832}
8833
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008834/* Append a new keycert entry to a (possibly empty) list */
8835static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8836 mbedtls_x509_crt *cert,
8837 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008838{
niisato8ee24222018-06-25 19:05:48 +09008839 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008840
niisato8ee24222018-06-25 19:05:48 +09008841 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8842 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008843 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008844
niisato8ee24222018-06-25 19:05:48 +09008845 new_cert->cert = cert;
8846 new_cert->key = key;
8847 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008848
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008849 /* Update head is the list was null, else add to the end */
8850 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008851 {
niisato8ee24222018-06-25 19:05:48 +09008852 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008853 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008854 else
8855 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008856 mbedtls_ssl_key_cert *cur = *head;
8857 while( cur->next != NULL )
8858 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008859 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008860 }
8861
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008862 return( 0 );
8863}
8864
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008865int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008866 mbedtls_x509_crt *own_cert,
8867 mbedtls_pk_context *pk_key )
8868{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008869 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008870}
8871
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008872void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008873 mbedtls_x509_crt *ca_chain,
8874 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008875{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008876 conf->ca_chain = ca_chain;
8877 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008878
8879#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8880 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8881 * cannot be used together. */
8882 conf->f_ca_cb = NULL;
8883 conf->p_ca_cb = NULL;
8884#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008885}
Hanno Becker5adaad92019-03-27 16:54:37 +00008886
8887#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8888void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008889 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008890 void *p_ca_cb )
8891{
8892 conf->f_ca_cb = f_ca_cb;
8893 conf->p_ca_cb = p_ca_cb;
8894
8895 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8896 * cannot be used together. */
8897 conf->ca_chain = NULL;
8898 conf->ca_crl = NULL;
8899}
8900#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008901#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008902
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008903#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8904int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8905 mbedtls_x509_crt *own_cert,
8906 mbedtls_pk_context *pk_key )
8907{
8908 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8909 own_cert, pk_key ) );
8910}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008911
8912void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8913 mbedtls_x509_crt *ca_chain,
8914 mbedtls_x509_crl *ca_crl )
8915{
8916 ssl->handshake->sni_ca_chain = ca_chain;
8917 ssl->handshake->sni_ca_crl = ca_crl;
8918}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008919
8920void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8921 int authmode )
8922{
8923 ssl->handshake->sni_authmode = authmode;
8924}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008925#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8926
Hanno Becker8927c832019-04-03 12:52:50 +01008927#if defined(MBEDTLS_X509_CRT_PARSE_C)
8928void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8929 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8930 void *p_vrfy )
8931{
8932 ssl->f_vrfy = f_vrfy;
8933 ssl->p_vrfy = p_vrfy;
8934}
8935#endif
8936
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008937#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008938/*
8939 * Set EC J-PAKE password for current handshake
8940 */
8941int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8942 const unsigned char *pw,
8943 size_t pw_len )
8944{
8945 mbedtls_ecjpake_role role;
8946
Janos Follath8eb64132016-06-03 15:40:57 +01008947 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008948 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8949
8950 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8951 role = MBEDTLS_ECJPAKE_SERVER;
8952 else
8953 role = MBEDTLS_ECJPAKE_CLIENT;
8954
8955 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8956 role,
8957 MBEDTLS_MD_SHA256,
8958 MBEDTLS_ECP_DP_SECP256R1,
8959 pw, pw_len ) );
8960}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008961#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008963#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008964
8965static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8966{
8967 /* Remove reference to existing PSK, if any. */
8968#if defined(MBEDTLS_USE_PSA_CRYPTO)
8969 if( conf->psk_opaque != 0 )
8970 {
8971 /* The maintenance of the PSK key slot is the
8972 * user's responsibility. */
8973 conf->psk_opaque = 0;
8974 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008975 /* This and the following branch should never
8976 * be taken simultaenously as we maintain the
8977 * invariant that raw and opaque PSKs are never
8978 * configured simultaneously. As a safeguard,
8979 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008980#endif /* MBEDTLS_USE_PSA_CRYPTO */
8981 if( conf->psk != NULL )
8982 {
8983 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8984
8985 mbedtls_free( conf->psk );
8986 conf->psk = NULL;
8987 conf->psk_len = 0;
8988 }
8989
8990 /* Remove reference to PSK identity, if any. */
8991 if( conf->psk_identity != NULL )
8992 {
8993 mbedtls_free( conf->psk_identity );
8994 conf->psk_identity = NULL;
8995 conf->psk_identity_len = 0;
8996 }
8997}
8998
Hanno Becker7390c712018-11-15 13:33:04 +00008999/* This function assumes that PSK identity in the SSL config is unset.
9000 * It checks that the provided identity is well-formed and attempts
9001 * to make a copy of it in the SSL config.
9002 * On failure, the PSK identity in the config remains unset. */
9003static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
9004 unsigned char const *psk_identity,
9005 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009006{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009007 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00009008 if( psk_identity == NULL ||
9009 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009010 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009011 {
9012 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9013 }
9014
Hanno Becker7390c712018-11-15 13:33:04 +00009015 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
9016 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009017 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02009018
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009019 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009020 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02009021
9022 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02009023}
9024
Hanno Becker7390c712018-11-15 13:33:04 +00009025int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
9026 const unsigned char *psk, size_t psk_len,
9027 const unsigned char *psk_identity, size_t psk_identity_len )
9028{
9029 int ret;
9030 /* Remove opaque/raw PSK + PSK Identity */
9031 ssl_conf_remove_psk( conf );
9032
9033 /* Check and set raw PSK */
9034 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
9035 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9036 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
9037 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9038 conf->psk_len = psk_len;
9039 memcpy( conf->psk, psk, conf->psk_len );
9040
9041 /* Check and set PSK Identity */
9042 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
9043 if( ret != 0 )
9044 ssl_conf_remove_psk( conf );
9045
9046 return( ret );
9047}
9048
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009049static void ssl_remove_psk( mbedtls_ssl_context *ssl )
9050{
9051#if defined(MBEDTLS_USE_PSA_CRYPTO)
9052 if( ssl->handshake->psk_opaque != 0 )
9053 {
9054 ssl->handshake->psk_opaque = 0;
9055 }
9056 else
9057#endif /* MBEDTLS_USE_PSA_CRYPTO */
9058 if( ssl->handshake->psk != NULL )
9059 {
9060 mbedtls_platform_zeroize( ssl->handshake->psk,
9061 ssl->handshake->psk_len );
9062 mbedtls_free( ssl->handshake->psk );
9063 ssl->handshake->psk_len = 0;
9064 }
9065}
9066
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009067int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9068 const unsigned char *psk, size_t psk_len )
9069{
9070 if( psk == NULL || ssl->handshake == NULL )
9071 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9072
9073 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9074 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9075
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009076 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009077
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009078 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009079 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009080
9081 ssl->handshake->psk_len = psk_len;
9082 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
9083
9084 return( 0 );
9085}
9086
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009087#if defined(MBEDTLS_USE_PSA_CRYPTO)
9088int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009089 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009090 const unsigned char *psk_identity,
9091 size_t psk_identity_len )
9092{
Hanno Becker7390c712018-11-15 13:33:04 +00009093 int ret;
9094 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009095 ssl_conf_remove_psk( conf );
9096
Hanno Becker7390c712018-11-15 13:33:04 +00009097 /* Check and set opaque PSK */
9098 if( psk_slot == 0 )
9099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009100 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009101
9102 /* Check and set PSK Identity */
9103 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9104 psk_identity_len );
9105 if( ret != 0 )
9106 ssl_conf_remove_psk( conf );
9107
9108 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009109}
9110
9111int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009112 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009113{
9114 if( psk_slot == 0 || ssl->handshake == NULL )
9115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9116
9117 ssl_remove_psk( ssl );
9118 ssl->handshake->psk_opaque = psk_slot;
9119 return( 0 );
9120}
9121#endif /* MBEDTLS_USE_PSA_CRYPTO */
9122
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009123void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009124 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009125 size_t),
9126 void *p_psk )
9127{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009128 conf->f_psk = f_psk;
9129 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009130}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009131#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009132
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009133#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009134
9135#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009136int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009137{
9138 int ret;
9139
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009140 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9141 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9142 {
9143 mbedtls_mpi_free( &conf->dhm_P );
9144 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009145 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009146 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009147
9148 return( 0 );
9149}
Hanno Becker470a8c42017-10-04 15:28:46 +01009150#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009151
Hanno Beckera90658f2017-10-04 15:29:08 +01009152int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9153 const unsigned char *dhm_P, size_t P_len,
9154 const unsigned char *dhm_G, size_t G_len )
9155{
9156 int ret;
9157
9158 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9159 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9160 {
9161 mbedtls_mpi_free( &conf->dhm_P );
9162 mbedtls_mpi_free( &conf->dhm_G );
9163 return( ret );
9164 }
9165
9166 return( 0 );
9167}
Paul Bakker5121ce52009-01-03 21:22:43 +00009168
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009169int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009170{
9171 int ret;
9172
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009173 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9174 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9175 {
9176 mbedtls_mpi_free( &conf->dhm_P );
9177 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009178 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009179 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009180
9181 return( 0 );
9182}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009183#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009184
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009185#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9186/*
9187 * Set the minimum length for Diffie-Hellman parameters
9188 */
9189void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9190 unsigned int bitlen )
9191{
9192 conf->dhm_min_bitlen = bitlen;
9193}
9194#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9195
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009196#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009197/*
9198 * Set allowed/preferred hashes for handshake signatures
9199 */
9200void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9201 const int *hashes )
9202{
9203 conf->sig_hashes = hashes;
9204}
Hanno Becker947194e2017-04-07 13:25:49 +01009205#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009206
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009207#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009208/*
9209 * Set the allowed elliptic curves
9210 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009211void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009212 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009213{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009214 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009215}
Hanno Becker947194e2017-04-07 13:25:49 +01009216#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009217
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009218#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009219int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009220{
Hanno Becker947194e2017-04-07 13:25:49 +01009221 /* Initialize to suppress unnecessary compiler warning */
9222 size_t hostname_len = 0;
9223
9224 /* Check if new hostname is valid before
9225 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009226 if( hostname != NULL )
9227 {
9228 hostname_len = strlen( hostname );
9229
9230 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9232 }
9233
9234 /* Now it's clear that we will overwrite the old hostname,
9235 * so we can free it safely */
9236
9237 if( ssl->hostname != NULL )
9238 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009239 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009240 mbedtls_free( ssl->hostname );
9241 }
9242
9243 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009244
Paul Bakker5121ce52009-01-03 21:22:43 +00009245 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009246 {
9247 ssl->hostname = NULL;
9248 }
9249 else
9250 {
9251 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009252 if( ssl->hostname == NULL )
9253 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009254
Hanno Becker947194e2017-04-07 13:25:49 +01009255 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009256
Hanno Becker947194e2017-04-07 13:25:49 +01009257 ssl->hostname[hostname_len] = '\0';
9258 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009259
9260 return( 0 );
9261}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009262#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009263
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009264#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009265void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009266 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009267 const unsigned char *, size_t),
9268 void *p_sni )
9269{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009270 conf->f_sni = f_sni;
9271 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009272}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009273#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009275#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009276int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009277{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009278 size_t cur_len, tot_len;
9279 const char **p;
9280
9281 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009282 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9283 * MUST NOT be truncated."
9284 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009285 */
9286 tot_len = 0;
9287 for( p = protos; *p != NULL; p++ )
9288 {
9289 cur_len = strlen( *p );
9290 tot_len += cur_len;
9291
9292 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009293 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009294 }
9295
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009296 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009297
9298 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009299}
9300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009301const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009302{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009303 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009304}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009305#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009306
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009307void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009308{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009309 conf->max_major_ver = major;
9310 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009311}
9312
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009313void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009314{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009315 conf->min_major_ver = major;
9316 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009317}
9318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009319#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009320void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009321{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009322 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009323}
9324#endif
9325
Janos Follath088ce432017-04-10 12:42:31 +01009326#if defined(MBEDTLS_SSL_SRV_C)
9327void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9328 char cert_req_ca_list )
9329{
9330 conf->cert_req_ca_list = cert_req_ca_list;
9331}
9332#endif
9333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009334#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009335void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009336{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009337 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009338}
9339#endif
9340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009342void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009343{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009344 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009345}
9346#endif
9347
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009348#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009349void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009350{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009351 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009352}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009353#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009356int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009357{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009358 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009359 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009361 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009362 }
9363
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009364 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009365
9366 return( 0 );
9367}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009368#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009370#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009371void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009372{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009373 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009374}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009375#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009378void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009379{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009380 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009381}
9382#endif
9383
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009384void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009385{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009386 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009387}
9388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009389#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009390void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009391{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009392 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009393}
9394
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009395void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009396{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009397 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009398}
9399
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009400void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009401 const unsigned char period[8] )
9402{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009403 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009405#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009407#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009408#if defined(MBEDTLS_SSL_CLI_C)
9409void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009410{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009411 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009412}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009413#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009414
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009415#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009416void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9417 mbedtls_ssl_ticket_write_t *f_ticket_write,
9418 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9419 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009420{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009421 conf->f_ticket_write = f_ticket_write;
9422 conf->f_ticket_parse = f_ticket_parse;
9423 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009424}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009425#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009426#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009427
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009428#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9429void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9430 mbedtls_ssl_export_keys_t *f_export_keys,
9431 void *p_export_keys )
9432{
9433 conf->f_export_keys = f_export_keys;
9434 conf->p_export_keys = p_export_keys;
9435}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009436
9437void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9438 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9439 void *p_export_keys )
9440{
9441 conf->f_export_keys_ext = f_export_keys_ext;
9442 conf->p_export_keys = p_export_keys;
9443}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009444#endif
9445
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009446#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009447void mbedtls_ssl_conf_async_private_cb(
9448 mbedtls_ssl_config *conf,
9449 mbedtls_ssl_async_sign_t *f_async_sign,
9450 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9451 mbedtls_ssl_async_resume_t *f_async_resume,
9452 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009453 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009454{
9455 conf->f_async_sign_start = f_async_sign;
9456 conf->f_async_decrypt_start = f_async_decrypt;
9457 conf->f_async_resume = f_async_resume;
9458 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009459 conf->p_async_config_data = async_config_data;
9460}
9461
Gilles Peskine8f97af72018-04-26 11:46:10 +02009462void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9463{
9464 return( conf->p_async_config_data );
9465}
9466
Gilles Peskine1febfef2018-04-30 11:54:39 +02009467void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009468{
9469 if( ssl->handshake == NULL )
9470 return( NULL );
9471 else
9472 return( ssl->handshake->user_async_ctx );
9473}
9474
Gilles Peskine1febfef2018-04-30 11:54:39 +02009475void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009476 void *ctx )
9477{
9478 if( ssl->handshake != NULL )
9479 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009480}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009481#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009482
Paul Bakker5121ce52009-01-03 21:22:43 +00009483/*
9484 * SSL get accessors
9485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009487{
9488 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9489}
9490
Hanno Becker8b170a02017-10-10 11:51:19 +01009491int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9492{
9493 /*
9494 * Case A: We're currently holding back
9495 * a message for further processing.
9496 */
9497
9498 if( ssl->keep_current_message == 1 )
9499 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009500 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009501 return( 1 );
9502 }
9503
9504 /*
9505 * Case B: Further records are pending in the current datagram.
9506 */
9507
9508#if defined(MBEDTLS_SSL_PROTO_DTLS)
9509 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9510 ssl->in_left > ssl->next_record_offset )
9511 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009512 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009513 return( 1 );
9514 }
9515#endif /* MBEDTLS_SSL_PROTO_DTLS */
9516
9517 /*
9518 * Case C: A handshake message is being processed.
9519 */
9520
Hanno Becker8b170a02017-10-10 11:51:19 +01009521 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9522 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009523 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009524 return( 1 );
9525 }
9526
9527 /*
9528 * Case D: An application data message is being processed
9529 */
9530 if( ssl->in_offt != NULL )
9531 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009532 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009533 return( 1 );
9534 }
9535
9536 /*
9537 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009538 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009539 * we implement support for multiple alerts in single records.
9540 */
9541
9542 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9543 return( 0 );
9544}
9545
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009546uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009547{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009548 if( ssl->session != NULL )
9549 return( ssl->session->verify_result );
9550
9551 if( ssl->session_negotiate != NULL )
9552 return( ssl->session_negotiate->verify_result );
9553
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009554 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009555}
9556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009557const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009558{
Paul Bakker926c8e42013-03-06 10:23:34 +01009559 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009560 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009562 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009563}
9564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009566{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009568 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009569 {
9570 switch( ssl->minor_ver )
9571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009573 return( "DTLSv1.0" );
9574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009575 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009576 return( "DTLSv1.2" );
9577
9578 default:
9579 return( "unknown (DTLS)" );
9580 }
9581 }
9582#endif
9583
Paul Bakker43ca69c2011-01-15 17:35:19 +00009584 switch( ssl->minor_ver )
9585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009586 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009587 return( "SSLv3.0" );
9588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009589 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009590 return( "TLSv1.0" );
9591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009592 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009593 return( "TLSv1.1" );
9594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009595 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009596 return( "TLSv1.2" );
9597
Paul Bakker43ca69c2011-01-15 17:35:19 +00009598 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009599 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009600 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009601}
9602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009604{
Hanno Becker3136ede2018-08-17 15:28:19 +01009605 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009606 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009607 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009608
Hanno Becker5903de42019-05-03 14:46:38 +01009609 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9610
Hanno Becker78640902018-08-13 16:35:15 +01009611 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009612 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614#if defined(MBEDTLS_ZLIB_SUPPORT)
9615 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9616 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009617#endif
9618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009619 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009621 case MBEDTLS_MODE_GCM:
9622 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009623 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009624 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009625 transform_expansion = transform->minlen;
9626 break;
9627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009628 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009629
9630 block_size = mbedtls_cipher_get_block_size(
9631 &transform->cipher_ctx_enc );
9632
Hanno Becker3136ede2018-08-17 15:28:19 +01009633 /* Expansion due to the addition of the MAC. */
9634 transform_expansion += transform->maclen;
9635
9636 /* Expansion due to the addition of CBC padding;
9637 * Theoretically up to 256 bytes, but we never use
9638 * more than the block size of the underlying cipher. */
9639 transform_expansion += block_size;
9640
9641 /* For TLS 1.1 or higher, an explicit IV is added
9642 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009643#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9644 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009645 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009646#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009647
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009648 break;
9649
9650 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009652 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009653 }
9654
Hanno Beckera0e20d02019-05-15 14:03:01 +01009655#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009656 if( transform->out_cid_len != 0 )
9657 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009658#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009659
Hanno Becker5903de42019-05-03 14:46:38 +01009660 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009661}
9662
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009663#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9664size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9665{
9666 size_t max_len;
9667
9668 /*
9669 * Assume mfl_code is correct since it was checked when set
9670 */
Angus Grattond8213d02016-05-25 20:56:48 +10009671 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009672
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009673 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009674 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009675 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009676 {
Angus Grattond8213d02016-05-25 20:56:48 +10009677 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009678 }
9679
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009680 /* During a handshake, use the value being negotiated */
9681 if( ssl->session_negotiate != NULL &&
9682 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9683 {
9684 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9685 }
9686
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009687 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009688}
9689#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9690
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009691#if defined(MBEDTLS_SSL_PROTO_DTLS)
9692static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9693{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009694 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9695 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9696 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9697 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9698 return ( 0 );
9699
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009700 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9701 return( ssl->mtu );
9702
9703 if( ssl->mtu == 0 )
9704 return( ssl->handshake->mtu );
9705
9706 return( ssl->mtu < ssl->handshake->mtu ?
9707 ssl->mtu : ssl->handshake->mtu );
9708}
9709#endif /* MBEDTLS_SSL_PROTO_DTLS */
9710
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009711int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9712{
9713 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9714
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009715#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9716 !defined(MBEDTLS_SSL_PROTO_DTLS)
9717 (void) ssl;
9718#endif
9719
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009720#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9721 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9722
9723 if( max_len > mfl )
9724 max_len = mfl;
9725#endif
9726
9727#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009728 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009729 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009730 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009731 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9732 const size_t overhead = (size_t) ret;
9733
9734 if( ret < 0 )
9735 return( ret );
9736
9737 if( mtu <= overhead )
9738 {
9739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9740 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9741 }
9742
9743 if( max_len > mtu - overhead )
9744 max_len = mtu - overhead;
9745 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009746#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009747
Hanno Becker0defedb2018-08-10 12:35:02 +01009748#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9749 !defined(MBEDTLS_SSL_PROTO_DTLS)
9750 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009751#endif
9752
9753 return( (int) max_len );
9754}
9755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009756#if defined(MBEDTLS_X509_CRT_PARSE_C)
9757const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009758{
9759 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009760 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009761
Hanno Beckere6824572019-02-07 13:18:46 +00009762#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009763 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009764#else
9765 return( NULL );
9766#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009767}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009768#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009771int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9772 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009773{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009774 if( ssl == NULL ||
9775 dst == NULL ||
9776 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009777 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009780 }
9781
Hanno Becker52055ae2019-02-06 14:30:46 +00009782 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009783}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009784#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009785
Paul Bakker5121ce52009-01-03 21:22:43 +00009786/*
Paul Bakker1961b702013-01-25 14:49:24 +01009787 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009789int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009790{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009791 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009792
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009793 if( ssl == NULL || ssl->conf == NULL )
9794 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009796#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009797 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009798 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009799#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009800#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009801 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009803#endif
9804
Paul Bakker1961b702013-01-25 14:49:24 +01009805 return( ret );
9806}
9807
9808/*
9809 * Perform the SSL handshake
9810 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009811int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009812{
9813 int ret = 0;
9814
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009815 if( ssl == NULL || ssl->conf == NULL )
9816 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009823
9824 if( ret != 0 )
9825 break;
9826 }
9827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009829
9830 return( ret );
9831}
9832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009833#if defined(MBEDTLS_SSL_RENEGOTIATION)
9834#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009835/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009836 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009837 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009838static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009839{
9840 int ret;
9841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009843
9844 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9846 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009847
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009848 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009849 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009850 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009851 return( ret );
9852 }
9853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009855
9856 return( 0 );
9857}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009858#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009859
9860/*
9861 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862 * - any side: calling mbedtls_ssl_renegotiate(),
9863 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9864 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009865 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009866 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009867 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009868 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009869static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009870{
9871 int ret;
9872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009874
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009875 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9876 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009877
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009878 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9879 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009880#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009881 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009882 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009883 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009884 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009885 ssl->handshake->out_msg_seq = 1;
9886 else
9887 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009888 }
9889#endif
9890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009891 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9892 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009894 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009896 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009897 return( ret );
9898 }
9899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009901
9902 return( 0 );
9903}
9904
9905/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009906 * Renegotiate current connection on client,
9907 * or request renegotiation on server
9908 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009909int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009910{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009911 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009912
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009913 if( ssl == NULL || ssl->conf == NULL )
9914 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009916#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009917 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009918 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009920 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9921 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009923 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009924
9925 /* Did we already try/start sending HelloRequest? */
9926 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009927 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009928
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009929 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009930 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009931#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009933#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009934 /*
9935 * On client, either start the renegotiation process or,
9936 * if already in progress, continue the handshake
9937 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009938 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009940 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9941 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009942
9943 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009945 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009946 return( ret );
9947 }
9948 }
9949 else
9950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009951 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009953 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009954 return( ret );
9955 }
9956 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009957#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009958
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009959 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009960}
9961
9962/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009963 * Check record counters and renegotiate if they're above the limit.
9964 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009965static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009966{
Andres AG2196c7f2016-12-15 17:01:16 +00009967 size_t ep_len = ssl_ep_len( ssl );
9968 int in_ctr_cmp;
9969 int out_ctr_cmp;
9970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009971 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9972 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009973 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009974 {
9975 return( 0 );
9976 }
9977
Andres AG2196c7f2016-12-15 17:01:16 +00009978 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9979 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009980 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009981 ssl->conf->renego_period + ep_len, 8 - ep_len );
9982
9983 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009984 {
9985 return( 0 );
9986 }
9987
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009989 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009990}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009991#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009992
9993/*
9994 * Receive application data decrypted from the SSL layer
9995 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009996int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009997{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009998 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009999 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010000
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010001 if( ssl == NULL || ssl->conf == NULL )
10002 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010006#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010007 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010009 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010010 return( ret );
10011
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010012 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010013 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010014 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010015 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010016 return( ret );
10017 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010018 }
10019#endif
10020
Hanno Becker4a810fb2017-05-24 16:27:30 +010010021 /*
10022 * Check if renegotiation is necessary and/or handshake is
10023 * in process. If yes, perform/continue, and fall through
10024 * if an unexpected packet is received while the client
10025 * is waiting for the ServerHello.
10026 *
10027 * (There is no equivalent to the last condition on
10028 * the server-side as it is not treated as within
10029 * a handshake while waiting for the ClientHello
10030 * after a renegotiation request.)
10031 */
10032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010033#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010034 ret = ssl_check_ctr_renegotiate( ssl );
10035 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10036 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010038 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010039 return( ret );
10040 }
10041#endif
10042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010043 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010045 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010046 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10047 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010049 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010050 return( ret );
10051 }
10052 }
10053
Hanno Beckere41158b2017-10-23 13:30:32 +010010054 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010055 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010056 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010057 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010058 if( ssl->f_get_timer != NULL &&
10059 ssl->f_get_timer( ssl->p_timer ) == -1 )
10060 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010061 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010062 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010063
Hanno Becker327c93b2018-08-15 13:56:18 +010010064 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010065 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010066 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10067 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010068
Hanno Becker4a810fb2017-05-24 16:27:30 +010010069 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10070 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010071 }
10072
10073 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010074 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010075 {
10076 /*
10077 * OpenSSL sends empty messages to randomize the IV
10078 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010079 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010081 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010082 return( 0 );
10083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010085 return( ret );
10086 }
10087 }
10088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010089 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010092
Hanno Becker4a810fb2017-05-24 16:27:30 +010010093 /*
10094 * - For client-side, expect SERVER_HELLO_REQUEST.
10095 * - For server-side, expect CLIENT_HELLO.
10096 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10097 */
10098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010100 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010101 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010102 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010105
10106 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010107#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010108 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010109 {
10110 continue;
10111 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010112#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010113 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010114 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010115#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010116
Hanno Becker4a810fb2017-05-24 16:27:30 +010010117#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010118 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010119 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010122
10123 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010124#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010125 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010126 {
10127 continue;
10128 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010129#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010130 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010131 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010132#endif /* MBEDTLS_SSL_SRV_C */
10133
Hanno Becker21df7f92017-10-17 11:03:26 +010010134#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010135 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010136 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10137 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10138 ssl->conf->allow_legacy_renegotiation ==
10139 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10140 {
10141 /*
10142 * Accept renegotiation request
10143 */
Paul Bakker48916f92012-09-16 19:57:18 +000010144
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010145 /* DTLS clients need to know renego is server-initiated */
10146#if defined(MBEDTLS_SSL_PROTO_DTLS)
10147 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10148 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10149 {
10150 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10151 }
10152#endif
10153 ret = ssl_start_renegotiation( ssl );
10154 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10155 ret != 0 )
10156 {
10157 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10158 return( ret );
10159 }
10160 }
10161 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010162#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010163 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010164 /*
10165 * Refuse renegotiation
10166 */
10167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010168 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010170#if defined(MBEDTLS_SSL_PROTO_SSL3)
10171 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010172 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010173 /* SSLv3 does not have a "no_renegotiation" warning, so
10174 we send a fatal alert and abort the connection. */
10175 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10176 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10177 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010178 }
10179 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010180#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10181#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10182 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10183 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010185 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10186 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10187 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010188 {
10189 return( ret );
10190 }
Paul Bakker48916f92012-09-16 19:57:18 +000010191 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010192 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010193#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10194 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10197 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010198 }
Paul Bakker48916f92012-09-16 19:57:18 +000010199 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010200
Hanno Becker90333da2017-10-10 11:27:13 +010010201 /* At this point, we don't know whether the renegotiation has been
10202 * completed or not. The cases to consider are the following:
10203 * 1) The renegotiation is complete. In this case, no new record
10204 * has been read yet.
10205 * 2) The renegotiation is incomplete because the client received
10206 * an application data record while awaiting the ServerHello.
10207 * 3) The renegotiation is incomplete because the client received
10208 * a non-handshake, non-application data message while awaiting
10209 * the ServerHello.
10210 * In each of these case, looping will be the proper action:
10211 * - For 1), the next iteration will read a new record and check
10212 * if it's application data.
10213 * - For 2), the loop condition isn't satisfied as application data
10214 * is present, hence continue is the same as break
10215 * - For 3), the loop condition is satisfied and read_record
10216 * will re-deliver the message that was held back by the client
10217 * when expecting the ServerHello.
10218 */
10219 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010220 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010221#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010222 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010223 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010224 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010225 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010226 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010229 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010230 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010231 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010232 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010233 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010234#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010236 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10237 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010240 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010241 }
10242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010243 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10246 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010247 }
10248
10249 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010250
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010251 /* We're going to return something now, cancel timer,
10252 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010253 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010254 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010255
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010256#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010257 /* If we requested renego but received AppData, resend HelloRequest.
10258 * Do it now, after setting in_offt, to avoid taking this branch
10259 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010260#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010261 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010262 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010263 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010264 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010266 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010267 return( ret );
10268 }
10269 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010270#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010271#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010272 }
10273
10274 n = ( len < ssl->in_msglen )
10275 ? len : ssl->in_msglen;
10276
10277 memcpy( buf, ssl->in_offt, n );
10278 ssl->in_msglen -= n;
10279
10280 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010281 {
10282 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010283 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010284 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010285 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010286 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010287 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010288 /* more data available */
10289 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010290 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010293
Paul Bakker23986e52011-04-24 08:57:21 +000010294 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010295}
10296
10297/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010298 * Send application data to be encrypted by the SSL layer, taking care of max
10299 * fragment length and buffer size.
10300 *
10301 * According to RFC 5246 Section 6.2.1:
10302 *
10303 * Zero-length fragments of Application data MAY be sent as they are
10304 * potentially useful as a traffic analysis countermeasure.
10305 *
10306 * Therefore, it is possible that the input message length is 0 and the
10307 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010308 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010309static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010310 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010311{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010312 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10313 const size_t max_len = (size_t) ret;
10314
10315 if( ret < 0 )
10316 {
10317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10318 return( ret );
10319 }
10320
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010321 if( len > max_len )
10322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010323#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010324 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010327 "maximum fragment length: %d > %d",
10328 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010329 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010330 }
10331 else
10332#endif
10333 len = max_len;
10334 }
Paul Bakker887bd502011-06-08 13:10:54 +000010335
Paul Bakker5121ce52009-01-03 21:22:43 +000010336 if( ssl->out_left != 0 )
10337 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010338 /*
10339 * The user has previously tried to send the data and
10340 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10341 * written. In this case, we expect the high-level write function
10342 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010344 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010347 return( ret );
10348 }
10349 }
Paul Bakker887bd502011-06-08 13:10:54 +000010350 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010351 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010352 /*
10353 * The user is trying to send a message the first time, so we need to
10354 * copy the data into the internal buffers and setup the data structure
10355 * to keep track of partial writes
10356 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010357 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010358 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010359 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010360
Hanno Becker67bc7c32018-08-06 11:33:50 +010010361 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010363 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010364 return( ret );
10365 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010366 }
10367
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010368 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010369}
10370
10371/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010372 * Write application data, doing 1/n-1 splitting if necessary.
10373 *
10374 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010375 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010376 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010378#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010379static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010380 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010381{
10382 int ret;
10383
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010384 if( ssl->conf->cbc_record_splitting ==
10385 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010386 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010387 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10388 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10389 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010390 {
10391 return( ssl_write_real( ssl, buf, len ) );
10392 }
10393
10394 if( ssl->split_done == 0 )
10395 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010396 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010397 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010398 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010399 }
10400
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010401 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10402 return( ret );
10403 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010404
10405 return( ret + 1 );
10406}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010407#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010408
10409/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010410 * Write application data (public-facing wrapper)
10411 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010412int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010413{
10414 int ret;
10415
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010417
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010418 if( ssl == NULL || ssl->conf == NULL )
10419 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10420
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010421#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010422 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10423 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010424 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010425 return( ret );
10426 }
10427#endif
10428
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010429 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010430 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010431 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010432 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010433 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010434 return( ret );
10435 }
10436 }
10437
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010438#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010439 ret = ssl_write_split( ssl, buf, len );
10440#else
10441 ret = ssl_write_real( ssl, buf, len );
10442#endif
10443
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010445
10446 return( ret );
10447}
10448
10449/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010450 * Notify the peer that the connection is being closed
10451 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010452int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010453{
10454 int ret;
10455
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010456 if( ssl == NULL || ssl->conf == NULL )
10457 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010460
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010461 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010462 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010464 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010466 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10467 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10468 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010470 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010471 return( ret );
10472 }
10473 }
10474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010476
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010477 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010478}
10479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010480void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010481{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010482 if( transform == NULL )
10483 return;
10484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010485#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010486 deflateEnd( &transform->ctx_deflate );
10487 inflateEnd( &transform->ctx_inflate );
10488#endif
10489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010490 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10491 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010492
Hanno Beckerd56ed242018-01-03 15:32:51 +000010493#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010494 mbedtls_md_free( &transform->md_ctx_enc );
10495 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010496#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010497
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010498 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010499}
10500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010501#if defined(MBEDTLS_X509_CRT_PARSE_C)
10502static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010503{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010504 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010505
10506 while( cur != NULL )
10507 {
10508 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010509 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010510 cur = next;
10511 }
10512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010513#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010514
Hanno Becker0271f962018-08-16 13:23:47 +010010515#if defined(MBEDTLS_SSL_PROTO_DTLS)
10516
10517static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10518{
10519 unsigned offset;
10520 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10521
10522 if( hs == NULL )
10523 return;
10524
Hanno Becker283f5ef2018-08-24 09:34:47 +010010525 ssl_free_buffered_record( ssl );
10526
Hanno Becker0271f962018-08-16 13:23:47 +010010527 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010528 ssl_buffering_free_slot( ssl, offset );
10529}
10530
10531static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10532 uint8_t slot )
10533{
10534 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10535 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010536
10537 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10538 return;
10539
Hanno Beckere605b192018-08-21 15:59:07 +010010540 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010541 {
Hanno Beckere605b192018-08-21 15:59:07 +010010542 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010543 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010544 mbedtls_free( hs_buf->data );
10545 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010546 }
10547}
10548
10549#endif /* MBEDTLS_SSL_PROTO_DTLS */
10550
Gilles Peskine9b562d52018-04-25 20:32:43 +020010551void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010552{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010553 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10554
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010555 if( handshake == NULL )
10556 return;
10557
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010558#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10559 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10560 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010561 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010562 handshake->async_in_progress = 0;
10563 }
10564#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10565
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010566#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10567 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10568 mbedtls_md5_free( &handshake->fin_md5 );
10569 mbedtls_sha1_free( &handshake->fin_sha1 );
10570#endif
10571#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10572#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010573#if defined(MBEDTLS_USE_PSA_CRYPTO)
10574 psa_hash_abort( &handshake->fin_sha256_psa );
10575#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010576 mbedtls_sha256_free( &handshake->fin_sha256 );
10577#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010578#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010579#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010580#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010581 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010582#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010583 mbedtls_sha512_free( &handshake->fin_sha512 );
10584#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010585#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010586#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010588#if defined(MBEDTLS_DHM_C)
10589 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010590#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010591#if defined(MBEDTLS_ECDH_C)
10592 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010593#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010594#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010595 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010596#if defined(MBEDTLS_SSL_CLI_C)
10597 mbedtls_free( handshake->ecjpake_cache );
10598 handshake->ecjpake_cache = NULL;
10599 handshake->ecjpake_cache_len = 0;
10600#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010601#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010602
Janos Follath4ae5c292016-02-10 11:27:43 +000010603#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10604 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010605 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010606 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010607#endif
10608
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010609#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10610 if( handshake->psk != NULL )
10611 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010612 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010613 mbedtls_free( handshake->psk );
10614 }
10615#endif
10616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010617#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10618 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010619 /*
10620 * Free only the linked list wrapper, not the keys themselves
10621 * since the belong to the SNI callback
10622 */
10623 if( handshake->sni_key_cert != NULL )
10624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010625 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010626
10627 while( cur != NULL )
10628 {
10629 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010630 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010631 cur = next;
10632 }
10633 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010634#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010635
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010636#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010637 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010638 if( handshake->ecrs_peer_cert != NULL )
10639 {
10640 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10641 mbedtls_free( handshake->ecrs_peer_cert );
10642 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010643#endif
10644
Hanno Becker75173122019-02-06 16:18:31 +000010645#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10646 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10647 mbedtls_pk_free( &handshake->peer_pubkey );
10648#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010650#if defined(MBEDTLS_SSL_PROTO_DTLS)
10651 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010652 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010653 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010654#endif
10655
Hanno Becker4a63ed42019-01-08 11:39:35 +000010656#if defined(MBEDTLS_ECDH_C) && \
10657 defined(MBEDTLS_USE_PSA_CRYPTO)
10658 psa_destroy_key( handshake->ecdh_psa_privkey );
10659#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10660
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010661 mbedtls_platform_zeroize( handshake,
10662 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010663}
10664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010665void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010666{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010667 if( session == NULL )
10668 return;
10669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010670#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010671 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010672#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010673
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010674#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010675 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010676#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010677
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010678 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010679}
10680
Paul Bakker5121ce52009-01-03 21:22:43 +000010681/*
10682 * Free an SSL context
10683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010684void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010685{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010686 if( ssl == NULL )
10687 return;
10688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010690
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010691 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010692 {
Angus Grattond8213d02016-05-25 20:56:48 +100010693 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010694 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010695 }
10696
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010697 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010698 {
Angus Grattond8213d02016-05-25 20:56:48 +100010699 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010700 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010701 }
10702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010703#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010704 if( ssl->compress_buf != NULL )
10705 {
Angus Grattond8213d02016-05-25 20:56:48 +100010706 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010707 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010708 }
10709#endif
10710
Paul Bakker48916f92012-09-16 19:57:18 +000010711 if( ssl->transform )
10712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010713 mbedtls_ssl_transform_free( ssl->transform );
10714 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010715 }
10716
10717 if( ssl->handshake )
10718 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010719 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010720 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10721 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010723 mbedtls_free( ssl->handshake );
10724 mbedtls_free( ssl->transform_negotiate );
10725 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010726 }
10727
Paul Bakkerc0463502013-02-14 11:19:38 +010010728 if( ssl->session )
10729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010730 mbedtls_ssl_session_free( ssl->session );
10731 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010732 }
10733
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010734#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010735 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010736 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010737 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010738 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010739 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010740#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010742#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10743 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10746 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010747 }
10748#endif
10749
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010750#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010751 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010752#endif
10753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010755
Paul Bakker86f04f42013-02-14 11:20:09 +010010756 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010757 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010758}
10759
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010760/*
10761 * Initialze mbedtls_ssl_config
10762 */
10763void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10764{
10765 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10766}
10767
Simon Butcherc97b6972015-12-27 23:48:17 +000010768#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010769static int ssl_preset_default_hashes[] = {
10770#if defined(MBEDTLS_SHA512_C)
10771 MBEDTLS_MD_SHA512,
10772 MBEDTLS_MD_SHA384,
10773#endif
10774#if defined(MBEDTLS_SHA256_C)
10775 MBEDTLS_MD_SHA256,
10776 MBEDTLS_MD_SHA224,
10777#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010778#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010779 MBEDTLS_MD_SHA1,
10780#endif
10781 MBEDTLS_MD_NONE
10782};
Simon Butcherc97b6972015-12-27 23:48:17 +000010783#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010784
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010785static int ssl_preset_suiteb_ciphersuites[] = {
10786 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10787 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10788 0
10789};
10790
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010791#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010792static int ssl_preset_suiteb_hashes[] = {
10793 MBEDTLS_MD_SHA256,
10794 MBEDTLS_MD_SHA384,
10795 MBEDTLS_MD_NONE
10796};
10797#endif
10798
10799#if defined(MBEDTLS_ECP_C)
10800static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010801#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010802 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010803#endif
10804#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010805 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010806#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010807 MBEDTLS_ECP_DP_NONE
10808};
10809#endif
10810
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010811/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010812 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010813 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010814int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010815 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010816{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010817#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010818 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010819#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010820
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010821 /* Use the functions here so that they are covered in tests,
10822 * but otherwise access member directly for efficiency */
10823 mbedtls_ssl_conf_endpoint( conf, endpoint );
10824 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010825
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010826 /*
10827 * Things that are common to all presets
10828 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010829#if defined(MBEDTLS_SSL_CLI_C)
10830 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10831 {
10832 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10833#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10834 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10835#endif
10836 }
10837#endif
10838
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010839#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010840 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010841#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010842
10843#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10844 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10845#endif
10846
10847#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10848 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10849#endif
10850
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010851#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10852 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10853#endif
10854
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010855#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010856 conf->f_cookie_write = ssl_cookie_write_dummy;
10857 conf->f_cookie_check = ssl_cookie_check_dummy;
10858#endif
10859
10860#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10861 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10862#endif
10863
Janos Follath088ce432017-04-10 12:42:31 +010010864#if defined(MBEDTLS_SSL_SRV_C)
10865 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10866#endif
10867
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010868#if defined(MBEDTLS_SSL_PROTO_DTLS)
10869 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10870 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10871#endif
10872
10873#if defined(MBEDTLS_SSL_RENEGOTIATION)
10874 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010875 memset( conf->renego_period, 0x00, 2 );
10876 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010877#endif
10878
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010879#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10880 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10881 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010882 const unsigned char dhm_p[] =
10883 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10884 const unsigned char dhm_g[] =
10885 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10886
Hanno Beckera90658f2017-10-04 15:29:08 +010010887 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10888 dhm_p, sizeof( dhm_p ),
10889 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010890 {
10891 return( ret );
10892 }
10893 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010894#endif
10895
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010896 /*
10897 * Preset-specific defaults
10898 */
10899 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010900 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010901 /*
10902 * NSA Suite B
10903 */
10904 case MBEDTLS_SSL_PRESET_SUITEB:
10905 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10906 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10907 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10908 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10909
10910 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10911 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10912 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10913 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10914 ssl_preset_suiteb_ciphersuites;
10915
10916#if defined(MBEDTLS_X509_CRT_PARSE_C)
10917 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010918#endif
10919
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010920#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010921 conf->sig_hashes = ssl_preset_suiteb_hashes;
10922#endif
10923
10924#if defined(MBEDTLS_ECP_C)
10925 conf->curve_list = ssl_preset_suiteb_curves;
10926#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010927 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010928
10929 /*
10930 * Default
10931 */
10932 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010933 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10934 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10935 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10936 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10937 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10938 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10939 MBEDTLS_SSL_MIN_MINOR_VERSION :
10940 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010941 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10942 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10943
10944#if defined(MBEDTLS_SSL_PROTO_DTLS)
10945 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10946 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10947#endif
10948
10949 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10950 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10951 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10952 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10953 mbedtls_ssl_list_ciphersuites();
10954
10955#if defined(MBEDTLS_X509_CRT_PARSE_C)
10956 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10957#endif
10958
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010959#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010960 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010961#endif
10962
10963#if defined(MBEDTLS_ECP_C)
10964 conf->curve_list = mbedtls_ecp_grp_id_list();
10965#endif
10966
10967#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10968 conf->dhm_min_bitlen = 1024;
10969#endif
10970 }
10971
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010972 return( 0 );
10973}
10974
10975/*
10976 * Free mbedtls_ssl_config
10977 */
10978void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10979{
10980#if defined(MBEDTLS_DHM_C)
10981 mbedtls_mpi_free( &conf->dhm_P );
10982 mbedtls_mpi_free( &conf->dhm_G );
10983#endif
10984
10985#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10986 if( conf->psk != NULL )
10987 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010988 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010989 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010990 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010991 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010992 }
10993
10994 if( conf->psk_identity != NULL )
10995 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010996 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010997 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010998 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010999 conf->psk_identity_len = 0;
11000 }
11001#endif
11002
11003#if defined(MBEDTLS_X509_CRT_PARSE_C)
11004 ssl_key_cert_free( conf->key_cert );
11005#endif
11006
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011007 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011008}
11009
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011010#if defined(MBEDTLS_PK_C) && \
11011 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011012/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011013 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011014 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011015unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011016{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011017#if defined(MBEDTLS_RSA_C)
11018 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
11019 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011020#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011021#if defined(MBEDTLS_ECDSA_C)
11022 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
11023 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011024#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011025 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011026}
11027
Hanno Becker7e5437a2017-04-28 17:15:26 +010011028unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
11029{
11030 switch( type ) {
11031 case MBEDTLS_PK_RSA:
11032 return( MBEDTLS_SSL_SIG_RSA );
11033 case MBEDTLS_PK_ECDSA:
11034 case MBEDTLS_PK_ECKEY:
11035 return( MBEDTLS_SSL_SIG_ECDSA );
11036 default:
11037 return( MBEDTLS_SSL_SIG_ANON );
11038 }
11039}
11040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011041mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011042{
11043 switch( sig )
11044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011045#if defined(MBEDTLS_RSA_C)
11046 case MBEDTLS_SSL_SIG_RSA:
11047 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011048#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011049#if defined(MBEDTLS_ECDSA_C)
11050 case MBEDTLS_SSL_SIG_ECDSA:
11051 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011052#endif
11053 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011054 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011055 }
11056}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011057#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011058
Hanno Becker7e5437a2017-04-28 17:15:26 +010011059#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
11060 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
11061
11062/* Find an entry in a signature-hash set matching a given hash algorithm. */
11063mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
11064 mbedtls_pk_type_t sig_alg )
11065{
11066 switch( sig_alg )
11067 {
11068 case MBEDTLS_PK_RSA:
11069 return( set->rsa );
11070 case MBEDTLS_PK_ECDSA:
11071 return( set->ecdsa );
11072 default:
11073 return( MBEDTLS_MD_NONE );
11074 }
11075}
11076
11077/* Add a signature-hash-pair to a signature-hash set */
11078void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
11079 mbedtls_pk_type_t sig_alg,
11080 mbedtls_md_type_t md_alg )
11081{
11082 switch( sig_alg )
11083 {
11084 case MBEDTLS_PK_RSA:
11085 if( set->rsa == MBEDTLS_MD_NONE )
11086 set->rsa = md_alg;
11087 break;
11088
11089 case MBEDTLS_PK_ECDSA:
11090 if( set->ecdsa == MBEDTLS_MD_NONE )
11091 set->ecdsa = md_alg;
11092 break;
11093
11094 default:
11095 break;
11096 }
11097}
11098
11099/* Allow exactly one hash algorithm for each signature. */
11100void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11101 mbedtls_md_type_t md_alg )
11102{
11103 set->rsa = md_alg;
11104 set->ecdsa = md_alg;
11105}
11106
11107#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11108 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11109
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011110/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011111 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011113mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011114{
11115 switch( hash )
11116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011117#if defined(MBEDTLS_MD5_C)
11118 case MBEDTLS_SSL_HASH_MD5:
11119 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011121#if defined(MBEDTLS_SHA1_C)
11122 case MBEDTLS_SSL_HASH_SHA1:
11123 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011124#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011125#if defined(MBEDTLS_SHA256_C)
11126 case MBEDTLS_SSL_HASH_SHA224:
11127 return( MBEDTLS_MD_SHA224 );
11128 case MBEDTLS_SSL_HASH_SHA256:
11129 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011130#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011131#if defined(MBEDTLS_SHA512_C)
11132 case MBEDTLS_SSL_HASH_SHA384:
11133 return( MBEDTLS_MD_SHA384 );
11134 case MBEDTLS_SSL_HASH_SHA512:
11135 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011136#endif
11137 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011138 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011139 }
11140}
11141
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011142/*
11143 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11144 */
11145unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11146{
11147 switch( md )
11148 {
11149#if defined(MBEDTLS_MD5_C)
11150 case MBEDTLS_MD_MD5:
11151 return( MBEDTLS_SSL_HASH_MD5 );
11152#endif
11153#if defined(MBEDTLS_SHA1_C)
11154 case MBEDTLS_MD_SHA1:
11155 return( MBEDTLS_SSL_HASH_SHA1 );
11156#endif
11157#if defined(MBEDTLS_SHA256_C)
11158 case MBEDTLS_MD_SHA224:
11159 return( MBEDTLS_SSL_HASH_SHA224 );
11160 case MBEDTLS_MD_SHA256:
11161 return( MBEDTLS_SSL_HASH_SHA256 );
11162#endif
11163#if defined(MBEDTLS_SHA512_C)
11164 case MBEDTLS_MD_SHA384:
11165 return( MBEDTLS_SSL_HASH_SHA384 );
11166 case MBEDTLS_MD_SHA512:
11167 return( MBEDTLS_SSL_HASH_SHA512 );
11168#endif
11169 default:
11170 return( MBEDTLS_SSL_HASH_NONE );
11171 }
11172}
11173
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011174#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011175/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011176 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011177 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011178 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011179int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011180{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011181 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011182
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011183 if( ssl->conf->curve_list == NULL )
11184 return( -1 );
11185
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011186 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011187 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011188 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011189
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011190 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011191}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011192#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011193
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011194#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011195/*
11196 * Check if a hash proposed by the peer is in our list.
11197 * Return 0 if we're willing to use it, -1 otherwise.
11198 */
11199int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11200 mbedtls_md_type_t md )
11201{
11202 const int *cur;
11203
11204 if( ssl->conf->sig_hashes == NULL )
11205 return( -1 );
11206
11207 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11208 if( *cur == (int) md )
11209 return( 0 );
11210
11211 return( -1 );
11212}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011213#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011215#if defined(MBEDTLS_X509_CRT_PARSE_C)
11216int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11217 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011218 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011219 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011220{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011221 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011222#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011223 int usage = 0;
11224#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011225#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011226 const char *ext_oid;
11227 size_t ext_len;
11228#endif
11229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011230#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11231 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011232 ((void) cert);
11233 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011234 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011235#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011237#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11238 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011239 {
11240 /* Server part of the key exchange */
11241 switch( ciphersuite->key_exchange )
11242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011243 case MBEDTLS_KEY_EXCHANGE_RSA:
11244 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011245 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011246 break;
11247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011248 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11249 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11250 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11251 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011252 break;
11253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011254 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11255 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011256 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011257 break;
11258
11259 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011260 case MBEDTLS_KEY_EXCHANGE_NONE:
11261 case MBEDTLS_KEY_EXCHANGE_PSK:
11262 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11263 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011264 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011265 usage = 0;
11266 }
11267 }
11268 else
11269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011270 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11271 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011272 }
11273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011274 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011275 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011276 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011277 ret = -1;
11278 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011279#else
11280 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011281#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011283#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11284 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011286 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11287 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011288 }
11289 else
11290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011291 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11292 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011293 }
11294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011295 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011296 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011297 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011298 ret = -1;
11299 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011300#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011301
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011302 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011303}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011304#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011305
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011306/*
11307 * Convert version numbers to/from wire format
11308 * and, for DTLS, to/from TLS equivalent.
11309 *
11310 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011311 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011312 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11313 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011315void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011316 unsigned char ver[2] )
11317{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011318#if defined(MBEDTLS_SSL_PROTO_DTLS)
11319 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011321 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011322 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11323
11324 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11325 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11326 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011327 else
11328#else
11329 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011330#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011331 {
11332 ver[0] = (unsigned char) major;
11333 ver[1] = (unsigned char) minor;
11334 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011335}
11336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011337void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011338 const unsigned char ver[2] )
11339{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011340#if defined(MBEDTLS_SSL_PROTO_DTLS)
11341 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011342 {
11343 *major = 255 - ver[0] + 2;
11344 *minor = 255 - ver[1] + 1;
11345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011346 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011347 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11348 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011349 else
11350#else
11351 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011352#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011353 {
11354 *major = ver[0];
11355 *minor = ver[1];
11356 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011357}
11358
Simon Butcher99000142016-10-13 17:21:01 +010011359int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11360{
11361#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11362 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11363 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11364
11365 switch( md )
11366 {
11367#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11368#if defined(MBEDTLS_MD5_C)
11369 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011370 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011371#endif
11372#if defined(MBEDTLS_SHA1_C)
11373 case MBEDTLS_SSL_HASH_SHA1:
11374 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11375 break;
11376#endif
11377#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11378#if defined(MBEDTLS_SHA512_C)
11379 case MBEDTLS_SSL_HASH_SHA384:
11380 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11381 break;
11382#endif
11383#if defined(MBEDTLS_SHA256_C)
11384 case MBEDTLS_SSL_HASH_SHA256:
11385 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11386 break;
11387#endif
11388 default:
11389 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11390 }
11391
11392 return 0;
11393#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11394 (void) ssl;
11395 (void) md;
11396
11397 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11398#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11399}
11400
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011401#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11402 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11403int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11404 unsigned char *output,
11405 unsigned char *data, size_t data_len )
11406{
11407 int ret = 0;
11408 mbedtls_md5_context mbedtls_md5;
11409 mbedtls_sha1_context mbedtls_sha1;
11410
11411 mbedtls_md5_init( &mbedtls_md5 );
11412 mbedtls_sha1_init( &mbedtls_sha1 );
11413
11414 /*
11415 * digitally-signed struct {
11416 * opaque md5_hash[16];
11417 * opaque sha_hash[20];
11418 * };
11419 *
11420 * md5_hash
11421 * MD5(ClientHello.random + ServerHello.random
11422 * + ServerParams);
11423 * sha_hash
11424 * SHA(ClientHello.random + ServerHello.random
11425 * + ServerParams);
11426 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011427 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 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_starts_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_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011433 ssl->handshake->randbytes, 64 ) ) != 0 )
11434 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011436 goto exit;
11437 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011438 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011439 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_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_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011444 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011446 goto exit;
11447 }
11448
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011449 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011450 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011451 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011452 goto exit;
11453 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011454 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011455 ssl->handshake->randbytes, 64 ) ) != 0 )
11456 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011458 goto exit;
11459 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011460 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011461 data_len ) ) != 0 )
11462 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011463 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011464 goto exit;
11465 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011466 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011467 output + 16 ) ) != 0 )
11468 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011469 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011470 goto exit;
11471 }
11472
11473exit:
11474 mbedtls_md5_free( &mbedtls_md5 );
11475 mbedtls_sha1_free( &mbedtls_sha1 );
11476
11477 if( ret != 0 )
11478 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11479 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11480
11481 return( ret );
11482
11483}
11484#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11485 MBEDTLS_SSL_PROTO_TLS1_1 */
11486
11487#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11488 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011489
11490#if defined(MBEDTLS_USE_PSA_CRYPTO)
11491int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11492 unsigned char *hash, size_t *hashlen,
11493 unsigned char *data, size_t data_len,
11494 mbedtls_md_type_t md_alg )
11495{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011496 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011497 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011498 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11499
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011500 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011501
11502 if( ( status = psa_hash_setup( &hash_operation,
11503 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011504 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011505 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011506 goto exit;
11507 }
11508
Andrzej Kurek814feff2019-01-14 04:35:19 -050011509 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11510 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011511 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011512 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011513 goto exit;
11514 }
11515
Andrzej Kurek814feff2019-01-14 04:35:19 -050011516 if( ( status = psa_hash_update( &hash_operation,
11517 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011518 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011519 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011520 goto exit;
11521 }
11522
Andrzej Kurek814feff2019-01-14 04:35:19 -050011523 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11524 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011525 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011526 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011527 goto exit;
11528 }
11529
11530exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011531 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011532 {
11533 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11534 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011535 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011536 {
11537 case PSA_ERROR_NOT_SUPPORTED:
11538 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011539 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011540 case PSA_ERROR_BUFFER_TOO_SMALL:
11541 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11542 case PSA_ERROR_INSUFFICIENT_MEMORY:
11543 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11544 default:
11545 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11546 }
11547 }
11548 return( 0 );
11549}
11550
11551#else
11552
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011553int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011554 unsigned char *hash, size_t *hashlen,
11555 unsigned char *data, size_t data_len,
11556 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011557{
11558 int ret = 0;
11559 mbedtls_md_context_t ctx;
11560 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011561 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011562
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011563 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011564
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011565 mbedtls_md_init( &ctx );
11566
11567 /*
11568 * digitally-signed struct {
11569 * opaque client_random[32];
11570 * opaque server_random[32];
11571 * ServerDHParams params;
11572 * };
11573 */
11574 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11575 {
11576 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11577 goto exit;
11578 }
11579 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11580 {
11581 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11582 goto exit;
11583 }
11584 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11585 {
11586 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11587 goto exit;
11588 }
11589 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11590 {
11591 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11592 goto exit;
11593 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011594 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011595 {
11596 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11597 goto exit;
11598 }
11599
11600exit:
11601 mbedtls_md_free( &ctx );
11602
11603 if( ret != 0 )
11604 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11605 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11606
11607 return( ret );
11608}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011609#endif /* MBEDTLS_USE_PSA_CRYPTO */
11610
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011611#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11612 MBEDTLS_SSL_PROTO_TLS1_2 */
11613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011614#endif /* MBEDTLS_SSL_TLS_C */