blob: 877fee833c7fcd4c37bdf4f21e12bec82d65d302 [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 tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000993 unsigned char keyblk[256];
994 unsigned char *key1;
995 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100996 unsigned char *mac_enc;
997 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000998 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200999 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001000 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001001 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 const mbedtls_cipher_info_t *cipher_info;
1003 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 mbedtls_ssl_session *session = ssl->session_negotiate;
1006 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
1007 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +00001008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001010
Jaeden Amero2de07f12019-06-05 13:32:08 +01001011#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1012 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001013 transform->encrypt_then_mac = session->encrypt_then_mac;
1014#endif
1015 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001016
1017 ciphersuite_info = handshake->ciphersuite_info;
1018 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001019 if( cipher_info == NULL )
1020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001022 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001024 }
1025
Hanno Beckere694c3e2017-12-27 21:34:08 +00001026 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001027 if( md_info == NULL )
1028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001030 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001032 }
1033
Hanno Beckera0e20d02019-05-15 14:03:01 +01001034#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001035 /* Copy own and peer's CID if the use of the CID
1036 * extension has been negotiated. */
1037 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1038 {
1039 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001040
Hanno Becker05154c32019-05-03 15:23:51 +01001041 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001042 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001043 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001044 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001045
1046 transform->out_cid_len = ssl->handshake->peer_cid_len;
1047 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1048 ssl->handshake->peer_cid_len );
1049 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1050 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001051 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001052#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001053
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 * Swap the client and server random values.
1056 */
Paul Bakker48916f92012-09-16 19:57:18 +00001057 memcpy( tmp, handshake->randbytes, 64 );
1058 memcpy( handshake->randbytes, tmp + 32, 32 );
1059 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001060 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001061
1062 /*
1063 * SSLv3:
1064 * key block =
1065 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1066 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1067 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1068 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1069 * ...
1070 *
1071 * TLSv1:
1072 * key block = PRF( master, "key expansion", randbytes )
1073 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001074 ret = handshake->tls_prf( session->master, 48, "key expansion",
1075 handshake->randbytes, 64, keyblk, 256 );
1076 if( ret != 0 )
1077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001079 return( ret );
1080 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1083 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1084 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1085 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1086 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001087
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 /*
1089 * Determine the appropriate key, IV and MAC length.
1090 */
Paul Bakker68884e32013-01-07 18:20:04 +01001091
Hanno Becker88aaf652017-12-27 08:17:40 +00001092 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001093
Hanno Becker8031d062018-01-03 15:32:31 +00001094#if defined(MBEDTLS_GCM_C) || \
1095 defined(MBEDTLS_CCM_C) || \
1096 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001098 cipher_info->mode == MBEDTLS_MODE_CCM ||
1099 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001101 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001102
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001103 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001104 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001105 transform->taglen =
1106 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001107
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001108 /* All modes haves 96-bit IVs;
1109 * GCM and CCM has 4 implicit and 8 explicit bytes
1110 * ChachaPoly has all 12 bytes implicit
1111 */
Paul Bakker68884e32013-01-07 18:20:04 +01001112 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001113 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1114 transform->fixed_ivlen = 12;
1115 else
1116 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001117
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001118 /* Minimum length of encrypted record */
1119 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001120 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001121 }
1122 else
Hanno Becker8031d062018-01-03 15:32:31 +00001123#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1124#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1125 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1126 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001127 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001128 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1130 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001133 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001134 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001135
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001136 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001137 mac_key_len = mbedtls_md_get_size( md_info );
1138 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001141 /*
1142 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1143 * (rfc 6066 page 13 or rfc 2104 section 4),
1144 * so we only need to adjust the length here.
1145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001149
1150#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1151 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001152 * HMAC implementation which also truncates the key
1153 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001154 mac_key_len = transform->maclen;
1155#endif
1156 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001158
1159 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001160 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001161
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001162 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001164 transform->minlen = transform->maclen;
1165 else
Paul Bakker68884e32013-01-07 18:20:04 +01001166 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001167 /*
1168 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001169 * 1. if EtM is in use: one block plus MAC
1170 * otherwise: * first multiple of blocklen greater than maclen
1171 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1174 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001175 {
1176 transform->minlen = transform->maclen
1177 + cipher_info->block_size;
1178 }
1179 else
1180#endif
1181 {
1182 transform->minlen = transform->maclen
1183 + cipher_info->block_size
1184 - transform->maclen % cipher_info->block_size;
1185 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1188 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1189 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001190 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001191 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001192#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1194 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1195 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001196 {
1197 transform->minlen += transform->ivlen;
1198 }
1199 else
1200#endif
1201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001203 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1204 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001205 }
Paul Bakker68884e32013-01-07 18:20:04 +01001206 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 }
Hanno Becker8031d062018-01-03 15:32:31 +00001208 else
1209#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1210 {
1211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1212 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1213 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001214
Hanno Becker88aaf652017-12-27 08:17:40 +00001215 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1216 (unsigned) keylen,
1217 (unsigned) transform->minlen,
1218 (unsigned) transform->ivlen,
1219 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001220
1221 /*
1222 * Finally setup the cipher contexts, IVs and MAC secrets.
1223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001225 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001226 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001227 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001228 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001229
Paul Bakker68884e32013-01-07 18:20:04 +01001230 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001231 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001232
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233 /*
1234 * This is not used in TLS v1.1.
1235 */
Paul Bakker48916f92012-09-16 19:57:18 +00001236 iv_copy_len = ( transform->fixed_ivlen ) ?
1237 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001238 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1239 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001240 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001241 }
1242 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243#endif /* MBEDTLS_SSL_CLI_C */
1244#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001245 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001247 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001248 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
Hanno Becker81c7b182017-11-09 18:39:33 +00001250 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001251 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001252
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001253 /*
1254 * This is not used in TLS v1.1.
1255 */
Paul Bakker48916f92012-09-16 19:57:18 +00001256 iv_copy_len = ( transform->fixed_ivlen ) ?
1257 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001258 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1259 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001260 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001262 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +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é-Gonnardd16d1cb2014-11-20 18:15:05 +01001268 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Hanno Beckerd56ed242018-01-03 15:32:51 +00001270#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271#if defined(MBEDTLS_SSL_PROTO_SSL3)
1272 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001273 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001274 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001277 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1278 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001279 }
1280
Hanno Becker81c7b182017-11-09 18:39:33 +00001281 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1282 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001283 }
1284 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1286#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1287 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1288 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001289 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001290 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1291 For AEAD-based ciphersuites, there is nothing to do here. */
1292 if( mac_key_len != 0 )
1293 {
1294 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1295 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1296 }
Paul Bakker68884e32013-01-07 18:20:04 +01001297 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001298 else
1299#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001302 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1303 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001304 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001305#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1308 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001309 {
1310 int ret = 0;
1311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001313
Hanno Becker88aaf652017-12-27 08:17:40 +00001314 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001315 transform->iv_enc, transform->iv_dec,
1316 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001317 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001318 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001321 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1322 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001323 }
1324 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001325#else
1326 ((void) mac_dec);
1327 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001329
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001330#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1331 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001332 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001333 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1334 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001335 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001336 iv_copy_len );
1337 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001338
1339 if( ssl->conf->f_export_keys_ext != NULL )
1340 {
1341 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1342 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001343 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001344 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001345 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001346 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001347 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001348 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001349#endif
1350
Hanno Beckerf704bef2018-11-16 15:21:18 +00001351#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001352
1353 /* Only use PSA-based ciphers for TLS-1.2.
1354 * That's relevant at least for TLS-1.0, where
1355 * we assume that mbedtls_cipher_crypt() updates
1356 * the structure field for the IV, which the PSA-based
1357 * implementation currently doesn't. */
1358#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1359 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001360 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001361 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001362 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001363 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1364 {
1365 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001366 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001367 }
1368
1369 if( ret == 0 )
1370 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001371 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001372 psa_fallthrough = 0;
1373 }
1374 else
1375 {
1376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1377 psa_fallthrough = 1;
1378 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001379 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001380 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001381 psa_fallthrough = 1;
1382#else
1383 psa_fallthrough = 1;
1384#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001385
Hanno Beckercb1cc802018-11-17 22:27:38 +00001386 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001387#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001388 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001389 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001390 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001391 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001392 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001393 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001394
Hanno Beckerf704bef2018-11-16 15:21:18 +00001395#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001396 /* Only use PSA-based ciphers for TLS-1.2.
1397 * That's relevant at least for TLS-1.0, where
1398 * we assume that mbedtls_cipher_crypt() updates
1399 * the structure field for the IV, which the PSA-based
1400 * implementation currently doesn't. */
1401#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1402 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001403 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001404 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001405 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001406 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1407 {
1408 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001409 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001410 }
1411
1412 if( ret == 0 )
1413 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001414 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001415 psa_fallthrough = 0;
1416 }
1417 else
1418 {
1419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1420 psa_fallthrough = 1;
1421 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001422 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001423 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001424 psa_fallthrough = 1;
1425#else
1426 psa_fallthrough = 1;
1427#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001428
Hanno Beckercb1cc802018-11-17 22:27:38 +00001429 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001430#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001431 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001432 cipher_info ) ) != 0 )
1433 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001435 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001436 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001439 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001443 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001444 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001447 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001451 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001452 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#if defined(MBEDTLS_CIPHER_MODE_CBC)
1455 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1458 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001461 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001462 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1465 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001468 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001469 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001472
Paul Bakker5121ce52009-01-03 21:22:43 +00001473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001475 // Initialize compression
1476 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001478 {
Paul Bakker16770332013-10-11 09:59:44 +02001479 if( ssl->compress_buf == NULL )
1480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001482 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001483 if( ssl->compress_buf == NULL )
1484 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001486 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001487 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1488 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001489 }
1490 }
1491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001493
Paul Bakker48916f92012-09-16 19:57:18 +00001494 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1495 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001496
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001497 if( deflateInit( &transform->ctx_deflate,
1498 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001499 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001502 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1503 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001504 }
1505 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001509end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001510 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1511 mbedtls_platform_zeroize( handshake->randbytes,
1512 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001513 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001514}
1515
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001516/*
1517 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
1518 *
1519 * Inputs:
1520 * - SSL/TLS minor version
1521 * - hash associated with the ciphersuite (only used by TLS 1.2)
1522 *
1523 * Ouputs:
1524 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1525 */
1526static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1527 int minor_ver,
1528 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001529{
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001530#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1531 (void) hash;
1532#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001533
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001534#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001535 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001536 {
1537 handshake->tls_prf = ssl3_prf;
1538 handshake->calc_verify = ssl_calc_verify_ssl;
1539 handshake->calc_finished = ssl_calc_finished_ssl;
1540 }
1541 else
1542#endif
1543#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001544 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001545 {
1546 handshake->tls_prf = tls1_prf;
1547 handshake->calc_verify = ssl_calc_verify_tls;
1548 handshake->calc_finished = ssl_calc_finished_tls;
1549 }
1550 else
1551#endif
1552#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1553#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001554 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1555 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001556 {
1557 handshake->tls_prf = tls_prf_sha384;
1558 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1559 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1560 }
1561 else
1562#endif
1563#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001564 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001565 {
1566 handshake->tls_prf = tls_prf_sha256;
1567 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1568 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1569 }
1570 else
1571#endif
1572#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1573 {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001574 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1575 }
1576
1577 return( 0 );
1578}
1579
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001580/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001581 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001582 *
1583 * Parameters:
1584 * [in/out] handshake
1585 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001586 * (PSA-PSK) ciphersuite_info, psk_opaque
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001587 * [out] premaster (cleared)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001588 * [out] master
1589 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1590 * debug: conf->f_dbg, conf->p_dbg
1591 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001592 * PSA-PSA: minor_ver, conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001593 */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001594static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001595 unsigned char *master,
Manuel Pégourié-Gonnard0d56aaa2019-05-03 09:58:33 +02001596 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001597{
1598 int ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001599
1600 /* cf. RFC 5246, Section 8.1:
1601 * "The master secret is always exactly 48 bytes in length." */
1602 size_t const master_secret_len = 48;
1603
1604#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1605 unsigned char session_hash[48];
1606#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1607
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001608 /* The label for the KDF used for key expansion.
1609 * This is either "master secret" or "extended master secret"
1610 * depending on whether the Extended Master Secret extension
1611 * is used. */
1612 char const *lbl = "master secret";
1613
1614 /* The salt for the KDF used for key expansion.
1615 * - If the Extended Master Secret extension is not used,
1616 * this is ClientHello.Random + ServerHello.Random
1617 * (see Sect. 8.1 in RFC 5246).
1618 * - If the Extended Master Secret extension is used,
1619 * this is the transcript of the handshake so far.
1620 * (see Sect. 4 in RFC 7627). */
1621 unsigned char const *salt = handshake->randbytes;
1622 size_t salt_len = 64;
1623
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001624#if !defined(MBEDTLS_DEBUG_C) && \
1625 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1626 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
1627 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
1628 (void) ssl;
1629#endif
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001630
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001631 if( handshake->resume != 0 )
1632 {
1633 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001634 return( 0 );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001635 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001636
1637#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001638 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001639 {
1640 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001641
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001642 lbl = "extended master secret";
1643 salt = session_hash;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001644 handshake->calc_verify( ssl, session_hash, &salt_len );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001645
1646 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
1647 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001648#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1649
1650#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001651 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1652 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001653 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001654 ssl_use_opaque_psk( ssl ) == 1 )
1655 {
1656 /* Perform PSK-to-MS expansion in a single step. */
1657 psa_status_t status;
1658 psa_algorithm_t alg;
1659 psa_key_handle_t psk;
1660 psa_key_derivation_operation_t derivation =
1661 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001662 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001663
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001665
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001666 psk = ssl->conf->psk_opaque;
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001667 if( handshake->psk_opaque != 0 )
1668 psk = handshake->psk_opaque;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001669
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001670 if( hash_alg == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001671 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001672 else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001673 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1674
1675 status = psa_key_derivation( &derivation, psk, alg,
1676 salt, salt_len,
1677 (unsigned char const *) lbl,
1678 (size_t) strlen( lbl ),
1679 master_secret_len );
1680 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001681 {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001682 psa_key_derivation_abort( &derivation );
1683 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001684 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001685
1686 status = psa_key_derivation_output_bytes( &derivation,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001687 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001688 master_secret_len );
1689 if( status != PSA_SUCCESS )
1690 {
1691 psa_key_derivation_abort( &derivation );
1692 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1693 }
1694
1695 status = psa_key_derivation_abort( &derivation );
1696 if( status != PSA_SUCCESS )
1697 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1698 }
1699 else
1700#endif
1701 {
1702 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1703 lbl, salt, salt_len,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001704 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001705 master_secret_len );
1706 if( ret != 0 )
1707 {
1708 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1709 return( ret );
1710 }
1711
1712 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1713 handshake->premaster,
1714 handshake->pmslen );
1715
1716 mbedtls_platform_zeroize( handshake->premaster,
1717 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001718 }
1719
1720 return( 0 );
1721}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001722
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001723int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1724{
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001725 int ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001726 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1727 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001728
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001729 ret = ssl_set_handshake_prfs( ssl->handshake,
1730 ssl->minor_ver,
1731 ciphersuite_info->mac );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001732 if( ret != 0 )
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001733 {
1734 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001735 return( ret );
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001736 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001737
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001738 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001739 ssl->session_negotiate->master,
1740 ssl );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001741 if( ret != 0 )
1742 {
1743 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1744 return( ret );
1745 }
1746
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001747 return( ssl_populate_transform( ssl ) );
1748}
1749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001751void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1752 unsigned char hash[36],
1753 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001754{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001755 mbedtls_md5_context md5;
1756 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001757 unsigned char pad_1[48];
1758 unsigned char pad_2[48];
1759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001761
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001762 mbedtls_md5_init( &md5 );
1763 mbedtls_sha1_init( &sha1 );
1764
1765 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1766 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001767
Paul Bakker380da532012-04-18 16:10:25 +00001768 memset( pad_1, 0x36, 48 );
1769 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001770
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001771 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1772 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1773 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001774
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001775 mbedtls_md5_starts_ret( &md5 );
1776 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1777 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1778 mbedtls_md5_update_ret( &md5, hash, 16 );
1779 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001780
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001781 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1782 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1783 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001784
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001785 mbedtls_sha1_starts_ret( &sha1 );
1786 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1787 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1788 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1789 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001790
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001791 *hlen = 36;
1792
1793 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001795
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001796 mbedtls_md5_free( &md5 );
1797 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001798
Paul Bakker380da532012-04-18 16:10:25 +00001799 return;
1800}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001804void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1805 unsigned char hash[36],
1806 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001807{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001808 mbedtls_md5_context md5;
1809 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001812
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001813 mbedtls_md5_init( &md5 );
1814 mbedtls_sha1_init( &sha1 );
1815
1816 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1817 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001818
Andrzej Kurekeb342242019-01-29 09:14:33 -05001819 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001820 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001821
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001822 *hlen = 36;
1823
1824 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001826
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001827 mbedtls_md5_free( &md5 );
1828 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001829
Paul Bakker380da532012-04-18 16:10:25 +00001830 return;
1831}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1835#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001836void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1837 unsigned char hash[32],
1838 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001839{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001840#if defined(MBEDTLS_USE_PSA_CRYPTO)
1841 size_t hash_size;
1842 psa_status_t status;
1843 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1844
1845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1846 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1847 if( status != PSA_SUCCESS )
1848 {
1849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1850 return;
1851 }
1852
1853 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1854 if( status != PSA_SUCCESS )
1855 {
1856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1857 return;
1858 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001859
1860 *hlen = 32;
1861 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1863#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001864 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001865
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001866 mbedtls_sha256_init( &sha256 );
1867
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001869
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001870 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001871 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001872
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001873 *hlen = 32;
1874
1875 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001877
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001878 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001879#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001880 return;
1881}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001885void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1886 unsigned char hash[48],
1887 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001888{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001889#if defined(MBEDTLS_USE_PSA_CRYPTO)
1890 size_t hash_size;
1891 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001892 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001893
1894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001895 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001896 if( status != PSA_SUCCESS )
1897 {
1898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1899 return;
1900 }
1901
Andrzej Kurek972fba52019-01-30 03:29:12 -05001902 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001903 if( status != PSA_SUCCESS )
1904 {
1905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1906 return;
1907 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001908
1909 *hlen = 48;
1910 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1912#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001913 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001914
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001915 mbedtls_sha512_init( &sha512 );
1916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001918
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001919 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001920 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001921
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001922 *hlen = 48;
1923
1924 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001926
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001927 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001928#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 return;
1930}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931#endif /* MBEDTLS_SHA512_C */
1932#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1935int 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 +02001936{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001937 unsigned char *p = ssl->handshake->premaster;
1938 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001939 const unsigned char *psk = ssl->conf->psk;
1940 size_t psk_len = ssl->conf->psk_len;
1941
1942 /* If the psk callback was called, use its result */
1943 if( ssl->handshake->psk != NULL )
1944 {
1945 psk = ssl->handshake->psk;
1946 psk_len = ssl->handshake->psk_len;
1947 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001948
1949 /*
1950 * PMS = struct {
1951 * opaque other_secret<0..2^16-1>;
1952 * opaque psk<0..2^16-1>;
1953 * };
1954 * with "other_secret" depending on the particular key exchange
1955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1957 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001958 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001959 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001961
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001962 *(p++) = (unsigned char)( psk_len >> 8 );
1963 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001964
1965 if( end < p || (size_t)( end - p ) < psk_len )
1966 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1967
1968 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001969 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001970 }
1971 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1973#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1974 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001975 {
1976 /*
1977 * other_secret already set by the ClientKeyExchange message,
1978 * and is 48 bytes long
1979 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001980 if( end - p < 2 )
1981 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1982
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001983 *p++ = 0;
1984 *p++ = 48;
1985 p += 48;
1986 }
1987 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1989#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1990 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001991 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001992 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001993 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001994
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001995 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001997 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001998 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002001 return( ret );
2002 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002003 *(p++) = (unsigned char)( len >> 8 );
2004 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002005 p += len;
2006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002008 }
2009 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2011#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2012 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002013 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002014 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002015 size_t zlen;
2016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002018 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002019 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002022 return( ret );
2023 }
2024
2025 *(p++) = (unsigned char)( zlen >> 8 );
2026 *(p++) = (unsigned char)( zlen );
2027 p += zlen;
2028
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002029 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2030 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002031 }
2032 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2036 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002037 }
2038
2039 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002040 if( end - p < 2 )
2041 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002042
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002043 *(p++) = (unsigned char)( psk_len >> 8 );
2044 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002045
2046 if( end < p || (size_t)( end - p ) < psk_len )
2047 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2048
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002049 memcpy( p, psk, psk_len );
2050 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002051
2052 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2053
2054 return( 0 );
2055}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002059/*
2060 * SSLv3.0 MAC functions
2061 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002062#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002063static void ssl_mac( mbedtls_md_context_t *md_ctx,
2064 const unsigned char *secret,
2065 const unsigned char *buf, size_t len,
2066 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002067 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002068{
2069 unsigned char header[11];
2070 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002071 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2073 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002074
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002075 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002077 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002078 else
Paul Bakker68884e32013-01-07 18:20:04 +01002079 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002080
2081 memcpy( header, ctr, 8 );
2082 header[ 8] = (unsigned char) type;
2083 header[ 9] = (unsigned char)( len >> 8 );
2084 header[10] = (unsigned char)( len );
2085
Paul Bakker68884e32013-01-07 18:20:04 +01002086 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 mbedtls_md_starts( md_ctx );
2088 mbedtls_md_update( md_ctx, secret, md_size );
2089 mbedtls_md_update( md_ctx, padding, padlen );
2090 mbedtls_md_update( md_ctx, header, 11 );
2091 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002092 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002093
Paul Bakker68884e32013-01-07 18:20:04 +01002094 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 mbedtls_md_starts( md_ctx );
2096 mbedtls_md_update( md_ctx, secret, md_size );
2097 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002098 mbedtls_md_update( md_ctx, out, md_size );
2099 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002100}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002102
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002103/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002104 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002105#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002106 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2107 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2108 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2109/* This function makes sure every byte in the memory region is accessed
2110 * (in ascending addresses order) */
2111static void ssl_read_memory( unsigned char *p, size_t len )
2112{
2113 unsigned char acc = 0;
2114 volatile unsigned char force;
2115
2116 for( ; len != 0; p++, len-- )
2117 acc ^= *p;
2118
2119 force = acc;
2120 (void) force;
2121}
2122#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2123
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002124/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002126 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002127
Hanno Beckera0e20d02019-05-15 14:03:01 +01002128#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002129/* This functions transforms a DTLS plaintext fragment and a record content
2130 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002131 *
2132 * struct {
2133 * opaque content[DTLSPlaintext.length];
2134 * ContentType real_type;
2135 * uint8 zeros[length_of_padding];
2136 * } DTLSInnerPlaintext;
2137 *
2138 * Input:
2139 * - `content`: The beginning of the buffer holding the
2140 * plaintext to be wrapped.
2141 * - `*content_size`: The length of the plaintext in Bytes.
2142 * - `max_len`: The number of Bytes available starting from
2143 * `content`. This must be `>= *content_size`.
2144 * - `rec_type`: The desired record content type.
2145 *
2146 * Output:
2147 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2148 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2149 *
2150 * Returns:
2151 * - `0` on success.
2152 * - A negative error code if `max_len` didn't offer enough space
2153 * for the expansion.
2154 */
2155static int ssl_cid_build_inner_plaintext( unsigned char *content,
2156 size_t *content_size,
2157 size_t remaining,
2158 uint8_t rec_type )
2159{
2160 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002161 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2162 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2163 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002164
2165 /* Write real content type */
2166 if( remaining == 0 )
2167 return( -1 );
2168 content[ len ] = rec_type;
2169 len++;
2170 remaining--;
2171
2172 if( remaining < pad )
2173 return( -1 );
2174 memset( content + len, 0, pad );
2175 len += pad;
2176 remaining -= pad;
2177
2178 *content_size = len;
2179 return( 0 );
2180}
2181
Hanno Becker07dc97d2019-05-20 15:08:01 +01002182/* This function parses a DTLSInnerPlaintext structure.
2183 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002184static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2185 size_t *content_size,
2186 uint8_t *rec_type )
2187{
2188 size_t remaining = *content_size;
2189
2190 /* Determine length of padding by skipping zeroes from the back. */
2191 do
2192 {
2193 if( remaining == 0 )
2194 return( -1 );
2195 remaining--;
2196 } while( content[ remaining ] == 0 );
2197
2198 *content_size = remaining;
2199 *rec_type = content[ remaining ];
2200
2201 return( 0 );
2202}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002203#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002204
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002205/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002206 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002207static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002208 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002209 mbedtls_record *rec )
2210{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002211 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002212 *
2213 * additional_data = seq_num + TLSCompressed.type +
2214 * TLSCompressed.version + TLSCompressed.length;
2215 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002216 * For the CID extension, this is extended as follows
2217 * (quoting draft-ietf-tls-dtls-connection-id-05,
2218 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002219 *
2220 * additional_data = seq_num + DTLSPlaintext.type +
2221 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002222 * cid +
2223 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002224 * length_of_DTLSInnerPlaintext;
2225 */
2226
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002227 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2228 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002229 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002230
Hanno Beckera0e20d02019-05-15 14:03:01 +01002231#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002232 if( rec->cid_len != 0 )
2233 {
2234 memcpy( add_data + 11, rec->cid, rec->cid_len );
2235 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2236 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2237 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2238 *add_data_len = 13 + 1 + rec->cid_len;
2239 }
2240 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002241#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002242 {
2243 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2244 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2245 *add_data_len = 13;
2246 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002247}
2248
Hanno Beckera18d1322018-01-03 14:27:32 +00002249int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2250 mbedtls_ssl_transform *transform,
2251 mbedtls_record *rec,
2252 int (*f_rng)(void *, unsigned char *, size_t),
2253 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002254{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002256 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002257 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002258 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002259 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002260 size_t post_avail;
2261
2262 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002263#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002264 ((void) ssl);
2265#endif
2266
2267 /* The PRNG is used for dynamic IV generation that's used
2268 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2269#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2270 ( defined(MBEDTLS_AES_C) || \
2271 defined(MBEDTLS_ARIA_C) || \
2272 defined(MBEDTLS_CAMELLIA_C) ) && \
2273 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2274 ((void) f_rng);
2275 ((void) p_rng);
2276#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002279
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002280 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002281 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2283 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2284 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002285 if( rec == NULL
2286 || rec->buf == NULL
2287 || rec->buf_len < rec->data_offset
2288 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002289#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002290 || rec->cid_len != 0
2291#endif
2292 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002293 {
2294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002296 }
2297
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002298 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002299 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002301 data, rec->data_len );
2302
2303 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2304
2305 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2306 {
2307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2308 (unsigned) rec->data_len,
2309 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2310 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2311 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002312
Hanno Beckera0e20d02019-05-15 14:03:01 +01002313#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002314 /*
2315 * Add CID information
2316 */
2317 rec->cid_len = transform->out_cid_len;
2318 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2319 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002320
2321 if( rec->cid_len != 0 )
2322 {
2323 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002324 * Wrap plaintext into DTLSInnerPlaintext structure.
2325 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002326 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002327 * Note that this changes `rec->data_len`, and hence
2328 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002329 */
2330 if( ssl_cid_build_inner_plaintext( data,
2331 &rec->data_len,
2332 post_avail,
2333 rec->type ) != 0 )
2334 {
2335 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2336 }
2337
2338 rec->type = MBEDTLS_SSL_MSG_CID;
2339 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002340#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002341
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002342 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2343
Paul Bakker5121ce52009-01-03 21:22:43 +00002344 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002345 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002346 */
Hanno Becker52344c22018-01-03 15:24:20 +00002347#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 if( mode == MBEDTLS_MODE_STREAM ||
2349 ( mode == MBEDTLS_MODE_CBC
2350#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002351 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002352#endif
2353 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002354 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002355 if( post_avail < transform->maclen )
2356 {
2357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2358 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2359 }
2360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002362 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002363 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002364 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002365 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2366 data, rec->data_len, rec->ctr, rec->type, mac );
2367 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002368 }
2369 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002370#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2372 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002373 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002374 {
Hanno Becker992b6872017-11-09 18:57:39 +00002375 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2376
Hanno Beckercab87e62019-04-29 13:52:53 +01002377 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002378
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002379 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002380 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002381 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2382 data, rec->data_len );
2383 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2384 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2385
2386 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002387 }
2388 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002389#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2392 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002393 }
2394
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002395 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2396 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002397
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002398 rec->data_len += transform->maclen;
2399 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002400 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002401 }
Hanno Becker52344c22018-01-03 15:24:20 +00002402#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002403
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002404 /*
2405 * Encrypt
2406 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2408 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002409 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002410 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002411 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002413 "including %d bytes of padding",
2414 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002416 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2417 transform->iv_enc, transform->ivlen,
2418 data, rec->data_len,
2419 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002422 return( ret );
2423 }
2424
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002425 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2428 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002429 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 }
Paul Bakker68884e32013-01-07 18:20:04 +01002431 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002433
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002434#if defined(MBEDTLS_GCM_C) || \
2435 defined(MBEDTLS_CCM_C) || \
2436 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002438 mode == MBEDTLS_MODE_CCM ||
2439 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002440 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002441 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002442 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002443 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002444
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002445 /* Check that there's space for both the authentication tag
2446 * and the explicit IV before and after the record content. */
2447 if( post_avail < transform->taglen ||
2448 rec->data_offset < explicit_iv_len )
2449 {
2450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2451 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2452 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002453
Paul Bakker68884e32013-01-07 18:20:04 +01002454 /*
2455 * Generate IV
2456 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002457 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2458 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002459 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002460 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002461 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2462 explicit_iv_len );
2463 /* Prefix record content with explicit IV. */
2464 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002465 }
2466 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2467 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002468 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002469 unsigned char i;
2470
2471 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2472
2473 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002474 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002475 }
2476 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002477 {
2478 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2480 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002481 }
2482
Hanno Beckercab87e62019-04-29 13:52:53 +01002483 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002484
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002485 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2486 iv, transform->ivlen );
2487 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002488 data - explicit_iv_len, explicit_iv_len );
2489 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002490 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002492 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002493 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002494
Paul Bakker68884e32013-01-07 18:20:04 +01002495 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002496 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002497 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002498
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002499 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002500 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002501 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002502 data, rec->data_len, /* source */
2503 data, &rec->data_len, /* destination */
2504 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002507 return( ret );
2508 }
2509
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002510 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2511 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002512
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002513 rec->data_len += transform->taglen + explicit_iv_len;
2514 rec->data_offset -= explicit_iv_len;
2515 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002516 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002517 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2520#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002521 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002524 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002525 size_t padlen, i;
2526 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002527
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002528 /* Currently we're always using minimal padding
2529 * (up to 255 bytes would be allowed). */
2530 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2531 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002532 padlen = 0;
2533
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002534 /* Check there's enough space in the buffer for the padding. */
2535 if( post_avail < padlen + 1 )
2536 {
2537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2538 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2539 }
2540
Paul Bakker5121ce52009-01-03 21:22:43 +00002541 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002542 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002543
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002544 rec->data_len += padlen + 1;
2545 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002548 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002549 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2550 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002551 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002552 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002553 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002554 if( f_rng == NULL )
2555 {
2556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2557 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2558 }
2559
2560 if( rec->data_offset < transform->ivlen )
2561 {
2562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2563 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2564 }
2565
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002566 /*
2567 * Generate IV
2568 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002569 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002570 if( ret != 0 )
2571 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002572
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002573 memcpy( data - transform->ivlen, transform->iv_enc,
2574 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002575
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002576 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002577#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002580 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002581 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002582 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002583
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002584 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2585 transform->iv_enc,
2586 transform->ivlen,
2587 data, rec->data_len,
2588 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002591 return( ret );
2592 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002593
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002594 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2597 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002598 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002601 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002602 {
2603 /*
2604 * Save IV in SSL3 and TLS1
2605 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002606 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2607 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002609 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002610#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002611 {
2612 data -= transform->ivlen;
2613 rec->data_offset -= transform->ivlen;
2614 rec->data_len += transform->ivlen;
2615 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002618 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002619 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002620 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2621
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002622 /*
2623 * MAC(MAC_write_key, seq_num +
2624 * TLSCipherText.type +
2625 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002626 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002627 * IV + // except for TLS 1.0
2628 * ENC(content + padding + padding_length));
2629 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002630
2631 if( post_avail < transform->maclen)
2632 {
2633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2634 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2635 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002636
Hanno Beckercab87e62019-04-29 13:52:53 +01002637 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002640 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002641 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002642
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002643 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002644 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002645 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2646 data, rec->data_len );
2647 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2648 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002649
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002650 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002651
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002652 rec->data_len += transform->maclen;
2653 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002654 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002655 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002657 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002658 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002660 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2663 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002664 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002666 /* Make extra sure authentication was performed, exactly once */
2667 if( auth_done != 1 )
2668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2670 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002671 }
2672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002674
2675 return( 0 );
2676}
2677
Hanno Becker605949f2019-07-12 08:23:59 +01002678int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002679 mbedtls_ssl_transform *transform,
2680 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002681{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002682 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002684 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002685#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002686 size_t padlen = 0, correct = 1;
2687#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002688 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002689 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002690 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002691
Hanno Beckera18d1322018-01-03 14:27:32 +00002692#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002693 ((void) ssl);
2694#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002697 if( rec == NULL ||
2698 rec->buf == NULL ||
2699 rec->buf_len < rec->data_offset ||
2700 rec->buf_len - rec->data_offset < rec->data_len )
2701 {
2702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002704 }
2705
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002706 data = rec->buf + rec->data_offset;
2707 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
Hanno Beckera0e20d02019-05-15 14:03:01 +01002709#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002710 /*
2711 * Match record's CID with incoming CID.
2712 */
Hanno Becker938489a2019-05-08 13:02:22 +01002713 if( rec->cid_len != transform->in_cid_len ||
2714 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2715 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002716 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002717 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002718#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2721 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002722 {
2723 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002724 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2725 transform->iv_dec,
2726 transform->ivlen,
2727 data, rec->data_len,
2728 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002731 return( ret );
2732 }
2733
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002734 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2737 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002738 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002739 }
Paul Bakker68884e32013-01-07 18:20:04 +01002740 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002742#if defined(MBEDTLS_GCM_C) || \
2743 defined(MBEDTLS_CCM_C) || \
2744 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002746 mode == MBEDTLS_MODE_CCM ||
2747 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002748 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002749 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002750 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002751
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002752 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002753 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002754 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002755
2756 /* Check that there's enough space for the explicit IV
2757 * (at the beginning of the record) and the MAC (at the
2758 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002759 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002762 "+ taglen (%d)", rec->data_len,
2763 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002765 }
Paul Bakker68884e32013-01-07 18:20:04 +01002766
Hanno Beckerd96a6522019-07-10 13:55:25 +01002767#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002768 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2769 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002770 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002771
Hanno Beckerd96a6522019-07-10 13:55:25 +01002772 /* Fixed */
2773 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2774 /* Explicit */
2775 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002776 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002777 else
2778#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2779#if defined(MBEDTLS_CHACHAPOLY_C)
2780 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002781 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002782 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002783 unsigned char i;
2784
2785 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2786
2787 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002788 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002789 }
2790 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002791#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002792 {
2793 /* Reminder if we ever add an AEAD mode with a different size */
2794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2795 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2796 }
2797
Hanno Beckerd96a6522019-07-10 13:55:25 +01002798 /* Group changes to data, data_len, and add_data, because
2799 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002800 data += explicit_iv_len;
2801 rec->data_offset += explicit_iv_len;
2802 rec->data_len -= explicit_iv_len + transform->taglen;
2803
Hanno Beckercab87e62019-04-29 13:52:53 +01002804 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002805 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002806 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002807
Hanno Beckerd96a6522019-07-10 13:55:25 +01002808 /* Because of the check above, we know that there are
2809 * explicit_iv_len Bytes preceeding data, and taglen
2810 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002811 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002812 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002813
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002814 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002815 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002816 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002817
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002818 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002819 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002820 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002821 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2822 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002823 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002824 data, rec->data_len,
2825 data, &olen,
2826 data + rec->data_len,
2827 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2832 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002833
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002834 return( ret );
2835 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002836 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002837
Hanno Beckerd96a6522019-07-10 13:55:25 +01002838 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002839 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2842 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002843 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002844 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002845 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2847#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002848 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002849 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002850 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002851 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002852
Paul Bakker5121ce52009-01-03 21:22:43 +00002853 /*
Paul Bakker45829992013-01-03 14:52:21 +01002854 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002855 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002857 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2858 {
2859 /* The ciphertext is prefixed with the CBC IV. */
2860 minlen += transform->ivlen;
2861 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002862#endif
Paul Bakker45829992013-01-03 14:52:21 +01002863
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002864 /* Size considerations:
2865 *
2866 * - The CBC cipher text must not be empty and hence
2867 * at least of size transform->ivlen.
2868 *
2869 * Together with the potential IV-prefix, this explains
2870 * the first of the two checks below.
2871 *
2872 * - The record must contain a MAC, either in plain or
2873 * encrypted, depending on whether Encrypt-then-MAC
2874 * is used or not.
2875 * - If it is, the message contains the IV-prefix,
2876 * the CBC ciphertext, and the MAC.
2877 * - If it is not, the padded plaintext, and hence
2878 * the CBC ciphertext, has at least length maclen + 1
2879 * because there is at least the padding length byte.
2880 *
2881 * As the CBC ciphertext is not empty, both cases give the
2882 * lower bound minlen + maclen + 1 on the record size, which
2883 * we test for in the second check below.
2884 */
2885 if( rec->data_len < minlen + transform->ivlen ||
2886 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002889 "+ 1 ) ( + expl IV )", rec->data_len,
2890 transform->ivlen,
2891 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002893 }
2894
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002895 /*
2896 * Authenticate before decrypt if enabled
2897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002899 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002900 {
Hanno Becker992b6872017-11-09 18:57:39 +00002901 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002904
Hanno Beckerd96a6522019-07-10 13:55:25 +01002905 /* Update data_len in tandem with add_data.
2906 *
2907 * The subtraction is safe because of the previous check
2908 * data_len >= minlen + maclen + 1.
2909 *
2910 * Afterwards, we know that data + data_len is followed by at
2911 * least maclen Bytes, which justifies the call to
2912 * mbedtls_ssl_safer_memcmp() below.
2913 *
2914 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002915 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002916 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002917
Hanno Beckerd96a6522019-07-10 13:55:25 +01002918 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002919 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2920 add_data_len );
2921 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2922 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002923 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2924 data, rec->data_len );
2925 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2926 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002927
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002928 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2929 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002930 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002931 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002932
Hanno Beckerd96a6522019-07-10 13:55:25 +01002933 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002934 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2935 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002939 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002940 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002941 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002943
2944 /*
2945 * Check length sanity
2946 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002947
2948 /* We know from above that data_len > minlen >= 0,
2949 * so the following check in particular implies that
2950 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002951 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002954 rec->data_len, transform->ivlen ) );
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 }
2957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002959 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002960 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002961 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002962 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002963 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002964 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002965 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002966
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002967 data += transform->ivlen;
2968 rec->data_offset += transform->ivlen;
2969 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002970 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002972
Hanno Beckerd96a6522019-07-10 13:55:25 +01002973 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
2974
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002975 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2976 transform->iv_dec, transform->ivlen,
2977 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002980 return( ret );
2981 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002982
Hanno Beckerd96a6522019-07-10 13:55:25 +01002983 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002984 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2987 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002988 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002991 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002992 {
2993 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002994 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
2995 * records is equivalent to CBC decryption of the concatenation
2996 * of the records; in other words, IVs are maintained across
2997 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02002998 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002999 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3000 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003001 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003002#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003003
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003004 /* Safe since data_len >= minlen + maclen + 1, so after having
3005 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01003006 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3007 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003008 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003009
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003010 if( auth_done == 1 )
3011 {
3012 correct *= ( rec->data_len >= padlen + 1 );
3013 padlen *= ( rec->data_len >= padlen + 1 );
3014 }
3015 else
Paul Bakker45829992013-01-03 14:52:21 +01003016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003018 if( rec->data_len < transform->maclen + padlen + 1 )
3019 {
3020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3021 rec->data_len,
3022 transform->maclen,
3023 padlen + 1 ) );
3024 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003025#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003026
3027 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3028 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003029 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003030
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003031 padlen++;
3032
3033 /* Regardless of the validity of the padding,
3034 * we have data_len >= padlen here. */
3035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003037 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003038 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003039 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041#if defined(MBEDTLS_SSL_DEBUG_ALL)
3042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003043 "should be no more than %d",
3044 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003045#endif
Paul Bakker45829992013-01-03 14:52:21 +01003046 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 }
3048 }
3049 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3051#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3052 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003053 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003054 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003055 /* The padding check involves a series of up to 256
3056 * consecutive memory reads at the end of the record
3057 * plaintext buffer. In order to hide the length and
3058 * validity of the padding, always perform exactly
3059 * `min(256,plaintext_len)` reads (but take into account
3060 * only the last `padlen` bytes for the padding check). */
3061 size_t pad_count = 0;
3062 size_t real_count = 0;
3063 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003064
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003065 /* Index of first padding byte; it has been ensured above
3066 * that the subtraction is safe. */
3067 size_t const padding_idx = rec->data_len - padlen;
3068 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3069 size_t const start_idx = rec->data_len - num_checks;
3070 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003071
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003072 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003073 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003074 real_count |= ( idx >= padding_idx );
3075 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003076 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003077 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003080 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003082#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003083 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003084 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003085 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3087 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3090 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003091 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003092
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003093 /* If the padding was found to be invalid, padlen == 0
3094 * and the subtraction is safe. If the padding was found valid,
3095 * padlen hasn't been changed and the previous assertion
3096 * data_len >= padlen still holds. */
3097 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003098 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003099 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003101 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3104 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003105 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003106
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003107#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003108 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003109 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003110#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003111
3112 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003113 * Authenticate if not done yet.
3114 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003115 */
Hanno Becker52344c22018-01-03 15:24:20 +00003116#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003117 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003118 {
Hanno Becker992b6872017-11-09 18:57:39 +00003119 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003120
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003121 /* If the initial value of padlen was such that
3122 * data_len < maclen + padlen + 1, then padlen
3123 * got reset to 1, and the initial check
3124 * data_len >= minlen + maclen + 1
3125 * guarantees that at this point we still
3126 * have at least data_len >= maclen.
3127 *
3128 * If the initial value of padlen was such that
3129 * data_len >= maclen + padlen + 1, then we have
3130 * subtracted either padlen + 1 (if the padding was correct)
3131 * or 0 (if the padding was incorrect) since then,
3132 * hence data_len >= maclen in any case.
3133 */
3134 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003135 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003138 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003139 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003140 ssl_mac( &transform->md_ctx_dec,
3141 transform->mac_dec,
3142 data, rec->data_len,
3143 rec->ctr, rec->type,
3144 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003145 }
3146 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3148#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3149 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003150 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003151 {
3152 /*
3153 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003154 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003155 *
3156 * Known timing attacks:
3157 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3158 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003159 * To compensate for different timings for the MAC calculation
3160 * depending on how much padding was removed (which is determined
3161 * by padlen), process extra_run more blocks through the hash
3162 * function.
3163 *
3164 * The formula in the paper is
3165 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3166 * where L1 is the size of the header plus the decrypted message
3167 * plus CBC padding and L2 is the size of the header plus the
3168 * decrypted message. This is for an underlying hash function
3169 * with 64-byte blocks.
3170 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3171 * correctly. We round down instead of up, so -56 is the correct
3172 * value for our calculations instead of -55.
3173 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003174 * Repeat the formula rather than defining a block_size variable.
3175 * This avoids requiring division by a variable at runtime
3176 * (which would be marginally less efficient and would require
3177 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003178 */
3179 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003180 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003181
3182 /*
3183 * The next two sizes are the minimum and maximum values of
3184 * in_msglen over all padlen values.
3185 *
3186 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003187 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003188 *
3189 * Note that max_len + maclen is never more than the buffer
3190 * length, as we previously did in_msglen -= maclen too.
3191 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003192 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003193 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3194
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003195 memset( tmp, 0, sizeof( tmp ) );
3196
3197 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003198 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003199#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3200 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003201 case MBEDTLS_MD_MD5:
3202 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003203 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003204 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003205 extra_run =
3206 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3207 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003208 break;
3209#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003210#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003211 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003212 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003213 extra_run =
3214 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3215 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003216 break;
3217#endif
3218 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3221 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003222
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003223 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003224
Hanno Beckercab87e62019-04-29 13:52:53 +01003225 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3226 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003227 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3228 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003229 /* Make sure we access everything even when padlen > 0. This
3230 * makes the synchronisation requirements for just-in-time
3231 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003232 ssl_read_memory( data + rec->data_len, padlen );
3233 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003234
3235 /* Call mbedtls_md_process at least once due to cache attacks
3236 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003237 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003238 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003239
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003240 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003241
3242 /* Make sure we access all the memory that could contain the MAC,
3243 * before we check it in the next code block. This makes the
3244 * synchronisation requirements for just-in-time Prime+Probe
3245 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003246 ssl_read_memory( data + min_len,
3247 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003248 }
3249 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3251 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3254 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003255 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003256
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003257#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003258 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3259 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003260#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003261
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003262 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3263 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003265#if defined(MBEDTLS_SSL_DEBUG_ALL)
3266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003267#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003268 correct = 0;
3269 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003270 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003271 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003272
3273 /*
3274 * Finally check the correct flag
3275 */
3276 if( correct == 0 )
3277 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003278#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003279
3280 /* Make extra sure authentication was performed, exactly once */
3281 if( auth_done != 1 )
3282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3284 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003285 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003286
Hanno Beckera0e20d02019-05-15 14:03:01 +01003287#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003288 if( rec->cid_len != 0 )
3289 {
3290 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3291 &rec->type );
3292 if( ret != 0 )
3293 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3294 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003295#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003298
3299 return( 0 );
3300}
3301
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003302#undef MAC_NONE
3303#undef MAC_PLAINTEXT
3304#undef MAC_CIPHERTEXT
3305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003307/*
3308 * Compression/decompression functions
3309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003311{
3312 int ret;
3313 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003314 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003315 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003316 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003319
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003320 if( len_pre == 0 )
3321 return( 0 );
3322
Paul Bakker2770fbd2012-07-03 13:30:23 +00003323 memcpy( msg_pre, ssl->out_msg, len_pre );
3324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003326 ssl->out_msglen ) );
3327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003329 ssl->out_msg, ssl->out_msglen );
3330
Paul Bakker48916f92012-09-16 19:57:18 +00003331 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3332 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3333 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003334 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003335
Paul Bakker48916f92012-09-16 19:57:18 +00003336 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003337 if( ret != Z_OK )
3338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3340 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003341 }
3342
Angus Grattond8213d02016-05-25 20:56:48 +10003343 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003344 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003347 ssl->out_msglen ) );
3348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003350 ssl->out_msg, ssl->out_msglen );
3351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003353
3354 return( 0 );
3355}
3356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003358{
3359 int ret;
3360 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003361 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003362 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003363 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003366
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003367 if( len_pre == 0 )
3368 return( 0 );
3369
Paul Bakker2770fbd2012-07-03 13:30:23 +00003370 memcpy( msg_pre, ssl->in_msg, len_pre );
3371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003373 ssl->in_msglen ) );
3374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003376 ssl->in_msg, ssl->in_msglen );
3377
Paul Bakker48916f92012-09-16 19:57:18 +00003378 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3379 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3380 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003381 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003382 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003383
Paul Bakker48916f92012-09-16 19:57:18 +00003384 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003385 if( ret != Z_OK )
3386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3388 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003389 }
3390
Angus Grattond8213d02016-05-25 20:56:48 +10003391 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003392 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003395 ssl->in_msglen ) );
3396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003398 ssl->in_msg, ssl->in_msglen );
3399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003401
3402 return( 0 );
3403}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3407static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409#if defined(MBEDTLS_SSL_PROTO_DTLS)
3410static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003411{
3412 /* If renegotiation is not enforced, retransmit until we would reach max
3413 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003414 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003415 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003416 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003417 unsigned char doublings = 1;
3418
3419 while( ratio != 0 )
3420 {
3421 ++doublings;
3422 ratio >>= 1;
3423 }
3424
3425 if( ++ssl->renego_records_seen > doublings )
3426 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003428 return( 0 );
3429 }
3430 }
3431
3432 return( ssl_write_hello_request( ssl ) );
3433}
3434#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003436
Paul Bakker5121ce52009-01-03 21:22:43 +00003437/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003438 * Fill the input message buffer by appending data to it.
3439 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003440 *
3441 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3442 * available (from this read and/or a previous one). Otherwise, an error code
3443 * is returned (possibly EOF or WANT_READ).
3444 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003445 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3446 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3447 * since we always read a whole datagram at once.
3448 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003449 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003450 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003451 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003453{
Paul Bakker23986e52011-04-24 08:57:21 +00003454 int ret;
3455 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003458
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003459 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003462 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003463 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003464 }
3465
Angus Grattond8213d02016-05-25 20:56:48 +10003466 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3469 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003470 }
3471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003473 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003474 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003475 uint32_t timeout;
3476
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003477 /* Just to be sure */
3478 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3479 {
3480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3481 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3483 }
3484
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003485 /*
3486 * The point is, we need to always read a full datagram at once, so we
3487 * sometimes read more then requested, and handle the additional data.
3488 * It could be the rest of the current record (while fetching the
3489 * header) and/or some other records in the same datagram.
3490 */
3491
3492 /*
3493 * Move to the next record in the already read datagram if applicable
3494 */
3495 if( ssl->next_record_offset != 0 )
3496 {
3497 if( ssl->in_left < ssl->next_record_offset )
3498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3500 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003501 }
3502
3503 ssl->in_left -= ssl->next_record_offset;
3504
3505 if( ssl->in_left != 0 )
3506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003508 ssl->next_record_offset ) );
3509 memmove( ssl->in_hdr,
3510 ssl->in_hdr + ssl->next_record_offset,
3511 ssl->in_left );
3512 }
3513
3514 ssl->next_record_offset = 0;
3515 }
3516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003518 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003519
3520 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003521 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003522 */
3523 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003526 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003527 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003528
3529 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003530 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003531 * are not at the beginning of a new record, the caller did something
3532 * wrong.
3533 */
3534 if( ssl->in_left != 0 )
3535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3537 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003538 }
3539
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003540 /*
3541 * Don't even try to read if time's out already.
3542 * This avoids by-passing the timer when repeatedly receiving messages
3543 * that will end up being dropped.
3544 */
3545 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003546 {
3547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003548 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003549 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003550 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003551 {
Angus Grattond8213d02016-05-25 20:56:48 +10003552 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003555 timeout = ssl->handshake->retransmit_timeout;
3556 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003557 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003560
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003561 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003562 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3563 timeout );
3564 else
3565 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003568
3569 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003571 }
3572
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003573 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003576 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003579 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003580 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003583 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003584 }
3585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003586 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003589 return( ret );
3590 }
3591
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003592 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003593 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003595 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003597 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003598 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003601 return( ret );
3602 }
3603
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003604 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003605 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003606#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003607 }
3608
Paul Bakker5121ce52009-01-03 21:22:43 +00003609 if( ret < 0 )
3610 return( ret );
3611
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003612 ssl->in_left = ret;
3613 }
3614 else
3615#endif
3616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003618 ssl->in_left, nb_want ) );
3619
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003620 while( ssl->in_left < nb_want )
3621 {
3622 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003623
3624 if( ssl_check_timer( ssl ) != 0 )
3625 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3626 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003627 {
3628 if( ssl->f_recv_timeout != NULL )
3629 {
3630 ret = ssl->f_recv_timeout( ssl->p_bio,
3631 ssl->in_hdr + ssl->in_left, len,
3632 ssl->conf->read_timeout );
3633 }
3634 else
3635 {
3636 ret = ssl->f_recv( ssl->p_bio,
3637 ssl->in_hdr + ssl->in_left, len );
3638 }
3639 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003642 ssl->in_left, nb_want ) );
3643 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003644
3645 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003647
3648 if( ret < 0 )
3649 return( ret );
3650
mohammad160352aecb92018-03-28 23:41:40 -07003651 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003652 {
Darryl Green11999bb2018-03-13 15:22:58 +00003653 MBEDTLS_SSL_DEBUG_MSG( 1,
3654 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003655 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003656 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3657 }
3658
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003659 ssl->in_left += ret;
3660 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003661 }
3662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003664
3665 return( 0 );
3666}
3667
3668/*
3669 * Flush any data not yet written
3670 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003672{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003673 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003674 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003677
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003678 if( ssl->f_send == NULL )
3679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003681 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003682 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003683 }
3684
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003685 /* Avoid incrementing counter if data is flushed */
3686 if( ssl->out_left == 0 )
3687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003689 return( 0 );
3690 }
3691
Paul Bakker5121ce52009-01-03 21:22:43 +00003692 while( ssl->out_left > 0 )
3693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003695 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003696
Hanno Becker2b1e3542018-08-06 11:19:13 +01003697 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003698 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003700 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003701
3702 if( ret <= 0 )
3703 return( ret );
3704
mohammad160352aecb92018-03-28 23:41:40 -07003705 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003706 {
Darryl Green11999bb2018-03-13 15:22:58 +00003707 MBEDTLS_SSL_DEBUG_MSG( 1,
3708 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003709 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003710 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3711 }
3712
Paul Bakker5121ce52009-01-03 21:22:43 +00003713 ssl->out_left -= ret;
3714 }
3715
Hanno Becker2b1e3542018-08-06 11:19:13 +01003716#if defined(MBEDTLS_SSL_PROTO_DTLS)
3717 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003718 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003719 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003720 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003721 else
3722#endif
3723 {
3724 ssl->out_hdr = ssl->out_buf + 8;
3725 }
3726 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003729
3730 return( 0 );
3731}
3732
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003733/*
3734 * Functions to handle the DTLS retransmission state machine
3735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003736#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003737/*
3738 * Append current handshake message to current outgoing flight
3739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003741{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003742 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3744 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3745 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003746
3747 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003748 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003749 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003751 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003752 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003753 }
3754
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003755 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003756 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003759 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003760 }
3761
3762 /* Copy current handshake message with headers */
3763 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3764 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003765 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003766 msg->next = NULL;
3767
3768 /* Append to the current flight */
3769 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003770 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003771 else
3772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003774 while( cur->next != NULL )
3775 cur = cur->next;
3776 cur->next = msg;
3777 }
3778
Hanno Becker3b235902018-08-06 09:54:53 +01003779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003780 return( 0 );
3781}
3782
3783/*
3784 * Free the current flight of handshake messages
3785 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003786static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003787{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003788 mbedtls_ssl_flight_item *cur = flight;
3789 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003790
3791 while( cur != NULL )
3792 {
3793 next = cur->next;
3794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795 mbedtls_free( cur->p );
3796 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003797
3798 cur = next;
3799 }
3800}
3801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003802#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3803static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003804#endif
3805
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003806/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003807 * Swap transform_out and out_ctr with the alternative ones
3808 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003809static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003810{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003812 unsigned char tmp_out_ctr[8];
3813
3814 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003816 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003817 return;
3818 }
3819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003820 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003821
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003822 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003823 tmp_transform = ssl->transform_out;
3824 ssl->transform_out = ssl->handshake->alt_transform_out;
3825 ssl->handshake->alt_transform_out = tmp_transform;
3826
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003827 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003828 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3829 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003830 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003831
3832 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003833 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3836 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3841 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003842 }
3843 }
3844#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003845}
3846
3847/*
3848 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003849 */
3850int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3851{
3852 int ret = 0;
3853
3854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3855
3856 ret = mbedtls_ssl_flight_transmit( ssl );
3857
3858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3859
3860 return( ret );
3861}
3862
3863/*
3864 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003865 *
3866 * Need to remember the current message in case flush_output returns
3867 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003868 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003869 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003870int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003871{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003872 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003875 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003876 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003878
3879 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003880 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003881 ssl_swap_epochs( ssl );
3882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003884 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003885
3886 while( ssl->handshake->cur_msg != NULL )
3887 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003888 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003889 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003890
Hanno Beckere1dcb032018-08-17 16:47:58 +01003891 int const is_finished =
3892 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3893 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3894
Hanno Becker04da1892018-08-14 13:22:10 +01003895 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3896 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3897
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003898 /* Swap epochs before sending Finished: we can't do it after
3899 * sending ChangeCipherSpec, in case write returns WANT_READ.
3900 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003901 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003902 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003904 ssl_swap_epochs( ssl );
3905 }
3906
Hanno Becker67bc7c32018-08-06 11:33:50 +01003907 ret = ssl_get_remaining_payload_in_datagram( ssl );
3908 if( ret < 0 )
3909 return( ret );
3910 max_frag_len = (size_t) ret;
3911
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003912 /* CCS is copied as is, while HS messages may need fragmentation */
3913 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3914 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003915 if( max_frag_len == 0 )
3916 {
3917 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3918 return( ret );
3919
3920 continue;
3921 }
3922
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003923 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003924 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003925 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003926
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003927 /* Update position inside current message */
3928 ssl->handshake->cur_msg_p += cur->len;
3929 }
3930 else
3931 {
3932 const unsigned char * const p = ssl->handshake->cur_msg_p;
3933 const size_t hs_len = cur->len - 12;
3934 const size_t frag_off = p - ( cur->p + 12 );
3935 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003936 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003937
Hanno Beckere1dcb032018-08-17 16:47:58 +01003938 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003939 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003940 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003941 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003942
Hanno Becker67bc7c32018-08-06 11:33:50 +01003943 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3944 return( ret );
3945
3946 continue;
3947 }
3948 max_hs_frag_len = max_frag_len - 12;
3949
3950 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3951 max_hs_frag_len : rem_len;
3952
3953 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003954 {
3955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003956 (unsigned) cur_hs_frag_len,
3957 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003958 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003959
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003960 /* Messages are stored with handshake headers as if not fragmented,
3961 * copy beginning of headers then fill fragmentation fields.
3962 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3963 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003964
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003965 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3966 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3967 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3968
Hanno Becker67bc7c32018-08-06 11:33:50 +01003969 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3970 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3971 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003972
3973 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3974
Hanno Becker3f7b9732018-08-28 09:53:25 +01003975 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003976 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3977 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003978 ssl->out_msgtype = cur->type;
3979
3980 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003981 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003982 }
3983
3984 /* If done with the current message move to the next one if any */
3985 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3986 {
3987 if( cur->next != NULL )
3988 {
3989 ssl->handshake->cur_msg = cur->next;
3990 ssl->handshake->cur_msg_p = cur->next->p + 12;
3991 }
3992 else
3993 {
3994 ssl->handshake->cur_msg = NULL;
3995 ssl->handshake->cur_msg_p = NULL;
3996 }
3997 }
3998
3999 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004000 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004003 return( ret );
4004 }
4005 }
4006
Hanno Becker67bc7c32018-08-06 11:33:50 +01004007 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4008 return( ret );
4009
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004010 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004011 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4012 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004013 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004016 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4017 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004018
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004020
4021 return( 0 );
4022}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004023
4024/*
4025 * To be called when the last message of an incoming flight is received.
4026 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004028{
4029 /* We won't need to resend that one any more */
4030 ssl_flight_free( ssl->handshake->flight );
4031 ssl->handshake->flight = NULL;
4032 ssl->handshake->cur_msg = NULL;
4033
4034 /* The next incoming flight will start with this msg_seq */
4035 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4036
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004037 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004038 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004039
Hanno Becker0271f962018-08-16 13:23:47 +01004040 /* Clear future message buffering structure. */
4041 ssl_buffering_free( ssl );
4042
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004043 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004044 ssl_set_timer( ssl, 0 );
4045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004046 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4047 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004049 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004050 }
4051 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004052 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004053}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004054
4055/*
4056 * To be called when the last message of an outgoing flight is send.
4057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004058void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004059{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004060 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004061 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004062
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é-Gonnard7de3c9e2014-09-29 15:29:48 +02004065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004067 }
4068 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004069 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004070}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004072
Paul Bakker5121ce52009-01-03 21:22:43 +00004073/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004074 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004075 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004076
4077/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004078 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004079 *
4080 * - fill in handshake headers
4081 * - update handshake checksum
4082 * - DTLS: save message for resending
4083 * - then pass to the record layer
4084 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004085 * DTLS: except for HelloRequest, messages are only queued, and will only be
4086 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004087 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004088 * Inputs:
4089 * - ssl->out_msglen: 4 + actual handshake message len
4090 * (4 is the size of handshake headers for TLS)
4091 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4092 * - ssl->out_msg + 4: the handshake message body
4093 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004094 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004095 * - ssl->out_msglen: the length of the record contents
4096 * (including handshake headers but excluding record headers)
4097 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004098 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004099int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004100{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004101 int ret;
4102 const size_t hs_len = ssl->out_msglen - 4;
4103 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004104
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4106
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004107 /*
4108 * Sanity checks
4109 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004110 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004111 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4112 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004113 /* In SSLv3, the client might send a NoCertificate alert. */
4114#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4115 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4116 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4117 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4118#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4119 {
4120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4121 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4122 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004123 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004124
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004125 /* Whenever we send anything different from a
4126 * HelloRequest we should be in a handshake - double check. */
4127 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4128 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004129 ssl->handshake == NULL )
4130 {
4131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4132 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4133 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004135#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004136 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004137 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004138 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004139 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4141 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004142 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004143#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004144
Hanno Beckerb50a2532018-08-06 11:52:54 +01004145 /* Double-check that we did not exceed the bounds
4146 * of the outgoing record buffer.
4147 * This should never fail as the various message
4148 * writing functions must obey the bounds of the
4149 * outgoing record buffer, but better be safe.
4150 *
4151 * Note: We deliberately do not check for the MTU or MFL here.
4152 */
4153 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4154 {
4155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4156 "size %u, maximum %u",
4157 (unsigned) ssl->out_msglen,
4158 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4159 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4160 }
4161
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004162 /*
4163 * Fill handshake headers
4164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004165 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004166 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004167 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4168 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4169 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004170
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004171 /*
4172 * DTLS has additional fields in the Handshake layer,
4173 * between the length field and the actual payload:
4174 * uint16 message_seq;
4175 * uint24 fragment_offset;
4176 * uint24 fragment_length;
4177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004178#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004179 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004180 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004181 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004182 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004183 {
4184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4185 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004186 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004187 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4189 }
4190
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004191 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004192 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004193
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004194 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004195 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004196 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004197 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4198 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4199 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004200 }
4201 else
4202 {
4203 ssl->out_msg[4] = 0;
4204 ssl->out_msg[5] = 0;
4205 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004206
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004207 /* Handshake hashes are computed without fragmentation,
4208 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004209 memset( ssl->out_msg + 6, 0x00, 3 );
4210 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004211 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004213
Hanno Becker0207e532018-08-28 10:28:28 +01004214 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004215 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4216 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004217 }
4218
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004219 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004221 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004222 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4223 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004224 {
4225 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004227 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004228 return( ret );
4229 }
4230 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004231 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004232#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004233 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004234 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004235 {
4236 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4237 return( ret );
4238 }
4239 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004240
4241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4242
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004243 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004244}
4245
4246/*
4247 * Record layer functions
4248 */
4249
4250/*
4251 * Write current record.
4252 *
4253 * Uses:
4254 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4255 * - ssl->out_msglen: length of the record content (excl headers)
4256 * - ssl->out_msg: record content
4257 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004258int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004259{
4260 int ret, done = 0;
4261 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004262 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004263
4264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004267 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004268 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004269 {
4270 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004272 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004273 return( ret );
4274 }
4275
4276 len = ssl->out_msglen;
4277 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004280#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4281 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004285 ret = mbedtls_ssl_hw_record_write( ssl );
4286 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004288 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4289 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004290 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004291
4292 if( ret == 0 )
4293 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004294 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004295#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004296 if( !done )
4297 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004298 unsigned i;
4299 size_t protected_record_size;
4300
Hanno Becker6430faf2019-05-08 11:57:13 +01004301 /* Skip writing the record content type to after the encryption,
4302 * as it may change when using the CID extension. */
4303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004304 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004305 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004306
Hanno Becker19859472018-08-06 09:40:20 +01004307 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004308 ssl->out_len[0] = (unsigned char)( len >> 8 );
4309 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004310
Paul Bakker48916f92012-09-16 19:57:18 +00004311 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004312 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004313 mbedtls_record rec;
4314
4315 rec.buf = ssl->out_iv;
4316 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4317 ( ssl->out_iv - ssl->out_buf );
4318 rec.data_len = ssl->out_msglen;
4319 rec.data_offset = ssl->out_msg - rec.buf;
4320
4321 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4322 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4323 ssl->conf->transport, rec.ver );
4324 rec.type = ssl->out_msgtype;
4325
Hanno Beckera0e20d02019-05-15 14:03:01 +01004326#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004327 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004328 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004329#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004330
Hanno Beckera18d1322018-01-03 14:27:32 +00004331 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004332 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004335 return( ret );
4336 }
4337
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004338 if( rec.data_offset != 0 )
4339 {
4340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4341 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4342 }
4343
Hanno Becker6430faf2019-05-08 11:57:13 +01004344 /* Update the record content type and CID. */
4345 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004346#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004347 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004348#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004349 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004350 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4351 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004352 }
4353
Hanno Becker5903de42019-05-03 14:46:38 +01004354 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004355
4356#if defined(MBEDTLS_SSL_PROTO_DTLS)
4357 /* In case of DTLS, double-check that we don't exceed
4358 * the remaining space in the datagram. */
4359 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4360 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004361 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004362 if( ret < 0 )
4363 return( ret );
4364
4365 if( protected_record_size > (size_t) ret )
4366 {
4367 /* Should never happen */
4368 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4369 }
4370 }
4371#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004372
Hanno Becker6430faf2019-05-08 11:57:13 +01004373 /* Now write the potentially updated record content type. */
4374 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004376 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004377 "version = [%d:%d], msglen = %d",
4378 ssl->out_hdr[0], ssl->out_hdr[1],
4379 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004382 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004383
4384 ssl->out_left += protected_record_size;
4385 ssl->out_hdr += protected_record_size;
4386 ssl_update_out_pointers( ssl, ssl->transform_out );
4387
Hanno Becker04484622018-08-06 09:49:38 +01004388 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4389 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4390 break;
4391
4392 /* The loop goes to its end iff the counter is wrapping */
4393 if( i == ssl_ep_len( ssl ) )
4394 {
4395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4396 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004398 }
4399
Hanno Becker67bc7c32018-08-06 11:33:50 +01004400#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004401 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4402 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004403 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004404 size_t remaining;
4405 ret = ssl_get_remaining_payload_in_datagram( ssl );
4406 if( ret < 0 )
4407 {
4408 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4409 ret );
4410 return( ret );
4411 }
4412
4413 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004414 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004415 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004416 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004417 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004418 else
4419 {
Hanno Becker513815a2018-08-20 11:56:09 +01004420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004421 }
4422 }
4423#endif /* MBEDTLS_SSL_PROTO_DTLS */
4424
4425 if( ( flush == SSL_FORCE_FLUSH ) &&
4426 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004428 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004429 return( ret );
4430 }
4431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004433
4434 return( 0 );
4435}
4436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004437#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004438
4439static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4440{
4441 if( ssl->in_msglen < ssl->in_hslen ||
4442 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4443 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4444 {
4445 return( 1 );
4446 }
4447 return( 0 );
4448}
Hanno Becker44650b72018-08-16 12:51:11 +01004449
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004450static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004451{
4452 return( ( ssl->in_msg[9] << 16 ) |
4453 ( ssl->in_msg[10] << 8 ) |
4454 ssl->in_msg[11] );
4455}
4456
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004457static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004458{
4459 return( ( ssl->in_msg[6] << 16 ) |
4460 ( ssl->in_msg[7] << 8 ) |
4461 ssl->in_msg[8] );
4462}
4463
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004464static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004465{
4466 uint32_t msg_len, frag_off, frag_len;
4467
4468 msg_len = ssl_get_hs_total_len( ssl );
4469 frag_off = ssl_get_hs_frag_off( ssl );
4470 frag_len = ssl_get_hs_frag_len( ssl );
4471
4472 if( frag_off > msg_len )
4473 return( -1 );
4474
4475 if( frag_len > msg_len - frag_off )
4476 return( -1 );
4477
4478 if( frag_len + 12 > ssl->in_msglen )
4479 return( -1 );
4480
4481 return( 0 );
4482}
4483
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004484/*
4485 * Mark bits in bitmask (used for DTLS HS reassembly)
4486 */
4487static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4488{
4489 unsigned int start_bits, end_bits;
4490
4491 start_bits = 8 - ( offset % 8 );
4492 if( start_bits != 8 )
4493 {
4494 size_t first_byte_idx = offset / 8;
4495
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004496 /* Special case */
4497 if( len <= start_bits )
4498 {
4499 for( ; len != 0; len-- )
4500 mask[first_byte_idx] |= 1 << ( start_bits - len );
4501
4502 /* Avoid potential issues with offset or len becoming invalid */
4503 return;
4504 }
4505
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004506 offset += start_bits; /* Now offset % 8 == 0 */
4507 len -= start_bits;
4508
4509 for( ; start_bits != 0; start_bits-- )
4510 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4511 }
4512
4513 end_bits = len % 8;
4514 if( end_bits != 0 )
4515 {
4516 size_t last_byte_idx = ( offset + len ) / 8;
4517
4518 len -= end_bits; /* Now len % 8 == 0 */
4519
4520 for( ; end_bits != 0; end_bits-- )
4521 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4522 }
4523
4524 memset( mask + offset / 8, 0xFF, len / 8 );
4525}
4526
4527/*
4528 * Check that bitmask is full
4529 */
4530static int ssl_bitmask_check( unsigned char *mask, size_t len )
4531{
4532 size_t i;
4533
4534 for( i = 0; i < len / 8; i++ )
4535 if( mask[i] != 0xFF )
4536 return( -1 );
4537
4538 for( i = 0; i < len % 8; i++ )
4539 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4540 return( -1 );
4541
4542 return( 0 );
4543}
4544
Hanno Becker56e205e2018-08-16 09:06:12 +01004545/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004546static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004547 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004548{
Hanno Becker56e205e2018-08-16 09:06:12 +01004549 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004550
Hanno Becker56e205e2018-08-16 09:06:12 +01004551 alloc_len = 12; /* Handshake header */
4552 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004553
Hanno Beckerd07df862018-08-16 09:14:58 +01004554 if( add_bitmap )
4555 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004556
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004557 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004558}
Hanno Becker56e205e2018-08-16 09:06:12 +01004559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004560#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004561
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004562static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004563{
4564 return( ( ssl->in_msg[1] << 16 ) |
4565 ( ssl->in_msg[2] << 8 ) |
4566 ssl->in_msg[3] );
4567}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004568
Simon Butcher99000142016-10-13 17:21:01 +01004569int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004570{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004571 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004574 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004575 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004576 }
4577
Hanno Becker12555c62018-08-16 12:47:53 +01004578 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004580 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004581 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004582 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004584#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004585 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004586 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004587 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004588 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004589
Hanno Becker44650b72018-08-16 12:51:11 +01004590 if( ssl_check_hs_header( ssl ) != 0 )
4591 {
4592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4593 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4594 }
4595
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004596 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004597 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4598 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4599 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4600 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004601 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004602 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4603 {
4604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4605 recv_msg_seq,
4606 ssl->handshake->in_msg_seq ) );
4607 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4608 }
4609
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004610 /* Retransmit only on last message from previous flight, to avoid
4611 * too many retransmissions.
4612 * Besides, No sane server ever retransmits HelloVerifyRequest */
4613 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004614 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004617 "message_seq = %d, start_of_flight = %d",
4618 recv_msg_seq,
4619 ssl->handshake->in_flight_start_seq ) );
4620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004621 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004624 return( ret );
4625 }
4626 }
4627 else
4628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004630 "message_seq = %d, expected = %d",
4631 recv_msg_seq,
4632 ssl->handshake->in_msg_seq ) );
4633 }
4634
Hanno Becker90333da2017-10-10 11:27:13 +01004635 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004636 }
4637 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004638
Hanno Becker6d97ef52018-08-16 13:09:04 +01004639 /* Message reassembly is handled alongside buffering of future
4640 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004641 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004642 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004643 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004645 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004646 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004647 }
4648 }
4649 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004651 /* With TLS we don't handle fragmentation (for now) */
4652 if( ssl->in_msglen < ssl->in_hslen )
4653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4655 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004656 }
4657
Simon Butcher99000142016-10-13 17:21:01 +01004658 return( 0 );
4659}
4660
4661void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4662{
Hanno Becker0271f962018-08-16 13:23:47 +01004663 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004664
Hanno Becker0271f962018-08-16 13:23:47 +01004665 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004666 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004667 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004668 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004669
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004670 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004671#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004672 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004673 ssl->handshake != NULL )
4674 {
Hanno Becker0271f962018-08-16 13:23:47 +01004675 unsigned offset;
4676 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004677
Hanno Becker0271f962018-08-16 13:23:47 +01004678 /* Increment handshake sequence number */
4679 hs->in_msg_seq++;
4680
4681 /*
4682 * Clear up handshake buffering and reassembly structure.
4683 */
4684
4685 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004686 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004687
4688 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004689 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4690 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004691 offset++, hs_buf++ )
4692 {
4693 *hs_buf = *(hs_buf + 1);
4694 }
4695
4696 /* Create a fresh last entry */
4697 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004698 }
4699#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004700}
4701
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004702/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004703 * DTLS anti-replay: RFC 6347 4.1.2.6
4704 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004705 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4706 * Bit n is set iff record number in_window_top - n has been seen.
4707 *
4708 * Usually, in_window_top is the last record number seen and the lsb of
4709 * in_window is set. The only exception is the initial state (record number 0
4710 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4713static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004714{
4715 ssl->in_window_top = 0;
4716 ssl->in_window = 0;
4717}
4718
4719static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4720{
4721 return( ( (uint64_t) buf[0] << 40 ) |
4722 ( (uint64_t) buf[1] << 32 ) |
4723 ( (uint64_t) buf[2] << 24 ) |
4724 ( (uint64_t) buf[3] << 16 ) |
4725 ( (uint64_t) buf[4] << 8 ) |
4726 ( (uint64_t) buf[5] ) );
4727}
4728
4729/*
4730 * Return 0 if sequence number is acceptable, -1 otherwise
4731 */
Hanno Becker0183d692019-07-12 08:50:37 +01004732int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004733{
4734 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4735 uint64_t bit;
4736
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004737 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004738 return( 0 );
4739
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004740 if( rec_seqnum > ssl->in_window_top )
4741 return( 0 );
4742
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004743 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004744
4745 if( bit >= 64 )
4746 return( -1 );
4747
4748 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4749 return( -1 );
4750
4751 return( 0 );
4752}
4753
4754/*
4755 * Update replay window on new validated record
4756 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004757void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004758{
4759 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4760
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004761 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004762 return;
4763
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004764 if( rec_seqnum > ssl->in_window_top )
4765 {
4766 /* Update window_top and the contents of the window */
4767 uint64_t shift = rec_seqnum - ssl->in_window_top;
4768
4769 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004770 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004771 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004772 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004773 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004774 ssl->in_window |= 1;
4775 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004776
4777 ssl->in_window_top = rec_seqnum;
4778 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004779 else
4780 {
4781 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004782 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004783
4784 if( bit < 64 ) /* Always true, but be extra sure */
4785 ssl->in_window |= (uint64_t) 1 << bit;
4786 }
4787}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004788#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004789
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004790#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004791/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004792static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4793
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004794/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004795 * Without any SSL context, check if a datagram looks like a ClientHello with
4796 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004797 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004798 *
4799 * - if cookie is valid, return 0
4800 * - if ClientHello looks superficially valid but cookie is not,
4801 * fill obuf and set olen, then
4802 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4803 * - otherwise return a specific error code
4804 */
4805static int ssl_check_dtls_clihlo_cookie(
4806 mbedtls_ssl_cookie_write_t *f_cookie_write,
4807 mbedtls_ssl_cookie_check_t *f_cookie_check,
4808 void *p_cookie,
4809 const unsigned char *cli_id, size_t cli_id_len,
4810 const unsigned char *in, size_t in_len,
4811 unsigned char *obuf, size_t buf_len, size_t *olen )
4812{
4813 size_t sid_len, cookie_len;
4814 unsigned char *p;
4815
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004816 /*
4817 * Structure of ClientHello with record and handshake headers,
4818 * and expected values. We don't need to check a lot, more checks will be
4819 * done when actually parsing the ClientHello - skipping those checks
4820 * avoids code duplication and does not make cookie forging any easier.
4821 *
4822 * 0-0 ContentType type; copied, must be handshake
4823 * 1-2 ProtocolVersion version; copied
4824 * 3-4 uint16 epoch; copied, must be 0
4825 * 5-10 uint48 sequence_number; copied
4826 * 11-12 uint16 length; (ignored)
4827 *
4828 * 13-13 HandshakeType msg_type; (ignored)
4829 * 14-16 uint24 length; (ignored)
4830 * 17-18 uint16 message_seq; copied
4831 * 19-21 uint24 fragment_offset; copied, must be 0
4832 * 22-24 uint24 fragment_length; (ignored)
4833 *
4834 * 25-26 ProtocolVersion client_version; (ignored)
4835 * 27-58 Random random; (ignored)
4836 * 59-xx SessionID session_id; 1 byte len + sid_len content
4837 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4838 * ...
4839 *
4840 * Minimum length is 61 bytes.
4841 */
4842 if( in_len < 61 ||
4843 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4844 in[3] != 0 || in[4] != 0 ||
4845 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4846 {
4847 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4848 }
4849
4850 sid_len = in[59];
4851 if( sid_len > in_len - 61 )
4852 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4853
4854 cookie_len = in[60 + sid_len];
4855 if( cookie_len > in_len - 60 )
4856 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4857
4858 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4859 cli_id, cli_id_len ) == 0 )
4860 {
4861 /* Valid cookie */
4862 return( 0 );
4863 }
4864
4865 /*
4866 * If we get here, we've got an invalid cookie, let's prepare HVR.
4867 *
4868 * 0-0 ContentType type; copied
4869 * 1-2 ProtocolVersion version; copied
4870 * 3-4 uint16 epoch; copied
4871 * 5-10 uint48 sequence_number; copied
4872 * 11-12 uint16 length; olen - 13
4873 *
4874 * 13-13 HandshakeType msg_type; hello_verify_request
4875 * 14-16 uint24 length; olen - 25
4876 * 17-18 uint16 message_seq; copied
4877 * 19-21 uint24 fragment_offset; copied
4878 * 22-24 uint24 fragment_length; olen - 25
4879 *
4880 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4881 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4882 *
4883 * Minimum length is 28.
4884 */
4885 if( buf_len < 28 )
4886 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4887
4888 /* Copy most fields and adapt others */
4889 memcpy( obuf, in, 25 );
4890 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4891 obuf[25] = 0xfe;
4892 obuf[26] = 0xff;
4893
4894 /* Generate and write actual cookie */
4895 p = obuf + 28;
4896 if( f_cookie_write( p_cookie,
4897 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4898 {
4899 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4900 }
4901
4902 *olen = p - obuf;
4903
4904 /* Go back and fill length fields */
4905 obuf[27] = (unsigned char)( *olen - 28 );
4906
4907 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4908 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4909 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4910
4911 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4912 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4913
4914 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4915}
4916
4917/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004918 * Handle possible client reconnect with the same UDP quadruplet
4919 * (RFC 6347 Section 4.2.8).
4920 *
4921 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4922 * that looks like a ClientHello.
4923 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004924 * - if the input looks like a ClientHello without cookies,
4925 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004926 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004927 * - if the input looks like a ClientHello with a valid cookie,
4928 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004929 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004930 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004931 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004932 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004933 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4934 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004935 */
4936static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4937{
4938 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004939 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004940
Hanno Becker2fddd372019-07-10 14:37:41 +01004941 if( ssl->conf->f_cookie_write == NULL ||
4942 ssl->conf->f_cookie_check == NULL )
4943 {
4944 /* If we can't use cookies to verify reachability of the peer,
4945 * drop the record. */
4946 return( 0 );
4947 }
4948
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004949 ret = ssl_check_dtls_clihlo_cookie(
4950 ssl->conf->f_cookie_write,
4951 ssl->conf->f_cookie_check,
4952 ssl->conf->p_cookie,
4953 ssl->cli_id, ssl->cli_id_len,
4954 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004955 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004956
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004957 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4958
4959 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004960 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004961 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004962 * If the error is permanent we'll catch it later,
4963 * if it's not, then hopefully it'll work next time. */
4964 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01004965 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004966 }
4967
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004968 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004969 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004970 /* Got a valid cookie, partially reset context */
4971 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4972 {
4973 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4974 return( ret );
4975 }
4976
4977 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004978 }
4979
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004980 return( ret );
4981}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004982#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004983
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004984static int ssl_check_record_type( uint8_t record_type )
4985{
4986 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4987 record_type != MBEDTLS_SSL_MSG_ALERT &&
4988 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4989 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4990 {
4991 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4992 }
4993
4994 return( 0 );
4995}
4996
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004997/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004998 * ContentType type;
4999 * ProtocolVersion version;
5000 * uint16 epoch; // DTLS only
5001 * uint48 sequence_number; // DTLS only
5002 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005003 *
5004 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005005 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005006 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5007 *
5008 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005009 * 1. proceed with the record if this function returns 0
5010 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5011 * 3. return CLIENT_RECONNECT if this function return that value
5012 * 4. drop the whole datagram if this function returns anything else.
5013 * Point 2 is needed when the peer is resending, and we have already received
5014 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005015 */
Hanno Becker331de3d2019-07-12 11:10:16 +01005016static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01005017 unsigned char *buf,
5018 size_t len,
5019 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005020{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005021 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005022
Hanno Beckere5e7e782019-07-11 12:29:35 +01005023 size_t const rec_hdr_type_offset = 0;
5024 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005025
Hanno Beckere5e7e782019-07-11 12:29:35 +01005026 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5027 rec_hdr_type_len;
5028 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005029
Hanno Beckere5e7e782019-07-11 12:29:35 +01005030 size_t const rec_hdr_ctr_len = 8;
5031#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01005032 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005033 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5034 rec_hdr_version_len;
5035
Hanno Beckera0e20d02019-05-15 14:03:01 +01005036#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01005037 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5038 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01005039 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005040#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5041#endif /* MBEDTLS_SSL_PROTO_DTLS */
5042
5043 size_t rec_hdr_len_offset; /* To be determined */
5044 size_t const rec_hdr_len_len = 2;
5045
5046 /*
5047 * Check minimum lengths for record header.
5048 */
5049
5050#if defined(MBEDTLS_SSL_PROTO_DTLS)
5051 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5052 {
5053 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5054 }
5055 else
5056#endif /* MBEDTLS_SSL_PROTO_DTLS */
5057 {
5058 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5059 }
5060
5061 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5062 {
5063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5064 (unsigned) len,
5065 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5066 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5067 }
5068
5069 /*
5070 * Parse and validate record content type
5071 */
5072
5073 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005074
5075 /* Check record content type */
5076#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5077 rec->cid_len = 0;
5078
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005079 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005080 ssl->conf->cid_len != 0 &&
5081 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005082 {
5083 /* Shift pointers to account for record header including CID
5084 * struct {
5085 * ContentType special_type = tls12_cid;
5086 * ProtocolVersion version;
5087 * uint16 epoch;
5088 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005089 * opaque cid[cid_length]; // Additional field compared to
5090 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005091 * uint16 length;
5092 * opaque enc_content[DTLSCiphertext.length];
5093 * } DTLSCiphertext;
5094 */
5095
5096 /* So far, we only support static CID lengths
5097 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005098 rec_hdr_cid_len = ssl->conf->cid_len;
5099 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005100
Hanno Beckere5e7e782019-07-11 12:29:35 +01005101 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005102 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5104 (unsigned) len,
5105 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005106 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005107 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005108
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02005109 /* configured CID len is guaranteed at most 255, see
5110 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5111 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005112 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005113 }
5114 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005115#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005116 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005117 if( ssl_check_record_type( rec->type ) )
5118 {
Hanno Becker54229812019-07-12 14:40:00 +01005119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5120 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005121 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5122 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005123 }
5124
Hanno Beckere5e7e782019-07-11 12:29:35 +01005125 /*
5126 * Parse and validate record version
5127 */
5128
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005129 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5130 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005131 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5132 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005133 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005134
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005135 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5138 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005139 }
5140
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005141 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5144 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005145 }
5146
Hanno Beckere5e7e782019-07-11 12:29:35 +01005147 /*
5148 * Parse/Copy record sequence number.
5149 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005150
Hanno Beckere5e7e782019-07-11 12:29:35 +01005151#if defined(MBEDTLS_SSL_PROTO_DTLS)
5152 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005153 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005154 /* Copy explicit record sequence number from input buffer. */
5155 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5156 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005157 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005158 else
5159#endif /* MBEDTLS_SSL_PROTO_DTLS */
5160 {
5161 /* Copy implicit record sequence number from SSL context structure. */
5162 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5163 }
Paul Bakker40e46942009-01-03 21:51:57 +00005164
Hanno Beckere5e7e782019-07-11 12:29:35 +01005165 /*
5166 * Parse record length.
5167 */
5168
Hanno Beckere5e7e782019-07-11 12:29:35 +01005169 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005170 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5171 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005172 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00005173
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005174 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005175 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005176 rec->type,
5177 major_ver, minor_ver, rec->data_len ) );
5178
5179 rec->buf = buf;
5180 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005181
Hanno Beckerd417cc92019-07-26 08:20:27 +01005182 if( rec->data_len == 0 )
5183 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005184
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005185 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005186 * DTLS-related tests.
5187 * Check epoch before checking length constraint because
5188 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5189 * message gets duplicated before the corresponding Finished message,
5190 * the second ChangeCipherSpec should be discarded because it belongs
5191 * to an old epoch, but not because its length is shorter than
5192 * the minimum record length for packets using the new record transform.
5193 * Note that these two kinds of failures are handled differently,
5194 * as an unexpected record is silently skipped but an invalid
5195 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005196 */
5197#if defined(MBEDTLS_SSL_PROTO_DTLS)
5198 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5199 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005200 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005201
Hanno Becker955a5c92019-07-10 17:12:07 +01005202 /* Check that the datagram is large enough to contain a record
5203 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005204 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005205 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5207 (unsigned) len,
5208 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005209 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5210 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005211
Hanno Becker37cfe732019-07-10 17:20:01 +01005212 /* Records from other, non-matching epochs are silently discarded.
5213 * (The case of same-port Client reconnects must be considered in
5214 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005215 if( rec_epoch != ssl->in_epoch )
5216 {
5217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5218 "expected %d, received %d",
5219 ssl->in_epoch, rec_epoch ) );
5220
Hanno Becker552f7472019-07-19 10:59:12 +01005221 /* Records from the next epoch are considered for buffering
5222 * (concretely: early Finished messages). */
5223 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005224 {
Hanno Becker552f7472019-07-19 10:59:12 +01005225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5226 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005227 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005228
Hanno Becker2fddd372019-07-10 14:37:41 +01005229 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005230 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005231#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005232 /* For records from the correct epoch, check whether their
5233 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005234 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005235 {
5236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5237 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5238 }
5239#endif
5240 }
5241#endif /* MBEDTLS_SSL_PROTO_DTLS */
5242
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005243 return( 0 );
5244}
Paul Bakker5121ce52009-01-03 21:22:43 +00005245
Paul Bakker5121ce52009-01-03 21:22:43 +00005246
Hanno Becker2fddd372019-07-10 14:37:41 +01005247#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5248static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5249{
5250 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5251
5252 /*
5253 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5254 * access the first byte of record content (handshake type), as we
5255 * have an active transform (possibly iv_len != 0), so use the
5256 * fact that the record header len is 13 instead.
5257 */
5258 if( rec_epoch == 0 &&
5259 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5260 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5261 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5262 ssl->in_left > 13 &&
5263 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5264 {
5265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5266 "from the same port" ) );
5267 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005268 }
5269
5270 return( 0 );
5271}
Hanno Becker2fddd372019-07-10 14:37:41 +01005272#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005273
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005274/*
5275 * If applicable, decrypt (and decompress) record content
5276 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005277static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5278 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005279{
5280 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005282 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005283 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005285#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5286 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005290 ret = mbedtls_ssl_hw_record_read( ssl );
5291 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005293 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5294 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005295 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005296
5297 if( ret == 0 )
5298 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005299 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005300#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005301 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005302 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005303 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005304
Hanno Beckera18d1322018-01-03 14:27:32 +00005305 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005306 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005308 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005309
Hanno Beckera0e20d02019-05-15 14:03:01 +01005310#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005311 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5312 ssl->conf->ignore_unexpected_cid
5313 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5314 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005315 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005316 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005317 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005318#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005319
Paul Bakker5121ce52009-01-03 21:22:43 +00005320 return( ret );
5321 }
5322
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005323 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005324 {
5325 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005326 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005327 }
5328
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005329 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005330 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005331
Hanno Beckera0e20d02019-05-15 14:03:01 +01005332#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005333 /* We have already checked the record content type
5334 * in ssl_parse_record_header(), failing or silently
5335 * dropping the record in the case of an unknown type.
5336 *
5337 * Since with the use of CIDs, the record content type
5338 * might change during decryption, re-check the record
5339 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005340 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005341 {
5342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5343 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5344 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005345#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005346
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005347 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005348 {
5349#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5350 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005351 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005352 {
5353 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5355 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5356 }
5357#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5358
5359 ssl->nb_zero++;
5360
5361 /*
5362 * Three or more empty messages may be a DoS attack
5363 * (excessive CPU consumption).
5364 */
5365 if( ssl->nb_zero > 3 )
5366 {
5367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005368 "messages, possible DoS attack" ) );
5369 /* Treat the records as if they were not properly authenticated,
5370 * thereby failing the connection if we see more than allowed
5371 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005372 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5373 }
5374 }
5375 else
5376 ssl->nb_zero = 0;
5377
5378#if defined(MBEDTLS_SSL_PROTO_DTLS)
5379 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5380 {
5381 ; /* in_ctr read from peer, not maintained internally */
5382 }
5383 else
5384#endif
5385 {
5386 unsigned i;
5387 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5388 if( ++ssl->in_ctr[i - 1] != 0 )
5389 break;
5390
5391 /* The loop goes to its end iff the counter is wrapping */
5392 if( i == ssl_ep_len( ssl ) )
5393 {
5394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5395 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5396 }
5397 }
5398
Paul Bakker5121ce52009-01-03 21:22:43 +00005399 }
5400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005402 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005403 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005404 {
5405 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005408 return( ret );
5409 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005410 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005411#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005413#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005414 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005417 }
5418#endif
5419
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005420 /* Check actual (decrypted) record content length against
5421 * configured maximum. */
5422 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5423 {
5424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5425 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5426 }
5427
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005428 return( 0 );
5429}
5430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005432
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005433/*
5434 * Read a record.
5435 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005436 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5437 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5438 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005439 */
Hanno Becker1097b342018-08-15 14:09:41 +01005440
5441/* Helper functions for mbedtls_ssl_read_record(). */
5442static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005443static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5444static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005445
Hanno Becker327c93b2018-08-15 13:56:18 +01005446int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005447 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005448{
5449 int ret;
5450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005452
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005453 if( ssl->keep_current_message == 0 )
5454 {
5455 do {
Simon Butcher99000142016-10-13 17:21:01 +01005456
Hanno Becker26994592018-08-15 14:14:59 +01005457 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005458 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005459 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005460
Hanno Beckere74d5562018-08-15 14:26:08 +01005461 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005462 {
Hanno Becker40f50842018-08-15 14:48:01 +01005463#if defined(MBEDTLS_SSL_PROTO_DTLS)
5464 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005465
Hanno Becker40f50842018-08-15 14:48:01 +01005466 /* We only check for buffered messages if the
5467 * current datagram is fully consumed. */
5468 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005469 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005470 {
Hanno Becker40f50842018-08-15 14:48:01 +01005471 if( ssl_load_buffered_message( ssl ) == 0 )
5472 have_buffered = 1;
5473 }
5474
5475 if( have_buffered == 0 )
5476#endif /* MBEDTLS_SSL_PROTO_DTLS */
5477 {
5478 ret = ssl_get_next_record( ssl );
5479 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5480 continue;
5481
5482 if( ret != 0 )
5483 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005484 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005485 return( ret );
5486 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005487 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005488 }
5489
5490 ret = mbedtls_ssl_handle_message_type( ssl );
5491
Hanno Becker40f50842018-08-15 14:48:01 +01005492#if defined(MBEDTLS_SSL_PROTO_DTLS)
5493 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5494 {
5495 /* Buffer future message */
5496 ret = ssl_buffer_message( ssl );
5497 if( ret != 0 )
5498 return( ret );
5499
5500 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5501 }
5502#endif /* MBEDTLS_SSL_PROTO_DTLS */
5503
Hanno Becker90333da2017-10-10 11:27:13 +01005504 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5505 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005506
5507 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005508 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005509 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005510 return( ret );
5511 }
5512
Hanno Becker327c93b2018-08-15 13:56:18 +01005513 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005514 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005515 {
5516 mbedtls_ssl_update_handshake_status( ssl );
5517 }
Simon Butcher99000142016-10-13 17:21:01 +01005518 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005519 else
Simon Butcher99000142016-10-13 17:21:01 +01005520 {
Hanno Becker02f59072018-08-15 14:00:24 +01005521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005522 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005523 }
5524
5525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5526
5527 return( 0 );
5528}
5529
Hanno Becker40f50842018-08-15 14:48:01 +01005530#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005531static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005532{
Hanno Becker40f50842018-08-15 14:48:01 +01005533 if( ssl->in_left > ssl->next_record_offset )
5534 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005535
Hanno Becker40f50842018-08-15 14:48:01 +01005536 return( 0 );
5537}
5538
5539static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5540{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005541 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005542 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005543 int ret = 0;
5544
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005545 if( hs == NULL )
5546 return( -1 );
5547
Hanno Beckere00ae372018-08-20 09:39:42 +01005548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5549
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005550 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5551 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5552 {
5553 /* Check if we have seen a ChangeCipherSpec before.
5554 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005555 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005556 {
5557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5558 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005559 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005560 }
5561
Hanno Becker39b8bc92018-08-28 17:17:13 +01005562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005563 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5564 ssl->in_msglen = 1;
5565 ssl->in_msg[0] = 1;
5566
5567 /* As long as they are equal, the exact value doesn't matter. */
5568 ssl->in_left = 0;
5569 ssl->next_record_offset = 0;
5570
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005571 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005572 goto exit;
5573 }
Hanno Becker37f95322018-08-16 13:55:32 +01005574
Hanno Beckerb8f50142018-08-28 10:01:34 +01005575#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005576 /* Debug only */
5577 {
5578 unsigned offset;
5579 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5580 {
5581 hs_buf = &hs->buffering.hs[offset];
5582 if( hs_buf->is_valid == 1 )
5583 {
5584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5585 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005586 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005587 }
5588 }
5589 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005590#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005591
5592 /* Check if we have buffered and/or fully reassembled the
5593 * next handshake message. */
5594 hs_buf = &hs->buffering.hs[0];
5595 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5596 {
5597 /* Synthesize a record containing the buffered HS message. */
5598 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5599 ( hs_buf->data[2] << 8 ) |
5600 hs_buf->data[3];
5601
5602 /* Double-check that we haven't accidentally buffered
5603 * a message that doesn't fit into the input buffer. */
5604 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5605 {
5606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5607 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5608 }
5609
5610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5611 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5612 hs_buf->data, msg_len + 12 );
5613
5614 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5615 ssl->in_hslen = msg_len + 12;
5616 ssl->in_msglen = msg_len + 12;
5617 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5618
5619 ret = 0;
5620 goto exit;
5621 }
5622 else
5623 {
5624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5625 hs->in_msg_seq ) );
5626 }
5627
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005628 ret = -1;
5629
5630exit:
5631
5632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5633 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005634}
5635
Hanno Beckera02b0b42018-08-21 17:20:27 +01005636static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5637 size_t desired )
5638{
5639 int offset;
5640 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5642 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005643
Hanno Becker01315ea2018-08-21 17:22:17 +01005644 /* Get rid of future records epoch first, if such exist. */
5645 ssl_free_buffered_record( ssl );
5646
5647 /* Check if we have enough space available now. */
5648 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5649 hs->buffering.total_bytes_buffered ) )
5650 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005652 return( 0 );
5653 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005654
Hanno Becker4f432ad2018-08-28 10:02:32 +01005655 /* We don't have enough space to buffer the next expected handshake
5656 * message. Remove buffers used for future messages to gain space,
5657 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005658 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5659 offset >= 0; offset-- )
5660 {
5661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5662 offset ) );
5663
Hanno Beckerb309b922018-08-23 13:18:05 +01005664 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005665
5666 /* Check if we have enough space available now. */
5667 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5668 hs->buffering.total_bytes_buffered ) )
5669 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005671 return( 0 );
5672 }
5673 }
5674
5675 return( -1 );
5676}
5677
Hanno Becker40f50842018-08-15 14:48:01 +01005678static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5679{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005680 int ret = 0;
5681 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5682
5683 if( hs == NULL )
5684 return( 0 );
5685
5686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5687
5688 switch( ssl->in_msgtype )
5689 {
5690 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005692
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005693 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005694 break;
5695
5696 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005697 {
5698 unsigned recv_msg_seq_offset;
5699 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5700 mbedtls_ssl_hs_buffer *hs_buf;
5701 size_t msg_len = ssl->in_hslen - 12;
5702
5703 /* We should never receive an old handshake
5704 * message - double-check nonetheless. */
5705 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5706 {
5707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5708 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5709 }
5710
5711 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5712 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5713 {
5714 /* Silently ignore -- message too far in the future */
5715 MBEDTLS_SSL_DEBUG_MSG( 2,
5716 ( "Ignore future HS message with sequence number %u, "
5717 "buffering window %u - %u",
5718 recv_msg_seq, ssl->handshake->in_msg_seq,
5719 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5720
5721 goto exit;
5722 }
5723
5724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5725 recv_msg_seq, recv_msg_seq_offset ) );
5726
5727 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5728
5729 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005730 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005731 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005732 size_t reassembly_buf_sz;
5733
Hanno Becker37f95322018-08-16 13:55:32 +01005734 hs_buf->is_fragmented =
5735 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5736
5737 /* We copy the message back into the input buffer
5738 * after reassembly, so check that it's not too large.
5739 * This is an implementation-specific limitation
5740 * and not one from the standard, hence it is not
5741 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005742 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005743 {
5744 /* Ignore message */
5745 goto exit;
5746 }
5747
Hanno Beckere0b150f2018-08-21 15:51:03 +01005748 /* Check if we have enough space to buffer the message. */
5749 if( hs->buffering.total_bytes_buffered >
5750 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5751 {
5752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5753 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5754 }
5755
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005756 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5757 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005758
5759 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5760 hs->buffering.total_bytes_buffered ) )
5761 {
5762 if( recv_msg_seq_offset > 0 )
5763 {
5764 /* If we can't buffer a future message because
5765 * of space limitations -- ignore. */
5766 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",
5767 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5768 (unsigned) hs->buffering.total_bytes_buffered ) );
5769 goto exit;
5770 }
Hanno Beckere1801392018-08-21 16:51:05 +01005771 else
5772 {
5773 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",
5774 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5775 (unsigned) hs->buffering.total_bytes_buffered ) );
5776 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005777
Hanno Beckera02b0b42018-08-21 17:20:27 +01005778 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005779 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005780 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",
5781 (unsigned) msg_len,
5782 (unsigned) reassembly_buf_sz,
5783 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005784 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005785 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5786 goto exit;
5787 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005788 }
5789
5790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5791 msg_len ) );
5792
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005793 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5794 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005795 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005796 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005797 goto exit;
5798 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005799 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005800
5801 /* Prepare final header: copy msg_type, length and message_seq,
5802 * then add standardised fragment_offset and fragment_length */
5803 memcpy( hs_buf->data, ssl->in_msg, 6 );
5804 memset( hs_buf->data + 6, 0, 3 );
5805 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5806
5807 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005808
5809 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005810 }
5811 else
5812 {
5813 /* Make sure msg_type and length are consistent */
5814 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5815 {
5816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5817 /* Ignore */
5818 goto exit;
5819 }
5820 }
5821
Hanno Becker4422bbb2018-08-20 09:40:19 +01005822 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005823 {
5824 size_t frag_len, frag_off;
5825 unsigned char * const msg = hs_buf->data + 12;
5826
5827 /*
5828 * Check and copy current fragment
5829 */
5830
5831 /* Validation of header fields already done in
5832 * mbedtls_ssl_prepare_handshake_record(). */
5833 frag_off = ssl_get_hs_frag_off( ssl );
5834 frag_len = ssl_get_hs_frag_len( ssl );
5835
5836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5837 frag_off, frag_len ) );
5838 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5839
5840 if( hs_buf->is_fragmented )
5841 {
5842 unsigned char * const bitmask = msg + msg_len;
5843 ssl_bitmask_set( bitmask, frag_off, frag_len );
5844 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5845 msg_len ) == 0 );
5846 }
5847 else
5848 {
5849 hs_buf->is_complete = 1;
5850 }
5851
5852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5853 hs_buf->is_complete ? "" : "not yet " ) );
5854 }
5855
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005856 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005857 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005858
5859 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005860 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005861 break;
5862 }
5863
5864exit:
5865
5866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5867 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005868}
5869#endif /* MBEDTLS_SSL_PROTO_DTLS */
5870
Hanno Becker1097b342018-08-15 14:09:41 +01005871static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005872{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005873 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005874 * Consume last content-layer message and potentially
5875 * update in_msglen which keeps track of the contents'
5876 * consumption state.
5877 *
5878 * (1) Handshake messages:
5879 * Remove last handshake message, move content
5880 * and adapt in_msglen.
5881 *
5882 * (2) Alert messages:
5883 * Consume whole record content, in_msglen = 0.
5884 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005885 * (3) Change cipher spec:
5886 * Consume whole record content, in_msglen = 0.
5887 *
5888 * (4) Application data:
5889 * Don't do anything - the record layer provides
5890 * the application data as a stream transport
5891 * and consumes through mbedtls_ssl_read only.
5892 *
5893 */
5894
5895 /* Case (1): Handshake messages */
5896 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005897 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005898 /* Hard assertion to be sure that no application data
5899 * is in flight, as corrupting ssl->in_msglen during
5900 * ssl->in_offt != NULL is fatal. */
5901 if( ssl->in_offt != NULL )
5902 {
5903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5904 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5905 }
5906
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005907 /*
5908 * Get next Handshake message in the current record
5909 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005910
Hanno Becker4a810fb2017-05-24 16:27:30 +01005911 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005912 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005913 * current handshake content: If DTLS handshake
5914 * fragmentation is used, that's the fragment
5915 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005916 * size here is faulty and should be changed at
5917 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005918 * (2) While it doesn't seem to cause problems, one
5919 * has to be very careful not to assume that in_hslen
5920 * is always <= in_msglen in a sensible communication.
5921 * Again, it's wrong for DTLS handshake fragmentation.
5922 * The following check is therefore mandatory, and
5923 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005924 * Additionally, ssl->in_hslen might be arbitrarily out of
5925 * bounds after handling a DTLS message with an unexpected
5926 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005927 */
5928 if( ssl->in_hslen < ssl->in_msglen )
5929 {
5930 ssl->in_msglen -= ssl->in_hslen;
5931 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5932 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005933
Hanno Becker4a810fb2017-05-24 16:27:30 +01005934 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5935 ssl->in_msg, ssl->in_msglen );
5936 }
5937 else
5938 {
5939 ssl->in_msglen = 0;
5940 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005941
Hanno Becker4a810fb2017-05-24 16:27:30 +01005942 ssl->in_hslen = 0;
5943 }
5944 /* Case (4): Application data */
5945 else if( ssl->in_offt != NULL )
5946 {
5947 return( 0 );
5948 }
5949 /* Everything else (CCS & Alerts) */
5950 else
5951 {
5952 ssl->in_msglen = 0;
5953 }
5954
Hanno Becker1097b342018-08-15 14:09:41 +01005955 return( 0 );
5956}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005957
Hanno Beckere74d5562018-08-15 14:26:08 +01005958static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5959{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005960 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005961 return( 1 );
5962
5963 return( 0 );
5964}
5965
Hanno Becker5f066e72018-08-16 14:56:31 +01005966#if defined(MBEDTLS_SSL_PROTO_DTLS)
5967
5968static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5969{
5970 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5971 if( hs == NULL )
5972 return;
5973
Hanno Becker01315ea2018-08-21 17:22:17 +01005974 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005975 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005976 hs->buffering.total_bytes_buffered -=
5977 hs->buffering.future_record.len;
5978
5979 mbedtls_free( hs->buffering.future_record.data );
5980 hs->buffering.future_record.data = NULL;
5981 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005982}
5983
5984static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5985{
5986 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5987 unsigned char * rec;
5988 size_t rec_len;
5989 unsigned rec_epoch;
5990
5991 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5992 return( 0 );
5993
5994 if( hs == NULL )
5995 return( 0 );
5996
Hanno Becker5f066e72018-08-16 14:56:31 +01005997 rec = hs->buffering.future_record.data;
5998 rec_len = hs->buffering.future_record.len;
5999 rec_epoch = hs->buffering.future_record.epoch;
6000
6001 if( rec == NULL )
6002 return( 0 );
6003
Hanno Becker4cb782d2018-08-20 11:19:05 +01006004 /* Only consider loading future records if the
6005 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006006 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006007 return( 0 );
6008
Hanno Becker5f066e72018-08-16 14:56:31 +01006009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6010
6011 if( rec_epoch != ssl->in_epoch )
6012 {
6013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6014 goto exit;
6015 }
6016
6017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6018
6019 /* Double-check that the record is not too large */
6020 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6021 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6022 {
6023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6024 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6025 }
6026
6027 memcpy( ssl->in_hdr, rec, rec_len );
6028 ssl->in_left = rec_len;
6029 ssl->next_record_offset = 0;
6030
6031 ssl_free_buffered_record( ssl );
6032
6033exit:
6034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6035 return( 0 );
6036}
6037
Hanno Becker519f15d2019-07-11 12:43:20 +01006038static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6039 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006040{
6041 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006042
6043 /* Don't buffer future records outside handshakes. */
6044 if( hs == NULL )
6045 return( 0 );
6046
6047 /* Only buffer handshake records (we are only interested
6048 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01006049 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006050 return( 0 );
6051
6052 /* Don't buffer more than one future epoch record. */
6053 if( hs->buffering.future_record.data != NULL )
6054 return( 0 );
6055
Hanno Becker01315ea2018-08-21 17:22:17 +01006056 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01006057 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006058 hs->buffering.total_bytes_buffered ) )
6059 {
6060 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 +01006061 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006062 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006063 return( 0 );
6064 }
6065
Hanno Becker5f066e72018-08-16 14:56:31 +01006066 /* Buffer record */
6067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6068 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01006069 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006070
6071 /* ssl_parse_record_header() only considers records
6072 * of the next epoch as candidates for buffering. */
6073 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006074 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006075
6076 hs->buffering.future_record.data =
6077 mbedtls_calloc( 1, hs->buffering.future_record.len );
6078 if( hs->buffering.future_record.data == NULL )
6079 {
6080 /* If we run out of RAM trying to buffer a
6081 * record from the next epoch, just ignore. */
6082 return( 0 );
6083 }
6084
Hanno Becker519f15d2019-07-11 12:43:20 +01006085 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006086
Hanno Becker519f15d2019-07-11 12:43:20 +01006087 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006088 return( 0 );
6089}
6090
6091#endif /* MBEDTLS_SSL_PROTO_DTLS */
6092
Hanno Beckere74d5562018-08-15 14:26:08 +01006093static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006094{
6095 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006096 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006097
Hanno Becker5f066e72018-08-16 14:56:31 +01006098#if defined(MBEDTLS_SSL_PROTO_DTLS)
6099 /* We might have buffered a future record; if so,
6100 * and if the epoch matches now, load it.
6101 * On success, this call will set ssl->in_left to
6102 * the length of the buffered record, so that
6103 * the calls to ssl_fetch_input() below will
6104 * essentially be no-ops. */
6105 ret = ssl_load_buffered_record( ssl );
6106 if( ret != 0 )
6107 return( ret );
6108#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006109
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006110 /* Ensure that we have enough space available for the default form
6111 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6112 * with no space for CIDs counted in). */
6113 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6114 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006116 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006117 return( ret );
6118 }
6119
Hanno Beckere5e7e782019-07-11 12:29:35 +01006120 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6121 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006123#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006124 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006125 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006126 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6127 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006128 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006129 if( ret != 0 )
6130 return( ret );
6131
6132 /* Fall through to handling of unexpected records */
6133 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6134 }
6135
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006136 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6137 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006138#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006139 /* Reset in pointers to default state for TLS/DTLS records,
6140 * assuming no CID and no offset between record content and
6141 * record plaintext. */
6142 ssl_update_in_pointers( ssl );
6143
Hanno Becker7ae20e02019-07-12 08:33:49 +01006144 /* Setup internal message pointers from record structure. */
6145 ssl->in_msgtype = rec.type;
6146#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6147 ssl->in_len = ssl->in_cid + rec.cid_len;
6148#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6149 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6150 ssl->in_msglen = rec.data_len;
6151
Hanno Becker2fddd372019-07-10 14:37:41 +01006152 ret = ssl_check_client_reconnect( ssl );
6153 if( ret != 0 )
6154 return( ret );
6155#endif
6156
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006157 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006158 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006159
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6161 "(header)" ) );
6162 }
6163 else
6164 {
6165 /* Skip invalid record and the rest of the datagram */
6166 ssl->next_record_offset = 0;
6167 ssl->in_left = 0;
6168
6169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6170 "(header)" ) );
6171 }
6172
6173 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006174 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006175 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006176 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006177#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006178 {
6179 return( ret );
6180 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006181 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006183#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006184 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006185 {
Hanno Beckera8814792019-07-10 15:01:45 +01006186 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006187 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006188 if( ssl->next_record_offset < ssl->in_left )
6189 {
6190 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6191 }
6192 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006193 else
6194#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006195 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006196 /*
6197 * Fetch record contents from underlying transport.
6198 */
Hanno Beckera3175662019-07-11 12:50:29 +01006199 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006200 if( ret != 0 )
6201 {
6202 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6203 return( ret );
6204 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006205
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006206 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006207 }
6208
6209 /*
6210 * Decrypt record contents.
6211 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006212
Hanno Beckerfdf66042019-07-11 13:07:45 +01006213 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006216 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006217 {
6218 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006219 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006220 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006221 /* Except when waiting for Finished as a bad mac here
6222 * probably means something went wrong in the handshake
6223 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6224 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6225 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6226 {
6227#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6228 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6229 {
6230 mbedtls_ssl_send_alert_message( ssl,
6231 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6232 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6233 }
6234#endif
6235 return( ret );
6236 }
6237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006238#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006239 if( ssl->conf->badmac_limit != 0 &&
6240 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6243 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006244 }
6245#endif
6246
Hanno Becker4a810fb2017-05-24 16:27:30 +01006247 /* As above, invalid records cause
6248 * dismissal of the whole datagram. */
6249
6250 ssl->next_record_offset = 0;
6251 ssl->in_left = 0;
6252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006254 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006255 }
6256
6257 return( ret );
6258 }
6259 else
6260#endif
6261 {
6262 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006263#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6264 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006266 mbedtls_ssl_send_alert_message( ssl,
6267 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6268 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006269 }
6270#endif
6271 return( ret );
6272 }
6273 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006274
Hanno Becker44d89b22019-07-12 09:40:44 +01006275
6276 /* Reset in pointers to default state for TLS/DTLS records,
6277 * assuming no CID and no offset between record content and
6278 * record plaintext. */
6279 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006280#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6281 ssl->in_len = ssl->in_cid + rec.cid_len;
6282#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6283 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006284
Hanno Becker8685c822019-07-12 09:37:30 +01006285 /* The record content type may change during decryption,
6286 * so re-read it. */
6287 ssl->in_msgtype = rec.type;
6288 /* Also update the input buffer, because unfortunately
6289 * the server-side ssl_parse_client_hello() reparses the
6290 * record header when receiving a ClientHello initiating
6291 * a renegotiation. */
6292 ssl->in_hdr[0] = rec.type;
6293 ssl->in_msg = rec.buf + rec.data_offset;
6294 ssl->in_msglen = rec.data_len;
6295 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6296 ssl->in_len[1] = (unsigned char)( rec.data_len );
6297
Simon Butcher99000142016-10-13 17:21:01 +01006298 return( 0 );
6299}
6300
6301int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6302{
6303 int ret;
6304
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006305 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006306 * Handle particular types of records
6307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006308 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006309 {
Simon Butcher99000142016-10-13 17:21:01 +01006310 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6311 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006312 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006313 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006314 }
6315
Hanno Beckere678eaa2018-08-21 14:57:46 +01006316 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006317 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006318 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006319 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6321 ssl->in_msglen ) );
6322 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006323 }
6324
Hanno Beckere678eaa2018-08-21 14:57:46 +01006325 if( ssl->in_msg[0] != 1 )
6326 {
6327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6328 ssl->in_msg[0] ) );
6329 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6330 }
6331
6332#if defined(MBEDTLS_SSL_PROTO_DTLS)
6333 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6334 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6335 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6336 {
6337 if( ssl->handshake == NULL )
6338 {
6339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6340 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6341 }
6342
6343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6344 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6345 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006346#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006347 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006349 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006350 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006351 if( ssl->in_msglen != 2 )
6352 {
6353 /* Note: Standard allows for more than one 2 byte alert
6354 to be packed in a single message, but Mbed TLS doesn't
6355 currently support this. */
6356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6357 ssl->in_msglen ) );
6358 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6359 }
6360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006362 ssl->in_msg[0], ssl->in_msg[1] ) );
6363
6364 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006365 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006366 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006367 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006370 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006371 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006372 }
6373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006374 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6375 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6378 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006379 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006380
6381#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6382 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6383 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6384 {
Hanno Becker90333da2017-10-10 11:27:13 +01006385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006386 /* Will be handled when trying to parse ServerHello */
6387 return( 0 );
6388 }
6389#endif
6390
6391#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6392 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6393 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6394 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6395 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6396 {
6397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6398 /* Will be handled in mbedtls_ssl_parse_certificate() */
6399 return( 0 );
6400 }
6401#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6402
6403 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006404 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006405 }
6406
Hanno Beckerc76c6192017-06-06 10:03:17 +01006407#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006408 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006409 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006410 /* Drop unexpected ApplicationData records,
6411 * except at the beginning of renegotiations */
6412 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6413 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6414#if defined(MBEDTLS_SSL_RENEGOTIATION)
6415 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6416 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006417#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006418 )
6419 {
6420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6421 return( MBEDTLS_ERR_SSL_NON_FATAL );
6422 }
6423
6424 if( ssl->handshake != NULL &&
6425 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6426 {
6427 ssl_handshake_wrapup_free_hs_transform( ssl );
6428 }
6429 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006430#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006431
Paul Bakker5121ce52009-01-03 21:22:43 +00006432 return( 0 );
6433}
6434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006435int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006436{
6437 int ret;
6438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006439 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6440 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6441 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006442 {
6443 return( ret );
6444 }
6445
6446 return( 0 );
6447}
6448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006449int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006450 unsigned char level,
6451 unsigned char message )
6452{
6453 int ret;
6454
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006455 if( ssl == NULL || ssl->conf == NULL )
6456 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006459 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006461 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006462 ssl->out_msglen = 2;
6463 ssl->out_msg[0] = level;
6464 ssl->out_msg[1] = message;
6465
Hanno Becker67bc7c32018-08-06 11:33:50 +01006466 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006468 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006469 return( ret );
6470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006472
6473 return( 0 );
6474}
6475
Hanno Beckerb9d44792019-02-08 07:19:04 +00006476#if defined(MBEDTLS_X509_CRT_PARSE_C)
6477static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6478{
6479#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6480 if( session->peer_cert != NULL )
6481 {
6482 mbedtls_x509_crt_free( session->peer_cert );
6483 mbedtls_free( session->peer_cert );
6484 session->peer_cert = NULL;
6485 }
6486#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6487 if( session->peer_cert_digest != NULL )
6488 {
6489 /* Zeroization is not necessary. */
6490 mbedtls_free( session->peer_cert_digest );
6491 session->peer_cert_digest = NULL;
6492 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6493 session->peer_cert_digest_len = 0;
6494 }
6495#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6496}
6497#endif /* MBEDTLS_X509_CRT_PARSE_C */
6498
Paul Bakker5121ce52009-01-03 21:22:43 +00006499/*
6500 * Handshake functions
6501 */
Hanno Becker21489932019-02-05 13:20:55 +00006502#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006503/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006504int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006505{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006506 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6507 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006510
Hanno Becker7177a882019-02-05 13:36:46 +00006511 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006514 ssl->state++;
6515 return( 0 );
6516 }
6517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6519 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006520}
6521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006522int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006523{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006524 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6525 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006528
Hanno Becker7177a882019-02-05 13:36:46 +00006529 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006532 ssl->state++;
6533 return( 0 );
6534 }
6535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6537 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006538}
Gilles Peskinef9828522017-05-03 12:28:43 +02006539
Hanno Becker21489932019-02-05 13:20:55 +00006540#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006541/* Some certificate support -> implement write and parse */
6542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006543int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006544{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006546 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006548 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6549 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006552
Hanno Becker7177a882019-02-05 13:36:46 +00006553 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006556 ssl->state++;
6557 return( 0 );
6558 }
6559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006561 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006562 {
6563 if( ssl->client_auth == 0 )
6564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006566 ssl->state++;
6567 return( 0 );
6568 }
6569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006571 /*
6572 * If using SSLv3 and got no cert, send an Alert message
6573 * (otherwise an empty Certificate message will be sent).
6574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006575 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6576 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006577 {
6578 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6580 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6581 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006584 goto write_msg;
6585 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006587 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006588#endif /* MBEDTLS_SSL_CLI_C */
6589#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006590 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006592 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6595 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006596 }
6597 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006598#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006600 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006601
6602 /*
6603 * 0 . 0 handshake type
6604 * 1 . 3 handshake length
6605 * 4 . 6 length of all certs
6606 * 7 . 9 length of cert. 1
6607 * 10 . n-1 peer certificate
6608 * n . n+2 length of cert. 2
6609 * n+3 . ... upper level cert, etc.
6610 */
6611 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006612 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006613
Paul Bakker29087132010-03-21 21:03:34 +00006614 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006615 {
6616 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006617 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006620 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006622 }
6623
6624 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6625 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6626 ssl->out_msg[i + 2] = (unsigned char)( n );
6627
6628 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6629 i += n; crt = crt->next;
6630 }
6631
6632 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6633 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6634 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6635
6636 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006637 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6638 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006639
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006640#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006641write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006642#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006643
6644 ssl->state++;
6645
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006646 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006647 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006648 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006649 return( ret );
6650 }
6651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006653
Paul Bakkered27a042013-04-18 22:46:23 +02006654 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006655}
6656
Hanno Becker84879e32019-01-31 07:44:03 +00006657#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006658
6659#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006660static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6661 unsigned char *crt_buf,
6662 size_t crt_buf_len )
6663{
6664 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6665
6666 if( peer_crt == NULL )
6667 return( -1 );
6668
6669 if( peer_crt->raw.len != crt_buf_len )
6670 return( -1 );
6671
Hanno Becker46f34d02019-02-08 14:00:04 +00006672 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006673}
Hanno Becker177475a2019-02-05 17:02:46 +00006674#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6675static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6676 unsigned char *crt_buf,
6677 size_t crt_buf_len )
6678{
6679 int ret;
6680 unsigned char const * const peer_cert_digest =
6681 ssl->session->peer_cert_digest;
6682 mbedtls_md_type_t const peer_cert_digest_type =
6683 ssl->session->peer_cert_digest_type;
6684 mbedtls_md_info_t const * const digest_info =
6685 mbedtls_md_info_from_type( peer_cert_digest_type );
6686 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6687 size_t digest_len;
6688
6689 if( peer_cert_digest == NULL || digest_info == NULL )
6690 return( -1 );
6691
6692 digest_len = mbedtls_md_get_size( digest_info );
6693 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6694 return( -1 );
6695
6696 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6697 if( ret != 0 )
6698 return( -1 );
6699
6700 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6701}
6702#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006703#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006704
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006705/*
6706 * Once the certificate message is read, parse it into a cert chain and
6707 * perform basic checks, but leave actual verification to the caller
6708 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006709static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6710 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006711{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006712 int ret;
6713#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6714 int crt_cnt=0;
6715#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006716 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006717 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006722 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6723 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006724 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006725 }
6726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6728 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006731 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6732 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006734 }
6735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006736 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006737
Paul Bakker5121ce52009-01-03 21:22:43 +00006738 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006740 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006741 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006742
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006743 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006744 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006747 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6748 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006750 }
6751
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006752 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6753 i += 3;
6754
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006755 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006756 while( i < ssl->in_hslen )
6757 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006758 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006759 if ( i + 3 > ssl->in_hslen ) {
6760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006761 mbedtls_ssl_send_alert_message( ssl,
6762 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6763 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006764 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6765 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006766 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6767 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006768 if( ssl->in_msg[i] != 0 )
6769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006771 mbedtls_ssl_send_alert_message( ssl,
6772 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6773 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006775 }
6776
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006777 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006778 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6779 | (unsigned int) ssl->in_msg[i + 2];
6780 i += 3;
6781
6782 if( n < 128 || i + n > ssl->in_hslen )
6783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006785 mbedtls_ssl_send_alert_message( ssl,
6786 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6787 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006788 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006789 }
6790
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006791 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006792#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6793 if( crt_cnt++ == 0 &&
6794 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6795 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006796 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006797 /* During client-side renegotiation, check that the server's
6798 * end-CRTs hasn't changed compared to the initial handshake,
6799 * mitigating the triple handshake attack. On success, reuse
6800 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006801 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6802 if( ssl_check_peer_crt_unchanged( ssl,
6803 &ssl->in_msg[i],
6804 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006805 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6807 mbedtls_ssl_send_alert_message( ssl,
6808 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6809 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6810 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006811 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006812
6813 /* Now we can safely free the original chain. */
6814 ssl_clear_peer_cert( ssl->session );
6815 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006816#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6817
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006818 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006819#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006820 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006821#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006822 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006823 * it in-place from the input buffer instead of making a copy. */
6824 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6825#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006826 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006827 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006828 case 0: /*ok*/
6829 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6830 /* Ignore certificate with an unknown algorithm: maybe a
6831 prior certificate was already trusted. */
6832 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006833
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006834 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6835 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6836 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006837
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006838 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6839 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6840 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006841
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006842 default:
6843 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6844 crt_parse_der_failed:
6845 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6846 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6847 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006848 }
6849
6850 i += n;
6851 }
6852
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006853 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006854 return( 0 );
6855}
6856
Hanno Becker4a55f632019-02-05 12:49:06 +00006857#if defined(MBEDTLS_SSL_SRV_C)
6858static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6859{
6860 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6861 return( -1 );
6862
6863#if defined(MBEDTLS_SSL_PROTO_SSL3)
6864 /*
6865 * Check if the client sent an empty certificate
6866 */
6867 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6868 {
6869 if( ssl->in_msglen == 2 &&
6870 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6871 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6872 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6873 {
6874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6875 return( 0 );
6876 }
6877
6878 return( -1 );
6879 }
6880#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6881
6882#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6883 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6884 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6885 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6886 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6887 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6888 {
6889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6890 return( 0 );
6891 }
6892
6893 return( -1 );
6894#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6895 MBEDTLS_SSL_PROTO_TLS1_2 */
6896}
6897#endif /* MBEDTLS_SSL_SRV_C */
6898
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006899/* Check if a certificate message is expected.
6900 * Return either
6901 * - SSL_CERTIFICATE_EXPECTED, or
6902 * - SSL_CERTIFICATE_SKIP
6903 * indicating whether a Certificate message is expected or not.
6904 */
6905#define SSL_CERTIFICATE_EXPECTED 0
6906#define SSL_CERTIFICATE_SKIP 1
6907static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6908 int authmode )
6909{
6910 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006911 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006912
6913 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6914 return( SSL_CERTIFICATE_SKIP );
6915
6916#if defined(MBEDTLS_SSL_SRV_C)
6917 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6918 {
6919 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6920 return( SSL_CERTIFICATE_SKIP );
6921
6922 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6923 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006924 ssl->session_negotiate->verify_result =
6925 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6926 return( SSL_CERTIFICATE_SKIP );
6927 }
6928 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006929#else
6930 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006931#endif /* MBEDTLS_SSL_SRV_C */
6932
6933 return( SSL_CERTIFICATE_EXPECTED );
6934}
6935
Hanno Becker68636192019-02-05 14:36:34 +00006936static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6937 int authmode,
6938 mbedtls_x509_crt *chain,
6939 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006940{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006941 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006942 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006943 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006944 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006945
Hanno Becker8927c832019-04-03 12:52:50 +01006946 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6947 void *p_vrfy;
6948
Hanno Becker68636192019-02-05 14:36:34 +00006949 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6950 return( 0 );
6951
Hanno Becker8927c832019-04-03 12:52:50 +01006952 if( ssl->f_vrfy != NULL )
6953 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006954 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006955 f_vrfy = ssl->f_vrfy;
6956 p_vrfy = ssl->p_vrfy;
6957 }
6958 else
6959 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006960 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006961 f_vrfy = ssl->conf->f_vrfy;
6962 p_vrfy = ssl->conf->p_vrfy;
6963 }
6964
Hanno Becker68636192019-02-05 14:36:34 +00006965 /*
6966 * Main check: verify certificate
6967 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006968#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6969 if( ssl->conf->f_ca_cb != NULL )
6970 {
6971 ((void) rs_ctx);
6972 have_ca_chain = 1;
6973
6974 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006975 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006976 chain,
6977 ssl->conf->f_ca_cb,
6978 ssl->conf->p_ca_cb,
6979 ssl->conf->cert_profile,
6980 ssl->hostname,
6981 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006982 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006983 }
6984 else
6985#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6986 {
6987 mbedtls_x509_crt *ca_chain;
6988 mbedtls_x509_crl *ca_crl;
6989
6990#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6991 if( ssl->handshake->sni_ca_chain != NULL )
6992 {
6993 ca_chain = ssl->handshake->sni_ca_chain;
6994 ca_crl = ssl->handshake->sni_ca_crl;
6995 }
6996 else
6997#endif
6998 {
6999 ca_chain = ssl->conf->ca_chain;
7000 ca_crl = ssl->conf->ca_crl;
7001 }
7002
7003 if( ca_chain != NULL )
7004 have_ca_chain = 1;
7005
7006 ret = mbedtls_x509_crt_verify_restartable(
7007 chain,
7008 ca_chain, ca_crl,
7009 ssl->conf->cert_profile,
7010 ssl->hostname,
7011 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007012 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007013 }
Hanno Becker68636192019-02-05 14:36:34 +00007014
7015 if( ret != 0 )
7016 {
7017 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7018 }
7019
7020#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7021 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7022 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7023#endif
7024
7025 /*
7026 * Secondary checks: always done, but change 'ret' only if it was 0
7027 */
7028
7029#if defined(MBEDTLS_ECP_C)
7030 {
7031 const mbedtls_pk_context *pk = &chain->pk;
7032
7033 /* If certificate uses an EC key, make sure the curve is OK */
7034 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
7035 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
7036 {
7037 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7038
7039 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7040 if( ret == 0 )
7041 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7042 }
7043 }
7044#endif /* MBEDTLS_ECP_C */
7045
7046 if( mbedtls_ssl_check_cert_usage( chain,
7047 ciphersuite_info,
7048 ! ssl->conf->endpoint,
7049 &ssl->session_negotiate->verify_result ) != 0 )
7050 {
7051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7052 if( ret == 0 )
7053 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7054 }
7055
7056 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7057 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7058 * with details encoded in the verification flags. All other kinds
7059 * of error codes, including those from the user provided f_vrfy
7060 * functions, are treated as fatal and lead to a failure of
7061 * ssl_parse_certificate even if verification was optional. */
7062 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7063 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7064 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7065 {
7066 ret = 0;
7067 }
7068
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007069 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007070 {
7071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7072 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7073 }
7074
7075 if( ret != 0 )
7076 {
7077 uint8_t alert;
7078
7079 /* The certificate may have been rejected for several reasons.
7080 Pick one and send the corresponding alert. Which alert to send
7081 may be a subject of debate in some cases. */
7082 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7083 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7084 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7085 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7086 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7087 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7088 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7089 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7090 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7091 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7092 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7093 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7094 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7095 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7096 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7097 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7098 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7099 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7100 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7101 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7102 else
7103 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7104 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7105 alert );
7106 }
7107
7108#if defined(MBEDTLS_DEBUG_C)
7109 if( ssl->session_negotiate->verify_result != 0 )
7110 {
7111 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7112 ssl->session_negotiate->verify_result ) );
7113 }
7114 else
7115 {
7116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7117 }
7118#endif /* MBEDTLS_DEBUG_C */
7119
7120 return( ret );
7121}
7122
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007123#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7124static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7125 unsigned char *start, size_t len )
7126{
7127 int ret;
7128 /* Remember digest of the peer's end-CRT. */
7129 ssl->session_negotiate->peer_cert_digest =
7130 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7131 if( ssl->session_negotiate->peer_cert_digest == NULL )
7132 {
7133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7134 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7135 mbedtls_ssl_send_alert_message( ssl,
7136 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7137 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7138
7139 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7140 }
7141
7142 ret = mbedtls_md( mbedtls_md_info_from_type(
7143 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7144 start, len,
7145 ssl->session_negotiate->peer_cert_digest );
7146
7147 ssl->session_negotiate->peer_cert_digest_type =
7148 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7149 ssl->session_negotiate->peer_cert_digest_len =
7150 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7151
7152 return( ret );
7153}
7154
7155static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7156 unsigned char *start, size_t len )
7157{
7158 unsigned char *end = start + len;
7159 int ret;
7160
7161 /* Make a copy of the peer's raw public key. */
7162 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7163 ret = mbedtls_pk_parse_subpubkey( &start, end,
7164 &ssl->handshake->peer_pubkey );
7165 if( ret != 0 )
7166 {
7167 /* We should have parsed the public key before. */
7168 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7169 }
7170
7171 return( 0 );
7172}
7173#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7174
Hanno Becker68636192019-02-05 14:36:34 +00007175int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7176{
7177 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007178 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007179#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7180 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7181 ? ssl->handshake->sni_authmode
7182 : ssl->conf->authmode;
7183#else
7184 const int authmode = ssl->conf->authmode;
7185#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007186 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007187 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007188
7189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7190
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007191 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7192 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007193 {
7194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007195 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007196 }
7197
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007198#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7199 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007200 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007201 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007202 chain = ssl->handshake->ecrs_peer_cert;
7203 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007204 goto crt_verify;
7205 }
7206#endif
7207
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007208 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007209 {
7210 /* mbedtls_ssl_read_record may have sent an alert already. We
7211 let it decide whether to alert. */
7212 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007213 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007214 }
7215
Hanno Becker4a55f632019-02-05 12:49:06 +00007216#if defined(MBEDTLS_SSL_SRV_C)
7217 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7218 {
7219 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007220
7221 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007222 ret = 0;
7223 else
7224 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007225
Hanno Becker6bdfab22019-02-05 13:11:17 +00007226 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007227 }
7228#endif /* MBEDTLS_SSL_SRV_C */
7229
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007230 /* Clear existing peer CRT structure in case we tried to
7231 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007232 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007233
7234 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7235 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007236 {
7237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7238 sizeof( mbedtls_x509_crt ) ) );
7239 mbedtls_ssl_send_alert_message( ssl,
7240 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7241 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007242
Hanno Becker3dad3112019-02-05 17:19:52 +00007243 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7244 goto exit;
7245 }
7246 mbedtls_x509_crt_init( chain );
7247
7248 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007249 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007250 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007251
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007252#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7253 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007254 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007255
7256crt_verify:
7257 if( ssl->handshake->ecrs_enabled)
7258 rs_ctx = &ssl->handshake->ecrs_ctx;
7259#endif
7260
Hanno Becker68636192019-02-05 14:36:34 +00007261 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007262 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007263 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007264 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007265
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007266#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007267 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007268 unsigned char *crt_start, *pk_start;
7269 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007270
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007271 /* We parse the CRT chain without copying, so
7272 * these pointers point into the input buffer,
7273 * and are hence still valid after freeing the
7274 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007275
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007276 crt_start = chain->raw.p;
7277 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007278
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007279 pk_start = chain->pk_raw.p;
7280 pk_len = chain->pk_raw.len;
7281
7282 /* Free the CRT structures before computing
7283 * digest and copying the peer's public key. */
7284 mbedtls_x509_crt_free( chain );
7285 mbedtls_free( chain );
7286 chain = NULL;
7287
7288 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007289 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007290 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007291
7292 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7293 if( ret != 0 )
7294 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007295 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007296#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7297 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007298 ssl->session_negotiate->peer_cert = chain;
7299 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007300#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007303
Hanno Becker6bdfab22019-02-05 13:11:17 +00007304exit:
7305
Hanno Becker3dad3112019-02-05 17:19:52 +00007306 if( ret == 0 )
7307 ssl->state++;
7308
7309#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7310 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7311 {
7312 ssl->handshake->ecrs_peer_cert = chain;
7313 chain = NULL;
7314 }
7315#endif
7316
7317 if( chain != NULL )
7318 {
7319 mbedtls_x509_crt_free( chain );
7320 mbedtls_free( chain );
7321 }
7322
Paul Bakker5121ce52009-01-03 21:22:43 +00007323 return( ret );
7324}
Hanno Becker21489932019-02-05 13:20:55 +00007325#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007328{
7329 int ret;
7330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007334 ssl->out_msglen = 1;
7335 ssl->out_msg[0] = 1;
7336
Paul Bakker5121ce52009-01-03 21:22:43 +00007337 ssl->state++;
7338
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007339 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007340 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007341 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007342 return( ret );
7343 }
7344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007346
7347 return( 0 );
7348}
7349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007351{
7352 int ret;
7353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007355
Hanno Becker327c93b2018-08-15 13:56:18 +01007356 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007358 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007359 return( ret );
7360 }
7361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007362 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007365 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7366 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007368 }
7369
Hanno Beckere678eaa2018-08-21 14:57:46 +01007370 /* CCS records are only accepted if they have length 1 and content '1',
7371 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007372
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007373 /*
7374 * Switch to our negotiated transform and session parameters for inbound
7375 * data.
7376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007378 ssl->transform_in = ssl->transform_negotiate;
7379 ssl->session_in = ssl->session_negotiate;
7380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007382 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007385 ssl_dtls_replay_reset( ssl );
7386#endif
7387
7388 /* Increment epoch */
7389 if( ++ssl->in_epoch == 0 )
7390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007392 /* This is highly unlikely to happen for legitimate reasons, so
7393 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007394 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007395 }
7396 }
7397 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007399 memset( ssl->in_ctr, 0, 8 );
7400
Hanno Becker79594fd2019-05-08 09:38:41 +01007401 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7404 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007406 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007409 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7410 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007412 }
7413 }
7414#endif
7415
Paul Bakker5121ce52009-01-03 21:22:43 +00007416 ssl->state++;
7417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007419
7420 return( 0 );
7421}
7422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7424 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007425{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007426 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7429 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7430 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007431 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007432 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007433#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7435#if defined(MBEDTLS_SHA512_C)
7436 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007437 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7438 else
7439#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440#if defined(MBEDTLS_SHA256_C)
7441 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007442 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007443 else
7444#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007448 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007449 }
Paul Bakker380da532012-04-18 16:10:25 +00007450}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007453{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7455 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007456 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7457 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007458#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7460#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007461#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007462 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007463 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7464#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007465 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007466#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007467#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007469#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007470 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007471 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007472#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007473 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007474#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007475#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007477}
7478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007479static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007480 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007481{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7483 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007484 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7485 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007486#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007487#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7488#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007489#if defined(MBEDTLS_USE_PSA_CRYPTO)
7490 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7491#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007492 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007493#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007494#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007496#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007497 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007498#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007499 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007500#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007501#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007503}
7504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7506 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7507static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007508 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007509{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007510 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7511 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007512}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007513#endif
Paul Bakker380da532012-04-18 16:10:25 +00007514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7516#if defined(MBEDTLS_SHA256_C)
7517static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007518 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007519{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007520#if defined(MBEDTLS_USE_PSA_CRYPTO)
7521 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7522#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007523 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007524#endif
Paul Bakker380da532012-04-18 16:10:25 +00007525}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007526#endif
Paul Bakker380da532012-04-18 16:10:25 +00007527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007528#if defined(MBEDTLS_SHA512_C)
7529static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007530 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007531{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007532#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007533 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007534#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007535 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007536#endif
Paul Bakker380da532012-04-18 16:10:25 +00007537}
Paul Bakker769075d2012-11-24 11:26:46 +01007538#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007539#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007541#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007542static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007543 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007544{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007545 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007546 mbedtls_md5_context md5;
7547 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007548
Paul Bakker5121ce52009-01-03 21:22:43 +00007549 unsigned char padbuf[48];
7550 unsigned char md5sum[16];
7551 unsigned char sha1sum[20];
7552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007554 if( !session )
7555 session = ssl->session;
7556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007558
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007559 mbedtls_md5_init( &md5 );
7560 mbedtls_sha1_init( &sha1 );
7561
7562 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7563 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007564
7565 /*
7566 * SSLv3:
7567 * hash =
7568 * MD5( master + pad2 +
7569 * MD5( handshake + sender + master + pad1 ) )
7570 * + SHA1( master + pad2 +
7571 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007572 */
7573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007574#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007575 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7576 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007577#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007580 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7581 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007582#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007585 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007586
Paul Bakker1ef83d62012-04-11 12:09:53 +00007587 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007588
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007589 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7590 mbedtls_md5_update_ret( &md5, session->master, 48 );
7591 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7592 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007593
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007594 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7595 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7596 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7597 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007598
Paul Bakker1ef83d62012-04-11 12:09:53 +00007599 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007600
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007601 mbedtls_md5_starts_ret( &md5 );
7602 mbedtls_md5_update_ret( &md5, session->master, 48 );
7603 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7604 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7605 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007606
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007607 mbedtls_sha1_starts_ret( &sha1 );
7608 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7609 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7610 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7611 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007613 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007614
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007615 mbedtls_md5_free( &md5 );
7616 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007617
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007618 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7619 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7620 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007623}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007627static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007629{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007630 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007631 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007632 mbedtls_md5_context md5;
7633 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007634 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007637 if( !session )
7638 session = ssl->session;
7639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007641
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007642 mbedtls_md5_init( &md5 );
7643 mbedtls_sha1_init( &sha1 );
7644
7645 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7646 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007647
Paul Bakker1ef83d62012-04-11 12:09:53 +00007648 /*
7649 * TLSv1:
7650 * hash = PRF( master, finished_label,
7651 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7652 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007654#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007655 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7656 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007657#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007659#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007660 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7661 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007662#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007665 ? "client finished"
7666 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007667
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007668 mbedtls_md5_finish_ret( &md5, padbuf );
7669 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007670
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007671 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007672 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007674 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007675
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007676 mbedtls_md5_free( &md5 );
7677 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007678
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007679 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007682}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007683#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007685#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7686#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007687static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007688 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007689{
7690 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007691 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007692 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007693#if defined(MBEDTLS_USE_PSA_CRYPTO)
7694 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007695 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007696 psa_status_t status;
7697#else
7698 mbedtls_sha256_context sha256;
7699#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007701 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007702 if( !session )
7703 session = ssl->session;
7704
Andrzej Kurekeb342242019-01-29 09:14:33 -05007705 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7706 ? "client finished"
7707 : "server finished";
7708
7709#if defined(MBEDTLS_USE_PSA_CRYPTO)
7710 sha256_psa = psa_hash_operation_init();
7711
7712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7713
7714 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7715 if( status != PSA_SUCCESS )
7716 {
7717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7718 return;
7719 }
7720
7721 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7722 if( status != PSA_SUCCESS )
7723 {
7724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7725 return;
7726 }
7727 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7728#else
7729
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007730 mbedtls_sha256_init( &sha256 );
7731
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007733
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007734 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007735
7736 /*
7737 * TLSv1.2:
7738 * hash = PRF( master, finished_label,
7739 * Hash( handshake ) )[0.11]
7740 */
7741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742#if !defined(MBEDTLS_SHA256_ALT)
7743 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007744 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007745#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007746
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007747 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007748 mbedtls_sha256_free( &sha256 );
7749#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007750
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007751 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007752 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007754 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007755
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007756 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007759}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007763static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007765{
7766 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007767 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007768 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007769#if defined(MBEDTLS_USE_PSA_CRYPTO)
7770 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007771 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007772 psa_status_t status;
7773#else
7774 mbedtls_sha512_context sha512;
7775#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007778 if( !session )
7779 session = ssl->session;
7780
Andrzej Kurekeb342242019-01-29 09:14:33 -05007781 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7782 ? "client finished"
7783 : "server finished";
7784
7785#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007786 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007787
7788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7789
Andrzej Kurek972fba52019-01-30 03:29:12 -05007790 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007791 if( status != PSA_SUCCESS )
7792 {
7793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7794 return;
7795 }
7796
Andrzej Kurek972fba52019-01-30 03:29:12 -05007797 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007798 if( status != PSA_SUCCESS )
7799 {
7800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7801 return;
7802 }
7803 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7804#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007805 mbedtls_sha512_init( &sha512 );
7806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007808
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007809 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007810
7811 /*
7812 * TLSv1.2:
7813 * hash = PRF( master, finished_label,
7814 * Hash( handshake ) )[0.11]
7815 */
7816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007817#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007818 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7819 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007820#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007821
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007822 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007823 mbedtls_sha512_free( &sha512 );
7824#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007825
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007826 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007827 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007830
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007831 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007834}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007835#endif /* MBEDTLS_SHA512_C */
7836#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007839{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007841
7842 /*
7843 * Free our handshake params
7844 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007845 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007847 ssl->handshake = NULL;
7848
7849 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007850 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007851 */
7852 if( ssl->transform )
7853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 mbedtls_ssl_transform_free( ssl->transform );
7855 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007856 }
7857 ssl->transform = ssl->transform_negotiate;
7858 ssl->transform_negotiate = NULL;
7859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007861}
7862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007864{
7865 int resume = ssl->handshake->resume;
7866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007867 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869#if defined(MBEDTLS_SSL_RENEGOTIATION)
7870 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007873 ssl->renego_records_seen = 0;
7874 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007875#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007876
7877 /*
7878 * Free the previous session and switch in the current one
7879 */
Paul Bakker0a597072012-09-25 21:55:46 +00007880 if( ssl->session )
7881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007883 /* RFC 7366 3.1: keep the EtM state */
7884 ssl->session_negotiate->encrypt_then_mac =
7885 ssl->session->encrypt_then_mac;
7886#endif
7887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888 mbedtls_ssl_session_free( ssl->session );
7889 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007890 }
7891 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007892 ssl->session_negotiate = NULL;
7893
Paul Bakker0a597072012-09-25 21:55:46 +00007894 /*
7895 * Add cache entry
7896 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007897 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007898 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007899 resume == 0 )
7900 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007901 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007903 }
Paul Bakker0a597072012-09-25 21:55:46 +00007904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007906 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007907 ssl->handshake->flight != NULL )
7908 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007909 /* Cancel handshake timer */
7910 ssl_set_timer( ssl, 0 );
7911
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007912 /* Keep last flight around in case we need to resend it:
7913 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007914 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007915 }
7916 else
7917#endif
7918 ssl_handshake_wrapup_free_hs_transform( ssl );
7919
Paul Bakker48916f92012-09-16 19:57:18 +00007920 ssl->state++;
7921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007923}
7924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007926{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007927 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007930
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007931 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007932
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007933 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007934
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007935 /*
7936 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7937 * may define some other value. Currently (early 2016), no defined
7938 * ciphersuite does this (and this is unlikely to change as activity has
7939 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7940 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007944 ssl->verify_data_len = hash_len;
7945 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007946#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007947
Paul Bakker5121ce52009-01-03 21:22:43 +00007948 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7950 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007951
7952 /*
7953 * In case of session resuming, invert the client and server
7954 * ChangeCipherSpec messages order.
7955 */
Paul Bakker0a597072012-09-25 21:55:46 +00007956 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007959 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007961#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007962#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007963 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007965#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007966 }
7967 else
7968 ssl->state++;
7969
Paul Bakker48916f92012-09-16 19:57:18 +00007970 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007971 * Switch to our negotiated transform and session parameters for outbound
7972 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007977 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007978 {
7979 unsigned char i;
7980
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007981 /* Remember current epoch settings for resending */
7982 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007983 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007984
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007985 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007986 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007987
7988 /* Increment epoch */
7989 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007990 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007991 break;
7992
7993 /* The loop goes to its end iff the counter is wrapping */
7994 if( i == 0 )
7995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7997 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007998 }
7999 }
8000 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01008002 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008003
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008004 ssl->transform_out = ssl->transform_negotiate;
8005 ssl->session_out = ssl->session_negotiate;
8006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008007#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8008 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8013 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008014 }
8015 }
8016#endif
8017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008019 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008020 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008021#endif
8022
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008023 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008024 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008025 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008026 return( ret );
8027 }
8028
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008029#if defined(MBEDTLS_SSL_PROTO_DTLS)
8030 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8031 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8032 {
8033 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8034 return( ret );
8035 }
8036#endif
8037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008039
8040 return( 0 );
8041}
8042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008043#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008044#define SSL_MAX_HASH_LEN 36
8045#else
8046#define SSL_MAX_HASH_LEN 12
8047#endif
8048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008049int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008050{
Paul Bakker23986e52011-04-24 08:57:21 +00008051 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008052 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008053 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008056
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008057 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008058
Hanno Becker327c93b2018-08-15 13:56:18 +01008059 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008061 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008062 return( ret );
8063 }
8064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008065 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008068 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8069 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008070 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008071 }
8072
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008073 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008074#if defined(MBEDTLS_SSL_PROTO_SSL3)
8075 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008076 hash_len = 36;
8077 else
8078#endif
8079 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8082 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
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_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008087 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008088 }
8089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008091 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008094 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8095 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008096 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008097 }
8098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008099#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008100 ssl->verify_data_len = hash_len;
8101 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008102#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008103
Paul Bakker0a597072012-09-25 21:55:46 +00008104 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008106#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008107 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008108 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008109#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008110#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008111 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008112 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008113#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008114 }
8115 else
8116 ssl->state++;
8117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008118#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008119 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008120 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008121#endif
8122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008124
8125 return( 0 );
8126}
8127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008128static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008130 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008132#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8133 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8134 mbedtls_md5_init( &handshake->fin_md5 );
8135 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008136 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8137 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008138#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008139#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8140#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008141#if defined(MBEDTLS_USE_PSA_CRYPTO)
8142 handshake->fin_sha256_psa = psa_hash_operation_init();
8143 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8144#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008146 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008147#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008148#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008150#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008151 handshake->fin_sha384_psa = psa_hash_operation_init();
8152 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008153#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008155 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008156#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008157#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008158#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008159
8160 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008161
8162#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8163 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8164 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8165#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008167#if defined(MBEDTLS_DHM_C)
8168 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008169#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008170#if defined(MBEDTLS_ECDH_C)
8171 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008172#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008173#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008174 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008175#if defined(MBEDTLS_SSL_CLI_C)
8176 handshake->ecjpake_cache = NULL;
8177 handshake->ecjpake_cache_len = 0;
8178#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008179#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008180
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008181#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008182 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008183#endif
8184
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008185#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8186 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8187#endif
Hanno Becker75173122019-02-06 16:18:31 +00008188
8189#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8190 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8191 mbedtls_pk_init( &handshake->peer_pubkey );
8192#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008193}
8194
Hanno Beckera18d1322018-01-03 14:27:32 +00008195void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008196{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008197 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8200 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008201
Hanno Beckerd56ed242018-01-03 15:32:51 +00008202#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008203 mbedtls_md_init( &transform->md_ctx_enc );
8204 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008205#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008206}
8207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008208void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008209{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008210 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008211}
8212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008213static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008214{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008215 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008216 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008218 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008219 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008220 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008221 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008222
8223 /*
8224 * Either the pointers are now NULL or cleared properly and can be freed.
8225 * Now allocate missing structures.
8226 */
8227 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008228 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008229 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008230 }
Paul Bakker48916f92012-09-16 19:57:18 +00008231
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008232 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008233 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008234 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008235 }
Paul Bakker48916f92012-09-16 19:57:18 +00008236
Paul Bakker82788fb2014-10-20 13:59:19 +02008237 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008238 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008239 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008240 }
Paul Bakker48916f92012-09-16 19:57:18 +00008241
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008242 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008243 if( ssl->handshake == NULL ||
8244 ssl->transform_negotiate == NULL ||
8245 ssl->session_negotiate == NULL )
8246 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249 mbedtls_free( ssl->handshake );
8250 mbedtls_free( ssl->transform_negotiate );
8251 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008252
8253 ssl->handshake = NULL;
8254 ssl->transform_negotiate = NULL;
8255 ssl->session_negotiate = NULL;
8256
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008257 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008258 }
8259
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008260 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008261 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008262 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008263 ssl_handshake_params_init( ssl->handshake );
8264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008265#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008266 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8267 {
8268 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008269
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008270 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8271 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8272 else
8273 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008274
8275 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008276 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008277#endif
8278
Paul Bakker48916f92012-09-16 19:57:18 +00008279 return( 0 );
8280}
8281
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008282#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008283/* Dummy cookie callbacks for defaults */
8284static int ssl_cookie_write_dummy( void *ctx,
8285 unsigned char **p, unsigned char *end,
8286 const unsigned char *cli_id, size_t cli_id_len )
8287{
8288 ((void) ctx);
8289 ((void) p);
8290 ((void) end);
8291 ((void) cli_id);
8292 ((void) cli_id_len);
8293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008294 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008295}
8296
8297static int ssl_cookie_check_dummy( void *ctx,
8298 const unsigned char *cookie, size_t cookie_len,
8299 const unsigned char *cli_id, size_t cli_id_len )
8300{
8301 ((void) ctx);
8302 ((void) cookie);
8303 ((void) cookie_len);
8304 ((void) cli_id);
8305 ((void) cli_id_len);
8306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008308}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008309#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008310
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008311/* Once ssl->out_hdr as the address of the beginning of the
8312 * next outgoing record is set, deduce the other pointers.
8313 *
8314 * Note: For TLS, we save the implicit record sequence number
8315 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8316 * and the caller has to make sure there's space for this.
8317 */
8318
8319static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8320 mbedtls_ssl_transform *transform )
8321{
8322#if defined(MBEDTLS_SSL_PROTO_DTLS)
8323 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8324 {
8325 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008326#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008327 ssl->out_cid = ssl->out_ctr + 8;
8328 ssl->out_len = ssl->out_cid;
8329 if( transform != NULL )
8330 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008331#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008332 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008333#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008334 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008335 }
8336 else
8337#endif
8338 {
8339 ssl->out_ctr = ssl->out_hdr - 8;
8340 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008341#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008342 ssl->out_cid = ssl->out_len;
8343#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008344 ssl->out_iv = ssl->out_hdr + 5;
8345 }
8346
8347 /* Adjust out_msg to make space for explicit IV, if used. */
8348 if( transform != NULL &&
8349 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8350 {
8351 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8352 }
8353 else
8354 ssl->out_msg = ssl->out_iv;
8355}
8356
8357/* Once ssl->in_hdr as the address of the beginning of the
8358 * next incoming record is set, deduce the other pointers.
8359 *
8360 * Note: For TLS, we save the implicit record sequence number
8361 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8362 * and the caller has to make sure there's space for this.
8363 */
8364
Hanno Becker79594fd2019-05-08 09:38:41 +01008365static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008366{
Hanno Becker79594fd2019-05-08 09:38:41 +01008367 /* This function sets the pointers to match the case
8368 * of unprotected TLS/DTLS records, with both ssl->in_iv
8369 * and ssl->in_msg pointing to the beginning of the record
8370 * content.
8371 *
8372 * When decrypting a protected record, ssl->in_msg
8373 * will be shifted to point to the beginning of the
8374 * record plaintext.
8375 */
8376
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008377#if defined(MBEDTLS_SSL_PROTO_DTLS)
8378 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8379 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008380 /* This sets the header pointers to match records
8381 * without CID. When we receive a record containing
8382 * a CID, the fields are shifted accordingly in
8383 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008384 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008385#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008386 ssl->in_cid = ssl->in_ctr + 8;
8387 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008388#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008389 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008390#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008391 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008392 }
8393 else
8394#endif
8395 {
8396 ssl->in_ctr = ssl->in_hdr - 8;
8397 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008398#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008399 ssl->in_cid = ssl->in_len;
8400#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008401 ssl->in_iv = ssl->in_hdr + 5;
8402 }
8403
Hanno Becker79594fd2019-05-08 09:38:41 +01008404 /* This will be adjusted at record decryption time. */
8405 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008406}
8407
Paul Bakker5121ce52009-01-03 21:22:43 +00008408/*
8409 * Initialize an SSL context
8410 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008411void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8412{
8413 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8414}
8415
8416/*
8417 * Setup an SSL context
8418 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008419
8420static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8421{
8422 /* Set the incoming and outgoing record pointers. */
8423#if defined(MBEDTLS_SSL_PROTO_DTLS)
8424 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8425 {
8426 ssl->out_hdr = ssl->out_buf;
8427 ssl->in_hdr = ssl->in_buf;
8428 }
8429 else
8430#endif /* MBEDTLS_SSL_PROTO_DTLS */
8431 {
8432 ssl->out_hdr = ssl->out_buf + 8;
8433 ssl->in_hdr = ssl->in_buf + 8;
8434 }
8435
8436 /* Derive other internal pointers. */
8437 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008438 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008439}
8440
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008441int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008442 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008443{
Paul Bakker48916f92012-09-16 19:57:18 +00008444 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008445
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008446 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008447
8448 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008449 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008450 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008451
8452 /* Set to NULL in case of an error condition */
8453 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008454
Angus Grattond8213d02016-05-25 20:56:48 +10008455 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8456 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008457 {
Angus Grattond8213d02016-05-25 20:56:48 +10008458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008459 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008460 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008461 }
8462
8463 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8464 if( ssl->out_buf == NULL )
8465 {
8466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008467 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008468 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008469 }
8470
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008471 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008472
Paul Bakker48916f92012-09-16 19:57:18 +00008473 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008474 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008475
8476 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008477
8478error:
8479 mbedtls_free( ssl->in_buf );
8480 mbedtls_free( ssl->out_buf );
8481
8482 ssl->conf = NULL;
8483
8484 ssl->in_buf = NULL;
8485 ssl->out_buf = NULL;
8486
8487 ssl->in_hdr = NULL;
8488 ssl->in_ctr = NULL;
8489 ssl->in_len = NULL;
8490 ssl->in_iv = NULL;
8491 ssl->in_msg = NULL;
8492
8493 ssl->out_hdr = NULL;
8494 ssl->out_ctr = NULL;
8495 ssl->out_len = NULL;
8496 ssl->out_iv = NULL;
8497 ssl->out_msg = NULL;
8498
k-stachowiak9f7798e2018-07-31 16:52:32 +02008499 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008500}
8501
8502/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008503 * Reset an initialized and used SSL context for re-use while retaining
8504 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008505 *
8506 * If partial is non-zero, keep data in the input buffer and client ID.
8507 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008508 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008509static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008510{
Paul Bakker48916f92012-09-16 19:57:18 +00008511 int ret;
8512
Hanno Becker7e772132018-08-10 12:38:21 +01008513#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8514 !defined(MBEDTLS_SSL_SRV_C)
8515 ((void) partial);
8516#endif
8517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008518 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008519
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008520 /* Cancel any possibly running timer */
8521 ssl_set_timer( ssl, 0 );
8522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008523#if defined(MBEDTLS_SSL_RENEGOTIATION)
8524 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008525 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008526
8527 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008528 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8529 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008530#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008531 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008532
Paul Bakker7eb013f2011-10-06 12:37:39 +00008533 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008534 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008535
8536 ssl->in_msgtype = 0;
8537 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008538#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008539 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008540 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008541#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008542#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008543 ssl_dtls_replay_reset( ssl );
8544#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008545
8546 ssl->in_hslen = 0;
8547 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008548
8549 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008550
8551 ssl->out_msgtype = 0;
8552 ssl->out_msglen = 0;
8553 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008554#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8555 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008556 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008557#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008558
Hanno Becker19859472018-08-06 09:40:20 +01008559 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8560
Paul Bakker48916f92012-09-16 19:57:18 +00008561 ssl->transform_in = NULL;
8562 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008563
Hanno Becker78640902018-08-13 16:35:15 +01008564 ssl->session_in = NULL;
8565 ssl->session_out = NULL;
8566
Angus Grattond8213d02016-05-25 20:56:48 +10008567 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008568
8569#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008570 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008571#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8572 {
8573 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008574 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008575 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008577#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8578 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8581 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008583 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8584 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008585 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008586 }
8587#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008588
Paul Bakker48916f92012-09-16 19:57:18 +00008589 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008591 mbedtls_ssl_transform_free( ssl->transform );
8592 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008593 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008594 }
Paul Bakker48916f92012-09-16 19:57:18 +00008595
Paul Bakkerc0463502013-02-14 11:19:38 +01008596 if( ssl->session )
8597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008598 mbedtls_ssl_session_free( ssl->session );
8599 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008600 ssl->session = NULL;
8601 }
8602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008603#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008604 ssl->alpn_chosen = NULL;
8605#endif
8606
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008607#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008608#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008609 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008610#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008611 {
8612 mbedtls_free( ssl->cli_id );
8613 ssl->cli_id = NULL;
8614 ssl->cli_id_len = 0;
8615 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008616#endif
8617
Paul Bakker48916f92012-09-16 19:57:18 +00008618 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8619 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008620
8621 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008622}
8623
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008624/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008625 * Reset an initialized and used SSL context for re-use while retaining
8626 * all application-set variables, function pointers and data.
8627 */
8628int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8629{
8630 return( ssl_session_reset_int( ssl, 0 ) );
8631}
8632
8633/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008634 * SSL set accessors
8635 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008636void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008637{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008638 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008639}
8640
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008641void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008642{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008643 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008644}
8645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008646#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008647void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008648{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008649 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008650}
8651#endif
8652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008653#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008654void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008655{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008656 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008657}
8658#endif
8659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008660#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008661
Hanno Becker1841b0a2018-08-24 11:13:57 +01008662void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8663 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008664{
8665 ssl->disable_datagram_packing = !allow_packing;
8666}
8667
8668void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8669 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008670{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008671 conf->hs_timeout_min = min;
8672 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008673}
8674#endif
8675
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008676void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008677{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008678 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008679}
8680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008681#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008682void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008683 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008684 void *p_vrfy )
8685{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008686 conf->f_vrfy = f_vrfy;
8687 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008689#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008690
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008691void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008692 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008693 void *p_rng )
8694{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008695 conf->f_rng = f_rng;
8696 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008697}
8698
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008699void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008700 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008701 void *p_dbg )
8702{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008703 conf->f_dbg = f_dbg;
8704 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008705}
8706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008707void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008708 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008709 mbedtls_ssl_send_t *f_send,
8710 mbedtls_ssl_recv_t *f_recv,
8711 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008712{
8713 ssl->p_bio = p_bio;
8714 ssl->f_send = f_send;
8715 ssl->f_recv = f_recv;
8716 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008717}
8718
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008719#if defined(MBEDTLS_SSL_PROTO_DTLS)
8720void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8721{
8722 ssl->mtu = mtu;
8723}
8724#endif
8725
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008726void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008727{
8728 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008729}
8730
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008731void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8732 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008733 mbedtls_ssl_set_timer_t *f_set_timer,
8734 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008735{
8736 ssl->p_timer = p_timer;
8737 ssl->f_set_timer = f_set_timer;
8738 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008739
8740 /* Make sure we start with no timer running */
8741 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008742}
8743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008744#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008745void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008746 void *p_cache,
8747 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8748 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008749{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008750 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008751 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008752 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008753}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008754#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008756#if defined(MBEDTLS_SSL_CLI_C)
8757int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008758{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008759 int ret;
8760
8761 if( ssl == NULL ||
8762 session == NULL ||
8763 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008764 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008766 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008767 }
8768
Hanno Becker52055ae2019-02-06 14:30:46 +00008769 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8770 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008771 return( ret );
8772
Paul Bakker0a597072012-09-25 21:55:46 +00008773 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008774
8775 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008776}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008777#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008778
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008779void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008780 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008781{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008782 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8783 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8784 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8785 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008786}
8787
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008788void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008789 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008790 int major, int minor )
8791{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008792 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008793 return;
8794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008795 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008796 return;
8797
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008798 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008799}
8800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008801#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008802void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008803 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008804{
8805 conf->cert_profile = profile;
8806}
8807
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008808/* Append a new keycert entry to a (possibly empty) list */
8809static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8810 mbedtls_x509_crt *cert,
8811 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008812{
niisato8ee24222018-06-25 19:05:48 +09008813 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008814
niisato8ee24222018-06-25 19:05:48 +09008815 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8816 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008817 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008818
niisato8ee24222018-06-25 19:05:48 +09008819 new_cert->cert = cert;
8820 new_cert->key = key;
8821 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008822
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008823 /* Update head is the list was null, else add to the end */
8824 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008825 {
niisato8ee24222018-06-25 19:05:48 +09008826 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008827 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008828 else
8829 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008830 mbedtls_ssl_key_cert *cur = *head;
8831 while( cur->next != NULL )
8832 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008833 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008834 }
8835
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008836 return( 0 );
8837}
8838
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008839int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008840 mbedtls_x509_crt *own_cert,
8841 mbedtls_pk_context *pk_key )
8842{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008843 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008844}
8845
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008846void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008847 mbedtls_x509_crt *ca_chain,
8848 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008849{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008850 conf->ca_chain = ca_chain;
8851 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008852
8853#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8854 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8855 * cannot be used together. */
8856 conf->f_ca_cb = NULL;
8857 conf->p_ca_cb = NULL;
8858#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008859}
Hanno Becker5adaad92019-03-27 16:54:37 +00008860
8861#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8862void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008863 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008864 void *p_ca_cb )
8865{
8866 conf->f_ca_cb = f_ca_cb;
8867 conf->p_ca_cb = p_ca_cb;
8868
8869 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8870 * cannot be used together. */
8871 conf->ca_chain = NULL;
8872 conf->ca_crl = NULL;
8873}
8874#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008875#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008876
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008877#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8878int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8879 mbedtls_x509_crt *own_cert,
8880 mbedtls_pk_context *pk_key )
8881{
8882 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8883 own_cert, pk_key ) );
8884}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008885
8886void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8887 mbedtls_x509_crt *ca_chain,
8888 mbedtls_x509_crl *ca_crl )
8889{
8890 ssl->handshake->sni_ca_chain = ca_chain;
8891 ssl->handshake->sni_ca_crl = ca_crl;
8892}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008893
8894void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8895 int authmode )
8896{
8897 ssl->handshake->sni_authmode = authmode;
8898}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008899#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8900
Hanno Becker8927c832019-04-03 12:52:50 +01008901#if defined(MBEDTLS_X509_CRT_PARSE_C)
8902void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8903 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8904 void *p_vrfy )
8905{
8906 ssl->f_vrfy = f_vrfy;
8907 ssl->p_vrfy = p_vrfy;
8908}
8909#endif
8910
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008911#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008912/*
8913 * Set EC J-PAKE password for current handshake
8914 */
8915int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8916 const unsigned char *pw,
8917 size_t pw_len )
8918{
8919 mbedtls_ecjpake_role role;
8920
Janos Follath8eb64132016-06-03 15:40:57 +01008921 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008922 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8923
8924 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8925 role = MBEDTLS_ECJPAKE_SERVER;
8926 else
8927 role = MBEDTLS_ECJPAKE_CLIENT;
8928
8929 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8930 role,
8931 MBEDTLS_MD_SHA256,
8932 MBEDTLS_ECP_DP_SECP256R1,
8933 pw, pw_len ) );
8934}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008935#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008937#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008938
8939static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8940{
8941 /* Remove reference to existing PSK, if any. */
8942#if defined(MBEDTLS_USE_PSA_CRYPTO)
8943 if( conf->psk_opaque != 0 )
8944 {
8945 /* The maintenance of the PSK key slot is the
8946 * user's responsibility. */
8947 conf->psk_opaque = 0;
8948 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008949 /* This and the following branch should never
8950 * be taken simultaenously as we maintain the
8951 * invariant that raw and opaque PSKs are never
8952 * configured simultaneously. As a safeguard,
8953 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008954#endif /* MBEDTLS_USE_PSA_CRYPTO */
8955 if( conf->psk != NULL )
8956 {
8957 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8958
8959 mbedtls_free( conf->psk );
8960 conf->psk = NULL;
8961 conf->psk_len = 0;
8962 }
8963
8964 /* Remove reference to PSK identity, if any. */
8965 if( conf->psk_identity != NULL )
8966 {
8967 mbedtls_free( conf->psk_identity );
8968 conf->psk_identity = NULL;
8969 conf->psk_identity_len = 0;
8970 }
8971}
8972
Hanno Becker7390c712018-11-15 13:33:04 +00008973/* This function assumes that PSK identity in the SSL config is unset.
8974 * It checks that the provided identity is well-formed and attempts
8975 * to make a copy of it in the SSL config.
8976 * On failure, the PSK identity in the config remains unset. */
8977static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8978 unsigned char const *psk_identity,
8979 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008980{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008981 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008982 if( psk_identity == NULL ||
8983 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008984 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008985 {
8986 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8987 }
8988
Hanno Becker7390c712018-11-15 13:33:04 +00008989 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8990 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008991 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008992
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008993 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008994 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008995
8996 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008997}
8998
Hanno Becker7390c712018-11-15 13:33:04 +00008999int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
9000 const unsigned char *psk, size_t psk_len,
9001 const unsigned char *psk_identity, size_t psk_identity_len )
9002{
9003 int ret;
9004 /* Remove opaque/raw PSK + PSK Identity */
9005 ssl_conf_remove_psk( conf );
9006
9007 /* Check and set raw PSK */
9008 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
9009 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9010 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
9011 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9012 conf->psk_len = psk_len;
9013 memcpy( conf->psk, psk, conf->psk_len );
9014
9015 /* Check and set PSK Identity */
9016 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
9017 if( ret != 0 )
9018 ssl_conf_remove_psk( conf );
9019
9020 return( ret );
9021}
9022
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009023static void ssl_remove_psk( mbedtls_ssl_context *ssl )
9024{
9025#if defined(MBEDTLS_USE_PSA_CRYPTO)
9026 if( ssl->handshake->psk_opaque != 0 )
9027 {
9028 ssl->handshake->psk_opaque = 0;
9029 }
9030 else
9031#endif /* MBEDTLS_USE_PSA_CRYPTO */
9032 if( ssl->handshake->psk != NULL )
9033 {
9034 mbedtls_platform_zeroize( ssl->handshake->psk,
9035 ssl->handshake->psk_len );
9036 mbedtls_free( ssl->handshake->psk );
9037 ssl->handshake->psk_len = 0;
9038 }
9039}
9040
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009041int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9042 const unsigned char *psk, size_t psk_len )
9043{
9044 if( psk == NULL || ssl->handshake == NULL )
9045 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9046
9047 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9048 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9049
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009050 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009051
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009052 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009053 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009054
9055 ssl->handshake->psk_len = psk_len;
9056 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
9057
9058 return( 0 );
9059}
9060
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009061#if defined(MBEDTLS_USE_PSA_CRYPTO)
9062int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009063 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009064 const unsigned char *psk_identity,
9065 size_t psk_identity_len )
9066{
Hanno Becker7390c712018-11-15 13:33:04 +00009067 int ret;
9068 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009069 ssl_conf_remove_psk( conf );
9070
Hanno Becker7390c712018-11-15 13:33:04 +00009071 /* Check and set opaque PSK */
9072 if( psk_slot == 0 )
9073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009074 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009075
9076 /* Check and set PSK Identity */
9077 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9078 psk_identity_len );
9079 if( ret != 0 )
9080 ssl_conf_remove_psk( conf );
9081
9082 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009083}
9084
9085int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009086 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009087{
9088 if( psk_slot == 0 || ssl->handshake == NULL )
9089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9090
9091 ssl_remove_psk( ssl );
9092 ssl->handshake->psk_opaque = psk_slot;
9093 return( 0 );
9094}
9095#endif /* MBEDTLS_USE_PSA_CRYPTO */
9096
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009097void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009098 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009099 size_t),
9100 void *p_psk )
9101{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009102 conf->f_psk = f_psk;
9103 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009104}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009105#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009106
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009107#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009108
9109#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009110int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009111{
9112 int ret;
9113
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009114 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9115 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9116 {
9117 mbedtls_mpi_free( &conf->dhm_P );
9118 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009119 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009120 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009121
9122 return( 0 );
9123}
Hanno Becker470a8c42017-10-04 15:28:46 +01009124#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009125
Hanno Beckera90658f2017-10-04 15:29:08 +01009126int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9127 const unsigned char *dhm_P, size_t P_len,
9128 const unsigned char *dhm_G, size_t G_len )
9129{
9130 int ret;
9131
9132 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9133 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9134 {
9135 mbedtls_mpi_free( &conf->dhm_P );
9136 mbedtls_mpi_free( &conf->dhm_G );
9137 return( ret );
9138 }
9139
9140 return( 0 );
9141}
Paul Bakker5121ce52009-01-03 21:22:43 +00009142
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009143int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009144{
9145 int ret;
9146
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009147 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9148 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9149 {
9150 mbedtls_mpi_free( &conf->dhm_P );
9151 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009152 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009153 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009154
9155 return( 0 );
9156}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009157#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009158
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009159#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9160/*
9161 * Set the minimum length for Diffie-Hellman parameters
9162 */
9163void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9164 unsigned int bitlen )
9165{
9166 conf->dhm_min_bitlen = bitlen;
9167}
9168#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9169
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009170#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009171/*
9172 * Set allowed/preferred hashes for handshake signatures
9173 */
9174void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9175 const int *hashes )
9176{
9177 conf->sig_hashes = hashes;
9178}
Hanno Becker947194e2017-04-07 13:25:49 +01009179#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009180
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009181#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009182/*
9183 * Set the allowed elliptic curves
9184 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009185void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009186 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009187{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009188 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009189}
Hanno Becker947194e2017-04-07 13:25:49 +01009190#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009191
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009192#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009193int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009194{
Hanno Becker947194e2017-04-07 13:25:49 +01009195 /* Initialize to suppress unnecessary compiler warning */
9196 size_t hostname_len = 0;
9197
9198 /* Check if new hostname is valid before
9199 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009200 if( hostname != NULL )
9201 {
9202 hostname_len = strlen( hostname );
9203
9204 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9205 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9206 }
9207
9208 /* Now it's clear that we will overwrite the old hostname,
9209 * so we can free it safely */
9210
9211 if( ssl->hostname != NULL )
9212 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009213 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009214 mbedtls_free( ssl->hostname );
9215 }
9216
9217 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009218
Paul Bakker5121ce52009-01-03 21:22:43 +00009219 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009220 {
9221 ssl->hostname = NULL;
9222 }
9223 else
9224 {
9225 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009226 if( ssl->hostname == NULL )
9227 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009228
Hanno Becker947194e2017-04-07 13:25:49 +01009229 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009230
Hanno Becker947194e2017-04-07 13:25:49 +01009231 ssl->hostname[hostname_len] = '\0';
9232 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009233
9234 return( 0 );
9235}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009236#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009237
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009238#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009239void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009240 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009241 const unsigned char *, size_t),
9242 void *p_sni )
9243{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009244 conf->f_sni = f_sni;
9245 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009246}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009247#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009249#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009250int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009251{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009252 size_t cur_len, tot_len;
9253 const char **p;
9254
9255 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009256 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9257 * MUST NOT be truncated."
9258 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009259 */
9260 tot_len = 0;
9261 for( p = protos; *p != NULL; p++ )
9262 {
9263 cur_len = strlen( *p );
9264 tot_len += cur_len;
9265
9266 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009268 }
9269
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009270 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009271
9272 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009273}
9274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009275const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009276{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009277 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009278}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009279#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009280
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009281void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009282{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009283 conf->max_major_ver = major;
9284 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009285}
9286
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009287void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009288{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009289 conf->min_major_ver = major;
9290 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009291}
9292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009293#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009294void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009295{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009296 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009297}
9298#endif
9299
Janos Follath088ce432017-04-10 12:42:31 +01009300#if defined(MBEDTLS_SSL_SRV_C)
9301void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9302 char cert_req_ca_list )
9303{
9304 conf->cert_req_ca_list = cert_req_ca_list;
9305}
9306#endif
9307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009309void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009310{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009311 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009312}
9313#endif
9314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009316void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009317{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009318 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009319}
9320#endif
9321
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009322#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009323void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009324{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009325 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009326}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009327#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009329#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009330int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009331{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009332 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009333 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009335 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009336 }
9337
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009338 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009339
9340 return( 0 );
9341}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009342#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009345void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009346{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009347 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009348}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009349#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009351#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009352void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009353{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009354 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009355}
9356#endif
9357
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009358void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009359{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009360 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009361}
9362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009363#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009364void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009365{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009366 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009367}
9368
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009369void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009370{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009371 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009372}
9373
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009374void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009375 const unsigned char period[8] )
9376{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009377 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009378}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009379#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009381#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009382#if defined(MBEDTLS_SSL_CLI_C)
9383void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009384{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009385 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009386}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009387#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009388
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009389#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009390void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9391 mbedtls_ssl_ticket_write_t *f_ticket_write,
9392 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9393 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009394{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009395 conf->f_ticket_write = f_ticket_write;
9396 conf->f_ticket_parse = f_ticket_parse;
9397 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009398}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009399#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009400#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009401
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009402#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9403void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9404 mbedtls_ssl_export_keys_t *f_export_keys,
9405 void *p_export_keys )
9406{
9407 conf->f_export_keys = f_export_keys;
9408 conf->p_export_keys = p_export_keys;
9409}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009410
9411void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9412 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9413 void *p_export_keys )
9414{
9415 conf->f_export_keys_ext = f_export_keys_ext;
9416 conf->p_export_keys = p_export_keys;
9417}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009418#endif
9419
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009420#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009421void mbedtls_ssl_conf_async_private_cb(
9422 mbedtls_ssl_config *conf,
9423 mbedtls_ssl_async_sign_t *f_async_sign,
9424 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9425 mbedtls_ssl_async_resume_t *f_async_resume,
9426 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009427 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009428{
9429 conf->f_async_sign_start = f_async_sign;
9430 conf->f_async_decrypt_start = f_async_decrypt;
9431 conf->f_async_resume = f_async_resume;
9432 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009433 conf->p_async_config_data = async_config_data;
9434}
9435
Gilles Peskine8f97af72018-04-26 11:46:10 +02009436void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9437{
9438 return( conf->p_async_config_data );
9439}
9440
Gilles Peskine1febfef2018-04-30 11:54:39 +02009441void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009442{
9443 if( ssl->handshake == NULL )
9444 return( NULL );
9445 else
9446 return( ssl->handshake->user_async_ctx );
9447}
9448
Gilles Peskine1febfef2018-04-30 11:54:39 +02009449void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009450 void *ctx )
9451{
9452 if( ssl->handshake != NULL )
9453 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009454}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009455#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009456
Paul Bakker5121ce52009-01-03 21:22:43 +00009457/*
9458 * SSL get accessors
9459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009460size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009461{
9462 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9463}
9464
Hanno Becker8b170a02017-10-10 11:51:19 +01009465int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9466{
9467 /*
9468 * Case A: We're currently holding back
9469 * a message for further processing.
9470 */
9471
9472 if( ssl->keep_current_message == 1 )
9473 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009474 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009475 return( 1 );
9476 }
9477
9478 /*
9479 * Case B: Further records are pending in the current datagram.
9480 */
9481
9482#if defined(MBEDTLS_SSL_PROTO_DTLS)
9483 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9484 ssl->in_left > ssl->next_record_offset )
9485 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009486 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009487 return( 1 );
9488 }
9489#endif /* MBEDTLS_SSL_PROTO_DTLS */
9490
9491 /*
9492 * Case C: A handshake message is being processed.
9493 */
9494
Hanno Becker8b170a02017-10-10 11:51:19 +01009495 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9496 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009497 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009498 return( 1 );
9499 }
9500
9501 /*
9502 * Case D: An application data message is being processed
9503 */
9504 if( ssl->in_offt != NULL )
9505 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009506 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009507 return( 1 );
9508 }
9509
9510 /*
9511 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009512 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009513 * we implement support for multiple alerts in single records.
9514 */
9515
9516 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9517 return( 0 );
9518}
9519
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009520uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009521{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009522 if( ssl->session != NULL )
9523 return( ssl->session->verify_result );
9524
9525 if( ssl->session_negotiate != NULL )
9526 return( ssl->session_negotiate->verify_result );
9527
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009528 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009529}
9530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009532{
Paul Bakker926c8e42013-03-06 10:23:34 +01009533 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009534 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009536 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009537}
9538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009539const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009540{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009542 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009543 {
9544 switch( ssl->minor_ver )
9545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009547 return( "DTLSv1.0" );
9548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009550 return( "DTLSv1.2" );
9551
9552 default:
9553 return( "unknown (DTLS)" );
9554 }
9555 }
9556#endif
9557
Paul Bakker43ca69c2011-01-15 17:35:19 +00009558 switch( ssl->minor_ver )
9559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009560 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009561 return( "SSLv3.0" );
9562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009564 return( "TLSv1.0" );
9565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009567 return( "TLSv1.1" );
9568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009569 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009570 return( "TLSv1.2" );
9571
Paul Bakker43ca69c2011-01-15 17:35:19 +00009572 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009573 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009574 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009575}
9576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009577int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009578{
Hanno Becker3136ede2018-08-17 15:28:19 +01009579 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009580 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009581 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009582
Hanno Becker5903de42019-05-03 14:46:38 +01009583 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9584
Hanno Becker78640902018-08-13 16:35:15 +01009585 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009586 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009588#if defined(MBEDTLS_ZLIB_SUPPORT)
9589 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9590 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009591#endif
9592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009593 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009595 case MBEDTLS_MODE_GCM:
9596 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009597 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009598 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009599 transform_expansion = transform->minlen;
9600 break;
9601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009603
9604 block_size = mbedtls_cipher_get_block_size(
9605 &transform->cipher_ctx_enc );
9606
Hanno Becker3136ede2018-08-17 15:28:19 +01009607 /* Expansion due to the addition of the MAC. */
9608 transform_expansion += transform->maclen;
9609
9610 /* Expansion due to the addition of CBC padding;
9611 * Theoretically up to 256 bytes, but we never use
9612 * more than the block size of the underlying cipher. */
9613 transform_expansion += block_size;
9614
9615 /* For TLS 1.1 or higher, an explicit IV is added
9616 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009617#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9618 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009619 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009620#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009621
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009622 break;
9623
9624 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009626 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009627 }
9628
Hanno Beckera0e20d02019-05-15 14:03:01 +01009629#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009630 if( transform->out_cid_len != 0 )
9631 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009632#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009633
Hanno Becker5903de42019-05-03 14:46:38 +01009634 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009635}
9636
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009637#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9638size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9639{
9640 size_t max_len;
9641
9642 /*
9643 * Assume mfl_code is correct since it was checked when set
9644 */
Angus Grattond8213d02016-05-25 20:56:48 +10009645 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009646
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009647 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009648 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009649 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009650 {
Angus Grattond8213d02016-05-25 20:56:48 +10009651 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009652 }
9653
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009654 /* During a handshake, use the value being negotiated */
9655 if( ssl->session_negotiate != NULL &&
9656 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9657 {
9658 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9659 }
9660
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009661 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009662}
9663#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9664
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009665#if defined(MBEDTLS_SSL_PROTO_DTLS)
9666static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9667{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009668 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9669 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9670 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9671 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9672 return ( 0 );
9673
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009674 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9675 return( ssl->mtu );
9676
9677 if( ssl->mtu == 0 )
9678 return( ssl->handshake->mtu );
9679
9680 return( ssl->mtu < ssl->handshake->mtu ?
9681 ssl->mtu : ssl->handshake->mtu );
9682}
9683#endif /* MBEDTLS_SSL_PROTO_DTLS */
9684
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009685int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9686{
9687 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9688
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009689#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9690 !defined(MBEDTLS_SSL_PROTO_DTLS)
9691 (void) ssl;
9692#endif
9693
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009694#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9695 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9696
9697 if( max_len > mfl )
9698 max_len = mfl;
9699#endif
9700
9701#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009702 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009703 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009704 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009705 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9706 const size_t overhead = (size_t) ret;
9707
9708 if( ret < 0 )
9709 return( ret );
9710
9711 if( mtu <= overhead )
9712 {
9713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9714 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9715 }
9716
9717 if( max_len > mtu - overhead )
9718 max_len = mtu - overhead;
9719 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009720#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009721
Hanno Becker0defedb2018-08-10 12:35:02 +01009722#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9723 !defined(MBEDTLS_SSL_PROTO_DTLS)
9724 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009725#endif
9726
9727 return( (int) max_len );
9728}
9729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009730#if defined(MBEDTLS_X509_CRT_PARSE_C)
9731const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009732{
9733 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009734 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009735
Hanno Beckere6824572019-02-07 13:18:46 +00009736#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009737 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009738#else
9739 return( NULL );
9740#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009741}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009742#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009744#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009745int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9746 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009747{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009748 if( ssl == NULL ||
9749 dst == NULL ||
9750 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009751 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009753 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009754 }
9755
Hanno Becker52055ae2019-02-06 14:30:46 +00009756 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009757}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009758#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009759
Paul Bakker5121ce52009-01-03 21:22:43 +00009760/*
Paul Bakker1961b702013-01-25 14:49:24 +01009761 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009763int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009764{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009765 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009766
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009767 if( ssl == NULL || ssl->conf == NULL )
9768 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009771 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009772 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009773#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009775 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009776 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009777#endif
9778
Paul Bakker1961b702013-01-25 14:49:24 +01009779 return( ret );
9780}
9781
9782/*
9783 * Perform the SSL handshake
9784 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009785int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009786{
9787 int ret = 0;
9788
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009789 if( ssl == NULL || ssl->conf == NULL )
9790 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009794 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009796 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009797
9798 if( ret != 0 )
9799 break;
9800 }
9801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009803
9804 return( ret );
9805}
9806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807#if defined(MBEDTLS_SSL_RENEGOTIATION)
9808#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009809/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009810 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009813{
9814 int ret;
9815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009817
9818 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9820 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009821
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009822 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009823 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009824 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009825 return( ret );
9826 }
9827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009829
9830 return( 0 );
9831}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009832#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009833
9834/*
9835 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009836 * - any side: calling mbedtls_ssl_renegotiate(),
9837 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9838 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009839 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009840 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009844{
9845 int ret;
9846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009848
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009849 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9850 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009851
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009852 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9853 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009854#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009855 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009856 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009857 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009858 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009859 ssl->handshake->out_msg_seq = 1;
9860 else
9861 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009862 }
9863#endif
9864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009865 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9866 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009868 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009870 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009871 return( ret );
9872 }
9873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009875
9876 return( 0 );
9877}
9878
9879/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009880 * Renegotiate current connection on client,
9881 * or request renegotiation on server
9882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009884{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009885 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009886
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009887 if( ssl == NULL || ssl->conf == NULL )
9888 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009890#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009891 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009892 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009894 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9895 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009897 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009898
9899 /* Did we already try/start sending HelloRequest? */
9900 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009901 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009902
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009903 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009904 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009905#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009907#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009908 /*
9909 * On client, either start the renegotiation process or,
9910 * if already in progress, continue the handshake
9911 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009912 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009914 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9915 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009916
9917 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009919 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009920 return( ret );
9921 }
9922 }
9923 else
9924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009925 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009928 return( ret );
9929 }
9930 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009931#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009932
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009933 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009934}
9935
9936/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009937 * Check record counters and renegotiate if they're above the limit.
9938 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009939static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009940{
Andres AG2196c7f2016-12-15 17:01:16 +00009941 size_t ep_len = ssl_ep_len( ssl );
9942 int in_ctr_cmp;
9943 int out_ctr_cmp;
9944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009945 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9946 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009947 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009948 {
9949 return( 0 );
9950 }
9951
Andres AG2196c7f2016-12-15 17:01:16 +00009952 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9953 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009954 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009955 ssl->conf->renego_period + ep_len, 8 - ep_len );
9956
9957 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009958 {
9959 return( 0 );
9960 }
9961
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009963 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009964}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009965#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009966
9967/*
9968 * Receive application data decrypted from the SSL layer
9969 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009970int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009971{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009972 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009973 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009974
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009975 if( ssl == NULL || ssl->conf == NULL )
9976 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009980#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009981 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009983 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009984 return( ret );
9985
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009986 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009987 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009988 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009989 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009990 return( ret );
9991 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009992 }
9993#endif
9994
Hanno Becker4a810fb2017-05-24 16:27:30 +01009995 /*
9996 * Check if renegotiation is necessary and/or handshake is
9997 * in process. If yes, perform/continue, and fall through
9998 * if an unexpected packet is received while the client
9999 * is waiting for the ServerHello.
10000 *
10001 * (There is no equivalent to the last condition on
10002 * the server-side as it is not treated as within
10003 * a handshake while waiting for the ClientHello
10004 * after a renegotiation request.)
10005 */
10006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010007#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010008 ret = ssl_check_ctr_renegotiate( ssl );
10009 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10010 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010012 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010013 return( ret );
10014 }
10015#endif
10016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010017 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010019 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010020 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10021 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010023 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010024 return( ret );
10025 }
10026 }
10027
Hanno Beckere41158b2017-10-23 13:30:32 +010010028 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010029 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010030 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010031 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010032 if( ssl->f_get_timer != NULL &&
10033 ssl->f_get_timer( ssl->p_timer ) == -1 )
10034 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010035 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010036 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010037
Hanno Becker327c93b2018-08-15 13:56:18 +010010038 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010039 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010040 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10041 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010042
Hanno Becker4a810fb2017-05-24 16:27:30 +010010043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10044 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010045 }
10046
10047 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010048 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010049 {
10050 /*
10051 * OpenSSL sends empty messages to randomize the IV
10052 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010053 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010055 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010056 return( 0 );
10057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010058 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010059 return( ret );
10060 }
10061 }
10062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010063 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010066
Hanno Becker4a810fb2017-05-24 16:27:30 +010010067 /*
10068 * - For client-side, expect SERVER_HELLO_REQUEST.
10069 * - For server-side, expect CLIENT_HELLO.
10070 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10071 */
10072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010073#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010074 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010075 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010076 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010079
10080 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010081#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010082 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010083 {
10084 continue;
10085 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010086#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010087 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010088 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010089#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010090
Hanno Becker4a810fb2017-05-24 16:27:30 +010010091#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010092 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010093 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
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 );
Paul Bakker48916f92012-09-16 19:57:18 +000010105 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010106#endif /* MBEDTLS_SSL_SRV_C */
10107
Hanno Becker21df7f92017-10-17 11:03:26 +010010108#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010109 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010110 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10111 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10112 ssl->conf->allow_legacy_renegotiation ==
10113 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10114 {
10115 /*
10116 * Accept renegotiation request
10117 */
Paul Bakker48916f92012-09-16 19:57:18 +000010118
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010119 /* DTLS clients need to know renego is server-initiated */
10120#if defined(MBEDTLS_SSL_PROTO_DTLS)
10121 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10122 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10123 {
10124 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10125 }
10126#endif
10127 ret = ssl_start_renegotiation( ssl );
10128 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10129 ret != 0 )
10130 {
10131 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10132 return( ret );
10133 }
10134 }
10135 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010136#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010137 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010138 /*
10139 * Refuse renegotiation
10140 */
10141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010142 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010144#if defined(MBEDTLS_SSL_PROTO_SSL3)
10145 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010146 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010147 /* SSLv3 does not have a "no_renegotiation" warning, so
10148 we send a fatal alert and abort the connection. */
10149 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10150 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10151 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010152 }
10153 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010154#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10155#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10156 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10157 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010159 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10160 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10161 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010162 {
10163 return( ret );
10164 }
Paul Bakker48916f92012-09-16 19:57:18 +000010165 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010166 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010167#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10168 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10171 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010172 }
Paul Bakker48916f92012-09-16 19:57:18 +000010173 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010174
Hanno Becker90333da2017-10-10 11:27:13 +010010175 /* At this point, we don't know whether the renegotiation has been
10176 * completed or not. The cases to consider are the following:
10177 * 1) The renegotiation is complete. In this case, no new record
10178 * has been read yet.
10179 * 2) The renegotiation is incomplete because the client received
10180 * an application data record while awaiting the ServerHello.
10181 * 3) The renegotiation is incomplete because the client received
10182 * a non-handshake, non-application data message while awaiting
10183 * the ServerHello.
10184 * In each of these case, looping will be the proper action:
10185 * - For 1), the next iteration will read a new record and check
10186 * if it's application data.
10187 * - For 2), the loop condition isn't satisfied as application data
10188 * is present, hence continue is the same as break
10189 * - For 3), the loop condition is satisfied and read_record
10190 * will re-deliver the message that was held back by the client
10191 * when expecting the ServerHello.
10192 */
10193 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010194 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010195#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010196 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010197 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010198 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010199 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010200 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010203 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010204 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010205 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010206 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010207 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010210 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10211 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010214 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010215 }
10216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010217 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10220 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010221 }
10222
10223 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010224
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010225 /* We're going to return something now, cancel timer,
10226 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010227 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010228 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010229
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010230#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010231 /* If we requested renego but received AppData, resend HelloRequest.
10232 * Do it now, after setting in_offt, to avoid taking this branch
10233 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010234#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010235 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010236 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010237 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010238 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010240 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010241 return( ret );
10242 }
10243 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010244#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010245#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010246 }
10247
10248 n = ( len < ssl->in_msglen )
10249 ? len : ssl->in_msglen;
10250
10251 memcpy( buf, ssl->in_offt, n );
10252 ssl->in_msglen -= n;
10253
10254 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010255 {
10256 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010257 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010258 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010259 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010260 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010261 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010262 /* more data available */
10263 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010264 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010267
Paul Bakker23986e52011-04-24 08:57:21 +000010268 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010269}
10270
10271/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010272 * Send application data to be encrypted by the SSL layer, taking care of max
10273 * fragment length and buffer size.
10274 *
10275 * According to RFC 5246 Section 6.2.1:
10276 *
10277 * Zero-length fragments of Application data MAY be sent as they are
10278 * potentially useful as a traffic analysis countermeasure.
10279 *
10280 * Therefore, it is possible that the input message length is 0 and the
10281 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010282 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010283static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010284 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010285{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010286 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10287 const size_t max_len = (size_t) ret;
10288
10289 if( ret < 0 )
10290 {
10291 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10292 return( ret );
10293 }
10294
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010295 if( len > max_len )
10296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010297#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010298 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010301 "maximum fragment length: %d > %d",
10302 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010303 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010304 }
10305 else
10306#endif
10307 len = max_len;
10308 }
Paul Bakker887bd502011-06-08 13:10:54 +000010309
Paul Bakker5121ce52009-01-03 21:22:43 +000010310 if( ssl->out_left != 0 )
10311 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010312 /*
10313 * The user has previously tried to send the data and
10314 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10315 * written. In this case, we expect the high-level write function
10316 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010318 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010320 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010321 return( ret );
10322 }
10323 }
Paul Bakker887bd502011-06-08 13:10:54 +000010324 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010325 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010326 /*
10327 * The user is trying to send a message the first time, so we need to
10328 * copy the data into the internal buffers and setup the data structure
10329 * to keep track of partial writes
10330 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010331 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010332 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010333 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010334
Hanno Becker67bc7c32018-08-06 11:33:50 +010010335 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010338 return( ret );
10339 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010340 }
10341
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010342 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010343}
10344
10345/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010346 * Write application data, doing 1/n-1 splitting if necessary.
10347 *
10348 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010349 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010350 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010352#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010353static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010354 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010355{
10356 int ret;
10357
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010358 if( ssl->conf->cbc_record_splitting ==
10359 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010360 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010361 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10362 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10363 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010364 {
10365 return( ssl_write_real( ssl, buf, len ) );
10366 }
10367
10368 if( ssl->split_done == 0 )
10369 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010370 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010371 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010372 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010373 }
10374
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010375 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10376 return( ret );
10377 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010378
10379 return( ret + 1 );
10380}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010381#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010382
10383/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010384 * Write application data (public-facing wrapper)
10385 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010386int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010387{
10388 int ret;
10389
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010391
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010392 if( ssl == NULL || ssl->conf == NULL )
10393 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10394
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010395#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010396 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10397 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010398 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010399 return( ret );
10400 }
10401#endif
10402
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010403 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010404 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010405 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010406 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010407 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010408 return( ret );
10409 }
10410 }
10411
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010412#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010413 ret = ssl_write_split( ssl, buf, len );
10414#else
10415 ret = ssl_write_real( ssl, buf, len );
10416#endif
10417
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010419
10420 return( ret );
10421}
10422
10423/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010424 * Notify the peer that the connection is being closed
10425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010426int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010427{
10428 int ret;
10429
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010430 if( ssl == NULL || ssl->conf == NULL )
10431 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010434
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010435 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010436 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010438 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010440 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10441 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10442 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010444 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010445 return( ret );
10446 }
10447 }
10448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010450
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010451 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010452}
10453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010454void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010455{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010456 if( transform == NULL )
10457 return;
10458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010459#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010460 deflateEnd( &transform->ctx_deflate );
10461 inflateEnd( &transform->ctx_inflate );
10462#endif
10463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010464 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10465 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010466
Hanno Beckerd56ed242018-01-03 15:32:51 +000010467#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010468 mbedtls_md_free( &transform->md_ctx_enc );
10469 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010470#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010471
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010472 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010473}
10474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010475#if defined(MBEDTLS_X509_CRT_PARSE_C)
10476static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010477{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010478 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010479
10480 while( cur != NULL )
10481 {
10482 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010483 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010484 cur = next;
10485 }
10486}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010487#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010488
Hanno Becker0271f962018-08-16 13:23:47 +010010489#if defined(MBEDTLS_SSL_PROTO_DTLS)
10490
10491static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10492{
10493 unsigned offset;
10494 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10495
10496 if( hs == NULL )
10497 return;
10498
Hanno Becker283f5ef2018-08-24 09:34:47 +010010499 ssl_free_buffered_record( ssl );
10500
Hanno Becker0271f962018-08-16 13:23:47 +010010501 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010502 ssl_buffering_free_slot( ssl, offset );
10503}
10504
10505static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10506 uint8_t slot )
10507{
10508 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10509 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010510
10511 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10512 return;
10513
Hanno Beckere605b192018-08-21 15:59:07 +010010514 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010515 {
Hanno Beckere605b192018-08-21 15:59:07 +010010516 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010517 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010518 mbedtls_free( hs_buf->data );
10519 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010520 }
10521}
10522
10523#endif /* MBEDTLS_SSL_PROTO_DTLS */
10524
Gilles Peskine9b562d52018-04-25 20:32:43 +020010525void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010526{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010527 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10528
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010529 if( handshake == NULL )
10530 return;
10531
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010532#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10533 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10534 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010535 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010536 handshake->async_in_progress = 0;
10537 }
10538#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10539
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010540#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10541 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10542 mbedtls_md5_free( &handshake->fin_md5 );
10543 mbedtls_sha1_free( &handshake->fin_sha1 );
10544#endif
10545#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10546#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010547#if defined(MBEDTLS_USE_PSA_CRYPTO)
10548 psa_hash_abort( &handshake->fin_sha256_psa );
10549#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010550 mbedtls_sha256_free( &handshake->fin_sha256 );
10551#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010552#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010553#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010554#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010555 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010556#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010557 mbedtls_sha512_free( &handshake->fin_sha512 );
10558#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010559#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010560#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010562#if defined(MBEDTLS_DHM_C)
10563 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010564#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010565#if defined(MBEDTLS_ECDH_C)
10566 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010567#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010568#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010569 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010570#if defined(MBEDTLS_SSL_CLI_C)
10571 mbedtls_free( handshake->ecjpake_cache );
10572 handshake->ecjpake_cache = NULL;
10573 handshake->ecjpake_cache_len = 0;
10574#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010575#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010576
Janos Follath4ae5c292016-02-10 11:27:43 +000010577#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10578 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010579 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010580 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010581#endif
10582
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010583#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10584 if( handshake->psk != NULL )
10585 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010586 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010587 mbedtls_free( handshake->psk );
10588 }
10589#endif
10590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010591#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10592 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010593 /*
10594 * Free only the linked list wrapper, not the keys themselves
10595 * since the belong to the SNI callback
10596 */
10597 if( handshake->sni_key_cert != NULL )
10598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010599 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010600
10601 while( cur != NULL )
10602 {
10603 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010604 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010605 cur = next;
10606 }
10607 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010608#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010609
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010610#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010611 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010612 if( handshake->ecrs_peer_cert != NULL )
10613 {
10614 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10615 mbedtls_free( handshake->ecrs_peer_cert );
10616 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010617#endif
10618
Hanno Becker75173122019-02-06 16:18:31 +000010619#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10620 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10621 mbedtls_pk_free( &handshake->peer_pubkey );
10622#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010624#if defined(MBEDTLS_SSL_PROTO_DTLS)
10625 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010626 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010627 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010628#endif
10629
Hanno Becker4a63ed42019-01-08 11:39:35 +000010630#if defined(MBEDTLS_ECDH_C) && \
10631 defined(MBEDTLS_USE_PSA_CRYPTO)
10632 psa_destroy_key( handshake->ecdh_psa_privkey );
10633#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10634
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010635 mbedtls_platform_zeroize( handshake,
10636 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010637}
10638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010639void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010640{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010641 if( session == NULL )
10642 return;
10643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010644#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010645 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010646#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010647
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010648#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010649 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010650#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010651
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010652 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010653}
10654
Paul Bakker5121ce52009-01-03 21:22:43 +000010655/*
10656 * Free an SSL context
10657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010658void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010659{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010660 if( ssl == NULL )
10661 return;
10662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010664
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010665 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010666 {
Angus Grattond8213d02016-05-25 20:56:48 +100010667 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010668 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010669 }
10670
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010671 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010672 {
Angus Grattond8213d02016-05-25 20:56:48 +100010673 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010674 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010675 }
10676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010677#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010678 if( ssl->compress_buf != NULL )
10679 {
Angus Grattond8213d02016-05-25 20:56:48 +100010680 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010681 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010682 }
10683#endif
10684
Paul Bakker48916f92012-09-16 19:57:18 +000010685 if( ssl->transform )
10686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010687 mbedtls_ssl_transform_free( ssl->transform );
10688 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010689 }
10690
10691 if( ssl->handshake )
10692 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010693 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010694 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10695 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010697 mbedtls_free( ssl->handshake );
10698 mbedtls_free( ssl->transform_negotiate );
10699 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010700 }
10701
Paul Bakkerc0463502013-02-14 11:19:38 +010010702 if( ssl->session )
10703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010704 mbedtls_ssl_session_free( ssl->session );
10705 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010706 }
10707
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010708#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010709 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010710 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010711 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010712 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010713 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010714#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010716#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10717 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10720 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010721 }
10722#endif
10723
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010724#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010725 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010726#endif
10727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010729
Paul Bakker86f04f42013-02-14 11:20:09 +010010730 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010731 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010732}
10733
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010734/*
10735 * Initialze mbedtls_ssl_config
10736 */
10737void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10738{
10739 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10740}
10741
Simon Butcherc97b6972015-12-27 23:48:17 +000010742#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010743static int ssl_preset_default_hashes[] = {
10744#if defined(MBEDTLS_SHA512_C)
10745 MBEDTLS_MD_SHA512,
10746 MBEDTLS_MD_SHA384,
10747#endif
10748#if defined(MBEDTLS_SHA256_C)
10749 MBEDTLS_MD_SHA256,
10750 MBEDTLS_MD_SHA224,
10751#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010752#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010753 MBEDTLS_MD_SHA1,
10754#endif
10755 MBEDTLS_MD_NONE
10756};
Simon Butcherc97b6972015-12-27 23:48:17 +000010757#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010758
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010759static int ssl_preset_suiteb_ciphersuites[] = {
10760 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10761 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10762 0
10763};
10764
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010765#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010766static int ssl_preset_suiteb_hashes[] = {
10767 MBEDTLS_MD_SHA256,
10768 MBEDTLS_MD_SHA384,
10769 MBEDTLS_MD_NONE
10770};
10771#endif
10772
10773#if defined(MBEDTLS_ECP_C)
10774static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010775#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010776 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010777#endif
10778#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010779 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010780#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010781 MBEDTLS_ECP_DP_NONE
10782};
10783#endif
10784
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010785/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010786 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010787 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010788int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010789 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010790{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010791#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010792 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010793#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010794
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010795 /* Use the functions here so that they are covered in tests,
10796 * but otherwise access member directly for efficiency */
10797 mbedtls_ssl_conf_endpoint( conf, endpoint );
10798 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010799
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010800 /*
10801 * Things that are common to all presets
10802 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010803#if defined(MBEDTLS_SSL_CLI_C)
10804 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10805 {
10806 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10807#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10808 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10809#endif
10810 }
10811#endif
10812
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010813#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010814 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010815#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010816
10817#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10818 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10819#endif
10820
10821#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10822 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10823#endif
10824
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010825#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10826 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10827#endif
10828
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010829#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010830 conf->f_cookie_write = ssl_cookie_write_dummy;
10831 conf->f_cookie_check = ssl_cookie_check_dummy;
10832#endif
10833
10834#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10835 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10836#endif
10837
Janos Follath088ce432017-04-10 12:42:31 +010010838#if defined(MBEDTLS_SSL_SRV_C)
10839 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10840#endif
10841
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010842#if defined(MBEDTLS_SSL_PROTO_DTLS)
10843 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10844 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10845#endif
10846
10847#if defined(MBEDTLS_SSL_RENEGOTIATION)
10848 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010849 memset( conf->renego_period, 0x00, 2 );
10850 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010851#endif
10852
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010853#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10854 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10855 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010856 const unsigned char dhm_p[] =
10857 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10858 const unsigned char dhm_g[] =
10859 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10860
Hanno Beckera90658f2017-10-04 15:29:08 +010010861 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10862 dhm_p, sizeof( dhm_p ),
10863 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010864 {
10865 return( ret );
10866 }
10867 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010868#endif
10869
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010870 /*
10871 * Preset-specific defaults
10872 */
10873 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010874 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010875 /*
10876 * NSA Suite B
10877 */
10878 case MBEDTLS_SSL_PRESET_SUITEB:
10879 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10880 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10881 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10882 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10883
10884 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10885 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10886 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10887 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10888 ssl_preset_suiteb_ciphersuites;
10889
10890#if defined(MBEDTLS_X509_CRT_PARSE_C)
10891 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010892#endif
10893
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010894#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010895 conf->sig_hashes = ssl_preset_suiteb_hashes;
10896#endif
10897
10898#if defined(MBEDTLS_ECP_C)
10899 conf->curve_list = ssl_preset_suiteb_curves;
10900#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010901 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010902
10903 /*
10904 * Default
10905 */
10906 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010907 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10908 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10909 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10910 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10911 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10912 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10913 MBEDTLS_SSL_MIN_MINOR_VERSION :
10914 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010915 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10916 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10917
10918#if defined(MBEDTLS_SSL_PROTO_DTLS)
10919 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10920 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10921#endif
10922
10923 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10924 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10925 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10926 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10927 mbedtls_ssl_list_ciphersuites();
10928
10929#if defined(MBEDTLS_X509_CRT_PARSE_C)
10930 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10931#endif
10932
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010933#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010934 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010935#endif
10936
10937#if defined(MBEDTLS_ECP_C)
10938 conf->curve_list = mbedtls_ecp_grp_id_list();
10939#endif
10940
10941#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10942 conf->dhm_min_bitlen = 1024;
10943#endif
10944 }
10945
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010946 return( 0 );
10947}
10948
10949/*
10950 * Free mbedtls_ssl_config
10951 */
10952void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10953{
10954#if defined(MBEDTLS_DHM_C)
10955 mbedtls_mpi_free( &conf->dhm_P );
10956 mbedtls_mpi_free( &conf->dhm_G );
10957#endif
10958
10959#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10960 if( conf->psk != NULL )
10961 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010962 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010963 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010964 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010965 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010966 }
10967
10968 if( conf->psk_identity != NULL )
10969 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010970 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010971 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010972 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010973 conf->psk_identity_len = 0;
10974 }
10975#endif
10976
10977#if defined(MBEDTLS_X509_CRT_PARSE_C)
10978 ssl_key_cert_free( conf->key_cert );
10979#endif
10980
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010981 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010982}
10983
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010984#if defined(MBEDTLS_PK_C) && \
10985 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010986/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010987 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010989unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010990{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010991#if defined(MBEDTLS_RSA_C)
10992 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10993 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010994#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010995#if defined(MBEDTLS_ECDSA_C)
10996 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10997 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010998#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010999 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011000}
11001
Hanno Becker7e5437a2017-04-28 17:15:26 +010011002unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
11003{
11004 switch( type ) {
11005 case MBEDTLS_PK_RSA:
11006 return( MBEDTLS_SSL_SIG_RSA );
11007 case MBEDTLS_PK_ECDSA:
11008 case MBEDTLS_PK_ECKEY:
11009 return( MBEDTLS_SSL_SIG_ECDSA );
11010 default:
11011 return( MBEDTLS_SSL_SIG_ANON );
11012 }
11013}
11014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011015mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011016{
11017 switch( sig )
11018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011019#if defined(MBEDTLS_RSA_C)
11020 case MBEDTLS_SSL_SIG_RSA:
11021 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011022#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011023#if defined(MBEDTLS_ECDSA_C)
11024 case MBEDTLS_SSL_SIG_ECDSA:
11025 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011026#endif
11027 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011028 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011029 }
11030}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011031#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011032
Hanno Becker7e5437a2017-04-28 17:15:26 +010011033#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
11034 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
11035
11036/* Find an entry in a signature-hash set matching a given hash algorithm. */
11037mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
11038 mbedtls_pk_type_t sig_alg )
11039{
11040 switch( sig_alg )
11041 {
11042 case MBEDTLS_PK_RSA:
11043 return( set->rsa );
11044 case MBEDTLS_PK_ECDSA:
11045 return( set->ecdsa );
11046 default:
11047 return( MBEDTLS_MD_NONE );
11048 }
11049}
11050
11051/* Add a signature-hash-pair to a signature-hash set */
11052void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
11053 mbedtls_pk_type_t sig_alg,
11054 mbedtls_md_type_t md_alg )
11055{
11056 switch( sig_alg )
11057 {
11058 case MBEDTLS_PK_RSA:
11059 if( set->rsa == MBEDTLS_MD_NONE )
11060 set->rsa = md_alg;
11061 break;
11062
11063 case MBEDTLS_PK_ECDSA:
11064 if( set->ecdsa == MBEDTLS_MD_NONE )
11065 set->ecdsa = md_alg;
11066 break;
11067
11068 default:
11069 break;
11070 }
11071}
11072
11073/* Allow exactly one hash algorithm for each signature. */
11074void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11075 mbedtls_md_type_t md_alg )
11076{
11077 set->rsa = md_alg;
11078 set->ecdsa = md_alg;
11079}
11080
11081#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11082 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11083
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011084/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011085 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011087mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011088{
11089 switch( hash )
11090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011091#if defined(MBEDTLS_MD5_C)
11092 case MBEDTLS_SSL_HASH_MD5:
11093 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011094#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011095#if defined(MBEDTLS_SHA1_C)
11096 case MBEDTLS_SSL_HASH_SHA1:
11097 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011098#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011099#if defined(MBEDTLS_SHA256_C)
11100 case MBEDTLS_SSL_HASH_SHA224:
11101 return( MBEDTLS_MD_SHA224 );
11102 case MBEDTLS_SSL_HASH_SHA256:
11103 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011104#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011105#if defined(MBEDTLS_SHA512_C)
11106 case MBEDTLS_SSL_HASH_SHA384:
11107 return( MBEDTLS_MD_SHA384 );
11108 case MBEDTLS_SSL_HASH_SHA512:
11109 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011110#endif
11111 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011112 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011113 }
11114}
11115
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011116/*
11117 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11118 */
11119unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11120{
11121 switch( md )
11122 {
11123#if defined(MBEDTLS_MD5_C)
11124 case MBEDTLS_MD_MD5:
11125 return( MBEDTLS_SSL_HASH_MD5 );
11126#endif
11127#if defined(MBEDTLS_SHA1_C)
11128 case MBEDTLS_MD_SHA1:
11129 return( MBEDTLS_SSL_HASH_SHA1 );
11130#endif
11131#if defined(MBEDTLS_SHA256_C)
11132 case MBEDTLS_MD_SHA224:
11133 return( MBEDTLS_SSL_HASH_SHA224 );
11134 case MBEDTLS_MD_SHA256:
11135 return( MBEDTLS_SSL_HASH_SHA256 );
11136#endif
11137#if defined(MBEDTLS_SHA512_C)
11138 case MBEDTLS_MD_SHA384:
11139 return( MBEDTLS_SSL_HASH_SHA384 );
11140 case MBEDTLS_MD_SHA512:
11141 return( MBEDTLS_SSL_HASH_SHA512 );
11142#endif
11143 default:
11144 return( MBEDTLS_SSL_HASH_NONE );
11145 }
11146}
11147
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011148#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011149/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011150 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011151 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011152 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011153int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011154{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011155 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011156
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011157 if( ssl->conf->curve_list == NULL )
11158 return( -1 );
11159
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011160 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011161 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011162 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011163
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011164 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011165}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011166#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011167
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011168#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011169/*
11170 * Check if a hash proposed by the peer is in our list.
11171 * Return 0 if we're willing to use it, -1 otherwise.
11172 */
11173int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11174 mbedtls_md_type_t md )
11175{
11176 const int *cur;
11177
11178 if( ssl->conf->sig_hashes == NULL )
11179 return( -1 );
11180
11181 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11182 if( *cur == (int) md )
11183 return( 0 );
11184
11185 return( -1 );
11186}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011187#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011189#if defined(MBEDTLS_X509_CRT_PARSE_C)
11190int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11191 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011192 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011193 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011194{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011195 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011196#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011197 int usage = 0;
11198#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011199#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011200 const char *ext_oid;
11201 size_t ext_len;
11202#endif
11203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011204#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11205 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011206 ((void) cert);
11207 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011208 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011209#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011211#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11212 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011213 {
11214 /* Server part of the key exchange */
11215 switch( ciphersuite->key_exchange )
11216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011217 case MBEDTLS_KEY_EXCHANGE_RSA:
11218 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011219 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011220 break;
11221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011222 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11223 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11224 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11225 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011226 break;
11227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011228 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11229 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011230 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011231 break;
11232
11233 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011234 case MBEDTLS_KEY_EXCHANGE_NONE:
11235 case MBEDTLS_KEY_EXCHANGE_PSK:
11236 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11237 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011238 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011239 usage = 0;
11240 }
11241 }
11242 else
11243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011244 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11245 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011246 }
11247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011248 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011249 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011250 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011251 ret = -1;
11252 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011253#else
11254 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011255#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011257#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11258 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011260 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11261 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011262 }
11263 else
11264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011265 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11266 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011267 }
11268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011269 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011270 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011271 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011272 ret = -1;
11273 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011274#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011275
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011276 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011277}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011278#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011279
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011280/*
11281 * Convert version numbers to/from wire format
11282 * and, for DTLS, to/from TLS equivalent.
11283 *
11284 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011285 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011286 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11287 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011289void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011290 unsigned char ver[2] )
11291{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011292#if defined(MBEDTLS_SSL_PROTO_DTLS)
11293 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011295 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011296 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11297
11298 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11299 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11300 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011301 else
11302#else
11303 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011304#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011305 {
11306 ver[0] = (unsigned char) major;
11307 ver[1] = (unsigned char) minor;
11308 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011309}
11310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011311void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011312 const unsigned char ver[2] )
11313{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011314#if defined(MBEDTLS_SSL_PROTO_DTLS)
11315 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011316 {
11317 *major = 255 - ver[0] + 2;
11318 *minor = 255 - ver[1] + 1;
11319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011320 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011321 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11322 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011323 else
11324#else
11325 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011326#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011327 {
11328 *major = ver[0];
11329 *minor = ver[1];
11330 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011331}
11332
Simon Butcher99000142016-10-13 17:21:01 +010011333int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11334{
11335#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11336 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11337 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11338
11339 switch( md )
11340 {
11341#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11342#if defined(MBEDTLS_MD5_C)
11343 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011344 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011345#endif
11346#if defined(MBEDTLS_SHA1_C)
11347 case MBEDTLS_SSL_HASH_SHA1:
11348 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11349 break;
11350#endif
11351#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11352#if defined(MBEDTLS_SHA512_C)
11353 case MBEDTLS_SSL_HASH_SHA384:
11354 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11355 break;
11356#endif
11357#if defined(MBEDTLS_SHA256_C)
11358 case MBEDTLS_SSL_HASH_SHA256:
11359 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11360 break;
11361#endif
11362 default:
11363 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11364 }
11365
11366 return 0;
11367#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11368 (void) ssl;
11369 (void) md;
11370
11371 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11372#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11373}
11374
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011375#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11376 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11377int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11378 unsigned char *output,
11379 unsigned char *data, size_t data_len )
11380{
11381 int ret = 0;
11382 mbedtls_md5_context mbedtls_md5;
11383 mbedtls_sha1_context mbedtls_sha1;
11384
11385 mbedtls_md5_init( &mbedtls_md5 );
11386 mbedtls_sha1_init( &mbedtls_sha1 );
11387
11388 /*
11389 * digitally-signed struct {
11390 * opaque md5_hash[16];
11391 * opaque sha_hash[20];
11392 * };
11393 *
11394 * md5_hash
11395 * MD5(ClientHello.random + ServerHello.random
11396 * + ServerParams);
11397 * sha_hash
11398 * SHA(ClientHello.random + ServerHello.random
11399 * + ServerParams);
11400 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011401 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011402 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011403 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011404 goto exit;
11405 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011406 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011407 ssl->handshake->randbytes, 64 ) ) != 0 )
11408 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011409 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011410 goto exit;
11411 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011412 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011413 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011414 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011415 goto exit;
11416 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011417 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011418 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011420 goto exit;
11421 }
11422
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011423 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011424 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011425 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011426 goto exit;
11427 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011428 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011429 ssl->handshake->randbytes, 64 ) ) != 0 )
11430 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011431 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_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_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011435 data_len ) ) != 0 )
11436 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011437 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011438 goto exit;
11439 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011440 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011441 output + 16 ) ) != 0 )
11442 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011443 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011444 goto exit;
11445 }
11446
11447exit:
11448 mbedtls_md5_free( &mbedtls_md5 );
11449 mbedtls_sha1_free( &mbedtls_sha1 );
11450
11451 if( ret != 0 )
11452 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11453 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11454
11455 return( ret );
11456
11457}
11458#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11459 MBEDTLS_SSL_PROTO_TLS1_1 */
11460
11461#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11462 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011463
11464#if defined(MBEDTLS_USE_PSA_CRYPTO)
11465int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11466 unsigned char *hash, size_t *hashlen,
11467 unsigned char *data, size_t data_len,
11468 mbedtls_md_type_t md_alg )
11469{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011470 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011471 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011472 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11473
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011474 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011475
11476 if( ( status = psa_hash_setup( &hash_operation,
11477 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011478 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011479 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011480 goto exit;
11481 }
11482
Andrzej Kurek814feff2019-01-14 04:35:19 -050011483 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11484 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011485 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011486 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011487 goto exit;
11488 }
11489
Andrzej Kurek814feff2019-01-14 04:35:19 -050011490 if( ( status = psa_hash_update( &hash_operation,
11491 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011492 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011493 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011494 goto exit;
11495 }
11496
Andrzej Kurek814feff2019-01-14 04:35:19 -050011497 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11498 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011499 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011500 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011501 goto exit;
11502 }
11503
11504exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011505 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011506 {
11507 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11508 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011509 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011510 {
11511 case PSA_ERROR_NOT_SUPPORTED:
11512 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011513 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011514 case PSA_ERROR_BUFFER_TOO_SMALL:
11515 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11516 case PSA_ERROR_INSUFFICIENT_MEMORY:
11517 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11518 default:
11519 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11520 }
11521 }
11522 return( 0 );
11523}
11524
11525#else
11526
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011527int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011528 unsigned char *hash, size_t *hashlen,
11529 unsigned char *data, size_t data_len,
11530 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011531{
11532 int ret = 0;
11533 mbedtls_md_context_t ctx;
11534 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011535 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011536
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011537 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011538
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011539 mbedtls_md_init( &ctx );
11540
11541 /*
11542 * digitally-signed struct {
11543 * opaque client_random[32];
11544 * opaque server_random[32];
11545 * ServerDHParams params;
11546 * };
11547 */
11548 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11549 {
11550 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11551 goto exit;
11552 }
11553 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11554 {
11555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11556 goto exit;
11557 }
11558 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11559 {
11560 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11561 goto exit;
11562 }
11563 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11564 {
11565 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11566 goto exit;
11567 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011568 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011569 {
11570 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11571 goto exit;
11572 }
11573
11574exit:
11575 mbedtls_md_free( &ctx );
11576
11577 if( ret != 0 )
11578 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11579 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11580
11581 return( ret );
11582}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011583#endif /* MBEDTLS_USE_PSA_CRYPTO */
11584
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011585#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11586 MBEDTLS_SSL_PROTO_TLS1_2 */
11587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011588#endif /* MBEDTLS_SSL_TLS_C */