blob: ad1430bce1af6bfd8453f38569829b356bee5ec3 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Hanno Becker2a43f6f2018-08-10 11:12:52 +010062static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010063static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010064
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010065/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020069 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010070 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010071#else
72 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010073#endif
74 return( 0 );
75}
76
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077/*
78 * Start a timer.
79 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020082{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020083 if( ssl->f_set_timer == NULL )
84 return;
85
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
87 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020088}
89
90/*
91 * Return -1 is timer is expired, 0 if it isn't.
92 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020094{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020095 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020096 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020097
98 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020099 {
100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200101 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200102 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200103
104 return( 0 );
105}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200106
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100107static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
108 mbedtls_ssl_transform *transform );
Hanno Becker79594fd2019-05-08 09:38:41 +0100109static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100110
Hanno Beckercfe45792019-07-03 16:13:00 +0100111#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker54229812019-07-12 14:40:00 +0100112static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
113 unsigned char *buf,
114 size_t len,
115 mbedtls_record *rec );
116
Hanno Beckercfe45792019-07-03 16:13:00 +0100117int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
118 unsigned char *buf,
119 size_t buflen )
120{
Hanno Becker54229812019-07-12 14:40:00 +0100121 int ret = 0;
122 mbedtls_record rec;
123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
124 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
125
126 /* We don't support record checking in TLS because
127 * (a) there doesn't seem to be a usecase for it, and
128 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
129 * and we'd need to backup the transform here.
130 */
131 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
132 {
133 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
134 goto exit;
135 }
136#if defined(MBEDTLS_SSL_PROTO_DTLS)
137 else
138 {
139 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
140 if( ret != 0 )
141 {
142 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
143 goto exit;
144 }
145
146 if( ssl->transform_in != NULL )
147 {
148 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
149 if( ret != 0 )
150 {
151 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
152 goto exit;
153 }
154 }
155 }
156#endif /* MBEDTLS_SSL_PROTO_DTLS */
157
158exit:
159 /* On success, we have decrypted the buffer in-place, so make
160 * sure we don't leak any plaintext data. */
161 mbedtls_platform_zeroize( buf, buflen );
162
163 /* For the purpose of this API, treat messages with unexpected CID
164 * as well as such from future epochs as unexpected. */
165 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
166 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
167 {
168 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
169 }
170
171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
172 return( ret );
Hanno Beckercfe45792019-07-03 16:13:00 +0100173}
174#endif /* MBEDTLS_SSL_RECORD_CHECKING */
175
Hanno Becker67bc7c32018-08-06 11:33:50 +0100176#define SSL_DONT_FORCE_FLUSH 0
177#define SSL_FORCE_FLUSH 1
178
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200179#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100180
Hanno Beckera0e20d02019-05-15 14:03:01 +0100181#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100182/* Top-level Connection ID API */
183
Hanno Becker8367ccc2019-05-14 11:30:10 +0100184int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
185 size_t len,
186 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100187{
188 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
189 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
190
Hanno Becker611ac772019-05-14 11:45:26 +0100191 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
192 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
193 {
194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
195 }
196
197 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100198 conf->cid_len = len;
199 return( 0 );
200}
201
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100202int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
203 int enable,
204 unsigned char const *own_cid,
205 size_t own_cid_len )
206{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100207 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
208 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
209
Hanno Beckerca092242019-04-25 16:01:49 +0100210 ssl->negotiate_cid = enable;
211 if( enable == MBEDTLS_SSL_CID_DISABLED )
212 {
213 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
214 return( 0 );
215 }
216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100217 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100218
Hanno Beckerad4a1372019-05-03 13:06:44 +0100219 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100220 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
222 (unsigned) own_cid_len,
223 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
225 }
226
227 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100228 /* Truncation is not an issue here because
229 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
230 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100231
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100232 return( 0 );
233}
234
235int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
236 int *enabled,
237 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
238 size_t *peer_cid_len )
239{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100240 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100241
Hanno Becker76a79ab2019-05-03 14:38:32 +0100242 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
243 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
244 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100245 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100246 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100247
Hanno Beckerc5f24222019-05-03 12:54:52 +0100248 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
249 * were used, but client and server requested the empty CID.
250 * This is indistinguishable from not using the CID extension
251 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100252 if( ssl->transform_in->in_cid_len == 0 &&
253 ssl->transform_in->out_cid_len == 0 )
254 {
255 return( 0 );
256 }
257
Hanno Becker615ef172019-05-22 16:50:35 +0100258 if( peer_cid_len != NULL )
259 {
260 *peer_cid_len = ssl->transform_in->out_cid_len;
261 if( peer_cid != NULL )
262 {
263 memcpy( peer_cid, ssl->transform_in->out_cid,
264 ssl->transform_in->out_cid_len );
265 }
266 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100267
268 *enabled = MBEDTLS_SSL_CID_ENABLED;
269
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100270 return( 0 );
271}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100272#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100273
Hanno Beckerd5847772018-08-28 10:09:23 +0100274/* Forward declarations for functions related to message buffering. */
275static void ssl_buffering_free( mbedtls_ssl_context *ssl );
276static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
277 uint8_t slot );
278static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
279static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
280static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
281static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100282static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
283 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100284static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100285
Hanno Beckera67dee22018-08-22 10:05:20 +0100286static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100287static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100288{
Hanno Becker11682cc2018-08-22 14:41:02 +0100289 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100290
291 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100292 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100293
294 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
295}
296
Hanno Becker67bc7c32018-08-06 11:33:50 +0100297static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
298{
Hanno Becker11682cc2018-08-22 14:41:02 +0100299 size_t const bytes_written = ssl->out_left;
300 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100301
302 /* Double-check that the write-index hasn't gone
303 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100304 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100305 {
306 /* Should never happen... */
307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
308 }
309
310 return( (int) ( mtu - bytes_written ) );
311}
312
313static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
314{
315 int ret;
316 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400317 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100318
319#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
320 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
321
322 if( max_len > mfl )
323 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100324
325 /* By the standard (RFC 6066 Sect. 4), the MFL extension
326 * only limits the maximum record payload size, so in theory
327 * we would be allowed to pack multiple records of payload size
328 * MFL into a single datagram. However, this would mean that there's
329 * no way to explicitly communicate MTU restrictions to the peer.
330 *
331 * The following reduction of max_len makes sure that we never
332 * write datagrams larger than MFL + Record Expansion Overhead.
333 */
334 if( max_len <= ssl->out_left )
335 return( 0 );
336
337 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100338#endif
339
340 ret = ssl_get_remaining_space_in_datagram( ssl );
341 if( ret < 0 )
342 return( ret );
343 remaining = (size_t) ret;
344
345 ret = mbedtls_ssl_get_record_expansion( ssl );
346 if( ret < 0 )
347 return( ret );
348 expansion = (size_t) ret;
349
350 if( remaining <= expansion )
351 return( 0 );
352
353 remaining -= expansion;
354 if( remaining >= max_len )
355 remaining = max_len;
356
357 return( (int) remaining );
358}
359
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200360/*
361 * Double the retransmit timeout value, within the allowed range,
362 * returning -1 if the maximum value has already been reached.
363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200365{
366 uint32_t new_timeout;
367
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200368 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200369 return( -1 );
370
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200371 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
372 * in the following way: after the initial transmission and a first
373 * retransmission, back off to a temporary estimated MTU of 508 bytes.
374 * This value is guaranteed to be deliverable (if not guaranteed to be
375 * delivered) of any compliant IPv4 (and IPv6) network, and should work
376 * on most non-IP stacks too. */
377 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400378 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200379 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
381 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200382
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200383 new_timeout = 2 * ssl->handshake->retransmit_timeout;
384
385 /* Avoid arithmetic overflow and range overflow */
386 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200387 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200388 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200389 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200390 }
391
392 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200394 ssl->handshake->retransmit_timeout ) );
395
396 return( 0 );
397}
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200400{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200401 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200403 ssl->handshake->retransmit_timeout ) );
404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200408/*
409 * Convert max_fragment_length codes to length.
410 * RFC 6066 says:
411 * enum{
412 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
413 * } MaxFragmentLength;
414 * and we add 0 -> extension unused
415 */
Angus Grattond8213d02016-05-25 20:56:48 +1000416static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200417{
Angus Grattond8213d02016-05-25 20:56:48 +1000418 switch( mfl )
419 {
420 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
421 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
422 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
423 return 512;
424 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
425 return 1024;
426 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
427 return 2048;
428 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
429 return 4096;
430 default:
431 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
432 }
433}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200435
Hanno Becker52055ae2019-02-06 14:30:46 +0000436int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
437 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200438{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_ssl_session_free( dst );
440 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000443
444#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200445 if( src->peer_cert != NULL )
446 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200447 int ret;
448
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200449 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200450 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200451 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200456 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200459 dst->peer_cert = NULL;
460 return( ret );
461 }
462 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000463#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000464 if( src->peer_cert_digest != NULL )
465 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000466 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000467 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000468 if( dst->peer_cert_digest == NULL )
469 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
470
471 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
472 src->peer_cert_digest_len );
473 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000474 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000475 }
476#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200479
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200480#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200481 if( src->ticket != NULL )
482 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200483 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200484 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200485 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200486
487 memcpy( dst->ticket, src->ticket, src->ticket_len );
488 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200489#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200490
491 return( 0 );
492}
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
495int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200496 const unsigned char *key_enc, const unsigned char *key_dec,
497 size_t keylen,
498 const unsigned char *iv_enc, const unsigned char *iv_dec,
499 size_t ivlen,
500 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200501 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
503int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
504int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
505int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
506int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
507#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000508
Paul Bakker5121ce52009-01-03 21:22:43 +0000509/*
510 * Key material generation
511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200513static int ssl3_prf( const unsigned char *secret, size_t slen,
514 const char *label,
515 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000516 unsigned char *dstbuf, size_t dlen )
517{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100518 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000519 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200520 mbedtls_md5_context md5;
521 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000522 unsigned char padding[16];
523 unsigned char sha1sum[20];
524 ((void)label);
525
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200526 mbedtls_md5_init( &md5 );
527 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200528
Paul Bakker5f70b252012-09-13 14:23:06 +0000529 /*
530 * SSLv3:
531 * block =
532 * MD5( secret + SHA1( 'A' + secret + random ) ) +
533 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
534 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
535 * ...
536 */
537 for( i = 0; i < dlen / 16; i++ )
538 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200539 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000540
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100541 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100542 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100543 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100544 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100545 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100546 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100547 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100548 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100549 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100550 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000551
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100552 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100553 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100554 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100555 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100556 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100557 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100558 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100559 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000560 }
561
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100562exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200563 mbedtls_md5_free( &md5 );
564 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000565
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500566 mbedtls_platform_zeroize( padding, sizeof( padding ) );
567 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000568
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100569 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000570}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200574static int tls1_prf( const unsigned char *secret, size_t slen,
575 const char *label,
576 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000577 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000578{
Paul Bakker23986e52011-04-24 08:57:21 +0000579 size_t nb, hs;
580 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200581 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300582 unsigned char *tmp;
583 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 const mbedtls_md_info_t *md_info;
586 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100587 int ret;
588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
Ron Eldor3b350852019-05-07 18:31:49 +0300591 tmp_len = 20 + strlen( label ) + rlen;
592 tmp = mbedtls_calloc( 1, tmp_len );
593 if( tmp == NULL )
594 {
595 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
596 goto exit;
597 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599 hs = ( slen + 1 ) / 2;
600 S1 = secret;
601 S2 = secret + slen - hs;
602
603 nb = strlen( label );
604 memcpy( tmp + 20, label, nb );
605 memcpy( tmp + 20 + nb, random, rlen );
606 nb += rlen;
607
608 /*
609 * First compute P_md5(secret,label+random)[0..dlen]
610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300612 {
613 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
614 goto exit;
615 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300618 {
619 goto exit;
620 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
623 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
624 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
626 for( i = 0; i < dlen; i += 16 )
627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_md_hmac_reset ( &md_ctx );
629 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
630 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_md_hmac_reset ( &md_ctx );
633 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
634 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
636 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
637
638 for( j = 0; j < k; j++ )
639 dstbuf[i + j] = h_i[j];
640 }
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100643
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 /*
645 * XOR out with P_sha1(secret,label+random)[0..dlen]
646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300648 {
649 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
650 goto exit;
651 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300654 {
655 goto exit;
656 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
659 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
660 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
662 for( i = 0; i < dlen; i += 20 )
663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_md_hmac_reset ( &md_ctx );
665 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
666 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_md_hmac_reset ( &md_ctx );
669 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
670 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
673
674 for( j = 0; j < k; j++ )
675 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
676 }
677
Ron Eldor3b350852019-05-07 18:31:49 +0300678exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100680
Ron Eldor3b350852019-05-07 18:31:49 +0300681 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500682 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
Ron Eldor3b350852019-05-07 18:31:49 +0300684 mbedtls_free( tmp );
685 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500690#if defined(MBEDTLS_USE_PSA_CRYPTO)
691static int tls_prf_generic( mbedtls_md_type_t md_type,
692 const unsigned char *secret, size_t slen,
693 const char *label,
694 const unsigned char *random, size_t rlen,
695 unsigned char *dstbuf, size_t dlen )
696{
697 psa_status_t status;
698 psa_algorithm_t alg;
Janos Follath53b8ec22019-08-08 10:28:27 +0100699 psa_key_attributes_t key_attributes;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500700 psa_key_handle_t master_slot;
Janos Follathda6ac012019-08-16 13:47:29 +0100701 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +0100702 PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500703
Andrzej Kurekc929a822019-01-14 03:51:11 -0500704 if( md_type == MBEDTLS_MD_SHA384 )
705 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
706 else
707 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
708
Janos Follath53b8ec22019-08-08 10:28:27 +0100709 key_attributes = psa_key_attributes_init();
710 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
711 psa_set_key_algorithm( &key_attributes, alg );
712 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500713
Janos Follath53b8ec22019-08-08 10:28:27 +0100714 status = psa_import_key( &key_attributes, secret, slen, &master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500715 if( status != PSA_SUCCESS )
716 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
717
Janos Follathda6ac012019-08-16 13:47:29 +0100718 status = psa_key_derivation( &derivation,
Andrzej Kurekc929a822019-01-14 03:51:11 -0500719 master_slot, alg,
720 random, rlen,
721 (unsigned char const *) label,
722 (size_t) strlen( label ),
723 dlen );
724 if( status != PSA_SUCCESS )
725 {
Janos Follathda6ac012019-08-16 13:47:29 +0100726 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500727 psa_destroy_key( master_slot );
728 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
729 }
730
Janos Follathda6ac012019-08-16 13:47:29 +0100731 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500732 if( status != PSA_SUCCESS )
733 {
Janos Follathda6ac012019-08-16 13:47:29 +0100734 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500735 psa_destroy_key( master_slot );
736 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
737 }
738
Janos Follathda6ac012019-08-16 13:47:29 +0100739 status = psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500740 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500741 {
742 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500743 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500744 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500745
746 status = psa_destroy_key( master_slot );
747 if( status != PSA_SUCCESS )
748 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
749
Andrzej Kurek33171262019-01-15 03:25:18 -0500750 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500751}
752
753#else /* MBEDTLS_USE_PSA_CRYPTO */
754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100756 const unsigned char *secret, size_t slen,
757 const char *label,
758 const unsigned char *random, size_t rlen,
759 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000760{
761 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100762 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300763 unsigned char *tmp;
764 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
766 const mbedtls_md_info_t *md_info;
767 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100768 int ret;
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100776
Ron Eldor3b350852019-05-07 18:31:49 +0300777 tmp_len = md_len + strlen( label ) + rlen;
778 tmp = mbedtls_calloc( 1, tmp_len );
779 if( tmp == NULL )
780 {
781 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
782 goto exit;
783 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000784
785 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100786 memcpy( tmp + md_len, label, nb );
787 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000788 nb += rlen;
789
790 /*
791 * Compute P_<hash>(secret, label + random)[0..dlen]
792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300794 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
797 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
798 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100799
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100800 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_md_hmac_reset ( &md_ctx );
803 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
804 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_md_hmac_reset ( &md_ctx );
807 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
808 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000809
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100810 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000811
812 for( j = 0; j < k; j++ )
813 dstbuf[i + j] = h_i[j];
814 }
815
Ron Eldor3b350852019-05-07 18:31:49 +0300816exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100818
Ron Eldor3b350852019-05-07 18:31:49 +0300819 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500820 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000821
Ron Eldor3b350852019-05-07 18:31:49 +0300822 mbedtls_free( tmp );
823
824 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000825}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500826#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100828static int tls_prf_sha256( const unsigned char *secret, size_t slen,
829 const char *label,
830 const unsigned char *random, size_t rlen,
831 unsigned char *dstbuf, size_t dlen )
832{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100834 label, random, rlen, dstbuf, dlen ) );
835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200839static int tls_prf_sha384( const unsigned char *secret, size_t slen,
840 const char *label,
841 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000842 unsigned char *dstbuf, size_t dlen )
843{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100845 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000846}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#endif /* MBEDTLS_SHA512_C */
848#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
853 defined(MBEDTLS_SSL_PROTO_TLS1_1)
854static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200855#endif
Paul Bakker380da532012-04-18 16:10:25 +0000856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200858static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200860#endif
861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200863static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200865#endif
866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
868#if defined(MBEDTLS_SHA256_C)
869static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200870static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200872#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874#if defined(MBEDTLS_SHA512_C)
875static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200876static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100878#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000880
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100881#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100882 defined(MBEDTLS_USE_PSA_CRYPTO)
883static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
884{
885 if( ssl->conf->f_psk != NULL )
886 {
887 /* If we've used a callback to select the PSK,
888 * the static configuration is irrelevant. */
889 if( ssl->handshake->psk_opaque != 0 )
890 return( 1 );
891
892 return( 0 );
893 }
894
895 if( ssl->conf->psk_opaque != 0 )
896 return( 1 );
897
898 return( 0 );
899}
900#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100901 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100902
Ron Eldorcf280092019-05-14 20:19:13 +0300903#if defined(MBEDTLS_SSL_EXPORT_KEYS)
904static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
905{
906#if defined(MBEDTLS_SSL_PROTO_SSL3)
907 if( tls_prf == ssl3_prf )
908 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300909 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300910 }
911 else
912#endif
913#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
914 if( tls_prf == tls1_prf )
915 {
916 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
917 }
918 else
919#endif
920#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
921#if defined(MBEDTLS_SHA512_C)
922 if( tls_prf == tls_prf_sha384 )
923 {
924 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
925 }
926 else
927#endif
928#if defined(MBEDTLS_SHA256_C)
929 if( tls_prf == tls_prf_sha256 )
930 {
931 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
932 }
933 else
934#endif
935#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
936 return( MBEDTLS_SSL_TLS_PRF_NONE );
937}
938#endif /* MBEDTLS_SSL_EXPORT_KEYS */
939
Ron Eldor51d3ab52019-05-12 14:54:30 +0300940int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
941 const unsigned char *secret, size_t slen,
942 const char *label,
943 const unsigned char *random, size_t rlen,
944 unsigned char *dstbuf, size_t dlen )
945{
946 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
947
948 switch( prf )
949 {
950#if defined(MBEDTLS_SSL_PROTO_SSL3)
951 case MBEDTLS_SSL_TLS_PRF_SSL3:
952 tls_prf = ssl3_prf;
953 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300954#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300955#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
956 case MBEDTLS_SSL_TLS_PRF_TLS1:
957 tls_prf = tls1_prf;
958 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300959#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
960
961#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300962#if defined(MBEDTLS_SHA512_C)
963 case MBEDTLS_SSL_TLS_PRF_SHA384:
964 tls_prf = tls_prf_sha384;
965 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300966#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300967#if defined(MBEDTLS_SHA256_C)
968 case MBEDTLS_SSL_TLS_PRF_SHA256:
969 tls_prf = tls_prf_sha256;
970 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300971#endif /* MBEDTLS_SHA256_C */
972#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300973 default:
974 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
975 }
976
977 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
978}
979
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200980/*
981 * This function will ultimetaly only be responsible for populating a
982 * transform structure from data passed as explicit parameters.
983 *
984 * For now however it's doing rather more in a rather less explicit way.
985 */
986static int ssl_populate_transform( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000987{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200988 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000989#if defined(MBEDTLS_USE_PSA_CRYPTO)
990 int psa_fallthrough;
991#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 unsigned char keyblk[256];
993 unsigned char *key1;
994 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100995 unsigned char *mac_enc;
996 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000997 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200998 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000999 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001000 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 const mbedtls_cipher_info_t *cipher_info;
1002 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 mbedtls_ssl_session *session = ssl->session_negotiate;
1005 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
1006 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +00001007
Jaeden Amero2de07f12019-06-05 13:32:08 +01001008#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1009 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001010 transform->encrypt_then_mac = session->encrypt_then_mac;
1011#endif
1012 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001013
1014 ciphersuite_info = handshake->ciphersuite_info;
1015 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001016 if( cipher_info == NULL )
1017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001019 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001021 }
1022
Hanno Beckere694c3e2017-12-27 21:34:08 +00001023 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001024 if( md_info == NULL )
1025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001027 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001029 }
1030
Hanno Beckera0e20d02019-05-15 14:03:01 +01001031#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001032 /* Copy own and peer's CID if the use of the CID
1033 * extension has been negotiated. */
1034 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1035 {
1036 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001037
Hanno Becker05154c32019-05-03 15:23:51 +01001038 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001039 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001040 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001041 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001042
1043 transform->out_cid_len = ssl->handshake->peer_cid_len;
1044 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1045 ssl->handshake->peer_cid_len );
1046 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1047 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001048 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001049#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001050
Paul Bakker5121ce52009-01-03 21:22:43 +00001051 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001052 * SSLv3:
1053 * key block =
1054 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1055 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1056 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1057 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1058 * ...
1059 *
1060 * TLSv1:
1061 * key block = PRF( master, "key expansion", randbytes )
1062 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001063 ret = handshake->tls_prf( session->master, 48, "key expansion",
1064 handshake->randbytes, 64, keyblk, 256 );
1065 if( ret != 0 )
1066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001068 return( ret );
1069 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1072 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1073 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1074 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1075 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001076
Paul Bakker5121ce52009-01-03 21:22:43 +00001077 /*
1078 * Determine the appropriate key, IV and MAC length.
1079 */
Paul Bakker68884e32013-01-07 18:20:04 +01001080
Hanno Becker88aaf652017-12-27 08:17:40 +00001081 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001082
Hanno Becker8031d062018-01-03 15:32:31 +00001083#if defined(MBEDTLS_GCM_C) || \
1084 defined(MBEDTLS_CCM_C) || \
1085 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001087 cipher_info->mode == MBEDTLS_MODE_CCM ||
1088 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001089 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001090 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001091
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001092 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001093 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001094 transform->taglen =
1095 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001096
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001097 /* All modes haves 96-bit IVs;
1098 * GCM and CCM has 4 implicit and 8 explicit bytes
1099 * ChachaPoly has all 12 bytes implicit
1100 */
Paul Bakker68884e32013-01-07 18:20:04 +01001101 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001102 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1103 transform->fixed_ivlen = 12;
1104 else
1105 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001106
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001107 /* Minimum length of encrypted record */
1108 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001109 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001110 }
1111 else
Hanno Becker8031d062018-01-03 15:32:31 +00001112#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1113#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1114 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1115 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001116 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001117 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1119 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001122 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001123 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001124
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001125 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001126 mac_key_len = mbedtls_md_get_size( md_info );
1127 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001130 /*
1131 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1132 * (rfc 6066 page 13 or rfc 2104 section 4),
1133 * so we only need to adjust the length here.
1134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001138
1139#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1140 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001141 * HMAC implementation which also truncates the key
1142 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001143 mac_key_len = transform->maclen;
1144#endif
1145 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001147
1148 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001149 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001150
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001151 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001153 transform->minlen = transform->maclen;
1154 else
Paul Bakker68884e32013-01-07 18:20:04 +01001155 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001156 /*
1157 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001158 * 1. if EtM is in use: one block plus MAC
1159 * otherwise: * first multiple of blocklen greater than maclen
1160 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1163 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001164 {
1165 transform->minlen = transform->maclen
1166 + cipher_info->block_size;
1167 }
1168 else
1169#endif
1170 {
1171 transform->minlen = transform->maclen
1172 + cipher_info->block_size
1173 - transform->maclen % cipher_info->block_size;
1174 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1177 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1178 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001179 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001180 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001181#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1183 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1184 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001185 {
1186 transform->minlen += transform->ivlen;
1187 }
1188 else
1189#endif
1190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001192 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1193 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001194 }
Paul Bakker68884e32013-01-07 18:20:04 +01001195 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 }
Hanno Becker8031d062018-01-03 15:32:31 +00001197 else
1198#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1199 {
1200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1202 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001203
Hanno Becker88aaf652017-12-27 08:17:40 +00001204 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1205 (unsigned) keylen,
1206 (unsigned) transform->minlen,
1207 (unsigned) transform->ivlen,
1208 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001209
1210 /*
1211 * Finally setup the cipher contexts, IVs and MAC secrets.
1212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001214 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001216 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001217 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001218
Paul Bakker68884e32013-01-07 18:20:04 +01001219 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001220 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001221
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001222 /*
1223 * This is not used in TLS v1.1.
1224 */
Paul Bakker48916f92012-09-16 19:57:18 +00001225 iv_copy_len = ( transform->fixed_ivlen ) ?
1226 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001227 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1228 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001229 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 }
1231 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232#endif /* MBEDTLS_SSL_CLI_C */
1233#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001234 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001235 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001236 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001237 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001238
Hanno Becker81c7b182017-11-09 18:39:33 +00001239 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001240 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001241
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001242 /*
1243 * This is not used in TLS v1.1.
1244 */
Paul Bakker48916f92012-09-16 19:57:18 +00001245 iv_copy_len = ( transform->fixed_ivlen ) ?
1246 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001247 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1248 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001249 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001251 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001255 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1256 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001257 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001258
Hanno Beckerd56ed242018-01-03 15:32:51 +00001259#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260#if defined(MBEDTLS_SSL_PROTO_SSL3)
1261 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001262 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001263 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001266 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1267 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001268 }
1269
Hanno Becker81c7b182017-11-09 18:39:33 +00001270 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1271 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001272 }
1273 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1275#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1276 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1277 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001278 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001279 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1280 For AEAD-based ciphersuites, there is nothing to do here. */
1281 if( mac_key_len != 0 )
1282 {
1283 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1284 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1285 }
Paul Bakker68884e32013-01-07 18:20:04 +01001286 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001287 else
1288#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001291 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1292 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001293 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001294#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1297 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001298 {
1299 int ret = 0;
1300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001302
Hanno Becker88aaf652017-12-27 08:17:40 +00001303 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001304 transform->iv_enc, transform->iv_dec,
1305 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001306 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001307 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001310 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1311 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001312 }
1313 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001314#else
1315 ((void) mac_dec);
1316 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001318
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001319#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1320 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001321 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001322 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1323 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001324 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001325 iv_copy_len );
1326 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001327
1328 if( ssl->conf->f_export_keys_ext != NULL )
1329 {
1330 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1331 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001332 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001333 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001334 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001335 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001336 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001337 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001338#endif
1339
Hanno Beckerf704bef2018-11-16 15:21:18 +00001340#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001341
1342 /* Only use PSA-based ciphers for TLS-1.2.
1343 * That's relevant at least for TLS-1.0, where
1344 * we assume that mbedtls_cipher_crypt() updates
1345 * the structure field for the IV, which the PSA-based
1346 * implementation currently doesn't. */
1347#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1348 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001349 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001350 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001351 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001352 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1353 {
1354 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001355 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001356 }
1357
1358 if( ret == 0 )
1359 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001360 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001361 psa_fallthrough = 0;
1362 }
1363 else
1364 {
1365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1366 psa_fallthrough = 1;
1367 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001368 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001369 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001370 psa_fallthrough = 1;
1371#else
1372 psa_fallthrough = 1;
1373#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001374
Hanno Beckercb1cc802018-11-17 22:27:38 +00001375 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001376#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001377 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001378 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001380 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001381 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001382 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001383
Hanno Beckerf704bef2018-11-16 15:21:18 +00001384#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001385 /* Only use PSA-based ciphers for TLS-1.2.
1386 * That's relevant at least for TLS-1.0, where
1387 * we assume that mbedtls_cipher_crypt() updates
1388 * the structure field for the IV, which the PSA-based
1389 * implementation currently doesn't. */
1390#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1391 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001392 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001393 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001394 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001395 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1396 {
1397 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001398 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001399 }
1400
1401 if( ret == 0 )
1402 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001403 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001404 psa_fallthrough = 0;
1405 }
1406 else
1407 {
1408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1409 psa_fallthrough = 1;
1410 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001411 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001412 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001413 psa_fallthrough = 1;
1414#else
1415 psa_fallthrough = 1;
1416#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001417
Hanno Beckercb1cc802018-11-17 22:27:38 +00001418 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001419#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001420 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001421 cipher_info ) ) != 0 )
1422 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001424 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001425 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001428 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001432 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001433 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001436 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001440 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001441 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443#if defined(MBEDTLS_CIPHER_MODE_CBC)
1444 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1447 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001450 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001451 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1454 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001457 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001458 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001459 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001461
Paul Bakker5121ce52009-01-03 21:22:43 +00001462
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001463 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001468
Paul Bakker48916f92012-09-16 19:57:18 +00001469 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1470 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001471
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001472 if( deflateInit( &transform->ctx_deflate,
1473 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001474 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001477 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1478 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001479 }
1480 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001482
Ron Eldore6992702019-05-07 18:27:13 +03001483end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001484 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Ron Eldore6992702019-05-07 18:27:13 +03001485 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001486}
1487
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001488/*
1489 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
1490 *
1491 * Inputs:
1492 * - SSL/TLS minor version
1493 * - hash associated with the ciphersuite (only used by TLS 1.2)
1494 *
1495 * Ouputs:
1496 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1497 */
1498static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1499 int minor_ver,
1500 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001501{
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001502#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1503 (void) hash;
1504#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001505
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001506#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001507 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001508 {
1509 handshake->tls_prf = ssl3_prf;
1510 handshake->calc_verify = ssl_calc_verify_ssl;
1511 handshake->calc_finished = ssl_calc_finished_ssl;
1512 }
1513 else
1514#endif
1515#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001516 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001517 {
1518 handshake->tls_prf = tls1_prf;
1519 handshake->calc_verify = ssl_calc_verify_tls;
1520 handshake->calc_finished = ssl_calc_finished_tls;
1521 }
1522 else
1523#endif
1524#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1525#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001526 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1527 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001528 {
1529 handshake->tls_prf = tls_prf_sha384;
1530 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1531 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1532 }
1533 else
1534#endif
1535#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001536 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001537 {
1538 handshake->tls_prf = tls_prf_sha256;
1539 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1540 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1541 }
1542 else
1543#endif
1544#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1545 {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001546 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1547 }
1548
1549 return( 0 );
1550}
1551
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001552/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001553 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001554 *
1555 * Parameters:
1556 * [in/out] handshake
1557 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001558 * (PSA-PSK) ciphersuite_info, psk_opaque
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001559 * [out] premaster (cleared)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001560 * [out] master
1561 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1562 * debug: conf->f_dbg, conf->p_dbg
1563 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001564 * PSA-PSA: minor_ver, conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001565 */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001566static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001567 unsigned char *master,
Manuel Pégourié-Gonnard0d56aaa2019-05-03 09:58:33 +02001568 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001569{
1570 int ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001571
1572 /* cf. RFC 5246, Section 8.1:
1573 * "The master secret is always exactly 48 bytes in length." */
1574 size_t const master_secret_len = 48;
1575
1576#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1577 unsigned char session_hash[48];
1578#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1579
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001580 /* The label for the KDF used for key expansion.
1581 * This is either "master secret" or "extended master secret"
1582 * depending on whether the Extended Master Secret extension
1583 * is used. */
1584 char const *lbl = "master secret";
1585
1586 /* The salt for the KDF used for key expansion.
1587 * - If the Extended Master Secret extension is not used,
1588 * this is ClientHello.Random + ServerHello.Random
1589 * (see Sect. 8.1 in RFC 5246).
1590 * - If the Extended Master Secret extension is used,
1591 * this is the transcript of the handshake so far.
1592 * (see Sect. 4 in RFC 7627). */
1593 unsigned char const *salt = handshake->randbytes;
1594 size_t salt_len = 64;
1595
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001596#if !defined(MBEDTLS_DEBUG_C) && \
1597 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1598 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
1599 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
1600 (void) ssl;
1601#endif
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001602
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001603 if( handshake->resume != 0 )
1604 {
1605 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001606 return( 0 );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001607 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001608
1609#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001610 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001611 {
1612 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001613
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001614 lbl = "extended master secret";
1615 salt = session_hash;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001616 handshake->calc_verify( ssl, session_hash, &salt_len );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001617
1618 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
1619 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001620#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1621
1622#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001623 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1624 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001625 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001626 ssl_use_opaque_psk( ssl ) == 1 )
1627 {
1628 /* Perform PSK-to-MS expansion in a single step. */
1629 psa_status_t status;
1630 psa_algorithm_t alg;
1631 psa_key_handle_t psk;
1632 psa_key_derivation_operation_t derivation =
1633 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001634 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001635
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001637
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001638 psk = ssl->conf->psk_opaque;
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001639 if( handshake->psk_opaque != 0 )
1640 psk = handshake->psk_opaque;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001641
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001642 if( hash_alg == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001643 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001644 else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001645 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1646
1647 status = psa_key_derivation( &derivation, psk, alg,
1648 salt, salt_len,
1649 (unsigned char const *) lbl,
1650 (size_t) strlen( lbl ),
1651 master_secret_len );
1652 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001653 {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001654 psa_key_derivation_abort( &derivation );
1655 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001656 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001657
1658 status = psa_key_derivation_output_bytes( &derivation,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001659 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001660 master_secret_len );
1661 if( status != PSA_SUCCESS )
1662 {
1663 psa_key_derivation_abort( &derivation );
1664 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1665 }
1666
1667 status = psa_key_derivation_abort( &derivation );
1668 if( status != PSA_SUCCESS )
1669 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1670 }
1671 else
1672#endif
1673 {
1674 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1675 lbl, salt, salt_len,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001676 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001677 master_secret_len );
1678 if( ret != 0 )
1679 {
1680 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1681 return( ret );
1682 }
1683
1684 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1685 handshake->premaster,
1686 handshake->pmslen );
1687
1688 mbedtls_platform_zeroize( handshake->premaster,
1689 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001690 }
1691
1692 return( 0 );
1693}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001694
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001695int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1696{
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001697 int ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001698 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1699 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001700
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1702
1703 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001704 ret = ssl_set_handshake_prfs( ssl->handshake,
1705 ssl->minor_ver,
1706 ciphersuite_info->mac );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001707 if( ret != 0 )
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001708 {
1709 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001710 return( ret );
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001711 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001712
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001713 /* Compute master secret if needed */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001714 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001715 ssl->session_negotiate->master,
1716 ssl );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001717 if( ret != 0 )
1718 {
1719 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1720 return( ret );
1721 }
1722
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001723 /* Swap the client and server random values:
1724 * - MS derivation wanted client+server (RFC 5246 8.1)
1725 * - key derivation wants server+client (RFC 5246 6.3) */
1726 {
1727 unsigned char tmp[64];
1728 memcpy( tmp, ssl->handshake->randbytes, 64 );
1729 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1730 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1731 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1732 }
1733
1734 /* Populate transform structure */
1735 ret = ssl_populate_transform( ssl );
1736 if( ret != 0 )
1737 {
1738 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1739 return( ret );
1740 }
1741
1742 /* We no longer need Server/ClientHello.random values */
1743 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1744 sizeof( ssl->handshake->randbytes ) );
1745
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001746 /* Allocate compression buffer */
1747#if defined(MBEDTLS_ZLIB_SUPPORT)
1748 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1749 ssl->compress_buf == NULL )
1750 {
1751 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1752 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1753 if( ssl->compress_buf == NULL )
1754 {
1755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
1756 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
1757 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1758 }
1759 }
1760#endif
1761
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1763
1764 return( 0 );
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001765}
1766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001768void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1769 unsigned char hash[36],
1770 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001771{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001772 mbedtls_md5_context md5;
1773 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001774 unsigned char pad_1[48];
1775 unsigned char pad_2[48];
1776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001778
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001779 mbedtls_md5_init( &md5 );
1780 mbedtls_sha1_init( &sha1 );
1781
1782 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1783 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001784
Paul Bakker380da532012-04-18 16:10:25 +00001785 memset( pad_1, 0x36, 48 );
1786 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001787
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001788 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1789 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1790 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001791
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001792 mbedtls_md5_starts_ret( &md5 );
1793 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1794 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1795 mbedtls_md5_update_ret( &md5, hash, 16 );
1796 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001797
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001798 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1799 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1800 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001801
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001802 mbedtls_sha1_starts_ret( &sha1 );
1803 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1804 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1805 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1806 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001807
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001808 *hlen = 36;
1809
1810 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001812
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001813 mbedtls_md5_free( &md5 );
1814 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001815
Paul Bakker380da532012-04-18 16:10:25 +00001816 return;
1817}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001821void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1822 unsigned char hash[36],
1823 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001824{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001825 mbedtls_md5_context md5;
1826 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001829
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001830 mbedtls_md5_init( &md5 );
1831 mbedtls_sha1_init( &sha1 );
1832
1833 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1834 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001835
Andrzej Kurekeb342242019-01-29 09:14:33 -05001836 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001837 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001838
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001839 *hlen = 36;
1840
1841 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001843
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001844 mbedtls_md5_free( &md5 );
1845 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001846
Paul Bakker380da532012-04-18 16:10:25 +00001847 return;
1848}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1852#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001853void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1854 unsigned char hash[32],
1855 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001856{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001857#if defined(MBEDTLS_USE_PSA_CRYPTO)
1858 size_t hash_size;
1859 psa_status_t status;
1860 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1861
1862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1863 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1864 if( status != PSA_SUCCESS )
1865 {
1866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1867 return;
1868 }
1869
1870 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1871 if( status != PSA_SUCCESS )
1872 {
1873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1874 return;
1875 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001876
1877 *hlen = 32;
1878 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1880#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001881 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001882
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001883 mbedtls_sha256_init( &sha256 );
1884
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001886
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001887 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001888 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001889
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001890 *hlen = 32;
1891
1892 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001894
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001895 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001896#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001897 return;
1898}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001902void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1903 unsigned char hash[48],
1904 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001905{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001906#if defined(MBEDTLS_USE_PSA_CRYPTO)
1907 size_t hash_size;
1908 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001909 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001910
1911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001912 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001913 if( status != PSA_SUCCESS )
1914 {
1915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1916 return;
1917 }
1918
Andrzej Kurek972fba52019-01-30 03:29:12 -05001919 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001920 if( status != PSA_SUCCESS )
1921 {
1922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1923 return;
1924 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001925
1926 *hlen = 48;
1927 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1929#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001930 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001931
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001932 mbedtls_sha512_init( &sha512 );
1933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001935
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001936 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001937 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001938
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001939 *hlen = 48;
1940
1941 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001943
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001944 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001945#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001946 return;
1947}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948#endif /* MBEDTLS_SHA512_C */
1949#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1952int 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 +02001953{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001954 unsigned char *p = ssl->handshake->premaster;
1955 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001956 const unsigned char *psk = ssl->conf->psk;
1957 size_t psk_len = ssl->conf->psk_len;
1958
1959 /* If the psk callback was called, use its result */
1960 if( ssl->handshake->psk != NULL )
1961 {
1962 psk = ssl->handshake->psk;
1963 psk_len = ssl->handshake->psk_len;
1964 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001965
1966 /*
1967 * PMS = struct {
1968 * opaque other_secret<0..2^16-1>;
1969 * opaque psk<0..2^16-1>;
1970 * };
1971 * with "other_secret" depending on the particular key exchange
1972 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1974 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001975 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001976 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001978
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001979 *(p++) = (unsigned char)( psk_len >> 8 );
1980 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001981
1982 if( end < p || (size_t)( end - p ) < psk_len )
1983 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1984
1985 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001986 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001987 }
1988 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1990#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1991 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001992 {
1993 /*
1994 * other_secret already set by the ClientKeyExchange message,
1995 * and is 48 bytes long
1996 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001997 if( end - p < 2 )
1998 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1999
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002000 *p++ = 0;
2001 *p++ = 48;
2002 p += 48;
2003 }
2004 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2006#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2007 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002008 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002009 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002010 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002011
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002012 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002014 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002015 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002018 return( ret );
2019 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002020 *(p++) = (unsigned char)( len >> 8 );
2021 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002022 p += len;
2023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002025 }
2026 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2028#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2029 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002030 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002031 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002032 size_t zlen;
2033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002035 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002036 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002039 return( ret );
2040 }
2041
2042 *(p++) = (unsigned char)( zlen >> 8 );
2043 *(p++) = (unsigned char)( zlen );
2044 p += zlen;
2045
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002046 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2047 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002048 }
2049 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002054 }
2055
2056 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002057 if( end - p < 2 )
2058 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002059
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002060 *(p++) = (unsigned char)( psk_len >> 8 );
2061 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002062
2063 if( end < p || (size_t)( end - p ) < psk_len )
2064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2065
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002066 memcpy( p, psk, psk_len );
2067 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002068
2069 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2070
2071 return( 0 );
2072}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002076/*
2077 * SSLv3.0 MAC functions
2078 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002079#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002080static void ssl_mac( mbedtls_md_context_t *md_ctx,
2081 const unsigned char *secret,
2082 const unsigned char *buf, size_t len,
2083 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002084 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002085{
2086 unsigned char header[11];
2087 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002088 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2090 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002091
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002092 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002094 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002095 else
Paul Bakker68884e32013-01-07 18:20:04 +01002096 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002097
2098 memcpy( header, ctr, 8 );
2099 header[ 8] = (unsigned char) type;
2100 header[ 9] = (unsigned char)( len >> 8 );
2101 header[10] = (unsigned char)( len );
2102
Paul Bakker68884e32013-01-07 18:20:04 +01002103 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 mbedtls_md_starts( md_ctx );
2105 mbedtls_md_update( md_ctx, secret, md_size );
2106 mbedtls_md_update( md_ctx, padding, padlen );
2107 mbedtls_md_update( md_ctx, header, 11 );
2108 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002109 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002110
Paul Bakker68884e32013-01-07 18:20:04 +01002111 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 mbedtls_md_starts( md_ctx );
2113 mbedtls_md_update( md_ctx, secret, md_size );
2114 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002115 mbedtls_md_update( md_ctx, out, md_size );
2116 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002117}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002119
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002120/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002121 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002122#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002123 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2124 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2125 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2126/* This function makes sure every byte in the memory region is accessed
2127 * (in ascending addresses order) */
2128static void ssl_read_memory( unsigned char *p, size_t len )
2129{
2130 unsigned char acc = 0;
2131 volatile unsigned char force;
2132
2133 for( ; len != 0; p++, len-- )
2134 acc ^= *p;
2135
2136 force = acc;
2137 (void) force;
2138}
2139#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2140
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002141/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002142 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002143 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002144
Hanno Beckera0e20d02019-05-15 14:03:01 +01002145#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002146/* This functions transforms a DTLS plaintext fragment and a record content
2147 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002148 *
2149 * struct {
2150 * opaque content[DTLSPlaintext.length];
2151 * ContentType real_type;
2152 * uint8 zeros[length_of_padding];
2153 * } DTLSInnerPlaintext;
2154 *
2155 * Input:
2156 * - `content`: The beginning of the buffer holding the
2157 * plaintext to be wrapped.
2158 * - `*content_size`: The length of the plaintext in Bytes.
2159 * - `max_len`: The number of Bytes available starting from
2160 * `content`. This must be `>= *content_size`.
2161 * - `rec_type`: The desired record content type.
2162 *
2163 * Output:
2164 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2165 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2166 *
2167 * Returns:
2168 * - `0` on success.
2169 * - A negative error code if `max_len` didn't offer enough space
2170 * for the expansion.
2171 */
2172static int ssl_cid_build_inner_plaintext( unsigned char *content,
2173 size_t *content_size,
2174 size_t remaining,
2175 uint8_t rec_type )
2176{
2177 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002178 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2179 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2180 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002181
2182 /* Write real content type */
2183 if( remaining == 0 )
2184 return( -1 );
2185 content[ len ] = rec_type;
2186 len++;
2187 remaining--;
2188
2189 if( remaining < pad )
2190 return( -1 );
2191 memset( content + len, 0, pad );
2192 len += pad;
2193 remaining -= pad;
2194
2195 *content_size = len;
2196 return( 0 );
2197}
2198
Hanno Becker07dc97d2019-05-20 15:08:01 +01002199/* This function parses a DTLSInnerPlaintext structure.
2200 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002201static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2202 size_t *content_size,
2203 uint8_t *rec_type )
2204{
2205 size_t remaining = *content_size;
2206
2207 /* Determine length of padding by skipping zeroes from the back. */
2208 do
2209 {
2210 if( remaining == 0 )
2211 return( -1 );
2212 remaining--;
2213 } while( content[ remaining ] == 0 );
2214
2215 *content_size = remaining;
2216 *rec_type = content[ remaining ];
2217
2218 return( 0 );
2219}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002220#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002221
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002222/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002223 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002224static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002225 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002226 mbedtls_record *rec )
2227{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002228 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002229 *
2230 * additional_data = seq_num + TLSCompressed.type +
2231 * TLSCompressed.version + TLSCompressed.length;
2232 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002233 * For the CID extension, this is extended as follows
2234 * (quoting draft-ietf-tls-dtls-connection-id-05,
2235 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002236 *
2237 * additional_data = seq_num + DTLSPlaintext.type +
2238 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002239 * cid +
2240 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002241 * length_of_DTLSInnerPlaintext;
2242 */
2243
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002244 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2245 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002246 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002247
Hanno Beckera0e20d02019-05-15 14:03:01 +01002248#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002249 if( rec->cid_len != 0 )
2250 {
2251 memcpy( add_data + 11, rec->cid, rec->cid_len );
2252 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2253 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2254 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2255 *add_data_len = 13 + 1 + rec->cid_len;
2256 }
2257 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002258#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002259 {
2260 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2261 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2262 *add_data_len = 13;
2263 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002264}
2265
Hanno Beckera18d1322018-01-03 14:27:32 +00002266int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2267 mbedtls_ssl_transform *transform,
2268 mbedtls_record *rec,
2269 int (*f_rng)(void *, unsigned char *, size_t),
2270 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002271{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002273 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002274 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002275 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002276 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002277 size_t post_avail;
2278
2279 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002280#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002281 ((void) ssl);
2282#endif
2283
2284 /* The PRNG is used for dynamic IV generation that's used
2285 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2286#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2287 ( defined(MBEDTLS_AES_C) || \
2288 defined(MBEDTLS_ARIA_C) || \
2289 defined(MBEDTLS_CAMELLIA_C) ) && \
2290 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2291 ((void) f_rng);
2292 ((void) p_rng);
2293#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002296
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002297 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002298 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2300 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2301 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002302 if( rec == NULL
2303 || rec->buf == NULL
2304 || rec->buf_len < rec->data_offset
2305 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002306#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002307 || rec->cid_len != 0
2308#endif
2309 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002310 {
2311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002313 }
2314
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002315 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002316 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002318 data, rec->data_len );
2319
2320 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2321
2322 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2323 {
2324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2325 (unsigned) rec->data_len,
2326 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2327 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2328 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002329
Hanno Beckera0e20d02019-05-15 14:03:01 +01002330#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002331 /*
2332 * Add CID information
2333 */
2334 rec->cid_len = transform->out_cid_len;
2335 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2336 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002337
2338 if( rec->cid_len != 0 )
2339 {
2340 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002341 * Wrap plaintext into DTLSInnerPlaintext structure.
2342 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002343 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002344 * Note that this changes `rec->data_len`, and hence
2345 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002346 */
2347 if( ssl_cid_build_inner_plaintext( data,
2348 &rec->data_len,
2349 post_avail,
2350 rec->type ) != 0 )
2351 {
2352 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2353 }
2354
2355 rec->type = MBEDTLS_SSL_MSG_CID;
2356 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002357#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002358
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002359 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2360
Paul Bakker5121ce52009-01-03 21:22:43 +00002361 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002362 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002363 */
Hanno Becker52344c22018-01-03 15:24:20 +00002364#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 if( mode == MBEDTLS_MODE_STREAM ||
2366 ( mode == MBEDTLS_MODE_CBC
2367#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002368 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002369#endif
2370 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002371 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002372 if( post_avail < transform->maclen )
2373 {
2374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2375 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2376 }
2377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002379 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002380 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002381 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002382 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2383 data, rec->data_len, rec->ctr, rec->type, mac );
2384 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002385 }
2386 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002387#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2389 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002390 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002391 {
Hanno Becker992b6872017-11-09 18:57:39 +00002392 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2393
Hanno Beckercab87e62019-04-29 13:52:53 +01002394 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002395
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002396 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002397 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002398 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2399 data, rec->data_len );
2400 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2401 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2402
2403 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002404 }
2405 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002406#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2409 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002410 }
2411
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002412 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2413 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002414
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002415 rec->data_len += transform->maclen;
2416 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002417 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002418 }
Hanno Becker52344c22018-01-03 15:24:20 +00002419#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002420
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002421 /*
2422 * Encrypt
2423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2425 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002427 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002428 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002430 "including %d bytes of padding",
2431 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002432
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002433 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2434 transform->iv_enc, transform->ivlen,
2435 data, rec->data_len,
2436 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002439 return( ret );
2440 }
2441
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002442 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2445 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002446 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 }
Paul Bakker68884e32013-01-07 18:20:04 +01002448 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002450
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002451#if defined(MBEDTLS_GCM_C) || \
2452 defined(MBEDTLS_CCM_C) || \
2453 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002455 mode == MBEDTLS_MODE_CCM ||
2456 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002457 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002458 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002459 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002460 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002461
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002462 /* Check that there's space for both the authentication tag
2463 * and the explicit IV before and after the record content. */
2464 if( post_avail < transform->taglen ||
2465 rec->data_offset < explicit_iv_len )
2466 {
2467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2468 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2469 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002470
Paul Bakker68884e32013-01-07 18:20:04 +01002471 /*
2472 * Generate IV
2473 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002474 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2475 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002476 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002477 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002478 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2479 explicit_iv_len );
2480 /* Prefix record content with explicit IV. */
2481 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002482 }
2483 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2484 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002485 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002486 unsigned char i;
2487
2488 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2489
2490 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002491 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002492 }
2493 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002494 {
2495 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2497 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002498 }
2499
Hanno Beckercab87e62019-04-29 13:52:53 +01002500 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002501
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002502 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2503 iv, transform->ivlen );
2504 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002505 data - explicit_iv_len, explicit_iv_len );
2506 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002507 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002509 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002510 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002511
Paul Bakker68884e32013-01-07 18:20:04 +01002512 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002513 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002514 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002515
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002516 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002517 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002518 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002519 data, rec->data_len, /* source */
2520 data, &rec->data_len, /* destination */
2521 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002524 return( ret );
2525 }
2526
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002527 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2528 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002529
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002530 rec->data_len += transform->taglen + explicit_iv_len;
2531 rec->data_offset -= explicit_iv_len;
2532 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002533 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002534 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2537#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002538 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002541 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002542 size_t padlen, i;
2543 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002544
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002545 /* Currently we're always using minimal padding
2546 * (up to 255 bytes would be allowed). */
2547 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2548 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002549 padlen = 0;
2550
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002551 /* Check there's enough space in the buffer for the padding. */
2552 if( post_avail < padlen + 1 )
2553 {
2554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2555 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2556 }
2557
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002559 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002561 rec->data_len += padlen + 1;
2562 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002565 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002566 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2567 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002568 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002569 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002570 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002571 if( f_rng == NULL )
2572 {
2573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2574 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2575 }
2576
2577 if( rec->data_offset < transform->ivlen )
2578 {
2579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2580 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2581 }
2582
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002583 /*
2584 * Generate IV
2585 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002586 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002587 if( ret != 0 )
2588 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002589
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002590 memcpy( data - transform->ivlen, transform->iv_enc,
2591 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002592
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002593 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002597 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002598 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002599 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002601 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2602 transform->iv_enc,
2603 transform->ivlen,
2604 data, rec->data_len,
2605 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002608 return( ret );
2609 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002610
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002611 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2614 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002615 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002618 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002619 {
2620 /*
2621 * Save IV in SSL3 and TLS1
2622 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002623 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2624 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002626 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002627#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002628 {
2629 data -= transform->ivlen;
2630 rec->data_offset -= transform->ivlen;
2631 rec->data_len += transform->ivlen;
2632 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002635 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002636 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002637 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2638
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002639 /*
2640 * MAC(MAC_write_key, seq_num +
2641 * TLSCipherText.type +
2642 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002643 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002644 * IV + // except for TLS 1.0
2645 * ENC(content + padding + padding_length));
2646 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002647
2648 if( post_avail < transform->maclen)
2649 {
2650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2651 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2652 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002653
Hanno Beckercab87e62019-04-29 13:52:53 +01002654 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002657 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002658 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002659
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002660 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002661 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002662 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2663 data, rec->data_len );
2664 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2665 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002666
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002667 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002668
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002669 rec->data_len += transform->maclen;
2670 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002671 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002672 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002675 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002677 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2680 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002681 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002682
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002683 /* Make extra sure authentication was performed, exactly once */
2684 if( auth_done != 1 )
2685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2687 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002688 }
2689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
2692 return( 0 );
2693}
2694
Hanno Becker605949f2019-07-12 08:23:59 +01002695int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002696 mbedtls_ssl_transform *transform,
2697 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002698{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002699 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002701 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002702#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002703 size_t padlen = 0, correct = 1;
2704#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002705 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002706 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002707 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002708
Hanno Beckera18d1322018-01-03 14:27:32 +00002709#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002710 ((void) ssl);
2711#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002714 if( rec == NULL ||
2715 rec->buf == NULL ||
2716 rec->buf_len < rec->data_offset ||
2717 rec->buf_len - rec->data_offset < rec->data_len )
2718 {
2719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002721 }
2722
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002723 data = rec->buf + rec->data_offset;
2724 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002725
Hanno Beckera0e20d02019-05-15 14:03:01 +01002726#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002727 /*
2728 * Match record's CID with incoming CID.
2729 */
Hanno Becker938489a2019-05-08 13:02:22 +01002730 if( rec->cid_len != transform->in_cid_len ||
2731 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2732 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002733 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002734 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002735#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2738 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002739 {
2740 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002741 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2742 transform->iv_dec,
2743 transform->ivlen,
2744 data, rec->data_len,
2745 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002748 return( ret );
2749 }
2750
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002751 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2754 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002755 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002756 }
Paul Bakker68884e32013-01-07 18:20:04 +01002757 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002759#if defined(MBEDTLS_GCM_C) || \
2760 defined(MBEDTLS_CCM_C) || \
2761 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002763 mode == MBEDTLS_MODE_CCM ||
2764 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002765 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002766 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002767 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002768
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002769 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002770 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002771 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002772
2773 /* Check that there's enough space for the explicit IV
2774 * (at the beginning of the record) and the MAC (at the
2775 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002776 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002779 "+ taglen (%d)", rec->data_len,
2780 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002782 }
Paul Bakker68884e32013-01-07 18:20:04 +01002783
Hanno Beckerd96a6522019-07-10 13:55:25 +01002784#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002785 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2786 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002787 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002788
Hanno Beckerd96a6522019-07-10 13:55:25 +01002789 /* Fixed */
2790 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2791 /* Explicit */
2792 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002793 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002794 else
2795#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2796#if defined(MBEDTLS_CHACHAPOLY_C)
2797 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002798 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002799 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002800 unsigned char i;
2801
2802 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2803
2804 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002805 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002806 }
2807 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002808#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002809 {
2810 /* Reminder if we ever add an AEAD mode with a different size */
2811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2812 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2813 }
2814
Hanno Beckerd96a6522019-07-10 13:55:25 +01002815 /* Group changes to data, data_len, and add_data, because
2816 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002817 data += explicit_iv_len;
2818 rec->data_offset += explicit_iv_len;
2819 rec->data_len -= explicit_iv_len + transform->taglen;
2820
Hanno Beckercab87e62019-04-29 13:52:53 +01002821 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002822 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002823 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002824
Hanno Beckerd96a6522019-07-10 13:55:25 +01002825 /* Because of the check above, we know that there are
2826 * explicit_iv_len Bytes preceeding data, and taglen
2827 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002828 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002829 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002830
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002831 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002832 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002833 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002834
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002835 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002836 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002837 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002838 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2839 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002840 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002841 data, rec->data_len,
2842 data, &olen,
2843 data + rec->data_len,
2844 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2849 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002850
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002851 return( ret );
2852 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002853 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002854
Hanno Beckerd96a6522019-07-10 13:55:25 +01002855 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002856 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2859 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002860 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002861 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002862 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2864#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002865 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002867 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002868 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002869
Paul Bakker5121ce52009-01-03 21:22:43 +00002870 /*
Paul Bakker45829992013-01-03 14:52:21 +01002871 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002872 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002874 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2875 {
2876 /* The ciphertext is prefixed with the CBC IV. */
2877 minlen += transform->ivlen;
2878 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002879#endif
Paul Bakker45829992013-01-03 14:52:21 +01002880
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002881 /* Size considerations:
2882 *
2883 * - The CBC cipher text must not be empty and hence
2884 * at least of size transform->ivlen.
2885 *
2886 * Together with the potential IV-prefix, this explains
2887 * the first of the two checks below.
2888 *
2889 * - The record must contain a MAC, either in plain or
2890 * encrypted, depending on whether Encrypt-then-MAC
2891 * is used or not.
2892 * - If it is, the message contains the IV-prefix,
2893 * the CBC ciphertext, and the MAC.
2894 * - If it is not, the padded plaintext, and hence
2895 * the CBC ciphertext, has at least length maclen + 1
2896 * because there is at least the padding length byte.
2897 *
2898 * As the CBC ciphertext is not empty, both cases give the
2899 * lower bound minlen + maclen + 1 on the record size, which
2900 * we test for in the second check below.
2901 */
2902 if( rec->data_len < minlen + transform->ivlen ||
2903 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002906 "+ 1 ) ( + expl IV )", rec->data_len,
2907 transform->ivlen,
2908 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002910 }
2911
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002912 /*
2913 * Authenticate before decrypt if enabled
2914 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002916 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002917 {
Hanno Becker992b6872017-11-09 18:57:39 +00002918 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002921
Hanno Beckerd96a6522019-07-10 13:55:25 +01002922 /* Update data_len in tandem with add_data.
2923 *
2924 * The subtraction is safe because of the previous check
2925 * data_len >= minlen + maclen + 1.
2926 *
2927 * Afterwards, we know that data + data_len is followed by at
2928 * least maclen Bytes, which justifies the call to
2929 * mbedtls_ssl_safer_memcmp() below.
2930 *
2931 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002932 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002933 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002934
Hanno Beckerd96a6522019-07-10 13:55:25 +01002935 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002936 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2937 add_data_len );
2938 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2939 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002940 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2941 data, rec->data_len );
2942 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2943 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002944
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002945 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2946 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002947 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002948 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002949
Hanno Beckerd96a6522019-07-10 13:55:25 +01002950 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002951 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2952 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002956 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002957 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002958 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002960
2961 /*
2962 * Check length sanity
2963 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002964
2965 /* We know from above that data_len > minlen >= 0,
2966 * so the following check in particular implies that
2967 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002968 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002971 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002973 }
2974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002976 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002977 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002978 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002979 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002980 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002981 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002982 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002983
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002984 data += transform->ivlen;
2985 rec->data_offset += transform->ivlen;
2986 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002987 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002989
Hanno Beckerd96a6522019-07-10 13:55:25 +01002990 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
2991
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002992 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2993 transform->iv_dec, transform->ivlen,
2994 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002997 return( ret );
2998 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002999
Hanno Beckerd96a6522019-07-10 13:55:25 +01003000 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003001 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3004 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003005 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003008 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003009 {
3010 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01003011 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3012 * records is equivalent to CBC decryption of the concatenation
3013 * of the records; in other words, IVs are maintained across
3014 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003015 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003016 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3017 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003018 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003019#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003020
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003021 /* Safe since data_len >= minlen + maclen + 1, so after having
3022 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01003023 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3024 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003025 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003026
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003027 if( auth_done == 1 )
3028 {
3029 correct *= ( rec->data_len >= padlen + 1 );
3030 padlen *= ( rec->data_len >= padlen + 1 );
3031 }
3032 else
Paul Bakker45829992013-01-03 14:52:21 +01003033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003035 if( rec->data_len < transform->maclen + padlen + 1 )
3036 {
3037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3038 rec->data_len,
3039 transform->maclen,
3040 padlen + 1 ) );
3041 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003042#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003043
3044 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3045 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003046 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003047
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003048 padlen++;
3049
3050 /* Regardless of the validity of the padding,
3051 * we have data_len >= padlen here. */
3052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003054 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003055 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003056 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003058#if defined(MBEDTLS_SSL_DEBUG_ALL)
3059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003060 "should be no more than %d",
3061 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003062#endif
Paul Bakker45829992013-01-03 14:52:21 +01003063 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003064 }
3065 }
3066 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3068#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3069 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003070 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003071 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003072 /* The padding check involves a series of up to 256
3073 * consecutive memory reads at the end of the record
3074 * plaintext buffer. In order to hide the length and
3075 * validity of the padding, always perform exactly
3076 * `min(256,plaintext_len)` reads (but take into account
3077 * only the last `padlen` bytes for the padding check). */
3078 size_t pad_count = 0;
3079 size_t real_count = 0;
3080 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003081
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003082 /* Index of first padding byte; it has been ensured above
3083 * that the subtraction is safe. */
3084 size_t const padding_idx = rec->data_len - padlen;
3085 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3086 size_t const start_idx = rec->data_len - num_checks;
3087 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003088
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003089 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003090 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003091 real_count |= ( idx >= padding_idx );
3092 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003093 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003094 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003097 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003099#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003100 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003101 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003102 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3104 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3107 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003108 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003109
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003110 /* If the padding was found to be invalid, padlen == 0
3111 * and the subtraction is safe. If the padding was found valid,
3112 * padlen hasn't been changed and the previous assertion
3113 * data_len >= padlen still holds. */
3114 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003115 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003116 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003118 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3121 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003122 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003123
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003124#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003126 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003127#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003128
3129 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003130 * Authenticate if not done yet.
3131 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003132 */
Hanno Becker52344c22018-01-03 15:24:20 +00003133#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003134 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003135 {
Hanno Becker992b6872017-11-09 18:57:39 +00003136 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003137
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003138 /* If the initial value of padlen was such that
3139 * data_len < maclen + padlen + 1, then padlen
3140 * got reset to 1, and the initial check
3141 * data_len >= minlen + maclen + 1
3142 * guarantees that at this point we still
3143 * have at least data_len >= maclen.
3144 *
3145 * If the initial value of padlen was such that
3146 * data_len >= maclen + padlen + 1, then we have
3147 * subtracted either padlen + 1 (if the padding was correct)
3148 * or 0 (if the padding was incorrect) since then,
3149 * hence data_len >= maclen in any case.
3150 */
3151 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003152 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003155 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003156 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003157 ssl_mac( &transform->md_ctx_dec,
3158 transform->mac_dec,
3159 data, rec->data_len,
3160 rec->ctr, rec->type,
3161 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003162 }
3163 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3165#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3166 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003167 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003168 {
3169 /*
3170 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003171 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003172 *
3173 * Known timing attacks:
3174 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3175 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003176 * To compensate for different timings for the MAC calculation
3177 * depending on how much padding was removed (which is determined
3178 * by padlen), process extra_run more blocks through the hash
3179 * function.
3180 *
3181 * The formula in the paper is
3182 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3183 * where L1 is the size of the header plus the decrypted message
3184 * plus CBC padding and L2 is the size of the header plus the
3185 * decrypted message. This is for an underlying hash function
3186 * with 64-byte blocks.
3187 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3188 * correctly. We round down instead of up, so -56 is the correct
3189 * value for our calculations instead of -55.
3190 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003191 * Repeat the formula rather than defining a block_size variable.
3192 * This avoids requiring division by a variable at runtime
3193 * (which would be marginally less efficient and would require
3194 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003195 */
3196 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003197 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003198
3199 /*
3200 * The next two sizes are the minimum and maximum values of
3201 * in_msglen over all padlen values.
3202 *
3203 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003204 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003205 *
3206 * Note that max_len + maclen is never more than the buffer
3207 * length, as we previously did in_msglen -= maclen too.
3208 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003209 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003210 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3211
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003212 memset( tmp, 0, sizeof( tmp ) );
3213
3214 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003215 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003216#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3217 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003218 case MBEDTLS_MD_MD5:
3219 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003220 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003221 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003222 extra_run =
3223 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3224 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003225 break;
3226#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003227#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003228 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003229 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003230 extra_run =
3231 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3232 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003233 break;
3234#endif
3235 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003237 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3238 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003239
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003240 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003241
Hanno Beckercab87e62019-04-29 13:52:53 +01003242 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3243 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003244 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3245 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003246 /* Make sure we access everything even when padlen > 0. This
3247 * makes the synchronisation requirements for just-in-time
3248 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003249 ssl_read_memory( data + rec->data_len, padlen );
3250 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003251
3252 /* Call mbedtls_md_process at least once due to cache attacks
3253 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003254 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003255 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003256
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003257 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003258
3259 /* Make sure we access all the memory that could contain the MAC,
3260 * before we check it in the next code block. This makes the
3261 * synchronisation requirements for just-in-time Prime+Probe
3262 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003263 ssl_read_memory( data + min_len,
3264 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003265 }
3266 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003267#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3268 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3271 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003272 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003273
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003274#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003275 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3276 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003277#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003278
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003279 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3280 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003282#if defined(MBEDTLS_SSL_DEBUG_ALL)
3283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003284#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003285 correct = 0;
3286 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003287 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003288 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003289
3290 /*
3291 * Finally check the correct flag
3292 */
3293 if( correct == 0 )
3294 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003295#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003296
3297 /* Make extra sure authentication was performed, exactly once */
3298 if( auth_done != 1 )
3299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3301 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003302 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003303
Hanno Beckera0e20d02019-05-15 14:03:01 +01003304#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003305 if( rec->cid_len != 0 )
3306 {
3307 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3308 &rec->type );
3309 if( ret != 0 )
3310 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3311 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003312#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003315
3316 return( 0 );
3317}
3318
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003319#undef MAC_NONE
3320#undef MAC_PLAINTEXT
3321#undef MAC_CIPHERTEXT
3322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003324/*
3325 * Compression/decompression functions
3326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003327static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003328{
3329 int ret;
3330 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003331 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003332 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003333 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003336
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003337 if( len_pre == 0 )
3338 return( 0 );
3339
Paul Bakker2770fbd2012-07-03 13:30:23 +00003340 memcpy( msg_pre, ssl->out_msg, len_pre );
3341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003343 ssl->out_msglen ) );
3344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003346 ssl->out_msg, ssl->out_msglen );
3347
Paul Bakker48916f92012-09-16 19:57:18 +00003348 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3349 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3350 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003351 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003352
Paul Bakker48916f92012-09-16 19:57:18 +00003353 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003354 if( ret != Z_OK )
3355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3357 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003358 }
3359
Angus Grattond8213d02016-05-25 20:56:48 +10003360 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003361 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003364 ssl->out_msglen ) );
3365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003367 ssl->out_msg, ssl->out_msglen );
3368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003370
3371 return( 0 );
3372}
3373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003375{
3376 int ret;
3377 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003378 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003379 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003380 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003383
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003384 if( len_pre == 0 )
3385 return( 0 );
3386
Paul Bakker2770fbd2012-07-03 13:30:23 +00003387 memcpy( msg_pre, ssl->in_msg, len_pre );
3388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003390 ssl->in_msglen ) );
3391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003392 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003393 ssl->in_msg, ssl->in_msglen );
3394
Paul Bakker48916f92012-09-16 19:57:18 +00003395 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3396 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3397 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003398 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003399 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003400
Paul Bakker48916f92012-09-16 19:57:18 +00003401 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003402 if( ret != Z_OK )
3403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3405 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003406 }
3407
Angus Grattond8213d02016-05-25 20:56:48 +10003408 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003409 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003412 ssl->in_msglen ) );
3413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003415 ssl->in_msg, ssl->in_msglen );
3416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003418
3419 return( 0 );
3420}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3424static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426#if defined(MBEDTLS_SSL_PROTO_DTLS)
3427static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003428{
3429 /* If renegotiation is not enforced, retransmit until we would reach max
3430 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003431 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003432 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003433 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003434 unsigned char doublings = 1;
3435
3436 while( ratio != 0 )
3437 {
3438 ++doublings;
3439 ratio >>= 1;
3440 }
3441
3442 if( ++ssl->renego_records_seen > doublings )
3443 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003445 return( 0 );
3446 }
3447 }
3448
3449 return( ssl_write_hello_request( ssl ) );
3450}
3451#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003453
Paul Bakker5121ce52009-01-03 21:22:43 +00003454/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003455 * Fill the input message buffer by appending data to it.
3456 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003457 *
3458 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3459 * available (from this read and/or a previous one). Otherwise, an error code
3460 * is returned (possibly EOF or WANT_READ).
3461 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003462 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3463 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3464 * since we always read a whole datagram at once.
3465 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003466 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003467 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003470{
Paul Bakker23986e52011-04-24 08:57:21 +00003471 int ret;
3472 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003475
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003476 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003479 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003481 }
3482
Angus Grattond8213d02016-05-25 20:56:48 +10003483 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3486 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003487 }
3488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003490 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003491 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003492 uint32_t timeout;
3493
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003494 /* Just to be sure */
3495 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3496 {
3497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3498 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3500 }
3501
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003502 /*
3503 * The point is, we need to always read a full datagram at once, so we
3504 * sometimes read more then requested, and handle the additional data.
3505 * It could be the rest of the current record (while fetching the
3506 * header) and/or some other records in the same datagram.
3507 */
3508
3509 /*
3510 * Move to the next record in the already read datagram if applicable
3511 */
3512 if( ssl->next_record_offset != 0 )
3513 {
3514 if( ssl->in_left < ssl->next_record_offset )
3515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3517 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003518 }
3519
3520 ssl->in_left -= ssl->next_record_offset;
3521
3522 if( ssl->in_left != 0 )
3523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003525 ssl->next_record_offset ) );
3526 memmove( ssl->in_hdr,
3527 ssl->in_hdr + ssl->next_record_offset,
3528 ssl->in_left );
3529 }
3530
3531 ssl->next_record_offset = 0;
3532 }
3533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003535 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003536
3537 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003538 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003539 */
3540 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003543 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003544 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003545
3546 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003547 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003548 * are not at the beginning of a new record, the caller did something
3549 * wrong.
3550 */
3551 if( ssl->in_left != 0 )
3552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3554 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003555 }
3556
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003557 /*
3558 * Don't even try to read if time's out already.
3559 * This avoids by-passing the timer when repeatedly receiving messages
3560 * that will end up being dropped.
3561 */
3562 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003563 {
3564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003565 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003566 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003567 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003568 {
Angus Grattond8213d02016-05-25 20:56:48 +10003569 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003572 timeout = ssl->handshake->retransmit_timeout;
3573 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003574 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003577
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003578 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003579 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3580 timeout );
3581 else
3582 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003585
3586 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003588 }
3589
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003590 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003593 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003596 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003597 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003600 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003601 }
3602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003606 return( ret );
3607 }
3608
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003609 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003610 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003612 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003614 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003615 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003618 return( ret );
3619 }
3620
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003621 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003622 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003624 }
3625
Paul Bakker5121ce52009-01-03 21:22:43 +00003626 if( ret < 0 )
3627 return( ret );
3628
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003629 ssl->in_left = ret;
3630 }
3631 else
3632#endif
3633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003635 ssl->in_left, nb_want ) );
3636
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003637 while( ssl->in_left < nb_want )
3638 {
3639 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003640
3641 if( ssl_check_timer( ssl ) != 0 )
3642 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3643 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003644 {
3645 if( ssl->f_recv_timeout != NULL )
3646 {
3647 ret = ssl->f_recv_timeout( ssl->p_bio,
3648 ssl->in_hdr + ssl->in_left, len,
3649 ssl->conf->read_timeout );
3650 }
3651 else
3652 {
3653 ret = ssl->f_recv( ssl->p_bio,
3654 ssl->in_hdr + ssl->in_left, len );
3655 }
3656 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003659 ssl->in_left, nb_want ) );
3660 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003661
3662 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003664
3665 if( ret < 0 )
3666 return( ret );
3667
mohammad160352aecb92018-03-28 23:41:40 -07003668 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003669 {
Darryl Green11999bb2018-03-13 15:22:58 +00003670 MBEDTLS_SSL_DEBUG_MSG( 1,
3671 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003672 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003673 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3674 }
3675
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003676 ssl->in_left += ret;
3677 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003678 }
3679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003681
3682 return( 0 );
3683}
3684
3685/*
3686 * Flush any data not yet written
3687 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003689{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003690 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003691 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003694
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003695 if( ssl->f_send == NULL )
3696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003698 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003699 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003700 }
3701
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003702 /* Avoid incrementing counter if data is flushed */
3703 if( ssl->out_left == 0 )
3704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003706 return( 0 );
3707 }
3708
Paul Bakker5121ce52009-01-03 21:22:43 +00003709 while( ssl->out_left > 0 )
3710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003712 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003713
Hanno Becker2b1e3542018-08-06 11:19:13 +01003714 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003715 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003718
3719 if( ret <= 0 )
3720 return( ret );
3721
mohammad160352aecb92018-03-28 23:41:40 -07003722 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003723 {
Darryl Green11999bb2018-03-13 15:22:58 +00003724 MBEDTLS_SSL_DEBUG_MSG( 1,
3725 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003726 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003727 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3728 }
3729
Paul Bakker5121ce52009-01-03 21:22:43 +00003730 ssl->out_left -= ret;
3731 }
3732
Hanno Becker2b1e3542018-08-06 11:19:13 +01003733#if defined(MBEDTLS_SSL_PROTO_DTLS)
3734 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003735 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003736 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003737 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003738 else
3739#endif
3740 {
3741 ssl->out_hdr = ssl->out_buf + 8;
3742 }
3743 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003746
3747 return( 0 );
3748}
3749
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003750/*
3751 * Functions to handle the DTLS retransmission state machine
3752 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003753#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003754/*
3755 * Append current handshake message to current outgoing flight
3756 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003757static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003758{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003759 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3761 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3762 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003763
3764 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003765 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003766 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003768 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003769 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003770 }
3771
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003772 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003773 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003775 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003776 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003777 }
3778
3779 /* Copy current handshake message with headers */
3780 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3781 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003782 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003783 msg->next = NULL;
3784
3785 /* Append to the current flight */
3786 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003787 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003788 else
3789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003791 while( cur->next != NULL )
3792 cur = cur->next;
3793 cur->next = msg;
3794 }
3795
Hanno Becker3b235902018-08-06 09:54:53 +01003796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003797 return( 0 );
3798}
3799
3800/*
3801 * Free the current flight of handshake messages
3802 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003803static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003804{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003805 mbedtls_ssl_flight_item *cur = flight;
3806 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003807
3808 while( cur != NULL )
3809 {
3810 next = cur->next;
3811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003812 mbedtls_free( cur->p );
3813 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003814
3815 cur = next;
3816 }
3817}
3818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003819#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3820static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003821#endif
3822
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003823/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003824 * Swap transform_out and out_ctr with the alternative ones
3825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003826static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003827{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003829 unsigned char tmp_out_ctr[8];
3830
3831 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003833 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003834 return;
3835 }
3836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003838
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003839 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003840 tmp_transform = ssl->transform_out;
3841 ssl->transform_out = ssl->handshake->alt_transform_out;
3842 ssl->handshake->alt_transform_out = tmp_transform;
3843
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003844 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003845 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3846 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003847 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003848
3849 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003850 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3853 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3858 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003859 }
3860 }
3861#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003862}
3863
3864/*
3865 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003866 */
3867int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3868{
3869 int ret = 0;
3870
3871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3872
3873 ret = mbedtls_ssl_flight_transmit( ssl );
3874
3875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3876
3877 return( ret );
3878}
3879
3880/*
3881 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003882 *
3883 * Need to remember the current message in case flush_output returns
3884 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003885 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003886 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003887int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003888{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003889 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003892 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003893 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003895
3896 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003897 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003898 ssl_swap_epochs( ssl );
3899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003900 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003901 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003902
3903 while( ssl->handshake->cur_msg != NULL )
3904 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003905 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003906 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003907
Hanno Beckere1dcb032018-08-17 16:47:58 +01003908 int const is_finished =
3909 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3910 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3911
Hanno Becker04da1892018-08-14 13:22:10 +01003912 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3913 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3914
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003915 /* Swap epochs before sending Finished: we can't do it after
3916 * sending ChangeCipherSpec, in case write returns WANT_READ.
3917 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003918 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003919 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003921 ssl_swap_epochs( ssl );
3922 }
3923
Hanno Becker67bc7c32018-08-06 11:33:50 +01003924 ret = ssl_get_remaining_payload_in_datagram( ssl );
3925 if( ret < 0 )
3926 return( ret );
3927 max_frag_len = (size_t) ret;
3928
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003929 /* CCS is copied as is, while HS messages may need fragmentation */
3930 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3931 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003932 if( max_frag_len == 0 )
3933 {
3934 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3935 return( ret );
3936
3937 continue;
3938 }
3939
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003940 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003941 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003942 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003943
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003944 /* Update position inside current message */
3945 ssl->handshake->cur_msg_p += cur->len;
3946 }
3947 else
3948 {
3949 const unsigned char * const p = ssl->handshake->cur_msg_p;
3950 const size_t hs_len = cur->len - 12;
3951 const size_t frag_off = p - ( cur->p + 12 );
3952 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003953 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003954
Hanno Beckere1dcb032018-08-17 16:47:58 +01003955 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003956 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003957 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003958 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003959
Hanno Becker67bc7c32018-08-06 11:33:50 +01003960 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3961 return( ret );
3962
3963 continue;
3964 }
3965 max_hs_frag_len = max_frag_len - 12;
3966
3967 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3968 max_hs_frag_len : rem_len;
3969
3970 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003971 {
3972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003973 (unsigned) cur_hs_frag_len,
3974 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003975 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003976
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003977 /* Messages are stored with handshake headers as if not fragmented,
3978 * copy beginning of headers then fill fragmentation fields.
3979 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3980 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003981
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003982 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3983 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3984 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3985
Hanno Becker67bc7c32018-08-06 11:33:50 +01003986 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3987 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3988 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003989
3990 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3991
Hanno Becker3f7b9732018-08-28 09:53:25 +01003992 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003993 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3994 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003995 ssl->out_msgtype = cur->type;
3996
3997 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003998 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003999 }
4000
4001 /* If done with the current message move to the next one if any */
4002 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4003 {
4004 if( cur->next != NULL )
4005 {
4006 ssl->handshake->cur_msg = cur->next;
4007 ssl->handshake->cur_msg_p = cur->next->p + 12;
4008 }
4009 else
4010 {
4011 ssl->handshake->cur_msg = NULL;
4012 ssl->handshake->cur_msg_p = NULL;
4013 }
4014 }
4015
4016 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004017 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004020 return( ret );
4021 }
4022 }
4023
Hanno Becker67bc7c32018-08-06 11:33:50 +01004024 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4025 return( ret );
4026
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004027 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004028 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4029 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004030 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004032 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004033 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4034 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004035
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004037
4038 return( 0 );
4039}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004040
4041/*
4042 * To be called when the last message of an incoming flight is received.
4043 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004044void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004045{
4046 /* We won't need to resend that one any more */
4047 ssl_flight_free( ssl->handshake->flight );
4048 ssl->handshake->flight = NULL;
4049 ssl->handshake->cur_msg = NULL;
4050
4051 /* The next incoming flight will start with this msg_seq */
4052 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4053
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004054 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004055 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004056
Hanno Becker0271f962018-08-16 13:23:47 +01004057 /* Clear future message buffering structure. */
4058 ssl_buffering_free( ssl );
4059
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004060 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004061 ssl_set_timer( ssl, 0 );
4062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004063 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4064 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004067 }
4068 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004069 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004070}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004071
4072/*
4073 * To be called when the last message of an outgoing flight is send.
4074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004075void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004076{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004077 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004078 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4081 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004083 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004084 }
4085 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004086 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004087}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004088#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004089
Paul Bakker5121ce52009-01-03 21:22:43 +00004090/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004091 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004092 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004093
4094/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004095 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004096 *
4097 * - fill in handshake headers
4098 * - update handshake checksum
4099 * - DTLS: save message for resending
4100 * - then pass to the record layer
4101 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004102 * DTLS: except for HelloRequest, messages are only queued, and will only be
4103 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004104 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004105 * Inputs:
4106 * - ssl->out_msglen: 4 + actual handshake message len
4107 * (4 is the size of handshake headers for TLS)
4108 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4109 * - ssl->out_msg + 4: the handshake message body
4110 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004111 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004112 * - ssl->out_msglen: the length of the record contents
4113 * (including handshake headers but excluding record headers)
4114 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004115 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004116int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004117{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004118 int ret;
4119 const size_t hs_len = ssl->out_msglen - 4;
4120 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004121
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4123
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004124 /*
4125 * Sanity checks
4126 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004127 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004128 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4129 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004130 /* In SSLv3, the client might send a NoCertificate alert. */
4131#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4132 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4133 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4134 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4135#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4136 {
4137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4138 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4139 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004140 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004141
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004142 /* Whenever we send anything different from a
4143 * HelloRequest we should be in a handshake - double check. */
4144 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4145 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004146 ssl->handshake == NULL )
4147 {
4148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4149 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4150 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004152#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004153 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004154 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004155 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004156 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4158 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004159 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004160#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004161
Hanno Beckerb50a2532018-08-06 11:52:54 +01004162 /* Double-check that we did not exceed the bounds
4163 * of the outgoing record buffer.
4164 * This should never fail as the various message
4165 * writing functions must obey the bounds of the
4166 * outgoing record buffer, but better be safe.
4167 *
4168 * Note: We deliberately do not check for the MTU or MFL here.
4169 */
4170 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4171 {
4172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4173 "size %u, maximum %u",
4174 (unsigned) ssl->out_msglen,
4175 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4176 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4177 }
4178
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004179 /*
4180 * Fill handshake headers
4181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004182 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004183 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004184 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4185 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4186 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004187
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004188 /*
4189 * DTLS has additional fields in the Handshake layer,
4190 * between the length field and the actual payload:
4191 * uint16 message_seq;
4192 * uint24 fragment_offset;
4193 * uint24 fragment_length;
4194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004195#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004196 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004197 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004198 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004199 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004200 {
4201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4202 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004203 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004204 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004205 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4206 }
4207
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004208 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004209 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004210
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004211 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004212 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004213 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004214 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4215 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4216 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004217 }
4218 else
4219 {
4220 ssl->out_msg[4] = 0;
4221 ssl->out_msg[5] = 0;
4222 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004223
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004224 /* Handshake hashes are computed without fragmentation,
4225 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004226 memset( ssl->out_msg + 6, 0x00, 3 );
4227 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004228 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004229#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004230
Hanno Becker0207e532018-08-28 10:28:28 +01004231 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004232 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4233 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004234 }
4235
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004236 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004238 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004239 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4240 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004241 {
4242 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004244 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004245 return( ret );
4246 }
4247 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004248 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004249#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004250 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004251 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004252 {
4253 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4254 return( ret );
4255 }
4256 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004257
4258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4259
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004260 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004261}
4262
4263/*
4264 * Record layer functions
4265 */
4266
4267/*
4268 * Write current record.
4269 *
4270 * Uses:
4271 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4272 * - ssl->out_msglen: length of the record content (excl headers)
4273 * - ssl->out_msg: record content
4274 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004275int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004276{
4277 int ret, done = 0;
4278 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004279 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004280
4281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004283#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004284 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004285 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004286 {
4287 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004289 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004290 return( ret );
4291 }
4292
4293 len = ssl->out_msglen;
4294 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004295#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4298 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004302 ret = mbedtls_ssl_hw_record_write( ssl );
4303 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4306 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004307 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004308
4309 if( ret == 0 )
4310 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004311 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004312#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004313 if( !done )
4314 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004315 unsigned i;
4316 size_t protected_record_size;
4317
Hanno Becker6430faf2019-05-08 11:57:13 +01004318 /* Skip writing the record content type to after the encryption,
4319 * as it may change when using the CID extension. */
4320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004321 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004322 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004323
Hanno Becker19859472018-08-06 09:40:20 +01004324 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004325 ssl->out_len[0] = (unsigned char)( len >> 8 );
4326 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004327
Paul Bakker48916f92012-09-16 19:57:18 +00004328 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004329 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004330 mbedtls_record rec;
4331
4332 rec.buf = ssl->out_iv;
4333 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4334 ( ssl->out_iv - ssl->out_buf );
4335 rec.data_len = ssl->out_msglen;
4336 rec.data_offset = ssl->out_msg - rec.buf;
4337
4338 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4339 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4340 ssl->conf->transport, rec.ver );
4341 rec.type = ssl->out_msgtype;
4342
Hanno Beckera0e20d02019-05-15 14:03:01 +01004343#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004344 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004345 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004346#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004347
Hanno Beckera18d1322018-01-03 14:27:32 +00004348 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004349 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004352 return( ret );
4353 }
4354
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004355 if( rec.data_offset != 0 )
4356 {
4357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4358 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4359 }
4360
Hanno Becker6430faf2019-05-08 11:57:13 +01004361 /* Update the record content type and CID. */
4362 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004363#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004364 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004365#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004366 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004367 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4368 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004369 }
4370
Hanno Becker5903de42019-05-03 14:46:38 +01004371 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004372
4373#if defined(MBEDTLS_SSL_PROTO_DTLS)
4374 /* In case of DTLS, double-check that we don't exceed
4375 * the remaining space in the datagram. */
4376 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4377 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004378 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004379 if( ret < 0 )
4380 return( ret );
4381
4382 if( protected_record_size > (size_t) ret )
4383 {
4384 /* Should never happen */
4385 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4386 }
4387 }
4388#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004389
Hanno Becker6430faf2019-05-08 11:57:13 +01004390 /* Now write the potentially updated record content type. */
4391 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004393 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004394 "version = [%d:%d], msglen = %d",
4395 ssl->out_hdr[0], ssl->out_hdr[1],
4396 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004398 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004399 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004400
4401 ssl->out_left += protected_record_size;
4402 ssl->out_hdr += protected_record_size;
4403 ssl_update_out_pointers( ssl, ssl->transform_out );
4404
Hanno Becker04484622018-08-06 09:49:38 +01004405 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4406 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4407 break;
4408
4409 /* The loop goes to its end iff the counter is wrapping */
4410 if( i == ssl_ep_len( ssl ) )
4411 {
4412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4413 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4414 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004415 }
4416
Hanno Becker67bc7c32018-08-06 11:33:50 +01004417#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004418 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4419 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004420 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004421 size_t remaining;
4422 ret = ssl_get_remaining_payload_in_datagram( ssl );
4423 if( ret < 0 )
4424 {
4425 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4426 ret );
4427 return( ret );
4428 }
4429
4430 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004431 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004432 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004433 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004434 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004435 else
4436 {
Hanno Becker513815a2018-08-20 11:56:09 +01004437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004438 }
4439 }
4440#endif /* MBEDTLS_SSL_PROTO_DTLS */
4441
4442 if( ( flush == SSL_FORCE_FLUSH ) &&
4443 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004446 return( ret );
4447 }
4448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004450
4451 return( 0 );
4452}
4453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004455
4456static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4457{
4458 if( ssl->in_msglen < ssl->in_hslen ||
4459 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4460 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4461 {
4462 return( 1 );
4463 }
4464 return( 0 );
4465}
Hanno Becker44650b72018-08-16 12:51:11 +01004466
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004467static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004468{
4469 return( ( ssl->in_msg[9] << 16 ) |
4470 ( ssl->in_msg[10] << 8 ) |
4471 ssl->in_msg[11] );
4472}
4473
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004474static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004475{
4476 return( ( ssl->in_msg[6] << 16 ) |
4477 ( ssl->in_msg[7] << 8 ) |
4478 ssl->in_msg[8] );
4479}
4480
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004481static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004482{
4483 uint32_t msg_len, frag_off, frag_len;
4484
4485 msg_len = ssl_get_hs_total_len( ssl );
4486 frag_off = ssl_get_hs_frag_off( ssl );
4487 frag_len = ssl_get_hs_frag_len( ssl );
4488
4489 if( frag_off > msg_len )
4490 return( -1 );
4491
4492 if( frag_len > msg_len - frag_off )
4493 return( -1 );
4494
4495 if( frag_len + 12 > ssl->in_msglen )
4496 return( -1 );
4497
4498 return( 0 );
4499}
4500
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004501/*
4502 * Mark bits in bitmask (used for DTLS HS reassembly)
4503 */
4504static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4505{
4506 unsigned int start_bits, end_bits;
4507
4508 start_bits = 8 - ( offset % 8 );
4509 if( start_bits != 8 )
4510 {
4511 size_t first_byte_idx = offset / 8;
4512
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004513 /* Special case */
4514 if( len <= start_bits )
4515 {
4516 for( ; len != 0; len-- )
4517 mask[first_byte_idx] |= 1 << ( start_bits - len );
4518
4519 /* Avoid potential issues with offset or len becoming invalid */
4520 return;
4521 }
4522
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004523 offset += start_bits; /* Now offset % 8 == 0 */
4524 len -= start_bits;
4525
4526 for( ; start_bits != 0; start_bits-- )
4527 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4528 }
4529
4530 end_bits = len % 8;
4531 if( end_bits != 0 )
4532 {
4533 size_t last_byte_idx = ( offset + len ) / 8;
4534
4535 len -= end_bits; /* Now len % 8 == 0 */
4536
4537 for( ; end_bits != 0; end_bits-- )
4538 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4539 }
4540
4541 memset( mask + offset / 8, 0xFF, len / 8 );
4542}
4543
4544/*
4545 * Check that bitmask is full
4546 */
4547static int ssl_bitmask_check( unsigned char *mask, size_t len )
4548{
4549 size_t i;
4550
4551 for( i = 0; i < len / 8; i++ )
4552 if( mask[i] != 0xFF )
4553 return( -1 );
4554
4555 for( i = 0; i < len % 8; i++ )
4556 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4557 return( -1 );
4558
4559 return( 0 );
4560}
4561
Hanno Becker56e205e2018-08-16 09:06:12 +01004562/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004563static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004564 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004565{
Hanno Becker56e205e2018-08-16 09:06:12 +01004566 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004567
Hanno Becker56e205e2018-08-16 09:06:12 +01004568 alloc_len = 12; /* Handshake header */
4569 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004570
Hanno Beckerd07df862018-08-16 09:14:58 +01004571 if( add_bitmap )
4572 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004573
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004574 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004575}
Hanno Becker56e205e2018-08-16 09:06:12 +01004576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004577#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004578
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004579static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004580{
4581 return( ( ssl->in_msg[1] << 16 ) |
4582 ( ssl->in_msg[2] << 8 ) |
4583 ssl->in_msg[3] );
4584}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004585
Simon Butcher99000142016-10-13 17:21:01 +01004586int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004587{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004588 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004591 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004592 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004593 }
4594
Hanno Becker12555c62018-08-16 12:47:53 +01004595 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004597 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004598 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004599 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004601#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004602 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004603 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004604 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004605 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004606
Hanno Becker44650b72018-08-16 12:51:11 +01004607 if( ssl_check_hs_header( ssl ) != 0 )
4608 {
4609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4610 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4611 }
4612
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004613 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004614 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4615 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4616 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4617 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004618 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004619 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4620 {
4621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4622 recv_msg_seq,
4623 ssl->handshake->in_msg_seq ) );
4624 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4625 }
4626
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004627 /* Retransmit only on last message from previous flight, to avoid
4628 * too many retransmissions.
4629 * Besides, No sane server ever retransmits HelloVerifyRequest */
4630 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004631 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004634 "message_seq = %d, start_of_flight = %d",
4635 recv_msg_seq,
4636 ssl->handshake->in_flight_start_seq ) );
4637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004638 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004640 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004641 return( ret );
4642 }
4643 }
4644 else
4645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004647 "message_seq = %d, expected = %d",
4648 recv_msg_seq,
4649 ssl->handshake->in_msg_seq ) );
4650 }
4651
Hanno Becker90333da2017-10-10 11:27:13 +01004652 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004653 }
4654 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004655
Hanno Becker6d97ef52018-08-16 13:09:04 +01004656 /* Message reassembly is handled alongside buffering of future
4657 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004658 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004659 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004660 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004663 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004664 }
4665 }
4666 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004667#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004668 /* With TLS we don't handle fragmentation (for now) */
4669 if( ssl->in_msglen < ssl->in_hslen )
4670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4672 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004673 }
4674
Simon Butcher99000142016-10-13 17:21:01 +01004675 return( 0 );
4676}
4677
4678void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4679{
Hanno Becker0271f962018-08-16 13:23:47 +01004680 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004681
Hanno Becker0271f962018-08-16 13:23:47 +01004682 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004683 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004684 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004685 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004686
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004687 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004688#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004689 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004690 ssl->handshake != NULL )
4691 {
Hanno Becker0271f962018-08-16 13:23:47 +01004692 unsigned offset;
4693 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004694
Hanno Becker0271f962018-08-16 13:23:47 +01004695 /* Increment handshake sequence number */
4696 hs->in_msg_seq++;
4697
4698 /*
4699 * Clear up handshake buffering and reassembly structure.
4700 */
4701
4702 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004703 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004704
4705 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004706 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4707 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004708 offset++, hs_buf++ )
4709 {
4710 *hs_buf = *(hs_buf + 1);
4711 }
4712
4713 /* Create a fresh last entry */
4714 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004715 }
4716#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004717}
4718
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004719/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004720 * DTLS anti-replay: RFC 6347 4.1.2.6
4721 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004722 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4723 * Bit n is set iff record number in_window_top - n has been seen.
4724 *
4725 * Usually, in_window_top is the last record number seen and the lsb of
4726 * in_window is set. The only exception is the initial state (record number 0
4727 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004729#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4730static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004731{
4732 ssl->in_window_top = 0;
4733 ssl->in_window = 0;
4734}
4735
4736static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4737{
4738 return( ( (uint64_t) buf[0] << 40 ) |
4739 ( (uint64_t) buf[1] << 32 ) |
4740 ( (uint64_t) buf[2] << 24 ) |
4741 ( (uint64_t) buf[3] << 16 ) |
4742 ( (uint64_t) buf[4] << 8 ) |
4743 ( (uint64_t) buf[5] ) );
4744}
4745
4746/*
4747 * Return 0 if sequence number is acceptable, -1 otherwise
4748 */
Hanno Becker0183d692019-07-12 08:50:37 +01004749int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004750{
4751 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4752 uint64_t bit;
4753
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004754 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004755 return( 0 );
4756
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004757 if( rec_seqnum > ssl->in_window_top )
4758 return( 0 );
4759
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004760 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004761
4762 if( bit >= 64 )
4763 return( -1 );
4764
4765 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4766 return( -1 );
4767
4768 return( 0 );
4769}
4770
4771/*
4772 * Update replay window on new validated record
4773 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004774void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004775{
4776 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4777
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004778 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004779 return;
4780
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004781 if( rec_seqnum > ssl->in_window_top )
4782 {
4783 /* Update window_top and the contents of the window */
4784 uint64_t shift = rec_seqnum - ssl->in_window_top;
4785
4786 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004787 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004788 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004789 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004790 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004791 ssl->in_window |= 1;
4792 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004793
4794 ssl->in_window_top = rec_seqnum;
4795 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004796 else
4797 {
4798 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004799 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004800
4801 if( bit < 64 ) /* Always true, but be extra sure */
4802 ssl->in_window |= (uint64_t) 1 << bit;
4803 }
4804}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004806
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004807#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004808/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004809static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4810
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004811/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004812 * Without any SSL context, check if a datagram looks like a ClientHello with
4813 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004814 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004815 *
4816 * - if cookie is valid, return 0
4817 * - if ClientHello looks superficially valid but cookie is not,
4818 * fill obuf and set olen, then
4819 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4820 * - otherwise return a specific error code
4821 */
4822static int ssl_check_dtls_clihlo_cookie(
4823 mbedtls_ssl_cookie_write_t *f_cookie_write,
4824 mbedtls_ssl_cookie_check_t *f_cookie_check,
4825 void *p_cookie,
4826 const unsigned char *cli_id, size_t cli_id_len,
4827 const unsigned char *in, size_t in_len,
4828 unsigned char *obuf, size_t buf_len, size_t *olen )
4829{
4830 size_t sid_len, cookie_len;
4831 unsigned char *p;
4832
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004833 /*
4834 * Structure of ClientHello with record and handshake headers,
4835 * and expected values. We don't need to check a lot, more checks will be
4836 * done when actually parsing the ClientHello - skipping those checks
4837 * avoids code duplication and does not make cookie forging any easier.
4838 *
4839 * 0-0 ContentType type; copied, must be handshake
4840 * 1-2 ProtocolVersion version; copied
4841 * 3-4 uint16 epoch; copied, must be 0
4842 * 5-10 uint48 sequence_number; copied
4843 * 11-12 uint16 length; (ignored)
4844 *
4845 * 13-13 HandshakeType msg_type; (ignored)
4846 * 14-16 uint24 length; (ignored)
4847 * 17-18 uint16 message_seq; copied
4848 * 19-21 uint24 fragment_offset; copied, must be 0
4849 * 22-24 uint24 fragment_length; (ignored)
4850 *
4851 * 25-26 ProtocolVersion client_version; (ignored)
4852 * 27-58 Random random; (ignored)
4853 * 59-xx SessionID session_id; 1 byte len + sid_len content
4854 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4855 * ...
4856 *
4857 * Minimum length is 61 bytes.
4858 */
4859 if( in_len < 61 ||
4860 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4861 in[3] != 0 || in[4] != 0 ||
4862 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4863 {
4864 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4865 }
4866
4867 sid_len = in[59];
4868 if( sid_len > in_len - 61 )
4869 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4870
4871 cookie_len = in[60 + sid_len];
4872 if( cookie_len > in_len - 60 )
4873 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4874
4875 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4876 cli_id, cli_id_len ) == 0 )
4877 {
4878 /* Valid cookie */
4879 return( 0 );
4880 }
4881
4882 /*
4883 * If we get here, we've got an invalid cookie, let's prepare HVR.
4884 *
4885 * 0-0 ContentType type; copied
4886 * 1-2 ProtocolVersion version; copied
4887 * 3-4 uint16 epoch; copied
4888 * 5-10 uint48 sequence_number; copied
4889 * 11-12 uint16 length; olen - 13
4890 *
4891 * 13-13 HandshakeType msg_type; hello_verify_request
4892 * 14-16 uint24 length; olen - 25
4893 * 17-18 uint16 message_seq; copied
4894 * 19-21 uint24 fragment_offset; copied
4895 * 22-24 uint24 fragment_length; olen - 25
4896 *
4897 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4898 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4899 *
4900 * Minimum length is 28.
4901 */
4902 if( buf_len < 28 )
4903 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4904
4905 /* Copy most fields and adapt others */
4906 memcpy( obuf, in, 25 );
4907 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4908 obuf[25] = 0xfe;
4909 obuf[26] = 0xff;
4910
4911 /* Generate and write actual cookie */
4912 p = obuf + 28;
4913 if( f_cookie_write( p_cookie,
4914 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4915 {
4916 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4917 }
4918
4919 *olen = p - obuf;
4920
4921 /* Go back and fill length fields */
4922 obuf[27] = (unsigned char)( *olen - 28 );
4923
4924 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4925 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4926 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4927
4928 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4929 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4930
4931 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4932}
4933
4934/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004935 * Handle possible client reconnect with the same UDP quadruplet
4936 * (RFC 6347 Section 4.2.8).
4937 *
4938 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4939 * that looks like a ClientHello.
4940 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004941 * - if the input looks like a ClientHello without cookies,
4942 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004943 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004944 * - if the input looks like a ClientHello with a valid cookie,
4945 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004946 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004947 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004948 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004949 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004950 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4951 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004952 */
4953static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4954{
4955 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004956 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004957
Hanno Becker2fddd372019-07-10 14:37:41 +01004958 if( ssl->conf->f_cookie_write == NULL ||
4959 ssl->conf->f_cookie_check == NULL )
4960 {
4961 /* If we can't use cookies to verify reachability of the peer,
4962 * drop the record. */
4963 return( 0 );
4964 }
4965
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004966 ret = ssl_check_dtls_clihlo_cookie(
4967 ssl->conf->f_cookie_write,
4968 ssl->conf->f_cookie_check,
4969 ssl->conf->p_cookie,
4970 ssl->cli_id, ssl->cli_id_len,
4971 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004972 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004973
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004974 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4975
4976 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004977 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004978 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004979 * If the error is permanent we'll catch it later,
4980 * if it's not, then hopefully it'll work next time. */
4981 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01004982 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004983 }
4984
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004985 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004986 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004987 /* Got a valid cookie, partially reset context */
4988 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4989 {
4990 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4991 return( ret );
4992 }
4993
4994 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004995 }
4996
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004997 return( ret );
4998}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004999#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005000
Hanno Beckerf661c9c2019-05-03 13:25:54 +01005001static int ssl_check_record_type( uint8_t record_type )
5002{
5003 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5004 record_type != MBEDTLS_SSL_MSG_ALERT &&
5005 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5006 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5007 {
5008 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5009 }
5010
5011 return( 0 );
5012}
5013
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005014/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005015 * ContentType type;
5016 * ProtocolVersion version;
5017 * uint16 epoch; // DTLS only
5018 * uint48 sequence_number; // DTLS only
5019 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005020 *
5021 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005022 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005023 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5024 *
5025 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005026 * 1. proceed with the record if this function returns 0
5027 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5028 * 3. return CLIENT_RECONNECT if this function return that value
5029 * 4. drop the whole datagram if this function returns anything else.
5030 * Point 2 is needed when the peer is resending, and we have already received
5031 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005032 */
Hanno Becker331de3d2019-07-12 11:10:16 +01005033static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01005034 unsigned char *buf,
5035 size_t len,
5036 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005037{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005038 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005039
Hanno Beckere5e7e782019-07-11 12:29:35 +01005040 size_t const rec_hdr_type_offset = 0;
5041 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005042
Hanno Beckere5e7e782019-07-11 12:29:35 +01005043 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5044 rec_hdr_type_len;
5045 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005046
Hanno Beckere5e7e782019-07-11 12:29:35 +01005047 size_t const rec_hdr_ctr_len = 8;
5048#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01005049 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005050 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5051 rec_hdr_version_len;
5052
Hanno Beckera0e20d02019-05-15 14:03:01 +01005053#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01005054 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5055 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01005056 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005057#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5058#endif /* MBEDTLS_SSL_PROTO_DTLS */
5059
5060 size_t rec_hdr_len_offset; /* To be determined */
5061 size_t const rec_hdr_len_len = 2;
5062
5063 /*
5064 * Check minimum lengths for record header.
5065 */
5066
5067#if defined(MBEDTLS_SSL_PROTO_DTLS)
5068 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5069 {
5070 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5071 }
5072 else
5073#endif /* MBEDTLS_SSL_PROTO_DTLS */
5074 {
5075 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5076 }
5077
5078 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5079 {
5080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5081 (unsigned) len,
5082 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5083 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5084 }
5085
5086 /*
5087 * Parse and validate record content type
5088 */
5089
5090 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005091
5092 /* Check record content type */
5093#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5094 rec->cid_len = 0;
5095
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005096 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005097 ssl->conf->cid_len != 0 &&
5098 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005099 {
5100 /* Shift pointers to account for record header including CID
5101 * struct {
5102 * ContentType special_type = tls12_cid;
5103 * ProtocolVersion version;
5104 * uint16 epoch;
5105 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005106 * opaque cid[cid_length]; // Additional field compared to
5107 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005108 * uint16 length;
5109 * opaque enc_content[DTLSCiphertext.length];
5110 * } DTLSCiphertext;
5111 */
5112
5113 /* So far, we only support static CID lengths
5114 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005115 rec_hdr_cid_len = ssl->conf->cid_len;
5116 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005117
Hanno Beckere5e7e782019-07-11 12:29:35 +01005118 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005119 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5121 (unsigned) len,
5122 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005123 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005124 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005125
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02005126 /* configured CID len is guaranteed at most 255, see
5127 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5128 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005129 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005130 }
5131 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005132#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005133 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005134 if( ssl_check_record_type( rec->type ) )
5135 {
Hanno Becker54229812019-07-12 14:40:00 +01005136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5137 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005138 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5139 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005140 }
5141
Hanno Beckere5e7e782019-07-11 12:29:35 +01005142 /*
5143 * Parse and validate record version
5144 */
5145
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005146 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5147 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005148 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5149 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005150 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005151
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005152 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5155 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005156 }
5157
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005158 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5161 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005162 }
5163
Hanno Beckere5e7e782019-07-11 12:29:35 +01005164 /*
5165 * Parse/Copy record sequence number.
5166 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005167
Hanno Beckere5e7e782019-07-11 12:29:35 +01005168#if defined(MBEDTLS_SSL_PROTO_DTLS)
5169 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005170 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005171 /* Copy explicit record sequence number from input buffer. */
5172 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5173 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005174 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005175 else
5176#endif /* MBEDTLS_SSL_PROTO_DTLS */
5177 {
5178 /* Copy implicit record sequence number from SSL context structure. */
5179 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5180 }
Paul Bakker40e46942009-01-03 21:51:57 +00005181
Hanno Beckere5e7e782019-07-11 12:29:35 +01005182 /*
5183 * Parse record length.
5184 */
5185
Hanno Beckere5e7e782019-07-11 12:29:35 +01005186 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005187 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5188 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005189 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00005190
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005191 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005192 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005193 rec->type,
5194 major_ver, minor_ver, rec->data_len ) );
5195
5196 rec->buf = buf;
5197 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005198
Hanno Beckerd417cc92019-07-26 08:20:27 +01005199 if( rec->data_len == 0 )
5200 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005201
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005202 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005203 * DTLS-related tests.
5204 * Check epoch before checking length constraint because
5205 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5206 * message gets duplicated before the corresponding Finished message,
5207 * the second ChangeCipherSpec should be discarded because it belongs
5208 * to an old epoch, but not because its length is shorter than
5209 * the minimum record length for packets using the new record transform.
5210 * Note that these two kinds of failures are handled differently,
5211 * as an unexpected record is silently skipped but an invalid
5212 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005213 */
5214#if defined(MBEDTLS_SSL_PROTO_DTLS)
5215 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5216 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005217 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005218
Hanno Becker955a5c92019-07-10 17:12:07 +01005219 /* Check that the datagram is large enough to contain a record
5220 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005221 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005222 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5224 (unsigned) len,
5225 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005226 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5227 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005228
Hanno Becker37cfe732019-07-10 17:20:01 +01005229 /* Records from other, non-matching epochs are silently discarded.
5230 * (The case of same-port Client reconnects must be considered in
5231 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005232 if( rec_epoch != ssl->in_epoch )
5233 {
5234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5235 "expected %d, received %d",
5236 ssl->in_epoch, rec_epoch ) );
5237
Hanno Becker552f7472019-07-19 10:59:12 +01005238 /* Records from the next epoch are considered for buffering
5239 * (concretely: early Finished messages). */
5240 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005241 {
Hanno Becker552f7472019-07-19 10:59:12 +01005242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5243 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005244 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005245
Hanno Becker2fddd372019-07-10 14:37:41 +01005246 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005247 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005248#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005249 /* For records from the correct epoch, check whether their
5250 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005251 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005252 {
5253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5254 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5255 }
5256#endif
5257 }
5258#endif /* MBEDTLS_SSL_PROTO_DTLS */
5259
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005260 return( 0 );
5261}
Paul Bakker5121ce52009-01-03 21:22:43 +00005262
Paul Bakker5121ce52009-01-03 21:22:43 +00005263
Hanno Becker2fddd372019-07-10 14:37:41 +01005264#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5265static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5266{
5267 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5268
5269 /*
5270 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5271 * access the first byte of record content (handshake type), as we
5272 * have an active transform (possibly iv_len != 0), so use the
5273 * fact that the record header len is 13 instead.
5274 */
5275 if( rec_epoch == 0 &&
5276 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5277 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5278 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5279 ssl->in_left > 13 &&
5280 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5281 {
5282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5283 "from the same port" ) );
5284 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005285 }
5286
5287 return( 0 );
5288}
Hanno Becker2fddd372019-07-10 14:37:41 +01005289#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005290
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005291/*
5292 * If applicable, decrypt (and decompress) record content
5293 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005294static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5295 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005296{
5297 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005300 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005302#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5303 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005307 ret = mbedtls_ssl_hw_record_read( ssl );
5308 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005310 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5311 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005312 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005313
5314 if( ret == 0 )
5315 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005316 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005317#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005318 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005319 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005320 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005321
Hanno Beckera18d1322018-01-03 14:27:32 +00005322 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005323 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005325 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005326
Hanno Beckera0e20d02019-05-15 14:03:01 +01005327#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005328 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5329 ssl->conf->ignore_unexpected_cid
5330 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5331 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005332 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005333 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005334 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005335#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005336
Paul Bakker5121ce52009-01-03 21:22:43 +00005337 return( ret );
5338 }
5339
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005340 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005341 {
5342 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005343 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005344 }
5345
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005346 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005347 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005348
Hanno Beckera0e20d02019-05-15 14:03:01 +01005349#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005350 /* We have already checked the record content type
5351 * in ssl_parse_record_header(), failing or silently
5352 * dropping the record in the case of an unknown type.
5353 *
5354 * Since with the use of CIDs, the record content type
5355 * might change during decryption, re-check the record
5356 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005357 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005358 {
5359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5360 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5361 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005362#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005363
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005364 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005365 {
5366#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5367 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005368 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005369 {
5370 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5372 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5373 }
5374#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5375
5376 ssl->nb_zero++;
5377
5378 /*
5379 * Three or more empty messages may be a DoS attack
5380 * (excessive CPU consumption).
5381 */
5382 if( ssl->nb_zero > 3 )
5383 {
5384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005385 "messages, possible DoS attack" ) );
5386 /* Treat the records as if they were not properly authenticated,
5387 * thereby failing the connection if we see more than allowed
5388 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005389 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5390 }
5391 }
5392 else
5393 ssl->nb_zero = 0;
5394
5395#if defined(MBEDTLS_SSL_PROTO_DTLS)
5396 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5397 {
5398 ; /* in_ctr read from peer, not maintained internally */
5399 }
5400 else
5401#endif
5402 {
5403 unsigned i;
5404 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5405 if( ++ssl->in_ctr[i - 1] != 0 )
5406 break;
5407
5408 /* The loop goes to its end iff the counter is wrapping */
5409 if( i == ssl_ep_len( ssl ) )
5410 {
5411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5412 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5413 }
5414 }
5415
Paul Bakker5121ce52009-01-03 21:22:43 +00005416 }
5417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005418#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005419 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005420 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005421 {
5422 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005424 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005425 return( ret );
5426 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005427 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005428#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005430#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005431 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005433 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005434 }
5435#endif
5436
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005437 /* Check actual (decrypted) record content length against
5438 * configured maximum. */
5439 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5440 {
5441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5442 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5443 }
5444
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005445 return( 0 );
5446}
5447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005448static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005449
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005450/*
5451 * Read a record.
5452 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005453 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5454 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5455 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005456 */
Hanno Becker1097b342018-08-15 14:09:41 +01005457
5458/* Helper functions for mbedtls_ssl_read_record(). */
5459static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005460static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5461static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005462
Hanno Becker327c93b2018-08-15 13:56:18 +01005463int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005464 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005465{
5466 int ret;
5467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005469
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005470 if( ssl->keep_current_message == 0 )
5471 {
5472 do {
Simon Butcher99000142016-10-13 17:21:01 +01005473
Hanno Becker26994592018-08-15 14:14:59 +01005474 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005475 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005476 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005477
Hanno Beckere74d5562018-08-15 14:26:08 +01005478 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005479 {
Hanno Becker40f50842018-08-15 14:48:01 +01005480#if defined(MBEDTLS_SSL_PROTO_DTLS)
5481 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005482
Hanno Becker40f50842018-08-15 14:48:01 +01005483 /* We only check for buffered messages if the
5484 * current datagram is fully consumed. */
5485 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005486 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005487 {
Hanno Becker40f50842018-08-15 14:48:01 +01005488 if( ssl_load_buffered_message( ssl ) == 0 )
5489 have_buffered = 1;
5490 }
5491
5492 if( have_buffered == 0 )
5493#endif /* MBEDTLS_SSL_PROTO_DTLS */
5494 {
5495 ret = ssl_get_next_record( ssl );
5496 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5497 continue;
5498
5499 if( ret != 0 )
5500 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005501 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005502 return( ret );
5503 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005504 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005505 }
5506
5507 ret = mbedtls_ssl_handle_message_type( ssl );
5508
Hanno Becker40f50842018-08-15 14:48:01 +01005509#if defined(MBEDTLS_SSL_PROTO_DTLS)
5510 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5511 {
5512 /* Buffer future message */
5513 ret = ssl_buffer_message( ssl );
5514 if( ret != 0 )
5515 return( ret );
5516
5517 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5518 }
5519#endif /* MBEDTLS_SSL_PROTO_DTLS */
5520
Hanno Becker90333da2017-10-10 11:27:13 +01005521 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5522 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005523
5524 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005525 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005526 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005527 return( ret );
5528 }
5529
Hanno Becker327c93b2018-08-15 13:56:18 +01005530 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005531 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005532 {
5533 mbedtls_ssl_update_handshake_status( ssl );
5534 }
Simon Butcher99000142016-10-13 17:21:01 +01005535 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005536 else
Simon Butcher99000142016-10-13 17:21:01 +01005537 {
Hanno Becker02f59072018-08-15 14:00:24 +01005538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005539 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005540 }
5541
5542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5543
5544 return( 0 );
5545}
5546
Hanno Becker40f50842018-08-15 14:48:01 +01005547#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005548static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005549{
Hanno Becker40f50842018-08-15 14:48:01 +01005550 if( ssl->in_left > ssl->next_record_offset )
5551 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005552
Hanno Becker40f50842018-08-15 14:48:01 +01005553 return( 0 );
5554}
5555
5556static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5557{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005558 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005559 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005560 int ret = 0;
5561
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005562 if( hs == NULL )
5563 return( -1 );
5564
Hanno Beckere00ae372018-08-20 09:39:42 +01005565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5566
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005567 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5568 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5569 {
5570 /* Check if we have seen a ChangeCipherSpec before.
5571 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005572 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005573 {
5574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5575 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005576 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005577 }
5578
Hanno Becker39b8bc92018-08-28 17:17:13 +01005579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005580 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5581 ssl->in_msglen = 1;
5582 ssl->in_msg[0] = 1;
5583
5584 /* As long as they are equal, the exact value doesn't matter. */
5585 ssl->in_left = 0;
5586 ssl->next_record_offset = 0;
5587
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005588 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005589 goto exit;
5590 }
Hanno Becker37f95322018-08-16 13:55:32 +01005591
Hanno Beckerb8f50142018-08-28 10:01:34 +01005592#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005593 /* Debug only */
5594 {
5595 unsigned offset;
5596 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5597 {
5598 hs_buf = &hs->buffering.hs[offset];
5599 if( hs_buf->is_valid == 1 )
5600 {
5601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5602 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005603 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005604 }
5605 }
5606 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005607#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005608
5609 /* Check if we have buffered and/or fully reassembled the
5610 * next handshake message. */
5611 hs_buf = &hs->buffering.hs[0];
5612 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5613 {
5614 /* Synthesize a record containing the buffered HS message. */
5615 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5616 ( hs_buf->data[2] << 8 ) |
5617 hs_buf->data[3];
5618
5619 /* Double-check that we haven't accidentally buffered
5620 * a message that doesn't fit into the input buffer. */
5621 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5622 {
5623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5624 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5625 }
5626
5627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5628 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5629 hs_buf->data, msg_len + 12 );
5630
5631 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5632 ssl->in_hslen = msg_len + 12;
5633 ssl->in_msglen = msg_len + 12;
5634 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5635
5636 ret = 0;
5637 goto exit;
5638 }
5639 else
5640 {
5641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5642 hs->in_msg_seq ) );
5643 }
5644
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005645 ret = -1;
5646
5647exit:
5648
5649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5650 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005651}
5652
Hanno Beckera02b0b42018-08-21 17:20:27 +01005653static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5654 size_t desired )
5655{
5656 int offset;
5657 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5659 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005660
Hanno Becker01315ea2018-08-21 17:22:17 +01005661 /* Get rid of future records epoch first, if such exist. */
5662 ssl_free_buffered_record( ssl );
5663
5664 /* Check if we have enough space available now. */
5665 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5666 hs->buffering.total_bytes_buffered ) )
5667 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005669 return( 0 );
5670 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005671
Hanno Becker4f432ad2018-08-28 10:02:32 +01005672 /* We don't have enough space to buffer the next expected handshake
5673 * message. Remove buffers used for future messages to gain space,
5674 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005675 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5676 offset >= 0; offset-- )
5677 {
5678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5679 offset ) );
5680
Hanno Beckerb309b922018-08-23 13:18:05 +01005681 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005682
5683 /* Check if we have enough space available now. */
5684 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5685 hs->buffering.total_bytes_buffered ) )
5686 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005688 return( 0 );
5689 }
5690 }
5691
5692 return( -1 );
5693}
5694
Hanno Becker40f50842018-08-15 14:48:01 +01005695static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5696{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005697 int ret = 0;
5698 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5699
5700 if( hs == NULL )
5701 return( 0 );
5702
5703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5704
5705 switch( ssl->in_msgtype )
5706 {
5707 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005709
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005710 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005711 break;
5712
5713 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005714 {
5715 unsigned recv_msg_seq_offset;
5716 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5717 mbedtls_ssl_hs_buffer *hs_buf;
5718 size_t msg_len = ssl->in_hslen - 12;
5719
5720 /* We should never receive an old handshake
5721 * message - double-check nonetheless. */
5722 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5723 {
5724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5726 }
5727
5728 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5729 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5730 {
5731 /* Silently ignore -- message too far in the future */
5732 MBEDTLS_SSL_DEBUG_MSG( 2,
5733 ( "Ignore future HS message with sequence number %u, "
5734 "buffering window %u - %u",
5735 recv_msg_seq, ssl->handshake->in_msg_seq,
5736 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5737
5738 goto exit;
5739 }
5740
5741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5742 recv_msg_seq, recv_msg_seq_offset ) );
5743
5744 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5745
5746 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005747 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005748 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005749 size_t reassembly_buf_sz;
5750
Hanno Becker37f95322018-08-16 13:55:32 +01005751 hs_buf->is_fragmented =
5752 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5753
5754 /* We copy the message back into the input buffer
5755 * after reassembly, so check that it's not too large.
5756 * This is an implementation-specific limitation
5757 * and not one from the standard, hence it is not
5758 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005759 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005760 {
5761 /* Ignore message */
5762 goto exit;
5763 }
5764
Hanno Beckere0b150f2018-08-21 15:51:03 +01005765 /* Check if we have enough space to buffer the message. */
5766 if( hs->buffering.total_bytes_buffered >
5767 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5768 {
5769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5770 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5771 }
5772
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005773 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5774 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005775
5776 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5777 hs->buffering.total_bytes_buffered ) )
5778 {
5779 if( recv_msg_seq_offset > 0 )
5780 {
5781 /* If we can't buffer a future message because
5782 * of space limitations -- ignore. */
5783 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",
5784 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5785 (unsigned) hs->buffering.total_bytes_buffered ) );
5786 goto exit;
5787 }
Hanno Beckere1801392018-08-21 16:51:05 +01005788 else
5789 {
5790 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",
5791 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5792 (unsigned) hs->buffering.total_bytes_buffered ) );
5793 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005794
Hanno Beckera02b0b42018-08-21 17:20:27 +01005795 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005796 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005797 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",
5798 (unsigned) msg_len,
5799 (unsigned) reassembly_buf_sz,
5800 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005801 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005802 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5803 goto exit;
5804 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005805 }
5806
5807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5808 msg_len ) );
5809
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005810 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5811 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005812 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005813 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005814 goto exit;
5815 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005816 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005817
5818 /* Prepare final header: copy msg_type, length and message_seq,
5819 * then add standardised fragment_offset and fragment_length */
5820 memcpy( hs_buf->data, ssl->in_msg, 6 );
5821 memset( hs_buf->data + 6, 0, 3 );
5822 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5823
5824 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005825
5826 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005827 }
5828 else
5829 {
5830 /* Make sure msg_type and length are consistent */
5831 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5832 {
5833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5834 /* Ignore */
5835 goto exit;
5836 }
5837 }
5838
Hanno Becker4422bbb2018-08-20 09:40:19 +01005839 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005840 {
5841 size_t frag_len, frag_off;
5842 unsigned char * const msg = hs_buf->data + 12;
5843
5844 /*
5845 * Check and copy current fragment
5846 */
5847
5848 /* Validation of header fields already done in
5849 * mbedtls_ssl_prepare_handshake_record(). */
5850 frag_off = ssl_get_hs_frag_off( ssl );
5851 frag_len = ssl_get_hs_frag_len( ssl );
5852
5853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5854 frag_off, frag_len ) );
5855 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5856
5857 if( hs_buf->is_fragmented )
5858 {
5859 unsigned char * const bitmask = msg + msg_len;
5860 ssl_bitmask_set( bitmask, frag_off, frag_len );
5861 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5862 msg_len ) == 0 );
5863 }
5864 else
5865 {
5866 hs_buf->is_complete = 1;
5867 }
5868
5869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5870 hs_buf->is_complete ? "" : "not yet " ) );
5871 }
5872
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005873 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005874 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005875
5876 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005877 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005878 break;
5879 }
5880
5881exit:
5882
5883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5884 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005885}
5886#endif /* MBEDTLS_SSL_PROTO_DTLS */
5887
Hanno Becker1097b342018-08-15 14:09:41 +01005888static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005889{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005890 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005891 * Consume last content-layer message and potentially
5892 * update in_msglen which keeps track of the contents'
5893 * consumption state.
5894 *
5895 * (1) Handshake messages:
5896 * Remove last handshake message, move content
5897 * and adapt in_msglen.
5898 *
5899 * (2) Alert messages:
5900 * Consume whole record content, in_msglen = 0.
5901 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005902 * (3) Change cipher spec:
5903 * Consume whole record content, in_msglen = 0.
5904 *
5905 * (4) Application data:
5906 * Don't do anything - the record layer provides
5907 * the application data as a stream transport
5908 * and consumes through mbedtls_ssl_read only.
5909 *
5910 */
5911
5912 /* Case (1): Handshake messages */
5913 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005914 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005915 /* Hard assertion to be sure that no application data
5916 * is in flight, as corrupting ssl->in_msglen during
5917 * ssl->in_offt != NULL is fatal. */
5918 if( ssl->in_offt != NULL )
5919 {
5920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5921 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5922 }
5923
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005924 /*
5925 * Get next Handshake message in the current record
5926 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005927
Hanno Becker4a810fb2017-05-24 16:27:30 +01005928 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005929 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005930 * current handshake content: If DTLS handshake
5931 * fragmentation is used, that's the fragment
5932 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005933 * size here is faulty and should be changed at
5934 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005935 * (2) While it doesn't seem to cause problems, one
5936 * has to be very careful not to assume that in_hslen
5937 * is always <= in_msglen in a sensible communication.
5938 * Again, it's wrong for DTLS handshake fragmentation.
5939 * The following check is therefore mandatory, and
5940 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005941 * Additionally, ssl->in_hslen might be arbitrarily out of
5942 * bounds after handling a DTLS message with an unexpected
5943 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005944 */
5945 if( ssl->in_hslen < ssl->in_msglen )
5946 {
5947 ssl->in_msglen -= ssl->in_hslen;
5948 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5949 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005950
Hanno Becker4a810fb2017-05-24 16:27:30 +01005951 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5952 ssl->in_msg, ssl->in_msglen );
5953 }
5954 else
5955 {
5956 ssl->in_msglen = 0;
5957 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005958
Hanno Becker4a810fb2017-05-24 16:27:30 +01005959 ssl->in_hslen = 0;
5960 }
5961 /* Case (4): Application data */
5962 else if( ssl->in_offt != NULL )
5963 {
5964 return( 0 );
5965 }
5966 /* Everything else (CCS & Alerts) */
5967 else
5968 {
5969 ssl->in_msglen = 0;
5970 }
5971
Hanno Becker1097b342018-08-15 14:09:41 +01005972 return( 0 );
5973}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005974
Hanno Beckere74d5562018-08-15 14:26:08 +01005975static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5976{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005977 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005978 return( 1 );
5979
5980 return( 0 );
5981}
5982
Hanno Becker5f066e72018-08-16 14:56:31 +01005983#if defined(MBEDTLS_SSL_PROTO_DTLS)
5984
5985static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5986{
5987 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5988 if( hs == NULL )
5989 return;
5990
Hanno Becker01315ea2018-08-21 17:22:17 +01005991 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005992 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005993 hs->buffering.total_bytes_buffered -=
5994 hs->buffering.future_record.len;
5995
5996 mbedtls_free( hs->buffering.future_record.data );
5997 hs->buffering.future_record.data = NULL;
5998 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005999}
6000
6001static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6002{
6003 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6004 unsigned char * rec;
6005 size_t rec_len;
6006 unsigned rec_epoch;
6007
6008 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6009 return( 0 );
6010
6011 if( hs == NULL )
6012 return( 0 );
6013
Hanno Becker5f066e72018-08-16 14:56:31 +01006014 rec = hs->buffering.future_record.data;
6015 rec_len = hs->buffering.future_record.len;
6016 rec_epoch = hs->buffering.future_record.epoch;
6017
6018 if( rec == NULL )
6019 return( 0 );
6020
Hanno Becker4cb782d2018-08-20 11:19:05 +01006021 /* Only consider loading future records if the
6022 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006023 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006024 return( 0 );
6025
Hanno Becker5f066e72018-08-16 14:56:31 +01006026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6027
6028 if( rec_epoch != ssl->in_epoch )
6029 {
6030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6031 goto exit;
6032 }
6033
6034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6035
6036 /* Double-check that the record is not too large */
6037 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6038 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6039 {
6040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6041 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6042 }
6043
6044 memcpy( ssl->in_hdr, rec, rec_len );
6045 ssl->in_left = rec_len;
6046 ssl->next_record_offset = 0;
6047
6048 ssl_free_buffered_record( ssl );
6049
6050exit:
6051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6052 return( 0 );
6053}
6054
Hanno Becker519f15d2019-07-11 12:43:20 +01006055static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6056 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006057{
6058 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006059
6060 /* Don't buffer future records outside handshakes. */
6061 if( hs == NULL )
6062 return( 0 );
6063
6064 /* Only buffer handshake records (we are only interested
6065 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01006066 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006067 return( 0 );
6068
6069 /* Don't buffer more than one future epoch record. */
6070 if( hs->buffering.future_record.data != NULL )
6071 return( 0 );
6072
Hanno Becker01315ea2018-08-21 17:22:17 +01006073 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01006074 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006075 hs->buffering.total_bytes_buffered ) )
6076 {
6077 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 +01006078 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006079 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006080 return( 0 );
6081 }
6082
Hanno Becker5f066e72018-08-16 14:56:31 +01006083 /* Buffer record */
6084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6085 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01006086 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006087
6088 /* ssl_parse_record_header() only considers records
6089 * of the next epoch as candidates for buffering. */
6090 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006091 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006092
6093 hs->buffering.future_record.data =
6094 mbedtls_calloc( 1, hs->buffering.future_record.len );
6095 if( hs->buffering.future_record.data == NULL )
6096 {
6097 /* If we run out of RAM trying to buffer a
6098 * record from the next epoch, just ignore. */
6099 return( 0 );
6100 }
6101
Hanno Becker519f15d2019-07-11 12:43:20 +01006102 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006103
Hanno Becker519f15d2019-07-11 12:43:20 +01006104 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006105 return( 0 );
6106}
6107
6108#endif /* MBEDTLS_SSL_PROTO_DTLS */
6109
Hanno Beckere74d5562018-08-15 14:26:08 +01006110static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006111{
6112 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006113 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006114
Hanno Becker5f066e72018-08-16 14:56:31 +01006115#if defined(MBEDTLS_SSL_PROTO_DTLS)
6116 /* We might have buffered a future record; if so,
6117 * and if the epoch matches now, load it.
6118 * On success, this call will set ssl->in_left to
6119 * the length of the buffered record, so that
6120 * the calls to ssl_fetch_input() below will
6121 * essentially be no-ops. */
6122 ret = ssl_load_buffered_record( ssl );
6123 if( ret != 0 )
6124 return( ret );
6125#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006126
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006127 /* Ensure that we have enough space available for the default form
6128 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6129 * with no space for CIDs counted in). */
6130 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6131 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006133 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006134 return( ret );
6135 }
6136
Hanno Beckere5e7e782019-07-11 12:29:35 +01006137 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6138 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006140#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006141 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006142 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006143 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6144 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006145 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006146 if( ret != 0 )
6147 return( ret );
6148
6149 /* Fall through to handling of unexpected records */
6150 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6151 }
6152
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006153 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6154 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006155#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006156 /* Reset in pointers to default state for TLS/DTLS records,
6157 * assuming no CID and no offset between record content and
6158 * record plaintext. */
6159 ssl_update_in_pointers( ssl );
6160
Hanno Becker7ae20e02019-07-12 08:33:49 +01006161 /* Setup internal message pointers from record structure. */
6162 ssl->in_msgtype = rec.type;
6163#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6164 ssl->in_len = ssl->in_cid + rec.cid_len;
6165#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6166 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6167 ssl->in_msglen = rec.data_len;
6168
Hanno Becker2fddd372019-07-10 14:37:41 +01006169 ret = ssl_check_client_reconnect( ssl );
6170 if( ret != 0 )
6171 return( ret );
6172#endif
6173
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006174 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006175 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006176
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6178 "(header)" ) );
6179 }
6180 else
6181 {
6182 /* Skip invalid record and the rest of the datagram */
6183 ssl->next_record_offset = 0;
6184 ssl->in_left = 0;
6185
6186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6187 "(header)" ) );
6188 }
6189
6190 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006191 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006192 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006193 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006194#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006195 {
6196 return( ret );
6197 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006198 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006200#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006201 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006202 {
Hanno Beckera8814792019-07-10 15:01:45 +01006203 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006204 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006205 if( ssl->next_record_offset < ssl->in_left )
6206 {
6207 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6208 }
6209 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006210 else
6211#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006212 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006213 /*
6214 * Fetch record contents from underlying transport.
6215 */
Hanno Beckera3175662019-07-11 12:50:29 +01006216 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006217 if( ret != 0 )
6218 {
6219 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6220 return( ret );
6221 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006222
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006223 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006224 }
6225
6226 /*
6227 * Decrypt record contents.
6228 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006229
Hanno Beckerfdf66042019-07-11 13:07:45 +01006230 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006233 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006234 {
6235 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006236 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006237 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006238 /* Except when waiting for Finished as a bad mac here
6239 * probably means something went wrong in the handshake
6240 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6241 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6242 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6243 {
6244#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6245 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6246 {
6247 mbedtls_ssl_send_alert_message( ssl,
6248 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6249 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6250 }
6251#endif
6252 return( ret );
6253 }
6254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006255#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006256 if( ssl->conf->badmac_limit != 0 &&
6257 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6260 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006261 }
6262#endif
6263
Hanno Becker4a810fb2017-05-24 16:27:30 +01006264 /* As above, invalid records cause
6265 * dismissal of the whole datagram. */
6266
6267 ssl->next_record_offset = 0;
6268 ssl->in_left = 0;
6269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006271 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006272 }
6273
6274 return( ret );
6275 }
6276 else
6277#endif
6278 {
6279 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006280#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6281 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006283 mbedtls_ssl_send_alert_message( ssl,
6284 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6285 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006286 }
6287#endif
6288 return( ret );
6289 }
6290 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006291
Hanno Becker44d89b22019-07-12 09:40:44 +01006292
6293 /* Reset in pointers to default state for TLS/DTLS records,
6294 * assuming no CID and no offset between record content and
6295 * record plaintext. */
6296 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006297#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6298 ssl->in_len = ssl->in_cid + rec.cid_len;
6299#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6300 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006301
Hanno Becker8685c822019-07-12 09:37:30 +01006302 /* The record content type may change during decryption,
6303 * so re-read it. */
6304 ssl->in_msgtype = rec.type;
6305 /* Also update the input buffer, because unfortunately
6306 * the server-side ssl_parse_client_hello() reparses the
6307 * record header when receiving a ClientHello initiating
6308 * a renegotiation. */
6309 ssl->in_hdr[0] = rec.type;
6310 ssl->in_msg = rec.buf + rec.data_offset;
6311 ssl->in_msglen = rec.data_len;
6312 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6313 ssl->in_len[1] = (unsigned char)( rec.data_len );
6314
Simon Butcher99000142016-10-13 17:21:01 +01006315 return( 0 );
6316}
6317
6318int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6319{
6320 int ret;
6321
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006322 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006323 * Handle particular types of records
6324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006325 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006326 {
Simon Butcher99000142016-10-13 17:21:01 +01006327 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6328 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006329 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006330 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006331 }
6332
Hanno Beckere678eaa2018-08-21 14:57:46 +01006333 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006334 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006335 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006336 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6338 ssl->in_msglen ) );
6339 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006340 }
6341
Hanno Beckere678eaa2018-08-21 14:57:46 +01006342 if( ssl->in_msg[0] != 1 )
6343 {
6344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6345 ssl->in_msg[0] ) );
6346 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6347 }
6348
6349#if defined(MBEDTLS_SSL_PROTO_DTLS)
6350 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6351 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6352 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6353 {
6354 if( ssl->handshake == NULL )
6355 {
6356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6357 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6358 }
6359
6360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6361 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6362 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006363#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006364 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006366 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006367 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006368 if( ssl->in_msglen != 2 )
6369 {
6370 /* Note: Standard allows for more than one 2 byte alert
6371 to be packed in a single message, but Mbed TLS doesn't
6372 currently support this. */
6373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6374 ssl->in_msglen ) );
6375 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6376 }
6377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006379 ssl->in_msg[0], ssl->in_msg[1] ) );
6380
6381 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006382 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006383 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006384 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006387 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006388 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006389 }
6390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006391 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6392 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6395 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006396 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006397
6398#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6399 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6400 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6401 {
Hanno Becker90333da2017-10-10 11:27:13 +01006402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006403 /* Will be handled when trying to parse ServerHello */
6404 return( 0 );
6405 }
6406#endif
6407
6408#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6409 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6410 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6411 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6412 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6413 {
6414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6415 /* Will be handled in mbedtls_ssl_parse_certificate() */
6416 return( 0 );
6417 }
6418#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6419
6420 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006421 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006422 }
6423
Hanno Beckerc76c6192017-06-06 10:03:17 +01006424#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006425 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006426 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006427 /* Drop unexpected ApplicationData records,
6428 * except at the beginning of renegotiations */
6429 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6430 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6431#if defined(MBEDTLS_SSL_RENEGOTIATION)
6432 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6433 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006434#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006435 )
6436 {
6437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6438 return( MBEDTLS_ERR_SSL_NON_FATAL );
6439 }
6440
6441 if( ssl->handshake != NULL &&
6442 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6443 {
6444 ssl_handshake_wrapup_free_hs_transform( ssl );
6445 }
6446 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006447#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006448
Paul Bakker5121ce52009-01-03 21:22:43 +00006449 return( 0 );
6450}
6451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006453{
6454 int ret;
6455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006456 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6457 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6458 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006459 {
6460 return( ret );
6461 }
6462
6463 return( 0 );
6464}
6465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006466int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006467 unsigned char level,
6468 unsigned char message )
6469{
6470 int ret;
6471
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006472 if( ssl == NULL || ssl->conf == NULL )
6473 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006476 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006479 ssl->out_msglen = 2;
6480 ssl->out_msg[0] = level;
6481 ssl->out_msg[1] = message;
6482
Hanno Becker67bc7c32018-08-06 11:33:50 +01006483 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006485 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006486 return( ret );
6487 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006489
6490 return( 0 );
6491}
6492
Hanno Beckerb9d44792019-02-08 07:19:04 +00006493#if defined(MBEDTLS_X509_CRT_PARSE_C)
6494static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6495{
6496#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6497 if( session->peer_cert != NULL )
6498 {
6499 mbedtls_x509_crt_free( session->peer_cert );
6500 mbedtls_free( session->peer_cert );
6501 session->peer_cert = NULL;
6502 }
6503#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6504 if( session->peer_cert_digest != NULL )
6505 {
6506 /* Zeroization is not necessary. */
6507 mbedtls_free( session->peer_cert_digest );
6508 session->peer_cert_digest = NULL;
6509 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6510 session->peer_cert_digest_len = 0;
6511 }
6512#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6513}
6514#endif /* MBEDTLS_X509_CRT_PARSE_C */
6515
Paul Bakker5121ce52009-01-03 21:22:43 +00006516/*
6517 * Handshake functions
6518 */
Hanno Becker21489932019-02-05 13:20:55 +00006519#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006520/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006521int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006522{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006523 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6524 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006527
Hanno Becker7177a882019-02-05 13:36:46 +00006528 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006531 ssl->state++;
6532 return( 0 );
6533 }
6534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6536 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006537}
6538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006539int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006540{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006541 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6542 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006545
Hanno Becker7177a882019-02-05 13:36:46 +00006546 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006549 ssl->state++;
6550 return( 0 );
6551 }
6552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6554 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006555}
Gilles Peskinef9828522017-05-03 12:28:43 +02006556
Hanno Becker21489932019-02-05 13:20:55 +00006557#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006558/* Some certificate support -> implement write and parse */
6559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006561{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006562 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006563 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006565 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6566 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006569
Hanno Becker7177a882019-02-05 13:36:46 +00006570 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006573 ssl->state++;
6574 return( 0 );
6575 }
6576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006577#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006578 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006579 {
6580 if( ssl->client_auth == 0 )
6581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006583 ssl->state++;
6584 return( 0 );
6585 }
6586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006587#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006588 /*
6589 * If using SSLv3 and got no cert, send an Alert message
6590 * (otherwise an empty Certificate message will be sent).
6591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006592 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6593 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006594 {
6595 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6597 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6598 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006601 goto write_msg;
6602 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006603#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006604 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006605#endif /* MBEDTLS_SSL_CLI_C */
6606#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006607 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006609 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6612 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006613 }
6614 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006615#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006617 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006618
6619 /*
6620 * 0 . 0 handshake type
6621 * 1 . 3 handshake length
6622 * 4 . 6 length of all certs
6623 * 7 . 9 length of cert. 1
6624 * 10 . n-1 peer certificate
6625 * n . n+2 length of cert. 2
6626 * n+3 . ... upper level cert, etc.
6627 */
6628 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006629 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006630
Paul Bakker29087132010-03-21 21:03:34 +00006631 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006632 {
6633 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006634 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006637 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006639 }
6640
6641 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6642 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6643 ssl->out_msg[i + 2] = (unsigned char)( n );
6644
6645 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6646 i += n; crt = crt->next;
6647 }
6648
6649 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6650 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6651 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6652
6653 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006654 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6655 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006656
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006657#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006658write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006659#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006660
6661 ssl->state++;
6662
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006663 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006664 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006665 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006666 return( ret );
6667 }
6668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006670
Paul Bakkered27a042013-04-18 22:46:23 +02006671 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006672}
6673
Hanno Becker84879e32019-01-31 07:44:03 +00006674#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006675
6676#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006677static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6678 unsigned char *crt_buf,
6679 size_t crt_buf_len )
6680{
6681 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6682
6683 if( peer_crt == NULL )
6684 return( -1 );
6685
6686 if( peer_crt->raw.len != crt_buf_len )
6687 return( -1 );
6688
Hanno Becker46f34d02019-02-08 14:00:04 +00006689 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006690}
Hanno Becker177475a2019-02-05 17:02:46 +00006691#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6692static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6693 unsigned char *crt_buf,
6694 size_t crt_buf_len )
6695{
6696 int ret;
6697 unsigned char const * const peer_cert_digest =
6698 ssl->session->peer_cert_digest;
6699 mbedtls_md_type_t const peer_cert_digest_type =
6700 ssl->session->peer_cert_digest_type;
6701 mbedtls_md_info_t const * const digest_info =
6702 mbedtls_md_info_from_type( peer_cert_digest_type );
6703 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6704 size_t digest_len;
6705
6706 if( peer_cert_digest == NULL || digest_info == NULL )
6707 return( -1 );
6708
6709 digest_len = mbedtls_md_get_size( digest_info );
6710 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6711 return( -1 );
6712
6713 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6714 if( ret != 0 )
6715 return( -1 );
6716
6717 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6718}
6719#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006720#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006721
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006722/*
6723 * Once the certificate message is read, parse it into a cert chain and
6724 * perform basic checks, but leave actual verification to the caller
6725 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006726static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6727 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006728{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006729 int ret;
6730#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6731 int crt_cnt=0;
6732#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006733 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006734 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006736 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006739 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6740 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006741 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006742 }
6743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006744 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6745 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
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_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006750 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006751 }
6752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006754
Paul Bakker5121ce52009-01-03 21:22:43 +00006755 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006756 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006757 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006758 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006759
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006760 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006764 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6765 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006767 }
6768
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006769 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6770 i += 3;
6771
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006772 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006773 while( i < ssl->in_hslen )
6774 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006775 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006776 if ( i + 3 > ssl->in_hslen ) {
6777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006778 mbedtls_ssl_send_alert_message( ssl,
6779 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6780 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006781 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6782 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006783 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6784 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006785 if( ssl->in_msg[i] != 0 )
6786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006788 mbedtls_ssl_send_alert_message( ssl,
6789 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6790 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006792 }
6793
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006794 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006795 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6796 | (unsigned int) ssl->in_msg[i + 2];
6797 i += 3;
6798
6799 if( n < 128 || i + n > ssl->in_hslen )
6800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006802 mbedtls_ssl_send_alert_message( ssl,
6803 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6804 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006805 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006806 }
6807
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006808 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006809#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6810 if( crt_cnt++ == 0 &&
6811 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6812 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006813 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006814 /* During client-side renegotiation, check that the server's
6815 * end-CRTs hasn't changed compared to the initial handshake,
6816 * mitigating the triple handshake attack. On success, reuse
6817 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006818 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6819 if( ssl_check_peer_crt_unchanged( ssl,
6820 &ssl->in_msg[i],
6821 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006822 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6824 mbedtls_ssl_send_alert_message( ssl,
6825 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6826 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6827 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006828 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006829
6830 /* Now we can safely free the original chain. */
6831 ssl_clear_peer_cert( ssl->session );
6832 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006833#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6834
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006835 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006836#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006837 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006838#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006839 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006840 * it in-place from the input buffer instead of making a copy. */
6841 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6842#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006843 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006844 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006845 case 0: /*ok*/
6846 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6847 /* Ignore certificate with an unknown algorithm: maybe a
6848 prior certificate was already trusted. */
6849 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006850
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006851 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6852 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6853 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006854
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006855 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6856 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6857 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006858
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006859 default:
6860 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6861 crt_parse_der_failed:
6862 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6863 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6864 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006865 }
6866
6867 i += n;
6868 }
6869
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006870 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006871 return( 0 );
6872}
6873
Hanno Becker4a55f632019-02-05 12:49:06 +00006874#if defined(MBEDTLS_SSL_SRV_C)
6875static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6876{
6877 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6878 return( -1 );
6879
6880#if defined(MBEDTLS_SSL_PROTO_SSL3)
6881 /*
6882 * Check if the client sent an empty certificate
6883 */
6884 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6885 {
6886 if( ssl->in_msglen == 2 &&
6887 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6888 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6889 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6890 {
6891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6892 return( 0 );
6893 }
6894
6895 return( -1 );
6896 }
6897#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6898
6899#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6900 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6901 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6902 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6903 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6904 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6905 {
6906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6907 return( 0 );
6908 }
6909
6910 return( -1 );
6911#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6912 MBEDTLS_SSL_PROTO_TLS1_2 */
6913}
6914#endif /* MBEDTLS_SSL_SRV_C */
6915
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006916/* Check if a certificate message is expected.
6917 * Return either
6918 * - SSL_CERTIFICATE_EXPECTED, or
6919 * - SSL_CERTIFICATE_SKIP
6920 * indicating whether a Certificate message is expected or not.
6921 */
6922#define SSL_CERTIFICATE_EXPECTED 0
6923#define SSL_CERTIFICATE_SKIP 1
6924static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6925 int authmode )
6926{
6927 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006928 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006929
6930 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6931 return( SSL_CERTIFICATE_SKIP );
6932
6933#if defined(MBEDTLS_SSL_SRV_C)
6934 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6935 {
6936 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6937 return( SSL_CERTIFICATE_SKIP );
6938
6939 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6940 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006941 ssl->session_negotiate->verify_result =
6942 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6943 return( SSL_CERTIFICATE_SKIP );
6944 }
6945 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006946#else
6947 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006948#endif /* MBEDTLS_SSL_SRV_C */
6949
6950 return( SSL_CERTIFICATE_EXPECTED );
6951}
6952
Hanno Becker68636192019-02-05 14:36:34 +00006953static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6954 int authmode,
6955 mbedtls_x509_crt *chain,
6956 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006957{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006958 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006959 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006960 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006961 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006962
Hanno Becker8927c832019-04-03 12:52:50 +01006963 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6964 void *p_vrfy;
6965
Hanno Becker68636192019-02-05 14:36:34 +00006966 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6967 return( 0 );
6968
Hanno Becker8927c832019-04-03 12:52:50 +01006969 if( ssl->f_vrfy != NULL )
6970 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006971 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006972 f_vrfy = ssl->f_vrfy;
6973 p_vrfy = ssl->p_vrfy;
6974 }
6975 else
6976 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006977 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006978 f_vrfy = ssl->conf->f_vrfy;
6979 p_vrfy = ssl->conf->p_vrfy;
6980 }
6981
Hanno Becker68636192019-02-05 14:36:34 +00006982 /*
6983 * Main check: verify certificate
6984 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006985#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6986 if( ssl->conf->f_ca_cb != NULL )
6987 {
6988 ((void) rs_ctx);
6989 have_ca_chain = 1;
6990
6991 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006992 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006993 chain,
6994 ssl->conf->f_ca_cb,
6995 ssl->conf->p_ca_cb,
6996 ssl->conf->cert_profile,
6997 ssl->hostname,
6998 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006999 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007000 }
7001 else
7002#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7003 {
7004 mbedtls_x509_crt *ca_chain;
7005 mbedtls_x509_crl *ca_crl;
7006
7007#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7008 if( ssl->handshake->sni_ca_chain != NULL )
7009 {
7010 ca_chain = ssl->handshake->sni_ca_chain;
7011 ca_crl = ssl->handshake->sni_ca_crl;
7012 }
7013 else
7014#endif
7015 {
7016 ca_chain = ssl->conf->ca_chain;
7017 ca_crl = ssl->conf->ca_crl;
7018 }
7019
7020 if( ca_chain != NULL )
7021 have_ca_chain = 1;
7022
7023 ret = mbedtls_x509_crt_verify_restartable(
7024 chain,
7025 ca_chain, ca_crl,
7026 ssl->conf->cert_profile,
7027 ssl->hostname,
7028 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007029 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007030 }
Hanno Becker68636192019-02-05 14:36:34 +00007031
7032 if( ret != 0 )
7033 {
7034 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7035 }
7036
7037#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7038 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7039 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7040#endif
7041
7042 /*
7043 * Secondary checks: always done, but change 'ret' only if it was 0
7044 */
7045
7046#if defined(MBEDTLS_ECP_C)
7047 {
7048 const mbedtls_pk_context *pk = &chain->pk;
7049
7050 /* If certificate uses an EC key, make sure the curve is OK */
7051 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
7052 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
7053 {
7054 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7055
7056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7057 if( ret == 0 )
7058 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7059 }
7060 }
7061#endif /* MBEDTLS_ECP_C */
7062
7063 if( mbedtls_ssl_check_cert_usage( chain,
7064 ciphersuite_info,
7065 ! ssl->conf->endpoint,
7066 &ssl->session_negotiate->verify_result ) != 0 )
7067 {
7068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7069 if( ret == 0 )
7070 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7071 }
7072
7073 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7074 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7075 * with details encoded in the verification flags. All other kinds
7076 * of error codes, including those from the user provided f_vrfy
7077 * functions, are treated as fatal and lead to a failure of
7078 * ssl_parse_certificate even if verification was optional. */
7079 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7080 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7081 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7082 {
7083 ret = 0;
7084 }
7085
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007086 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007087 {
7088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7089 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7090 }
7091
7092 if( ret != 0 )
7093 {
7094 uint8_t alert;
7095
7096 /* The certificate may have been rejected for several reasons.
7097 Pick one and send the corresponding alert. Which alert to send
7098 may be a subject of debate in some cases. */
7099 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7100 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7101 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7102 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7103 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7104 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7105 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7106 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7107 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7108 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7109 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7110 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7111 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7112 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7113 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7114 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7115 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7116 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7117 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7118 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7119 else
7120 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7121 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7122 alert );
7123 }
7124
7125#if defined(MBEDTLS_DEBUG_C)
7126 if( ssl->session_negotiate->verify_result != 0 )
7127 {
7128 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7129 ssl->session_negotiate->verify_result ) );
7130 }
7131 else
7132 {
7133 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7134 }
7135#endif /* MBEDTLS_DEBUG_C */
7136
7137 return( ret );
7138}
7139
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007140#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7141static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7142 unsigned char *start, size_t len )
7143{
7144 int ret;
7145 /* Remember digest of the peer's end-CRT. */
7146 ssl->session_negotiate->peer_cert_digest =
7147 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7148 if( ssl->session_negotiate->peer_cert_digest == NULL )
7149 {
7150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7151 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7152 mbedtls_ssl_send_alert_message( ssl,
7153 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7154 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7155
7156 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7157 }
7158
7159 ret = mbedtls_md( mbedtls_md_info_from_type(
7160 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7161 start, len,
7162 ssl->session_negotiate->peer_cert_digest );
7163
7164 ssl->session_negotiate->peer_cert_digest_type =
7165 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7166 ssl->session_negotiate->peer_cert_digest_len =
7167 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7168
7169 return( ret );
7170}
7171
7172static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7173 unsigned char *start, size_t len )
7174{
7175 unsigned char *end = start + len;
7176 int ret;
7177
7178 /* Make a copy of the peer's raw public key. */
7179 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7180 ret = mbedtls_pk_parse_subpubkey( &start, end,
7181 &ssl->handshake->peer_pubkey );
7182 if( ret != 0 )
7183 {
7184 /* We should have parsed the public key before. */
7185 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7186 }
7187
7188 return( 0 );
7189}
7190#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7191
Hanno Becker68636192019-02-05 14:36:34 +00007192int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7193{
7194 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007195 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007196#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7197 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7198 ? ssl->handshake->sni_authmode
7199 : ssl->conf->authmode;
7200#else
7201 const int authmode = ssl->conf->authmode;
7202#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007203 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007204 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007205
7206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7207
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007208 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7209 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007210 {
7211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007212 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007213 }
7214
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007215#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7216 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007217 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007218 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007219 chain = ssl->handshake->ecrs_peer_cert;
7220 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007221 goto crt_verify;
7222 }
7223#endif
7224
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007225 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007226 {
7227 /* mbedtls_ssl_read_record may have sent an alert already. We
7228 let it decide whether to alert. */
7229 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007230 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007231 }
7232
Hanno Becker4a55f632019-02-05 12:49:06 +00007233#if defined(MBEDTLS_SSL_SRV_C)
7234 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7235 {
7236 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007237
7238 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007239 ret = 0;
7240 else
7241 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007242
Hanno Becker6bdfab22019-02-05 13:11:17 +00007243 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007244 }
7245#endif /* MBEDTLS_SSL_SRV_C */
7246
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007247 /* Clear existing peer CRT structure in case we tried to
7248 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007249 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007250
7251 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7252 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007253 {
7254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7255 sizeof( mbedtls_x509_crt ) ) );
7256 mbedtls_ssl_send_alert_message( ssl,
7257 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7258 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007259
Hanno Becker3dad3112019-02-05 17:19:52 +00007260 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7261 goto exit;
7262 }
7263 mbedtls_x509_crt_init( chain );
7264
7265 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007266 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007267 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007268
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007269#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7270 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007271 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007272
7273crt_verify:
7274 if( ssl->handshake->ecrs_enabled)
7275 rs_ctx = &ssl->handshake->ecrs_ctx;
7276#endif
7277
Hanno Becker68636192019-02-05 14:36:34 +00007278 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007279 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007280 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007281 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007282
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007283#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007284 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007285 unsigned char *crt_start, *pk_start;
7286 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007287
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007288 /* We parse the CRT chain without copying, so
7289 * these pointers point into the input buffer,
7290 * and are hence still valid after freeing the
7291 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007292
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007293 crt_start = chain->raw.p;
7294 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007295
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007296 pk_start = chain->pk_raw.p;
7297 pk_len = chain->pk_raw.len;
7298
7299 /* Free the CRT structures before computing
7300 * digest and copying the peer's public key. */
7301 mbedtls_x509_crt_free( chain );
7302 mbedtls_free( chain );
7303 chain = NULL;
7304
7305 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007306 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007307 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007308
7309 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7310 if( ret != 0 )
7311 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007312 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007313#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7314 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007315 ssl->session_negotiate->peer_cert = chain;
7316 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007317#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007320
Hanno Becker6bdfab22019-02-05 13:11:17 +00007321exit:
7322
Hanno Becker3dad3112019-02-05 17:19:52 +00007323 if( ret == 0 )
7324 ssl->state++;
7325
7326#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7327 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7328 {
7329 ssl->handshake->ecrs_peer_cert = chain;
7330 chain = NULL;
7331 }
7332#endif
7333
7334 if( chain != NULL )
7335 {
7336 mbedtls_x509_crt_free( chain );
7337 mbedtls_free( chain );
7338 }
7339
Paul Bakker5121ce52009-01-03 21:22:43 +00007340 return( ret );
7341}
Hanno Becker21489932019-02-05 13:20:55 +00007342#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007345{
7346 int ret;
7347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007351 ssl->out_msglen = 1;
7352 ssl->out_msg[0] = 1;
7353
Paul Bakker5121ce52009-01-03 21:22:43 +00007354 ssl->state++;
7355
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007356 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007357 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007358 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007359 return( ret );
7360 }
7361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007363
7364 return( 0 );
7365}
7366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007368{
7369 int ret;
7370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007372
Hanno Becker327c93b2018-08-15 13:56:18 +01007373 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007376 return( ret );
7377 }
7378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007382 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7383 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007385 }
7386
Hanno Beckere678eaa2018-08-21 14:57:46 +01007387 /* CCS records are only accepted if they have length 1 and content '1',
7388 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007389
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007390 /*
7391 * Switch to our negotiated transform and session parameters for inbound
7392 * data.
7393 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007394 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007395 ssl->transform_in = ssl->transform_negotiate;
7396 ssl->session_in = ssl->session_negotiate;
7397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007399 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007402 ssl_dtls_replay_reset( ssl );
7403#endif
7404
7405 /* Increment epoch */
7406 if( ++ssl->in_epoch == 0 )
7407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007409 /* This is highly unlikely to happen for legitimate reasons, so
7410 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007412 }
7413 }
7414 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007416 memset( ssl->in_ctr, 0, 8 );
7417
Hanno Becker79594fd2019-05-08 09:38:41 +01007418 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7421 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007425 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007426 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7427 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007429 }
7430 }
7431#endif
7432
Paul Bakker5121ce52009-01-03 21:22:43 +00007433 ssl->state++;
7434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007436
7437 return( 0 );
7438}
7439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7441 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007442{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007443 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7446 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7447 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007448 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007449 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007450#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7452#if defined(MBEDTLS_SHA512_C)
7453 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007454 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7455 else
7456#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457#if defined(MBEDTLS_SHA256_C)
7458 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007459 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007460 else
7461#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007465 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007466 }
Paul Bakker380da532012-04-18 16:10:25 +00007467}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007469void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007470{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007471#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7472 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007473 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7474 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007475#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7477#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007478#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007479 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007480 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7481#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007482 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007483#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007484#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007485#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007486#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007487 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007488 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007489#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007490 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007491#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007492#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007494}
7495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007496static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007497 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007498{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7500 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007501 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7502 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007503#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7505#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007506#if defined(MBEDTLS_USE_PSA_CRYPTO)
7507 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7508#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007509 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007510#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007511#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007513#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007514 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007515#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007516 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007517#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007518#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007519#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007520}
7521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007522#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7523 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7524static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007525 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007526{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007527 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7528 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007529}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007530#endif
Paul Bakker380da532012-04-18 16:10:25 +00007531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7533#if defined(MBEDTLS_SHA256_C)
7534static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007535 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007536{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007537#if defined(MBEDTLS_USE_PSA_CRYPTO)
7538 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7539#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007540 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007541#endif
Paul Bakker380da532012-04-18 16:10:25 +00007542}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007543#endif
Paul Bakker380da532012-04-18 16:10:25 +00007544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007545#if defined(MBEDTLS_SHA512_C)
7546static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007547 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007548{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007549#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007550 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007551#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007552 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007553#endif
Paul Bakker380da532012-04-18 16:10:25 +00007554}
Paul Bakker769075d2012-11-24 11:26:46 +01007555#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007559static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007561{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007562 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007563 mbedtls_md5_context md5;
7564 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007565
Paul Bakker5121ce52009-01-03 21:22:43 +00007566 unsigned char padbuf[48];
7567 unsigned char md5sum[16];
7568 unsigned char sha1sum[20];
7569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007570 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007571 if( !session )
7572 session = ssl->session;
7573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007575
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007576 mbedtls_md5_init( &md5 );
7577 mbedtls_sha1_init( &sha1 );
7578
7579 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7580 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007581
7582 /*
7583 * SSLv3:
7584 * hash =
7585 * MD5( master + pad2 +
7586 * MD5( handshake + sender + master + pad1 ) )
7587 * + SHA1( master + pad2 +
7588 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007589 */
7590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007591#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007592 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7593 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007594#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007597 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7598 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007599#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007601 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007602 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007603
Paul Bakker1ef83d62012-04-11 12:09:53 +00007604 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007605
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007606 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7607 mbedtls_md5_update_ret( &md5, session->master, 48 );
7608 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7609 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007610
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007611 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7612 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7613 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7614 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007615
Paul Bakker1ef83d62012-04-11 12:09:53 +00007616 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007617
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007618 mbedtls_md5_starts_ret( &md5 );
7619 mbedtls_md5_update_ret( &md5, session->master, 48 );
7620 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7621 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7622 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007623
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007624 mbedtls_sha1_starts_ret( &sha1 );
7625 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7626 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7627 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7628 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007631
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007632 mbedtls_md5_free( &md5 );
7633 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007634
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007635 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7636 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7637 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007640}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007643#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007644static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007646{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007647 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007648 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007649 mbedtls_md5_context md5;
7650 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007651 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007654 if( !session )
7655 session = ssl->session;
7656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007658
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007659 mbedtls_md5_init( &md5 );
7660 mbedtls_sha1_init( &sha1 );
7661
7662 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7663 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007664
Paul Bakker1ef83d62012-04-11 12:09:53 +00007665 /*
7666 * TLSv1:
7667 * hash = PRF( master, finished_label,
7668 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7669 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007671#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007672 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7673 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007674#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007677 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7678 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007679#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007681 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007682 ? "client finished"
7683 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007684
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007685 mbedtls_md5_finish_ret( &md5, padbuf );
7686 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007687
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007688 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007689 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007692
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007693 mbedtls_md5_free( &md5 );
7694 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007695
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007696 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007699}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7703#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007704static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007706{
7707 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007708 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007709 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007710#if defined(MBEDTLS_USE_PSA_CRYPTO)
7711 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007712 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007713 psa_status_t status;
7714#else
7715 mbedtls_sha256_context sha256;
7716#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007718 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007719 if( !session )
7720 session = ssl->session;
7721
Andrzej Kurekeb342242019-01-29 09:14:33 -05007722 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7723 ? "client finished"
7724 : "server finished";
7725
7726#if defined(MBEDTLS_USE_PSA_CRYPTO)
7727 sha256_psa = psa_hash_operation_init();
7728
7729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7730
7731 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7732 if( status != PSA_SUCCESS )
7733 {
7734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7735 return;
7736 }
7737
7738 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7739 if( status != PSA_SUCCESS )
7740 {
7741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7742 return;
7743 }
7744 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7745#else
7746
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007747 mbedtls_sha256_init( &sha256 );
7748
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007750
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007751 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007752
7753 /*
7754 * TLSv1.2:
7755 * hash = PRF( master, finished_label,
7756 * Hash( handshake ) )[0.11]
7757 */
7758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007759#if !defined(MBEDTLS_SHA256_ALT)
7760 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007761 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007762#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007763
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007764 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007765 mbedtls_sha256_free( &sha256 );
7766#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007767
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007768 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007769 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007772
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007773 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007776}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007779#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007780static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007781 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007782{
7783 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007784 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007785 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007786#if defined(MBEDTLS_USE_PSA_CRYPTO)
7787 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007788 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007789 psa_status_t status;
7790#else
7791 mbedtls_sha512_context sha512;
7792#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007795 if( !session )
7796 session = ssl->session;
7797
Andrzej Kurekeb342242019-01-29 09:14:33 -05007798 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7799 ? "client finished"
7800 : "server finished";
7801
7802#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007803 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007804
7805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7806
Andrzej Kurek972fba52019-01-30 03:29:12 -05007807 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007808 if( status != PSA_SUCCESS )
7809 {
7810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7811 return;
7812 }
7813
Andrzej Kurek972fba52019-01-30 03:29:12 -05007814 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007815 if( status != PSA_SUCCESS )
7816 {
7817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7818 return;
7819 }
7820 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7821#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007822 mbedtls_sha512_init( &sha512 );
7823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007825
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007826 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007827
7828 /*
7829 * TLSv1.2:
7830 * hash = PRF( master, finished_label,
7831 * Hash( handshake ) )[0.11]
7832 */
7833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007835 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7836 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007837#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007838
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007839 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007840 mbedtls_sha512_free( &sha512 );
7841#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007842
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007843 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007844 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007847
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007848 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007851}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852#endif /* MBEDTLS_SHA512_C */
7853#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007856{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007858
7859 /*
7860 * Free our handshake params
7861 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007862 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007864 ssl->handshake = NULL;
7865
7866 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007867 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007868 */
7869 if( ssl->transform )
7870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871 mbedtls_ssl_transform_free( ssl->transform );
7872 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007873 }
7874 ssl->transform = ssl->transform_negotiate;
7875 ssl->transform_negotiate = NULL;
7876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007878}
7879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007881{
7882 int resume = ssl->handshake->resume;
7883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886#if defined(MBEDTLS_SSL_RENEGOTIATION)
7887 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007890 ssl->renego_records_seen = 0;
7891 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007892#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007893
7894 /*
7895 * Free the previous session and switch in the current one
7896 */
Paul Bakker0a597072012-09-25 21:55:46 +00007897 if( ssl->session )
7898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007900 /* RFC 7366 3.1: keep the EtM state */
7901 ssl->session_negotiate->encrypt_then_mac =
7902 ssl->session->encrypt_then_mac;
7903#endif
7904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905 mbedtls_ssl_session_free( ssl->session );
7906 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007907 }
7908 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007909 ssl->session_negotiate = NULL;
7910
Paul Bakker0a597072012-09-25 21:55:46 +00007911 /*
7912 * Add cache entry
7913 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007914 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007915 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007916 resume == 0 )
7917 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007918 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007920 }
Paul Bakker0a597072012-09-25 21:55:46 +00007921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007923 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007924 ssl->handshake->flight != NULL )
7925 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007926 /* Cancel handshake timer */
7927 ssl_set_timer( ssl, 0 );
7928
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007929 /* Keep last flight around in case we need to resend it:
7930 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007931 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007932 }
7933 else
7934#endif
7935 ssl_handshake_wrapup_free_hs_transform( ssl );
7936
Paul Bakker48916f92012-09-16 19:57:18 +00007937 ssl->state++;
7938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007940}
7941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007942int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007943{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007944 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007947
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007948 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007949
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007950 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007951
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007952 /*
7953 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7954 * may define some other value. Currently (early 2016), no defined
7955 * ciphersuite does this (and this is unlikely to change as activity has
7956 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7957 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007961 ssl->verify_data_len = hash_len;
7962 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007963#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007964
Paul Bakker5121ce52009-01-03 21:22:43 +00007965 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7967 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007968
7969 /*
7970 * In case of session resuming, invert the client and server
7971 * ChangeCipherSpec messages order.
7972 */
Paul Bakker0a597072012-09-25 21:55:46 +00007973 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007976 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007978#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007980 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007982#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007983 }
7984 else
7985 ssl->state++;
7986
Paul Bakker48916f92012-09-16 19:57:18 +00007987 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007988 * Switch to our negotiated transform and session parameters for outbound
7989 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007991 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007994 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007995 {
7996 unsigned char i;
7997
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007998 /* Remember current epoch settings for resending */
7999 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01008000 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008001
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008002 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008003 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008004
8005 /* Increment epoch */
8006 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008007 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008008 break;
8009
8010 /* The loop goes to its end iff the counter is wrapping */
8011 if( i == 0 )
8012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8014 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008015 }
8016 }
8017 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01008019 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008020
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008021 ssl->transform_out = ssl->transform_negotiate;
8022 ssl->session_out = ssl->session_negotiate;
8023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008024#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8025 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008027 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008029 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8030 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008031 }
8032 }
8033#endif
8034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008035#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008036 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008038#endif
8039
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008040 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008041 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008042 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008043 return( ret );
8044 }
8045
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008046#if defined(MBEDTLS_SSL_PROTO_DTLS)
8047 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8048 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8049 {
8050 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8051 return( ret );
8052 }
8053#endif
8054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008056
8057 return( 0 );
8058}
8059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008060#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008061#define SSL_MAX_HASH_LEN 36
8062#else
8063#define SSL_MAX_HASH_LEN 12
8064#endif
8065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008066int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008067{
Paul Bakker23986e52011-04-24 08:57:21 +00008068 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008069 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008070 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008073
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008074 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008075
Hanno Becker327c93b2018-08-15 13:56:18 +01008076 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008078 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008079 return( ret );
8080 }
8081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008082 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008085 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8086 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008087 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008088 }
8089
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008090 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008091#if defined(MBEDTLS_SSL_PROTO_SSL3)
8092 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008093 hash_len = 36;
8094 else
8095#endif
8096 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008098 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8099 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008102 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8103 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008104 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008105 }
8106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008107 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008108 buf, hash_len ) != 0 )
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 defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008117 ssl->verify_data_len = hash_len;
8118 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008119#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008120
Paul Bakker0a597072012-09-25 21:55:46 +00008121 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008124 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008125 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008126#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008127#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008128 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008130#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008131 }
8132 else
8133 ssl->state++;
8134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008136 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008137 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008138#endif
8139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008141
8142 return( 0 );
8143}
8144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008146{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008147 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8150 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8151 mbedtls_md5_init( &handshake->fin_md5 );
8152 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008153 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8154 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008155#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008156#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8157#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008158#if defined(MBEDTLS_USE_PSA_CRYPTO)
8159 handshake->fin_sha256_psa = psa_hash_operation_init();
8160 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8161#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008162 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008163 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008164#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008165#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008166#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008167#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008168 handshake->fin_sha384_psa = psa_hash_operation_init();
8169 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008170#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008171 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008172 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
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#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008176
8177 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008178
8179#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8180 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8181 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8182#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008184#if defined(MBEDTLS_DHM_C)
8185 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008186#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008187#if defined(MBEDTLS_ECDH_C)
8188 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008189#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008190#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008191 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008192#if defined(MBEDTLS_SSL_CLI_C)
8193 handshake->ecjpake_cache = NULL;
8194 handshake->ecjpake_cache_len = 0;
8195#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008196#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008197
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008198#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008199 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008200#endif
8201
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008202#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8203 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8204#endif
Hanno Becker75173122019-02-06 16:18:31 +00008205
8206#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8207 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8208 mbedtls_pk_init( &handshake->peer_pubkey );
8209#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008210}
8211
Hanno Beckera18d1322018-01-03 14:27:32 +00008212void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008213{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008214 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008216 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8217 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008218
Hanno Beckerd56ed242018-01-03 15:32:51 +00008219#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220 mbedtls_md_init( &transform->md_ctx_enc );
8221 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008222#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008223}
8224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008225void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008226{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008227 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008228}
8229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008230static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008231{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008232 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008233 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008234 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008235 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008236 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008237 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008238 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008239
8240 /*
8241 * Either the pointers are now NULL or cleared properly and can be freed.
8242 * Now allocate missing structures.
8243 */
8244 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008245 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008246 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008247 }
Paul Bakker48916f92012-09-16 19:57:18 +00008248
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008249 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008250 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008251 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008252 }
Paul Bakker48916f92012-09-16 19:57:18 +00008253
Paul Bakker82788fb2014-10-20 13:59:19 +02008254 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008255 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008256 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008257 }
Paul Bakker48916f92012-09-16 19:57:18 +00008258
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008259 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008260 if( ssl->handshake == NULL ||
8261 ssl->transform_negotiate == NULL ||
8262 ssl->session_negotiate == NULL )
8263 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008266 mbedtls_free( ssl->handshake );
8267 mbedtls_free( ssl->transform_negotiate );
8268 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008269
8270 ssl->handshake = NULL;
8271 ssl->transform_negotiate = NULL;
8272 ssl->session_negotiate = NULL;
8273
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008274 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008275 }
8276
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008277 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008278 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008279 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008280 ssl_handshake_params_init( ssl->handshake );
8281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008282#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008283 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8284 {
8285 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008286
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008287 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8288 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8289 else
8290 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008291
8292 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008293 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008294#endif
8295
Paul Bakker48916f92012-09-16 19:57:18 +00008296 return( 0 );
8297}
8298
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008299#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008300/* Dummy cookie callbacks for defaults */
8301static int ssl_cookie_write_dummy( void *ctx,
8302 unsigned char **p, unsigned char *end,
8303 const unsigned char *cli_id, size_t cli_id_len )
8304{
8305 ((void) ctx);
8306 ((void) p);
8307 ((void) end);
8308 ((void) cli_id);
8309 ((void) cli_id_len);
8310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008311 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008312}
8313
8314static int ssl_cookie_check_dummy( void *ctx,
8315 const unsigned char *cookie, size_t cookie_len,
8316 const unsigned char *cli_id, size_t cli_id_len )
8317{
8318 ((void) ctx);
8319 ((void) cookie);
8320 ((void) cookie_len);
8321 ((void) cli_id);
8322 ((void) cli_id_len);
8323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008324 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008325}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008326#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008327
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008328/* Once ssl->out_hdr as the address of the beginning of the
8329 * next outgoing record is set, deduce the other pointers.
8330 *
8331 * Note: For TLS, we save the implicit record sequence number
8332 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8333 * and the caller has to make sure there's space for this.
8334 */
8335
8336static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8337 mbedtls_ssl_transform *transform )
8338{
8339#if defined(MBEDTLS_SSL_PROTO_DTLS)
8340 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8341 {
8342 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008343#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008344 ssl->out_cid = ssl->out_ctr + 8;
8345 ssl->out_len = ssl->out_cid;
8346 if( transform != NULL )
8347 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008348#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008349 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008350#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008351 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008352 }
8353 else
8354#endif
8355 {
8356 ssl->out_ctr = ssl->out_hdr - 8;
8357 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008358#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008359 ssl->out_cid = ssl->out_len;
8360#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008361 ssl->out_iv = ssl->out_hdr + 5;
8362 }
8363
8364 /* Adjust out_msg to make space for explicit IV, if used. */
8365 if( transform != NULL &&
8366 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8367 {
8368 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8369 }
8370 else
8371 ssl->out_msg = ssl->out_iv;
8372}
8373
8374/* Once ssl->in_hdr as the address of the beginning of the
8375 * next incoming record is set, deduce the other pointers.
8376 *
8377 * Note: For TLS, we save the implicit record sequence number
8378 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8379 * and the caller has to make sure there's space for this.
8380 */
8381
Hanno Becker79594fd2019-05-08 09:38:41 +01008382static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008383{
Hanno Becker79594fd2019-05-08 09:38:41 +01008384 /* This function sets the pointers to match the case
8385 * of unprotected TLS/DTLS records, with both ssl->in_iv
8386 * and ssl->in_msg pointing to the beginning of the record
8387 * content.
8388 *
8389 * When decrypting a protected record, ssl->in_msg
8390 * will be shifted to point to the beginning of the
8391 * record plaintext.
8392 */
8393
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008394#if defined(MBEDTLS_SSL_PROTO_DTLS)
8395 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8396 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008397 /* This sets the header pointers to match records
8398 * without CID. When we receive a record containing
8399 * a CID, the fields are shifted accordingly in
8400 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008401 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008402#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008403 ssl->in_cid = ssl->in_ctr + 8;
8404 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008405#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008406 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008407#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008408 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008409 }
8410 else
8411#endif
8412 {
8413 ssl->in_ctr = ssl->in_hdr - 8;
8414 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008415#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008416 ssl->in_cid = ssl->in_len;
8417#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008418 ssl->in_iv = ssl->in_hdr + 5;
8419 }
8420
Hanno Becker79594fd2019-05-08 09:38:41 +01008421 /* This will be adjusted at record decryption time. */
8422 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008423}
8424
Paul Bakker5121ce52009-01-03 21:22:43 +00008425/*
8426 * Initialize an SSL context
8427 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008428void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8429{
8430 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8431}
8432
8433/*
8434 * Setup an SSL context
8435 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008436
8437static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8438{
8439 /* Set the incoming and outgoing record pointers. */
8440#if defined(MBEDTLS_SSL_PROTO_DTLS)
8441 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8442 {
8443 ssl->out_hdr = ssl->out_buf;
8444 ssl->in_hdr = ssl->in_buf;
8445 }
8446 else
8447#endif /* MBEDTLS_SSL_PROTO_DTLS */
8448 {
8449 ssl->out_hdr = ssl->out_buf + 8;
8450 ssl->in_hdr = ssl->in_buf + 8;
8451 }
8452
8453 /* Derive other internal pointers. */
8454 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008455 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008456}
8457
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008458int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008459 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008460{
Paul Bakker48916f92012-09-16 19:57:18 +00008461 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008462
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008463 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008464
8465 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008466 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008467 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008468
8469 /* Set to NULL in case of an error condition */
8470 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008471
Angus Grattond8213d02016-05-25 20:56:48 +10008472 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8473 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008474 {
Angus Grattond8213d02016-05-25 20:56:48 +10008475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008476 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008477 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008478 }
8479
8480 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8481 if( ssl->out_buf == NULL )
8482 {
8483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008484 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008485 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008486 }
8487
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008488 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008489
Paul Bakker48916f92012-09-16 19:57:18 +00008490 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008491 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008492
8493 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008494
8495error:
8496 mbedtls_free( ssl->in_buf );
8497 mbedtls_free( ssl->out_buf );
8498
8499 ssl->conf = NULL;
8500
8501 ssl->in_buf = NULL;
8502 ssl->out_buf = NULL;
8503
8504 ssl->in_hdr = NULL;
8505 ssl->in_ctr = NULL;
8506 ssl->in_len = NULL;
8507 ssl->in_iv = NULL;
8508 ssl->in_msg = NULL;
8509
8510 ssl->out_hdr = NULL;
8511 ssl->out_ctr = NULL;
8512 ssl->out_len = NULL;
8513 ssl->out_iv = NULL;
8514 ssl->out_msg = NULL;
8515
k-stachowiak9f7798e2018-07-31 16:52:32 +02008516 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008517}
8518
8519/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008520 * Reset an initialized and used SSL context for re-use while retaining
8521 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008522 *
8523 * If partial is non-zero, keep data in the input buffer and client ID.
8524 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008525 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008526static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008527{
Paul Bakker48916f92012-09-16 19:57:18 +00008528 int ret;
8529
Hanno Becker7e772132018-08-10 12:38:21 +01008530#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8531 !defined(MBEDTLS_SSL_SRV_C)
8532 ((void) partial);
8533#endif
8534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008535 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008536
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008537 /* Cancel any possibly running timer */
8538 ssl_set_timer( ssl, 0 );
8539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008540#if defined(MBEDTLS_SSL_RENEGOTIATION)
8541 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008542 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008543
8544 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008545 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8546 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008547#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008548 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008549
Paul Bakker7eb013f2011-10-06 12:37:39 +00008550 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008551 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008552
8553 ssl->in_msgtype = 0;
8554 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008555#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008556 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008557 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008558#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008559#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008560 ssl_dtls_replay_reset( ssl );
8561#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008562
8563 ssl->in_hslen = 0;
8564 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008565
8566 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008567
8568 ssl->out_msgtype = 0;
8569 ssl->out_msglen = 0;
8570 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008571#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8572 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008573 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008574#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008575
Hanno Becker19859472018-08-06 09:40:20 +01008576 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8577
Paul Bakker48916f92012-09-16 19:57:18 +00008578 ssl->transform_in = NULL;
8579 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008580
Hanno Becker78640902018-08-13 16:35:15 +01008581 ssl->session_in = NULL;
8582 ssl->session_out = NULL;
8583
Angus Grattond8213d02016-05-25 20:56:48 +10008584 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008585
8586#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008587 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008588#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8589 {
8590 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008591 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008592 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008594#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8595 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8598 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008600 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8601 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008602 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008603 }
8604#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008605
Paul Bakker48916f92012-09-16 19:57:18 +00008606 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008608 mbedtls_ssl_transform_free( ssl->transform );
8609 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008610 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008611 }
Paul Bakker48916f92012-09-16 19:57:18 +00008612
Paul Bakkerc0463502013-02-14 11:19:38 +01008613 if( ssl->session )
8614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008615 mbedtls_ssl_session_free( ssl->session );
8616 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008617 ssl->session = NULL;
8618 }
8619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008620#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008621 ssl->alpn_chosen = NULL;
8622#endif
8623
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008624#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008625#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008626 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008627#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008628 {
8629 mbedtls_free( ssl->cli_id );
8630 ssl->cli_id = NULL;
8631 ssl->cli_id_len = 0;
8632 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008633#endif
8634
Paul Bakker48916f92012-09-16 19:57:18 +00008635 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8636 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008637
8638 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008639}
8640
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008641/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008642 * Reset an initialized and used SSL context for re-use while retaining
8643 * all application-set variables, function pointers and data.
8644 */
8645int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8646{
8647 return( ssl_session_reset_int( ssl, 0 ) );
8648}
8649
8650/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008651 * SSL set accessors
8652 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008653void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008654{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008655 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008656}
8657
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008658void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008659{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008660 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008661}
8662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008663#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008664void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008665{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008666 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008667}
8668#endif
8669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008670#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008671void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008672{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008673 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008674}
8675#endif
8676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008677#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008678
Hanno Becker1841b0a2018-08-24 11:13:57 +01008679void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8680 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008681{
8682 ssl->disable_datagram_packing = !allow_packing;
8683}
8684
8685void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8686 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008687{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008688 conf->hs_timeout_min = min;
8689 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008690}
8691#endif
8692
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008693void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008694{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008695 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008696}
8697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008698#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008699void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008700 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008701 void *p_vrfy )
8702{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008703 conf->f_vrfy = f_vrfy;
8704 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008705}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008706#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008707
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008708void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008709 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008710 void *p_rng )
8711{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008712 conf->f_rng = f_rng;
8713 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008714}
8715
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008716void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008717 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008718 void *p_dbg )
8719{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008720 conf->f_dbg = f_dbg;
8721 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008722}
8723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008724void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008725 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008726 mbedtls_ssl_send_t *f_send,
8727 mbedtls_ssl_recv_t *f_recv,
8728 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008729{
8730 ssl->p_bio = p_bio;
8731 ssl->f_send = f_send;
8732 ssl->f_recv = f_recv;
8733 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008734}
8735
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008736#if defined(MBEDTLS_SSL_PROTO_DTLS)
8737void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8738{
8739 ssl->mtu = mtu;
8740}
8741#endif
8742
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008743void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008744{
8745 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008746}
8747
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008748void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8749 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008750 mbedtls_ssl_set_timer_t *f_set_timer,
8751 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008752{
8753 ssl->p_timer = p_timer;
8754 ssl->f_set_timer = f_set_timer;
8755 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008756
8757 /* Make sure we start with no timer running */
8758 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008759}
8760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008761#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008762void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008763 void *p_cache,
8764 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8765 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008766{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008767 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008768 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008769 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008770}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008771#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008773#if defined(MBEDTLS_SSL_CLI_C)
8774int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008775{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008776 int ret;
8777
8778 if( ssl == NULL ||
8779 session == NULL ||
8780 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008781 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008783 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008784 }
8785
Hanno Becker52055ae2019-02-06 14:30:46 +00008786 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8787 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008788 return( ret );
8789
Paul Bakker0a597072012-09-25 21:55:46 +00008790 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008791
8792 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008793}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008794#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008795
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008796void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008797 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008798{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008799 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8800 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8801 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8802 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008803}
8804
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008805void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008806 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008807 int major, int minor )
8808{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008809 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008810 return;
8811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008813 return;
8814
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008815 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008816}
8817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008818#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008819void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008820 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008821{
8822 conf->cert_profile = profile;
8823}
8824
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008825/* Append a new keycert entry to a (possibly empty) list */
8826static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8827 mbedtls_x509_crt *cert,
8828 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008829{
niisato8ee24222018-06-25 19:05:48 +09008830 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008831
niisato8ee24222018-06-25 19:05:48 +09008832 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8833 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008834 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008835
niisato8ee24222018-06-25 19:05:48 +09008836 new_cert->cert = cert;
8837 new_cert->key = key;
8838 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008839
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008840 /* Update head is the list was null, else add to the end */
8841 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008842 {
niisato8ee24222018-06-25 19:05:48 +09008843 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008844 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008845 else
8846 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008847 mbedtls_ssl_key_cert *cur = *head;
8848 while( cur->next != NULL )
8849 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008850 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008851 }
8852
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008853 return( 0 );
8854}
8855
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008856int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008857 mbedtls_x509_crt *own_cert,
8858 mbedtls_pk_context *pk_key )
8859{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008860 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008861}
8862
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008863void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008864 mbedtls_x509_crt *ca_chain,
8865 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008866{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008867 conf->ca_chain = ca_chain;
8868 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008869
8870#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8871 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8872 * cannot be used together. */
8873 conf->f_ca_cb = NULL;
8874 conf->p_ca_cb = NULL;
8875#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008876}
Hanno Becker5adaad92019-03-27 16:54:37 +00008877
8878#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8879void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008880 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008881 void *p_ca_cb )
8882{
8883 conf->f_ca_cb = f_ca_cb;
8884 conf->p_ca_cb = p_ca_cb;
8885
8886 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8887 * cannot be used together. */
8888 conf->ca_chain = NULL;
8889 conf->ca_crl = NULL;
8890}
8891#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008892#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008893
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008894#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8895int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8896 mbedtls_x509_crt *own_cert,
8897 mbedtls_pk_context *pk_key )
8898{
8899 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8900 own_cert, pk_key ) );
8901}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008902
8903void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8904 mbedtls_x509_crt *ca_chain,
8905 mbedtls_x509_crl *ca_crl )
8906{
8907 ssl->handshake->sni_ca_chain = ca_chain;
8908 ssl->handshake->sni_ca_crl = ca_crl;
8909}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008910
8911void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8912 int authmode )
8913{
8914 ssl->handshake->sni_authmode = authmode;
8915}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008916#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8917
Hanno Becker8927c832019-04-03 12:52:50 +01008918#if defined(MBEDTLS_X509_CRT_PARSE_C)
8919void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8920 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8921 void *p_vrfy )
8922{
8923 ssl->f_vrfy = f_vrfy;
8924 ssl->p_vrfy = p_vrfy;
8925}
8926#endif
8927
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008928#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008929/*
8930 * Set EC J-PAKE password for current handshake
8931 */
8932int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8933 const unsigned char *pw,
8934 size_t pw_len )
8935{
8936 mbedtls_ecjpake_role role;
8937
Janos Follath8eb64132016-06-03 15:40:57 +01008938 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008939 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8940
8941 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8942 role = MBEDTLS_ECJPAKE_SERVER;
8943 else
8944 role = MBEDTLS_ECJPAKE_CLIENT;
8945
8946 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8947 role,
8948 MBEDTLS_MD_SHA256,
8949 MBEDTLS_ECP_DP_SECP256R1,
8950 pw, pw_len ) );
8951}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008952#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008954#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008955
8956static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8957{
8958 /* Remove reference to existing PSK, if any. */
8959#if defined(MBEDTLS_USE_PSA_CRYPTO)
8960 if( conf->psk_opaque != 0 )
8961 {
8962 /* The maintenance of the PSK key slot is the
8963 * user's responsibility. */
8964 conf->psk_opaque = 0;
8965 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008966 /* This and the following branch should never
8967 * be taken simultaenously as we maintain the
8968 * invariant that raw and opaque PSKs are never
8969 * configured simultaneously. As a safeguard,
8970 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008971#endif /* MBEDTLS_USE_PSA_CRYPTO */
8972 if( conf->psk != NULL )
8973 {
8974 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8975
8976 mbedtls_free( conf->psk );
8977 conf->psk = NULL;
8978 conf->psk_len = 0;
8979 }
8980
8981 /* Remove reference to PSK identity, if any. */
8982 if( conf->psk_identity != NULL )
8983 {
8984 mbedtls_free( conf->psk_identity );
8985 conf->psk_identity = NULL;
8986 conf->psk_identity_len = 0;
8987 }
8988}
8989
Hanno Becker7390c712018-11-15 13:33:04 +00008990/* This function assumes that PSK identity in the SSL config is unset.
8991 * It checks that the provided identity is well-formed and attempts
8992 * to make a copy of it in the SSL config.
8993 * On failure, the PSK identity in the config remains unset. */
8994static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8995 unsigned char const *psk_identity,
8996 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008997{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008998 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008999 if( psk_identity == NULL ||
9000 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009001 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009002 {
9003 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9004 }
9005
Hanno Becker7390c712018-11-15 13:33:04 +00009006 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
9007 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009008 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02009009
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009010 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009011 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02009012
9013 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02009014}
9015
Hanno Becker7390c712018-11-15 13:33:04 +00009016int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
9017 const unsigned char *psk, size_t psk_len,
9018 const unsigned char *psk_identity, size_t psk_identity_len )
9019{
9020 int ret;
9021 /* Remove opaque/raw PSK + PSK Identity */
9022 ssl_conf_remove_psk( conf );
9023
9024 /* Check and set raw PSK */
9025 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
9026 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9027 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
9028 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9029 conf->psk_len = psk_len;
9030 memcpy( conf->psk, psk, conf->psk_len );
9031
9032 /* Check and set PSK Identity */
9033 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
9034 if( ret != 0 )
9035 ssl_conf_remove_psk( conf );
9036
9037 return( ret );
9038}
9039
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009040static void ssl_remove_psk( mbedtls_ssl_context *ssl )
9041{
9042#if defined(MBEDTLS_USE_PSA_CRYPTO)
9043 if( ssl->handshake->psk_opaque != 0 )
9044 {
9045 ssl->handshake->psk_opaque = 0;
9046 }
9047 else
9048#endif /* MBEDTLS_USE_PSA_CRYPTO */
9049 if( ssl->handshake->psk != NULL )
9050 {
9051 mbedtls_platform_zeroize( ssl->handshake->psk,
9052 ssl->handshake->psk_len );
9053 mbedtls_free( ssl->handshake->psk );
9054 ssl->handshake->psk_len = 0;
9055 }
9056}
9057
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009058int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9059 const unsigned char *psk, size_t psk_len )
9060{
9061 if( psk == NULL || ssl->handshake == NULL )
9062 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9063
9064 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9065 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9066
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009067 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009068
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009069 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009070 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009071
9072 ssl->handshake->psk_len = psk_len;
9073 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
9074
9075 return( 0 );
9076}
9077
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009078#if defined(MBEDTLS_USE_PSA_CRYPTO)
9079int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009080 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009081 const unsigned char *psk_identity,
9082 size_t psk_identity_len )
9083{
Hanno Becker7390c712018-11-15 13:33:04 +00009084 int ret;
9085 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009086 ssl_conf_remove_psk( conf );
9087
Hanno Becker7390c712018-11-15 13:33:04 +00009088 /* Check and set opaque PSK */
9089 if( psk_slot == 0 )
9090 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009091 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009092
9093 /* Check and set PSK Identity */
9094 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9095 psk_identity_len );
9096 if( ret != 0 )
9097 ssl_conf_remove_psk( conf );
9098
9099 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009100}
9101
9102int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009103 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009104{
9105 if( psk_slot == 0 || ssl->handshake == NULL )
9106 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9107
9108 ssl_remove_psk( ssl );
9109 ssl->handshake->psk_opaque = psk_slot;
9110 return( 0 );
9111}
9112#endif /* MBEDTLS_USE_PSA_CRYPTO */
9113
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009114void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009115 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009116 size_t),
9117 void *p_psk )
9118{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009119 conf->f_psk = f_psk;
9120 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009121}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009122#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009123
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009124#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009125
9126#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009127int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009128{
9129 int ret;
9130
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009131 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9132 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9133 {
9134 mbedtls_mpi_free( &conf->dhm_P );
9135 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009136 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009137 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009138
9139 return( 0 );
9140}
Hanno Becker470a8c42017-10-04 15:28:46 +01009141#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009142
Hanno Beckera90658f2017-10-04 15:29:08 +01009143int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9144 const unsigned char *dhm_P, size_t P_len,
9145 const unsigned char *dhm_G, size_t G_len )
9146{
9147 int ret;
9148
9149 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9150 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9151 {
9152 mbedtls_mpi_free( &conf->dhm_P );
9153 mbedtls_mpi_free( &conf->dhm_G );
9154 return( ret );
9155 }
9156
9157 return( 0 );
9158}
Paul Bakker5121ce52009-01-03 21:22:43 +00009159
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009160int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009161{
9162 int ret;
9163
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009164 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9165 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9166 {
9167 mbedtls_mpi_free( &conf->dhm_P );
9168 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009169 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009170 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009171
9172 return( 0 );
9173}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009174#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009175
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009176#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9177/*
9178 * Set the minimum length for Diffie-Hellman parameters
9179 */
9180void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9181 unsigned int bitlen )
9182{
9183 conf->dhm_min_bitlen = bitlen;
9184}
9185#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9186
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009187#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009188/*
9189 * Set allowed/preferred hashes for handshake signatures
9190 */
9191void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9192 const int *hashes )
9193{
9194 conf->sig_hashes = hashes;
9195}
Hanno Becker947194e2017-04-07 13:25:49 +01009196#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009197
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009198#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009199/*
9200 * Set the allowed elliptic curves
9201 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009202void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009203 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009204{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009205 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009206}
Hanno Becker947194e2017-04-07 13:25:49 +01009207#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009208
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009209#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009210int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009211{
Hanno Becker947194e2017-04-07 13:25:49 +01009212 /* Initialize to suppress unnecessary compiler warning */
9213 size_t hostname_len = 0;
9214
9215 /* Check if new hostname is valid before
9216 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009217 if( hostname != NULL )
9218 {
9219 hostname_len = strlen( hostname );
9220
9221 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9222 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9223 }
9224
9225 /* Now it's clear that we will overwrite the old hostname,
9226 * so we can free it safely */
9227
9228 if( ssl->hostname != NULL )
9229 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009230 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009231 mbedtls_free( ssl->hostname );
9232 }
9233
9234 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009235
Paul Bakker5121ce52009-01-03 21:22:43 +00009236 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009237 {
9238 ssl->hostname = NULL;
9239 }
9240 else
9241 {
9242 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009243 if( ssl->hostname == NULL )
9244 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009245
Hanno Becker947194e2017-04-07 13:25:49 +01009246 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009247
Hanno Becker947194e2017-04-07 13:25:49 +01009248 ssl->hostname[hostname_len] = '\0';
9249 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009250
9251 return( 0 );
9252}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009253#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009254
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009255#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009256void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009257 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009258 const unsigned char *, size_t),
9259 void *p_sni )
9260{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009261 conf->f_sni = f_sni;
9262 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009263}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009264#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009266#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009267int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009268{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009269 size_t cur_len, tot_len;
9270 const char **p;
9271
9272 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009273 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9274 * MUST NOT be truncated."
9275 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009276 */
9277 tot_len = 0;
9278 for( p = protos; *p != NULL; p++ )
9279 {
9280 cur_len = strlen( *p );
9281 tot_len += cur_len;
9282
9283 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009284 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009285 }
9286
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009287 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009288
9289 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009290}
9291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009292const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009293{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009294 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009295}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009297
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009298void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009299{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009300 conf->max_major_ver = major;
9301 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009302}
9303
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009304void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009305{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009306 conf->min_major_ver = major;
9307 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009308}
9309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009311void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009312{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009313 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009314}
9315#endif
9316
Janos Follath088ce432017-04-10 12:42:31 +01009317#if defined(MBEDTLS_SSL_SRV_C)
9318void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9319 char cert_req_ca_list )
9320{
9321 conf->cert_req_ca_list = cert_req_ca_list;
9322}
9323#endif
9324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009325#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009326void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009327{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009328 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009329}
9330#endif
9331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009332#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009333void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009334{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009335 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009336}
9337#endif
9338
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009339#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009340void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009341{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009342 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009343}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009344#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009346#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009347int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009348{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009349 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009350 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009352 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009353 }
9354
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009355 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009356
9357 return( 0 );
9358}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009361#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009362void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009363{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009364 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009365}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009366#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009368#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009369void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009370{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009371 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009372}
9373#endif
9374
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009375void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009376{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009377 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009378}
9379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009380#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009381void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009382{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009383 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009384}
9385
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009386void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009387{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009388 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009389}
9390
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009391void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009392 const unsigned char period[8] )
9393{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009394 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009395}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009396#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009398#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009399#if defined(MBEDTLS_SSL_CLI_C)
9400void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009401{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009402 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009403}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009404#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009405
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009406#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009407void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9408 mbedtls_ssl_ticket_write_t *f_ticket_write,
9409 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9410 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009411{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009412 conf->f_ticket_write = f_ticket_write;
9413 conf->f_ticket_parse = f_ticket_parse;
9414 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009415}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009416#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009417#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009418
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009419#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9420void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9421 mbedtls_ssl_export_keys_t *f_export_keys,
9422 void *p_export_keys )
9423{
9424 conf->f_export_keys = f_export_keys;
9425 conf->p_export_keys = p_export_keys;
9426}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009427
9428void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9429 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9430 void *p_export_keys )
9431{
9432 conf->f_export_keys_ext = f_export_keys_ext;
9433 conf->p_export_keys = p_export_keys;
9434}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009435#endif
9436
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009437#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009438void mbedtls_ssl_conf_async_private_cb(
9439 mbedtls_ssl_config *conf,
9440 mbedtls_ssl_async_sign_t *f_async_sign,
9441 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9442 mbedtls_ssl_async_resume_t *f_async_resume,
9443 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009444 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009445{
9446 conf->f_async_sign_start = f_async_sign;
9447 conf->f_async_decrypt_start = f_async_decrypt;
9448 conf->f_async_resume = f_async_resume;
9449 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009450 conf->p_async_config_data = async_config_data;
9451}
9452
Gilles Peskine8f97af72018-04-26 11:46:10 +02009453void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9454{
9455 return( conf->p_async_config_data );
9456}
9457
Gilles Peskine1febfef2018-04-30 11:54:39 +02009458void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009459{
9460 if( ssl->handshake == NULL )
9461 return( NULL );
9462 else
9463 return( ssl->handshake->user_async_ctx );
9464}
9465
Gilles Peskine1febfef2018-04-30 11:54:39 +02009466void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009467 void *ctx )
9468{
9469 if( ssl->handshake != NULL )
9470 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009471}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009472#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009473
Paul Bakker5121ce52009-01-03 21:22:43 +00009474/*
9475 * SSL get accessors
9476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009478{
9479 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9480}
9481
Hanno Becker8b170a02017-10-10 11:51:19 +01009482int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9483{
9484 /*
9485 * Case A: We're currently holding back
9486 * a message for further processing.
9487 */
9488
9489 if( ssl->keep_current_message == 1 )
9490 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009492 return( 1 );
9493 }
9494
9495 /*
9496 * Case B: Further records are pending in the current datagram.
9497 */
9498
9499#if defined(MBEDTLS_SSL_PROTO_DTLS)
9500 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9501 ssl->in_left > ssl->next_record_offset )
9502 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009503 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009504 return( 1 );
9505 }
9506#endif /* MBEDTLS_SSL_PROTO_DTLS */
9507
9508 /*
9509 * Case C: A handshake message is being processed.
9510 */
9511
Hanno Becker8b170a02017-10-10 11:51:19 +01009512 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9513 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009514 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009515 return( 1 );
9516 }
9517
9518 /*
9519 * Case D: An application data message is being processed
9520 */
9521 if( ssl->in_offt != NULL )
9522 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009523 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009524 return( 1 );
9525 }
9526
9527 /*
9528 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009529 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009530 * we implement support for multiple alerts in single records.
9531 */
9532
9533 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9534 return( 0 );
9535}
9536
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009537uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009538{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009539 if( ssl->session != NULL )
9540 return( ssl->session->verify_result );
9541
9542 if( ssl->session_negotiate != NULL )
9543 return( ssl->session_negotiate->verify_result );
9544
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009545 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009546}
9547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009548const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009549{
Paul Bakker926c8e42013-03-06 10:23:34 +01009550 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009551 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009553 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009554}
9555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009557{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009559 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009560 {
9561 switch( ssl->minor_ver )
9562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009564 return( "DTLSv1.0" );
9565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009567 return( "DTLSv1.2" );
9568
9569 default:
9570 return( "unknown (DTLS)" );
9571 }
9572 }
9573#endif
9574
Paul Bakker43ca69c2011-01-15 17:35:19 +00009575 switch( ssl->minor_ver )
9576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009577 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009578 return( "SSLv3.0" );
9579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009580 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009581 return( "TLSv1.0" );
9582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009583 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009584 return( "TLSv1.1" );
9585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009586 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009587 return( "TLSv1.2" );
9588
Paul Bakker43ca69c2011-01-15 17:35:19 +00009589 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009590 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009591 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009592}
9593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009594int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009595{
Hanno Becker3136ede2018-08-17 15:28:19 +01009596 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009597 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009598 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009599
Hanno Becker5903de42019-05-03 14:46:38 +01009600 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9601
Hanno Becker78640902018-08-13 16:35:15 +01009602 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009603 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009605#if defined(MBEDTLS_ZLIB_SUPPORT)
9606 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9607 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009608#endif
9609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009610 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009612 case MBEDTLS_MODE_GCM:
9613 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009614 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009615 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009616 transform_expansion = transform->minlen;
9617 break;
9618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009619 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009620
9621 block_size = mbedtls_cipher_get_block_size(
9622 &transform->cipher_ctx_enc );
9623
Hanno Becker3136ede2018-08-17 15:28:19 +01009624 /* Expansion due to the addition of the MAC. */
9625 transform_expansion += transform->maclen;
9626
9627 /* Expansion due to the addition of CBC padding;
9628 * Theoretically up to 256 bytes, but we never use
9629 * more than the block size of the underlying cipher. */
9630 transform_expansion += block_size;
9631
9632 /* For TLS 1.1 or higher, an explicit IV is added
9633 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009634#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9635 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009636 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009637#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009638
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009639 break;
9640
9641 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009642 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009643 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009644 }
9645
Hanno Beckera0e20d02019-05-15 14:03:01 +01009646#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009647 if( transform->out_cid_len != 0 )
9648 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009649#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009650
Hanno Becker5903de42019-05-03 14:46:38 +01009651 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009652}
9653
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009654#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9655size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9656{
9657 size_t max_len;
9658
9659 /*
9660 * Assume mfl_code is correct since it was checked when set
9661 */
Angus Grattond8213d02016-05-25 20:56:48 +10009662 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009663
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009664 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009665 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009666 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009667 {
Angus Grattond8213d02016-05-25 20:56:48 +10009668 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009669 }
9670
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009671 /* During a handshake, use the value being negotiated */
9672 if( ssl->session_negotiate != NULL &&
9673 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9674 {
9675 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9676 }
9677
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009678 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009679}
9680#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9681
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009682#if defined(MBEDTLS_SSL_PROTO_DTLS)
9683static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9684{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009685 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9686 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9687 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9688 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9689 return ( 0 );
9690
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009691 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9692 return( ssl->mtu );
9693
9694 if( ssl->mtu == 0 )
9695 return( ssl->handshake->mtu );
9696
9697 return( ssl->mtu < ssl->handshake->mtu ?
9698 ssl->mtu : ssl->handshake->mtu );
9699}
9700#endif /* MBEDTLS_SSL_PROTO_DTLS */
9701
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009702int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9703{
9704 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9705
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009706#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9707 !defined(MBEDTLS_SSL_PROTO_DTLS)
9708 (void) ssl;
9709#endif
9710
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009711#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9712 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9713
9714 if( max_len > mfl )
9715 max_len = mfl;
9716#endif
9717
9718#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009719 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009720 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009721 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009722 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9723 const size_t overhead = (size_t) ret;
9724
9725 if( ret < 0 )
9726 return( ret );
9727
9728 if( mtu <= overhead )
9729 {
9730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9731 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9732 }
9733
9734 if( max_len > mtu - overhead )
9735 max_len = mtu - overhead;
9736 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009737#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009738
Hanno Becker0defedb2018-08-10 12:35:02 +01009739#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9740 !defined(MBEDTLS_SSL_PROTO_DTLS)
9741 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009742#endif
9743
9744 return( (int) max_len );
9745}
9746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009747#if defined(MBEDTLS_X509_CRT_PARSE_C)
9748const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009749{
9750 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009751 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009752
Hanno Beckere6824572019-02-07 13:18:46 +00009753#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009754 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009755#else
9756 return( NULL );
9757#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009758}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009759#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009761#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009762int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9763 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009764{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009765 if( ssl == NULL ||
9766 dst == NULL ||
9767 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009768 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009771 }
9772
Hanno Becker52055ae2019-02-06 14:30:46 +00009773 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009774}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009775#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009776
Paul Bakker5121ce52009-01-03 21:22:43 +00009777/*
Paul Bakker1961b702013-01-25 14:49:24 +01009778 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009779 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009780int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009781{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009782 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009783
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009784 if( ssl == NULL || ssl->conf == NULL )
9785 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009787#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009788 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009789 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009790#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009791#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009792 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009793 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009794#endif
9795
Paul Bakker1961b702013-01-25 14:49:24 +01009796 return( ret );
9797}
9798
9799/*
9800 * Perform the SSL handshake
9801 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009803{
9804 int ret = 0;
9805
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009806 if( ssl == NULL || ssl->conf == NULL )
9807 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009811 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009813 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009814
9815 if( ret != 0 )
9816 break;
9817 }
9818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009820
9821 return( ret );
9822}
9823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009824#if defined(MBEDTLS_SSL_RENEGOTIATION)
9825#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009826/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009827 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009828 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009830{
9831 int ret;
9832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009834
9835 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009836 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9837 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009838
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009839 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009840 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009841 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009842 return( ret );
9843 }
9844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009846
9847 return( 0 );
9848}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009850
9851/*
9852 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009853 * - any side: calling mbedtls_ssl_renegotiate(),
9854 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9855 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009856 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009857 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009858 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009860static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009861{
9862 int ret;
9863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009865
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009866 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9867 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009868
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009869 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9870 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009871#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009872 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009873 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009874 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009875 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009876 ssl->handshake->out_msg_seq = 1;
9877 else
9878 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009879 }
9880#endif
9881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009882 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9883 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009885 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009887 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009888 return( ret );
9889 }
9890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009892
9893 return( 0 );
9894}
9895
9896/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009897 * Renegotiate current connection on client,
9898 * or request renegotiation on server
9899 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009901{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009902 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009903
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009904 if( ssl == NULL || ssl->conf == NULL )
9905 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009907#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009908 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009909 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009911 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9912 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009914 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009915
9916 /* Did we already try/start sending HelloRequest? */
9917 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009918 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009919
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009920 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009921 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009922#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009924#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009925 /*
9926 * On client, either start the renegotiation process or,
9927 * if already in progress, continue the handshake
9928 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009929 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009931 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9932 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009933
9934 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009936 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009937 return( ret );
9938 }
9939 }
9940 else
9941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009942 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009944 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009945 return( ret );
9946 }
9947 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009948#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009949
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009950 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009951}
9952
9953/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009954 * Check record counters and renegotiate if they're above the limit.
9955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009956static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009957{
Andres AG2196c7f2016-12-15 17:01:16 +00009958 size_t ep_len = ssl_ep_len( ssl );
9959 int in_ctr_cmp;
9960 int out_ctr_cmp;
9961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009962 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9963 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009964 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009965 {
9966 return( 0 );
9967 }
9968
Andres AG2196c7f2016-12-15 17:01:16 +00009969 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9970 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009971 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009972 ssl->conf->renego_period + ep_len, 8 - ep_len );
9973
9974 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009975 {
9976 return( 0 );
9977 }
9978
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009980 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009981}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009982#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009983
9984/*
9985 * Receive application data decrypted from the SSL layer
9986 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009987int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009988{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009989 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009990 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009991
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009992 if( ssl == NULL || ssl->conf == NULL )
9993 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009997#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009998 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010000 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010001 return( ret );
10002
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010003 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010004 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010005 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010006 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010007 return( ret );
10008 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010009 }
10010#endif
10011
Hanno Becker4a810fb2017-05-24 16:27:30 +010010012 /*
10013 * Check if renegotiation is necessary and/or handshake is
10014 * in process. If yes, perform/continue, and fall through
10015 * if an unexpected packet is received while the client
10016 * is waiting for the ServerHello.
10017 *
10018 * (There is no equivalent to the last condition on
10019 * the server-side as it is not treated as within
10020 * a handshake while waiting for the ClientHello
10021 * after a renegotiation request.)
10022 */
10023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010024#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010025 ret = ssl_check_ctr_renegotiate( ssl );
10026 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10027 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010029 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010030 return( ret );
10031 }
10032#endif
10033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010034 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010036 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010037 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10038 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010040 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010041 return( ret );
10042 }
10043 }
10044
Hanno Beckere41158b2017-10-23 13:30:32 +010010045 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010046 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010047 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010048 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010049 if( ssl->f_get_timer != NULL &&
10050 ssl->f_get_timer( ssl->p_timer ) == -1 )
10051 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010052 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010053 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010054
Hanno Becker327c93b2018-08-15 13:56:18 +010010055 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010056 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010057 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10058 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010059
Hanno Becker4a810fb2017-05-24 16:27:30 +010010060 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10061 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010062 }
10063
10064 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010065 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010066 {
10067 /*
10068 * OpenSSL sends empty messages to randomize the IV
10069 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010070 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010072 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010073 return( 0 );
10074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010075 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010076 return( ret );
10077 }
10078 }
10079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010083
Hanno Becker4a810fb2017-05-24 16:27:30 +010010084 /*
10085 * - For client-side, expect SERVER_HELLO_REQUEST.
10086 * - For server-side, expect CLIENT_HELLO.
10087 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10088 */
10089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010090#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010091 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010092 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010093 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010096
10097 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010098#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010099 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010100 {
10101 continue;
10102 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010103#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010104 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010105 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010106#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010107
Hanno Becker4a810fb2017-05-24 16:27:30 +010010108#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010109 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010110 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010113
10114 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010116 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010117 {
10118 continue;
10119 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010121 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010122 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010123#endif /* MBEDTLS_SSL_SRV_C */
10124
Hanno Becker21df7f92017-10-17 11:03:26 +010010125#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010126 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010127 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10128 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10129 ssl->conf->allow_legacy_renegotiation ==
10130 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10131 {
10132 /*
10133 * Accept renegotiation request
10134 */
Paul Bakker48916f92012-09-16 19:57:18 +000010135
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010136 /* DTLS clients need to know renego is server-initiated */
10137#if defined(MBEDTLS_SSL_PROTO_DTLS)
10138 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10139 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10140 {
10141 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10142 }
10143#endif
10144 ret = ssl_start_renegotiation( ssl );
10145 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10146 ret != 0 )
10147 {
10148 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10149 return( ret );
10150 }
10151 }
10152 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010153#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010154 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010155 /*
10156 * Refuse renegotiation
10157 */
10158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010159 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010161#if defined(MBEDTLS_SSL_PROTO_SSL3)
10162 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010163 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010164 /* SSLv3 does not have a "no_renegotiation" warning, so
10165 we send a fatal alert and abort the connection. */
10166 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10167 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10168 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010169 }
10170 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010171#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10172#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10173 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10174 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010176 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10177 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10178 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010179 {
10180 return( ret );
10181 }
Paul Bakker48916f92012-09-16 19:57:18 +000010182 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010183 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010184#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10185 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10188 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010189 }
Paul Bakker48916f92012-09-16 19:57:18 +000010190 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010191
Hanno Becker90333da2017-10-10 11:27:13 +010010192 /* At this point, we don't know whether the renegotiation has been
10193 * completed or not. The cases to consider are the following:
10194 * 1) The renegotiation is complete. In this case, no new record
10195 * has been read yet.
10196 * 2) The renegotiation is incomplete because the client received
10197 * an application data record while awaiting the ServerHello.
10198 * 3) The renegotiation is incomplete because the client received
10199 * a non-handshake, non-application data message while awaiting
10200 * the ServerHello.
10201 * In each of these case, looping will be the proper action:
10202 * - For 1), the next iteration will read a new record and check
10203 * if it's application data.
10204 * - For 2), the loop condition isn't satisfied as application data
10205 * is present, hence continue is the same as break
10206 * - For 3), the loop condition is satisfied and read_record
10207 * will re-deliver the message that was held back by the client
10208 * when expecting the ServerHello.
10209 */
10210 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010211 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010212#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010213 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010214 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010215 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010216 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010217 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010220 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010221 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010222 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010223 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010224 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010225#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010227 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10228 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010230 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010231 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010232 }
10233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010234 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10237 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010238 }
10239
10240 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010241
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010242 /* We're going to return something now, cancel timer,
10243 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010244 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010245 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010246
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010247#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010248 /* If we requested renego but received AppData, resend HelloRequest.
10249 * Do it now, after setting in_offt, to avoid taking this branch
10250 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010251#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010252 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010253 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010254 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010255 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010257 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010258 return( ret );
10259 }
10260 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010261#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010262#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010263 }
10264
10265 n = ( len < ssl->in_msglen )
10266 ? len : ssl->in_msglen;
10267
10268 memcpy( buf, ssl->in_offt, n );
10269 ssl->in_msglen -= n;
10270
10271 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010272 {
10273 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010274 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010275 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010276 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010277 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010278 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010279 /* more data available */
10280 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010281 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010284
Paul Bakker23986e52011-04-24 08:57:21 +000010285 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010286}
10287
10288/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010289 * Send application data to be encrypted by the SSL layer, taking care of max
10290 * fragment length and buffer size.
10291 *
10292 * According to RFC 5246 Section 6.2.1:
10293 *
10294 * Zero-length fragments of Application data MAY be sent as they are
10295 * potentially useful as a traffic analysis countermeasure.
10296 *
10297 * Therefore, it is possible that the input message length is 0 and the
10298 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010299 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010300static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010301 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010302{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010303 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10304 const size_t max_len = (size_t) ret;
10305
10306 if( ret < 0 )
10307 {
10308 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10309 return( ret );
10310 }
10311
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010312 if( len > max_len )
10313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010314#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010315 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010318 "maximum fragment length: %d > %d",
10319 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010321 }
10322 else
10323#endif
10324 len = max_len;
10325 }
Paul Bakker887bd502011-06-08 13:10:54 +000010326
Paul Bakker5121ce52009-01-03 21:22:43 +000010327 if( ssl->out_left != 0 )
10328 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010329 /*
10330 * The user has previously tried to send the data and
10331 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10332 * written. In this case, we expect the high-level write function
10333 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010335 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010338 return( ret );
10339 }
10340 }
Paul Bakker887bd502011-06-08 13:10:54 +000010341 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010342 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010343 /*
10344 * The user is trying to send a message the first time, so we need to
10345 * copy the data into the internal buffers and setup the data structure
10346 * to keep track of partial writes
10347 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010348 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010349 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010350 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010351
Hanno Becker67bc7c32018-08-06 11:33:50 +010010352 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010354 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010355 return( ret );
10356 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010357 }
10358
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010359 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010360}
10361
10362/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010363 * Write application data, doing 1/n-1 splitting if necessary.
10364 *
10365 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010366 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010367 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010369#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010370static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010371 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010372{
10373 int ret;
10374
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010375 if( ssl->conf->cbc_record_splitting ==
10376 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010377 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010378 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10379 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10380 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010381 {
10382 return( ssl_write_real( ssl, buf, len ) );
10383 }
10384
10385 if( ssl->split_done == 0 )
10386 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010387 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010388 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010389 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010390 }
10391
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010392 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10393 return( ret );
10394 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010395
10396 return( ret + 1 );
10397}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010398#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010399
10400/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010401 * Write application data (public-facing wrapper)
10402 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010403int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010404{
10405 int ret;
10406
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010408
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010409 if( ssl == NULL || ssl->conf == NULL )
10410 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10411
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010412#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010413 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10414 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010415 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010416 return( ret );
10417 }
10418#endif
10419
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010420 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010421 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010422 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010423 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010424 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010425 return( ret );
10426 }
10427 }
10428
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010429#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010430 ret = ssl_write_split( ssl, buf, len );
10431#else
10432 ret = ssl_write_real( ssl, buf, len );
10433#endif
10434
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010436
10437 return( ret );
10438}
10439
10440/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010441 * Notify the peer that the connection is being closed
10442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010443int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010444{
10445 int ret;
10446
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010447 if( ssl == NULL || ssl->conf == NULL )
10448 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010451
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010452 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010453 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010455 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010457 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10458 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10459 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010462 return( ret );
10463 }
10464 }
10465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010467
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010468 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010469}
10470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010471void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010472{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010473 if( transform == NULL )
10474 return;
10475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010476#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010477 deflateEnd( &transform->ctx_deflate );
10478 inflateEnd( &transform->ctx_inflate );
10479#endif
10480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010481 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10482 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010483
Hanno Beckerd56ed242018-01-03 15:32:51 +000010484#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010485 mbedtls_md_free( &transform->md_ctx_enc );
10486 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010487#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010488
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010489 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010490}
10491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010492#if defined(MBEDTLS_X509_CRT_PARSE_C)
10493static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010494{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010495 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010496
10497 while( cur != NULL )
10498 {
10499 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010500 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010501 cur = next;
10502 }
10503}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010504#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010505
Hanno Becker0271f962018-08-16 13:23:47 +010010506#if defined(MBEDTLS_SSL_PROTO_DTLS)
10507
10508static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10509{
10510 unsigned offset;
10511 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10512
10513 if( hs == NULL )
10514 return;
10515
Hanno Becker283f5ef2018-08-24 09:34:47 +010010516 ssl_free_buffered_record( ssl );
10517
Hanno Becker0271f962018-08-16 13:23:47 +010010518 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010519 ssl_buffering_free_slot( ssl, offset );
10520}
10521
10522static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10523 uint8_t slot )
10524{
10525 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10526 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010527
10528 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10529 return;
10530
Hanno Beckere605b192018-08-21 15:59:07 +010010531 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010532 {
Hanno Beckere605b192018-08-21 15:59:07 +010010533 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010534 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010535 mbedtls_free( hs_buf->data );
10536 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010537 }
10538}
10539
10540#endif /* MBEDTLS_SSL_PROTO_DTLS */
10541
Gilles Peskine9b562d52018-04-25 20:32:43 +020010542void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010543{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010544 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10545
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010546 if( handshake == NULL )
10547 return;
10548
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010549#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10550 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10551 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010552 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010553 handshake->async_in_progress = 0;
10554 }
10555#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10556
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010557#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10558 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10559 mbedtls_md5_free( &handshake->fin_md5 );
10560 mbedtls_sha1_free( &handshake->fin_sha1 );
10561#endif
10562#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10563#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010564#if defined(MBEDTLS_USE_PSA_CRYPTO)
10565 psa_hash_abort( &handshake->fin_sha256_psa );
10566#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010567 mbedtls_sha256_free( &handshake->fin_sha256 );
10568#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010569#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010570#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010571#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010572 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010573#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010574 mbedtls_sha512_free( &handshake->fin_sha512 );
10575#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010576#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010577#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010579#if defined(MBEDTLS_DHM_C)
10580 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010581#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010582#if defined(MBEDTLS_ECDH_C)
10583 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010584#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010585#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010586 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010587#if defined(MBEDTLS_SSL_CLI_C)
10588 mbedtls_free( handshake->ecjpake_cache );
10589 handshake->ecjpake_cache = NULL;
10590 handshake->ecjpake_cache_len = 0;
10591#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010592#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010593
Janos Follath4ae5c292016-02-10 11:27:43 +000010594#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10595 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010596 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010597 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010598#endif
10599
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010600#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10601 if( handshake->psk != NULL )
10602 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010603 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010604 mbedtls_free( handshake->psk );
10605 }
10606#endif
10607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010608#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10609 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010610 /*
10611 * Free only the linked list wrapper, not the keys themselves
10612 * since the belong to the SNI callback
10613 */
10614 if( handshake->sni_key_cert != NULL )
10615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010616 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010617
10618 while( cur != NULL )
10619 {
10620 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010621 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010622 cur = next;
10623 }
10624 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010625#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010626
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010627#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010628 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010629 if( handshake->ecrs_peer_cert != NULL )
10630 {
10631 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10632 mbedtls_free( handshake->ecrs_peer_cert );
10633 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010634#endif
10635
Hanno Becker75173122019-02-06 16:18:31 +000010636#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10637 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10638 mbedtls_pk_free( &handshake->peer_pubkey );
10639#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010641#if defined(MBEDTLS_SSL_PROTO_DTLS)
10642 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010643 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010644 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010645#endif
10646
Hanno Becker4a63ed42019-01-08 11:39:35 +000010647#if defined(MBEDTLS_ECDH_C) && \
10648 defined(MBEDTLS_USE_PSA_CRYPTO)
10649 psa_destroy_key( handshake->ecdh_psa_privkey );
10650#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10651
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010652 mbedtls_platform_zeroize( handshake,
10653 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010654}
10655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010656void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010657{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010658 if( session == NULL )
10659 return;
10660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010661#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010662 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010663#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010664
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010665#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010666 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010667#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010668
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010669 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010670}
10671
Paul Bakker5121ce52009-01-03 21:22:43 +000010672/*
10673 * Free an SSL context
10674 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010675void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010676{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010677 if( ssl == NULL )
10678 return;
10679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010681
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010682 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010683 {
Angus Grattond8213d02016-05-25 20:56:48 +100010684 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010685 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010686 }
10687
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010688 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010689 {
Angus Grattond8213d02016-05-25 20:56:48 +100010690 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010691 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010692 }
10693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010694#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010695 if( ssl->compress_buf != NULL )
10696 {
Angus Grattond8213d02016-05-25 20:56:48 +100010697 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010698 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010699 }
10700#endif
10701
Paul Bakker48916f92012-09-16 19:57:18 +000010702 if( ssl->transform )
10703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010704 mbedtls_ssl_transform_free( ssl->transform );
10705 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010706 }
10707
10708 if( ssl->handshake )
10709 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010710 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010711 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10712 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010714 mbedtls_free( ssl->handshake );
10715 mbedtls_free( ssl->transform_negotiate );
10716 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010717 }
10718
Paul Bakkerc0463502013-02-14 11:19:38 +010010719 if( ssl->session )
10720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010721 mbedtls_ssl_session_free( ssl->session );
10722 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010723 }
10724
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010725#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010726 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010727 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010728 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010729 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010730 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010731#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010733#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10734 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10737 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010738 }
10739#endif
10740
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010741#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010742 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010743#endif
10744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010746
Paul Bakker86f04f42013-02-14 11:20:09 +010010747 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010748 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010749}
10750
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010751/*
10752 * Initialze mbedtls_ssl_config
10753 */
10754void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10755{
10756 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10757}
10758
Simon Butcherc97b6972015-12-27 23:48:17 +000010759#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010760static int ssl_preset_default_hashes[] = {
10761#if defined(MBEDTLS_SHA512_C)
10762 MBEDTLS_MD_SHA512,
10763 MBEDTLS_MD_SHA384,
10764#endif
10765#if defined(MBEDTLS_SHA256_C)
10766 MBEDTLS_MD_SHA256,
10767 MBEDTLS_MD_SHA224,
10768#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010769#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010770 MBEDTLS_MD_SHA1,
10771#endif
10772 MBEDTLS_MD_NONE
10773};
Simon Butcherc97b6972015-12-27 23:48:17 +000010774#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010775
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010776static int ssl_preset_suiteb_ciphersuites[] = {
10777 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10778 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10779 0
10780};
10781
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010782#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010783static int ssl_preset_suiteb_hashes[] = {
10784 MBEDTLS_MD_SHA256,
10785 MBEDTLS_MD_SHA384,
10786 MBEDTLS_MD_NONE
10787};
10788#endif
10789
10790#if defined(MBEDTLS_ECP_C)
10791static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010792#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010793 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010794#endif
10795#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010796 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010797#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010798 MBEDTLS_ECP_DP_NONE
10799};
10800#endif
10801
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010802/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010803 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010804 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010805int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010806 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010807{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010808#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010809 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010810#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010811
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010812 /* Use the functions here so that they are covered in tests,
10813 * but otherwise access member directly for efficiency */
10814 mbedtls_ssl_conf_endpoint( conf, endpoint );
10815 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010816
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010817 /*
10818 * Things that are common to all presets
10819 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010820#if defined(MBEDTLS_SSL_CLI_C)
10821 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10822 {
10823 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10824#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10825 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10826#endif
10827 }
10828#endif
10829
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010830#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010831 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010832#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010833
10834#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10835 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10836#endif
10837
10838#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10839 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10840#endif
10841
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010842#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10843 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10844#endif
10845
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010846#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010847 conf->f_cookie_write = ssl_cookie_write_dummy;
10848 conf->f_cookie_check = ssl_cookie_check_dummy;
10849#endif
10850
10851#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10852 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10853#endif
10854
Janos Follath088ce432017-04-10 12:42:31 +010010855#if defined(MBEDTLS_SSL_SRV_C)
10856 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10857#endif
10858
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010859#if defined(MBEDTLS_SSL_PROTO_DTLS)
10860 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10861 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10862#endif
10863
10864#if defined(MBEDTLS_SSL_RENEGOTIATION)
10865 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010866 memset( conf->renego_period, 0x00, 2 );
10867 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010868#endif
10869
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010870#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10871 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10872 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010873 const unsigned char dhm_p[] =
10874 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10875 const unsigned char dhm_g[] =
10876 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10877
Hanno Beckera90658f2017-10-04 15:29:08 +010010878 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10879 dhm_p, sizeof( dhm_p ),
10880 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010881 {
10882 return( ret );
10883 }
10884 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010885#endif
10886
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010887 /*
10888 * Preset-specific defaults
10889 */
10890 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010891 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010892 /*
10893 * NSA Suite B
10894 */
10895 case MBEDTLS_SSL_PRESET_SUITEB:
10896 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10897 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10898 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10899 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10900
10901 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10902 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10903 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10904 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10905 ssl_preset_suiteb_ciphersuites;
10906
10907#if defined(MBEDTLS_X509_CRT_PARSE_C)
10908 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010909#endif
10910
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010911#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010912 conf->sig_hashes = ssl_preset_suiteb_hashes;
10913#endif
10914
10915#if defined(MBEDTLS_ECP_C)
10916 conf->curve_list = ssl_preset_suiteb_curves;
10917#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010918 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010919
10920 /*
10921 * Default
10922 */
10923 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010924 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10925 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10926 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10927 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10928 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10929 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10930 MBEDTLS_SSL_MIN_MINOR_VERSION :
10931 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010932 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10933 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10934
10935#if defined(MBEDTLS_SSL_PROTO_DTLS)
10936 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10937 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10938#endif
10939
10940 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10941 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10942 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10943 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10944 mbedtls_ssl_list_ciphersuites();
10945
10946#if defined(MBEDTLS_X509_CRT_PARSE_C)
10947 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10948#endif
10949
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010950#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010951 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010952#endif
10953
10954#if defined(MBEDTLS_ECP_C)
10955 conf->curve_list = mbedtls_ecp_grp_id_list();
10956#endif
10957
10958#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10959 conf->dhm_min_bitlen = 1024;
10960#endif
10961 }
10962
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010963 return( 0 );
10964}
10965
10966/*
10967 * Free mbedtls_ssl_config
10968 */
10969void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10970{
10971#if defined(MBEDTLS_DHM_C)
10972 mbedtls_mpi_free( &conf->dhm_P );
10973 mbedtls_mpi_free( &conf->dhm_G );
10974#endif
10975
10976#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10977 if( conf->psk != NULL )
10978 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010979 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010980 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010981 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010982 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010983 }
10984
10985 if( conf->psk_identity != NULL )
10986 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010987 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010988 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010989 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010990 conf->psk_identity_len = 0;
10991 }
10992#endif
10993
10994#if defined(MBEDTLS_X509_CRT_PARSE_C)
10995 ssl_key_cert_free( conf->key_cert );
10996#endif
10997
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010998 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010999}
11000
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011001#if defined(MBEDTLS_PK_C) && \
11002 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011003/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011004 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011005 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011006unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011007{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011008#if defined(MBEDTLS_RSA_C)
11009 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
11010 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011011#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011012#if defined(MBEDTLS_ECDSA_C)
11013 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
11014 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011015#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011016 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011017}
11018
Hanno Becker7e5437a2017-04-28 17:15:26 +010011019unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
11020{
11021 switch( type ) {
11022 case MBEDTLS_PK_RSA:
11023 return( MBEDTLS_SSL_SIG_RSA );
11024 case MBEDTLS_PK_ECDSA:
11025 case MBEDTLS_PK_ECKEY:
11026 return( MBEDTLS_SSL_SIG_ECDSA );
11027 default:
11028 return( MBEDTLS_SSL_SIG_ANON );
11029 }
11030}
11031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011032mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011033{
11034 switch( sig )
11035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011036#if defined(MBEDTLS_RSA_C)
11037 case MBEDTLS_SSL_SIG_RSA:
11038 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011040#if defined(MBEDTLS_ECDSA_C)
11041 case MBEDTLS_SSL_SIG_ECDSA:
11042 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011043#endif
11044 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011045 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011046 }
11047}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011048#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011049
Hanno Becker7e5437a2017-04-28 17:15:26 +010011050#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
11051 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
11052
11053/* Find an entry in a signature-hash set matching a given hash algorithm. */
11054mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
11055 mbedtls_pk_type_t sig_alg )
11056{
11057 switch( sig_alg )
11058 {
11059 case MBEDTLS_PK_RSA:
11060 return( set->rsa );
11061 case MBEDTLS_PK_ECDSA:
11062 return( set->ecdsa );
11063 default:
11064 return( MBEDTLS_MD_NONE );
11065 }
11066}
11067
11068/* Add a signature-hash-pair to a signature-hash set */
11069void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
11070 mbedtls_pk_type_t sig_alg,
11071 mbedtls_md_type_t md_alg )
11072{
11073 switch( sig_alg )
11074 {
11075 case MBEDTLS_PK_RSA:
11076 if( set->rsa == MBEDTLS_MD_NONE )
11077 set->rsa = md_alg;
11078 break;
11079
11080 case MBEDTLS_PK_ECDSA:
11081 if( set->ecdsa == MBEDTLS_MD_NONE )
11082 set->ecdsa = md_alg;
11083 break;
11084
11085 default:
11086 break;
11087 }
11088}
11089
11090/* Allow exactly one hash algorithm for each signature. */
11091void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11092 mbedtls_md_type_t md_alg )
11093{
11094 set->rsa = md_alg;
11095 set->ecdsa = md_alg;
11096}
11097
11098#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11099 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11100
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011101/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011102 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011104mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011105{
11106 switch( hash )
11107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011108#if defined(MBEDTLS_MD5_C)
11109 case MBEDTLS_SSL_HASH_MD5:
11110 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011111#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011112#if defined(MBEDTLS_SHA1_C)
11113 case MBEDTLS_SSL_HASH_SHA1:
11114 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011115#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011116#if defined(MBEDTLS_SHA256_C)
11117 case MBEDTLS_SSL_HASH_SHA224:
11118 return( MBEDTLS_MD_SHA224 );
11119 case MBEDTLS_SSL_HASH_SHA256:
11120 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011121#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011122#if defined(MBEDTLS_SHA512_C)
11123 case MBEDTLS_SSL_HASH_SHA384:
11124 return( MBEDTLS_MD_SHA384 );
11125 case MBEDTLS_SSL_HASH_SHA512:
11126 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011127#endif
11128 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011129 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011130 }
11131}
11132
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011133/*
11134 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11135 */
11136unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11137{
11138 switch( md )
11139 {
11140#if defined(MBEDTLS_MD5_C)
11141 case MBEDTLS_MD_MD5:
11142 return( MBEDTLS_SSL_HASH_MD5 );
11143#endif
11144#if defined(MBEDTLS_SHA1_C)
11145 case MBEDTLS_MD_SHA1:
11146 return( MBEDTLS_SSL_HASH_SHA1 );
11147#endif
11148#if defined(MBEDTLS_SHA256_C)
11149 case MBEDTLS_MD_SHA224:
11150 return( MBEDTLS_SSL_HASH_SHA224 );
11151 case MBEDTLS_MD_SHA256:
11152 return( MBEDTLS_SSL_HASH_SHA256 );
11153#endif
11154#if defined(MBEDTLS_SHA512_C)
11155 case MBEDTLS_MD_SHA384:
11156 return( MBEDTLS_SSL_HASH_SHA384 );
11157 case MBEDTLS_MD_SHA512:
11158 return( MBEDTLS_SSL_HASH_SHA512 );
11159#endif
11160 default:
11161 return( MBEDTLS_SSL_HASH_NONE );
11162 }
11163}
11164
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011165#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011166/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011167 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011168 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011169 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011170int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011171{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011172 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011173
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011174 if( ssl->conf->curve_list == NULL )
11175 return( -1 );
11176
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011177 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011178 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011179 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011180
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011181 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011182}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011183#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011184
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011185#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011186/*
11187 * Check if a hash proposed by the peer is in our list.
11188 * Return 0 if we're willing to use it, -1 otherwise.
11189 */
11190int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11191 mbedtls_md_type_t md )
11192{
11193 const int *cur;
11194
11195 if( ssl->conf->sig_hashes == NULL )
11196 return( -1 );
11197
11198 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11199 if( *cur == (int) md )
11200 return( 0 );
11201
11202 return( -1 );
11203}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011204#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011206#if defined(MBEDTLS_X509_CRT_PARSE_C)
11207int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11208 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011209 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011210 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011211{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011212 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011213#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011214 int usage = 0;
11215#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011216#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011217 const char *ext_oid;
11218 size_t ext_len;
11219#endif
11220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011221#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11222 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011223 ((void) cert);
11224 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011225 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011226#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011228#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11229 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011230 {
11231 /* Server part of the key exchange */
11232 switch( ciphersuite->key_exchange )
11233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011234 case MBEDTLS_KEY_EXCHANGE_RSA:
11235 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011236 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011237 break;
11238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011239 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11240 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11241 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11242 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011243 break;
11244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011245 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11246 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011247 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011248 break;
11249
11250 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011251 case MBEDTLS_KEY_EXCHANGE_NONE:
11252 case MBEDTLS_KEY_EXCHANGE_PSK:
11253 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11254 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011255 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011256 usage = 0;
11257 }
11258 }
11259 else
11260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011261 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11262 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011263 }
11264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011265 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011266 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011267 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011268 ret = -1;
11269 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011270#else
11271 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011272#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011274#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11275 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011277 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11278 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011279 }
11280 else
11281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011282 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11283 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011284 }
11285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011286 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011287 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011288 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011289 ret = -1;
11290 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011291#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011292
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011293 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011294}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011295#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011296
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011297/*
11298 * Convert version numbers to/from wire format
11299 * and, for DTLS, to/from TLS equivalent.
11300 *
11301 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011302 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011303 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11304 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011306void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011307 unsigned char ver[2] )
11308{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011309#if defined(MBEDTLS_SSL_PROTO_DTLS)
11310 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011312 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011313 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11314
11315 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11316 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11317 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011318 else
11319#else
11320 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011321#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011322 {
11323 ver[0] = (unsigned char) major;
11324 ver[1] = (unsigned char) minor;
11325 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011326}
11327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011328void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011329 const unsigned char ver[2] )
11330{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011331#if defined(MBEDTLS_SSL_PROTO_DTLS)
11332 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011333 {
11334 *major = 255 - ver[0] + 2;
11335 *minor = 255 - ver[1] + 1;
11336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011337 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011338 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11339 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011340 else
11341#else
11342 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011343#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011344 {
11345 *major = ver[0];
11346 *minor = ver[1];
11347 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011348}
11349
Simon Butcher99000142016-10-13 17:21:01 +010011350int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11351{
11352#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11353 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11354 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11355
11356 switch( md )
11357 {
11358#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11359#if defined(MBEDTLS_MD5_C)
11360 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011361 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011362#endif
11363#if defined(MBEDTLS_SHA1_C)
11364 case MBEDTLS_SSL_HASH_SHA1:
11365 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11366 break;
11367#endif
11368#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11369#if defined(MBEDTLS_SHA512_C)
11370 case MBEDTLS_SSL_HASH_SHA384:
11371 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11372 break;
11373#endif
11374#if defined(MBEDTLS_SHA256_C)
11375 case MBEDTLS_SSL_HASH_SHA256:
11376 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11377 break;
11378#endif
11379 default:
11380 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11381 }
11382
11383 return 0;
11384#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11385 (void) ssl;
11386 (void) md;
11387
11388 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11389#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11390}
11391
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011392#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11393 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11394int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11395 unsigned char *output,
11396 unsigned char *data, size_t data_len )
11397{
11398 int ret = 0;
11399 mbedtls_md5_context mbedtls_md5;
11400 mbedtls_sha1_context mbedtls_sha1;
11401
11402 mbedtls_md5_init( &mbedtls_md5 );
11403 mbedtls_sha1_init( &mbedtls_sha1 );
11404
11405 /*
11406 * digitally-signed struct {
11407 * opaque md5_hash[16];
11408 * opaque sha_hash[20];
11409 * };
11410 *
11411 * md5_hash
11412 * MD5(ClientHello.random + ServerHello.random
11413 * + ServerParams);
11414 * sha_hash
11415 * SHA(ClientHello.random + ServerHello.random
11416 * + ServerParams);
11417 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011418 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011419 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011420 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011421 goto exit;
11422 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011423 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011424 ssl->handshake->randbytes, 64 ) ) != 0 )
11425 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011426 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011427 goto exit;
11428 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011429 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011430 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011431 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011432 goto exit;
11433 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011434 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011435 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011437 goto exit;
11438 }
11439
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011440 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011441 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011443 goto exit;
11444 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011445 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011446 ssl->handshake->randbytes, 64 ) ) != 0 )
11447 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011449 goto exit;
11450 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011451 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011452 data_len ) ) != 0 )
11453 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011455 goto exit;
11456 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011457 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011458 output + 16 ) ) != 0 )
11459 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011460 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011461 goto exit;
11462 }
11463
11464exit:
11465 mbedtls_md5_free( &mbedtls_md5 );
11466 mbedtls_sha1_free( &mbedtls_sha1 );
11467
11468 if( ret != 0 )
11469 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11470 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11471
11472 return( ret );
11473
11474}
11475#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11476 MBEDTLS_SSL_PROTO_TLS1_1 */
11477
11478#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11479 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011480
11481#if defined(MBEDTLS_USE_PSA_CRYPTO)
11482int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11483 unsigned char *hash, size_t *hashlen,
11484 unsigned char *data, size_t data_len,
11485 mbedtls_md_type_t md_alg )
11486{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011487 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011488 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011489 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11490
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011492
11493 if( ( status = psa_hash_setup( &hash_operation,
11494 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011495 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011496 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011497 goto exit;
11498 }
11499
Andrzej Kurek814feff2019-01-14 04:35:19 -050011500 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11501 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011502 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011503 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011504 goto exit;
11505 }
11506
Andrzej Kurek814feff2019-01-14 04:35:19 -050011507 if( ( status = psa_hash_update( &hash_operation,
11508 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011509 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011510 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011511 goto exit;
11512 }
11513
Andrzej Kurek814feff2019-01-14 04:35:19 -050011514 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11515 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011516 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011517 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011518 goto exit;
11519 }
11520
11521exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011522 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011523 {
11524 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11525 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011526 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011527 {
11528 case PSA_ERROR_NOT_SUPPORTED:
11529 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011530 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011531 case PSA_ERROR_BUFFER_TOO_SMALL:
11532 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11533 case PSA_ERROR_INSUFFICIENT_MEMORY:
11534 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11535 default:
11536 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11537 }
11538 }
11539 return( 0 );
11540}
11541
11542#else
11543
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011544int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011545 unsigned char *hash, size_t *hashlen,
11546 unsigned char *data, size_t data_len,
11547 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011548{
11549 int ret = 0;
11550 mbedtls_md_context_t ctx;
11551 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011552 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011553
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011554 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011555
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011556 mbedtls_md_init( &ctx );
11557
11558 /*
11559 * digitally-signed struct {
11560 * opaque client_random[32];
11561 * opaque server_random[32];
11562 * ServerDHParams params;
11563 * };
11564 */
11565 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11566 {
11567 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11568 goto exit;
11569 }
11570 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11571 {
11572 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11573 goto exit;
11574 }
11575 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11576 {
11577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11578 goto exit;
11579 }
11580 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11581 {
11582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11583 goto exit;
11584 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011585 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011586 {
11587 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11588 goto exit;
11589 }
11590
11591exit:
11592 mbedtls_md_free( &ctx );
11593
11594 if( ret != 0 )
11595 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11596 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11597
11598 return( ret );
11599}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011600#endif /* MBEDTLS_USE_PSA_CRYPTO */
11601
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011602#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11603 MBEDTLS_SSL_PROTO_TLS1_2 */
11604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011605#endif /* MBEDTLS_SSL_TLS_C */