blob: 913d6f9e26384c990d58a4e00049b35b8d399277 [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)
858static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
859static 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)
863static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
864static 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 );
870static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
871static 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 );
876static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
877static 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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000981{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200982 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000983#if defined(MBEDTLS_USE_PSA_CRYPTO)
984 int psa_fallthrough;
985#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000986 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000987 unsigned char keyblk[256];
988 unsigned char *key1;
989 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100990 unsigned char *mac_enc;
991 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000992 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200993 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000994 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000995 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 const mbedtls_cipher_info_t *cipher_info;
997 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100998
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000999 /* cf. RFC 5246, Section 8.1:
1000 * "The master secret is always exactly 48 bytes in length." */
1001 size_t const master_secret_len = 48;
1002
Hanno Becker35b23c72018-10-23 12:10:41 +01001003#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1004 unsigned char session_hash[48];
1005#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_ssl_session *session = ssl->session_negotiate;
1008 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
1009 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +00001010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001012
Jaeden Amero2de07f12019-06-05 13:32:08 +01001013#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1014 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001015 transform->encrypt_then_mac = session->encrypt_then_mac;
1016#endif
1017 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001018
1019 ciphersuite_info = handshake->ciphersuite_info;
1020 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001021 if( cipher_info == NULL )
1022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001024 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001026 }
1027
Hanno Beckere694c3e2017-12-27 21:34:08 +00001028 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001029 if( md_info == NULL )
1030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001032 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001034 }
1035
Hanno Beckera0e20d02019-05-15 14:03:01 +01001036#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001037 /* Copy own and peer's CID if the use of the CID
1038 * extension has been negotiated. */
1039 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1040 {
1041 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001042
Hanno Becker05154c32019-05-03 15:23:51 +01001043 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001044 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001045 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001046 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001047
1048 transform->out_cid_len = ssl->handshake->peer_cid_len;
1049 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1050 ssl->handshake->peer_cid_len );
1051 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1052 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001053 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001054#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001055
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +00001057 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +00001058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059#if defined(MBEDTLS_SSL_PROTO_SSL3)
1060 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001061 {
Paul Bakker48916f92012-09-16 19:57:18 +00001062 handshake->tls_prf = ssl3_prf;
1063 handshake->calc_verify = ssl_calc_verify_ssl;
1064 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001065 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001066 else
1067#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1069 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001070 {
Paul Bakker48916f92012-09-16 19:57:18 +00001071 handshake->tls_prf = tls1_prf;
1072 handshake->calc_verify = ssl_calc_verify_tls;
1073 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001074 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001075 else
1076#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1078#if defined(MBEDTLS_SHA512_C)
1079 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001080 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001081 {
Paul Bakker48916f92012-09-16 19:57:18 +00001082 handshake->tls_prf = tls_prf_sha384;
1083 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1084 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001085 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001086 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#if defined(MBEDTLS_SHA256_C)
1089 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001090 {
Paul Bakker48916f92012-09-16 19:57:18 +00001091 handshake->tls_prf = tls_prf_sha256;
1092 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1093 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001094 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001095 else
1096#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1100 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001101 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001102
1103 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 * SSLv3:
1105 * master =
1106 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1107 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1108 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001109 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001110 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001111 * master = PRF( premaster, "master secret", randbytes )[0..47]
1112 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001113 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001114 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001115 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1116 }
1117 else
1118 {
1119 /* The label for the KDF used for key expansion.
1120 * This is either "master secret" or "extended master secret"
1121 * depending on whether the Extended Master Secret extension
1122 * is used. */
1123 char const *lbl = "master secret";
1124
1125 /* The salt for the KDF used for key expansion.
1126 * - If the Extended Master Secret extension is not used,
1127 * this is ClientHello.Random + ServerHello.Random
1128 * (see Sect. 8.1 in RFC 5246).
1129 * - If the Extended Master Secret extension is used,
1130 * this is the transcript of the handshake so far.
1131 * (see Sect. 4 in RFC 7627). */
1132 unsigned char const *salt = handshake->randbytes;
1133 size_t salt_len = 64;
1134
1135#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001139
Hanno Becker35b23c72018-10-23 12:10:41 +01001140 lbl = "extended master secret";
1141 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001142 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1144 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001147 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1148 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001149 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001150 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001151 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001152#endif /* MBEDTLS_SHA512_C */
1153 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001154 }
1155 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001157 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001158
Hanno Becker35b23c72018-10-23 12:10:41 +01001159 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001160 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001161#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1162
Hanno Becker7d0a5692018-10-23 15:26:22 +01001163#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1164 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1165 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1166 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1167 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001168 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001169 /* Perform PSK-to-MS expansion in a single step. */
1170 psa_status_t status;
1171 psa_algorithm_t alg;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001172 psa_key_handle_t psk;
Janos Follathda6ac012019-08-16 13:47:29 +01001173 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +01001174 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001175
1176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1177
1178 psk = ssl->conf->psk_opaque;
1179 if( ssl->handshake->psk_opaque != 0 )
1180 psk = ssl->handshake->psk_opaque;
1181
Hanno Becker22bf1452019-04-05 11:21:08 +01001182 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001183 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1184 else
1185 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1186
Janos Follathda6ac012019-08-16 13:47:29 +01001187 status = psa_key_derivation( &derivation, psk, alg,
Hanno Becker7d0a5692018-10-23 15:26:22 +01001188 salt, salt_len,
1189 (unsigned char const *) lbl,
1190 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001191 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001192 if( status != PSA_SUCCESS )
1193 {
Janos Follathda6ac012019-08-16 13:47:29 +01001194 psa_key_derivation_abort( &derivation );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001195 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1196 }
1197
Janos Follathda6ac012019-08-16 13:47:29 +01001198 status = psa_key_derivation_output_bytes( &derivation,
Janos Follath6de99db2019-07-30 12:55:06 +01001199 session->master,
1200 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001201 if( status != PSA_SUCCESS )
1202 {
Janos Follathda6ac012019-08-16 13:47:29 +01001203 psa_key_derivation_abort( &derivation );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001204 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1205 }
1206
Janos Follathda6ac012019-08-16 13:47:29 +01001207 status = psa_key_derivation_abort( &derivation );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001208 if( status != PSA_SUCCESS )
1209 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001210 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001211 else
1212#endif
1213 {
1214 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1215 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001216 session->master,
1217 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001218 if( ret != 0 )
1219 {
1220 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1221 return( ret );
1222 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001223
Hanno Becker7d0a5692018-10-23 15:26:22 +01001224 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1225 handshake->premaster,
1226 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001227
Hanno Becker7d0a5692018-10-23 15:26:22 +01001228 mbedtls_platform_zeroize( handshake->premaster,
1229 sizeof(handshake->premaster) );
1230 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001232
1233 /*
1234 * Swap the client and server random values.
1235 */
Paul Bakker48916f92012-09-16 19:57:18 +00001236 memcpy( tmp, handshake->randbytes, 64 );
1237 memcpy( handshake->randbytes, tmp + 32, 32 );
1238 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001239 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001240
1241 /*
1242 * SSLv3:
1243 * key block =
1244 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1245 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1246 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1247 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1248 * ...
1249 *
1250 * TLSv1:
1251 * key block = PRF( master, "key expansion", randbytes )
1252 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001253 ret = handshake->tls_prf( session->master, 48, "key expansion",
1254 handshake->randbytes, 64, keyblk, 256 );
1255 if( ret != 0 )
1256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001258 return( ret );
1259 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1262 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1263 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1264 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1265 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266
Paul Bakker5121ce52009-01-03 21:22:43 +00001267 /*
1268 * Determine the appropriate key, IV and MAC length.
1269 */
Paul Bakker68884e32013-01-07 18:20:04 +01001270
Hanno Becker88aaf652017-12-27 08:17:40 +00001271 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001272
Hanno Becker8031d062018-01-03 15:32:31 +00001273#if defined(MBEDTLS_GCM_C) || \
1274 defined(MBEDTLS_CCM_C) || \
1275 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001277 cipher_info->mode == MBEDTLS_MODE_CCM ||
1278 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001279 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001280 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001281
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001282 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001283 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001284 transform->taglen =
1285 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001286
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001287 /* All modes haves 96-bit IVs;
1288 * GCM and CCM has 4 implicit and 8 explicit bytes
1289 * ChachaPoly has all 12 bytes implicit
1290 */
Paul Bakker68884e32013-01-07 18:20:04 +01001291 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001292 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1293 transform->fixed_ivlen = 12;
1294 else
1295 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001296
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001297 /* Minimum length of encrypted record */
1298 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001299 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001300 }
1301 else
Hanno Becker8031d062018-01-03 15:32:31 +00001302#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1303#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1304 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1305 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001306 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001307 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1309 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001312 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001313 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001314
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001315 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001316 mac_key_len = mbedtls_md_get_size( md_info );
1317 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001320 /*
1321 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1322 * (rfc 6066 page 13 or rfc 2104 section 4),
1323 * so we only need to adjust the length here.
1324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001328
1329#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1330 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001331 * HMAC implementation which also truncates the key
1332 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001333 mac_key_len = transform->maclen;
1334#endif
1335 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001337
1338 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001339 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001340
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001341 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001343 transform->minlen = transform->maclen;
1344 else
Paul Bakker68884e32013-01-07 18:20:04 +01001345 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001346 /*
1347 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001348 * 1. if EtM is in use: one block plus MAC
1349 * otherwise: * first multiple of blocklen greater than maclen
1350 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1353 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001354 {
1355 transform->minlen = transform->maclen
1356 + cipher_info->block_size;
1357 }
1358 else
1359#endif
1360 {
1361 transform->minlen = transform->maclen
1362 + cipher_info->block_size
1363 - transform->maclen % cipher_info->block_size;
1364 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1367 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1368 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001369 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001370 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001371#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1373 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1374 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001375 {
1376 transform->minlen += transform->ivlen;
1377 }
1378 else
1379#endif
1380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001382 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1383 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001384 }
Paul Bakker68884e32013-01-07 18:20:04 +01001385 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 }
Hanno Becker8031d062018-01-03 15:32:31 +00001387 else
1388#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1389 {
1390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1391 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1392 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001393
Hanno Becker88aaf652017-12-27 08:17:40 +00001394 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1395 (unsigned) keylen,
1396 (unsigned) transform->minlen,
1397 (unsigned) transform->ivlen,
1398 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
1400 /*
1401 * Finally setup the cipher contexts, IVs and MAC secrets.
1402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001404 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001406 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001407 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001408
Paul Bakker68884e32013-01-07 18:20:04 +01001409 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001410 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001411
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001412 /*
1413 * This is not used in TLS v1.1.
1414 */
Paul Bakker48916f92012-09-16 19:57:18 +00001415 iv_copy_len = ( transform->fixed_ivlen ) ?
1416 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001417 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1418 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001419 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001420 }
1421 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#endif /* MBEDTLS_SSL_CLI_C */
1423#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001424 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001425 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001426 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001427 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001428
Hanno Becker81c7b182017-11-09 18:39:33 +00001429 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001430 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001431
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001432 /*
1433 * This is not used in TLS v1.1.
1434 */
Paul Bakker48916f92012-09-16 19:57:18 +00001435 iv_copy_len = ( transform->fixed_ivlen ) ?
1436 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001437 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1438 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001439 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001441 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001445 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1446 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001447 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001448
Hanno Beckerd56ed242018-01-03 15:32:51 +00001449#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#if defined(MBEDTLS_SSL_PROTO_SSL3)
1451 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001452 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001453 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001456 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1457 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001458 }
1459
Hanno Becker81c7b182017-11-09 18:39:33 +00001460 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1461 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001462 }
1463 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1465#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1466 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1467 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001468 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001469 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1470 For AEAD-based ciphersuites, there is nothing to do here. */
1471 if( mac_key_len != 0 )
1472 {
1473 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1474 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1475 }
Paul Bakker68884e32013-01-07 18:20:04 +01001476 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001477 else
1478#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001481 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1482 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001483 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001484#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1487 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001488 {
1489 int ret = 0;
1490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001492
Hanno Becker88aaf652017-12-27 08:17:40 +00001493 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001494 transform->iv_enc, transform->iv_dec,
1495 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001496 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001497 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001500 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1501 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001502 }
1503 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001504#else
1505 ((void) mac_dec);
1506 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001508
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001509#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1510 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001511 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001512 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1513 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001514 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001515 iv_copy_len );
1516 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001517
1518 if( ssl->conf->f_export_keys_ext != NULL )
1519 {
1520 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1521 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001522 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001523 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001524 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001525 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001526 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001527 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001528#endif
1529
Hanno Beckerf704bef2018-11-16 15:21:18 +00001530#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001531
1532 /* Only use PSA-based ciphers for TLS-1.2.
1533 * That's relevant at least for TLS-1.0, where
1534 * we assume that mbedtls_cipher_crypt() updates
1535 * the structure field for the IV, which the PSA-based
1536 * implementation currently doesn't. */
1537#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1538 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001539 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001540 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001541 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001542 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1543 {
1544 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001545 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001546 }
1547
1548 if( ret == 0 )
1549 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001551 psa_fallthrough = 0;
1552 }
1553 else
1554 {
1555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1556 psa_fallthrough = 1;
1557 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001558 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001559 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001560 psa_fallthrough = 1;
1561#else
1562 psa_fallthrough = 1;
1563#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001564
Hanno Beckercb1cc802018-11-17 22:27:38 +00001565 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001566#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001567 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001568 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001569 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001570 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001571 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001572 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001573
Hanno Beckerf704bef2018-11-16 15:21:18 +00001574#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001575 /* Only use PSA-based ciphers for TLS-1.2.
1576 * That's relevant at least for TLS-1.0, where
1577 * we assume that mbedtls_cipher_crypt() updates
1578 * the structure field for the IV, which the PSA-based
1579 * implementation currently doesn't. */
1580#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1581 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001582 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001583 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001584 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001585 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1586 {
1587 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001588 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001589 }
1590
1591 if( ret == 0 )
1592 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001593 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001594 psa_fallthrough = 0;
1595 }
1596 else
1597 {
1598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1599 psa_fallthrough = 1;
1600 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001601 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001602 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001603 psa_fallthrough = 1;
1604#else
1605 psa_fallthrough = 1;
1606#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001607
Hanno Beckercb1cc802018-11-17 22:27:38 +00001608 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001609#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001610 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001611 cipher_info ) ) != 0 )
1612 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001613 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001614 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001615 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001618 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001622 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001623 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001626 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001630 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001631 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#if defined(MBEDTLS_CIPHER_MODE_CBC)
1634 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1637 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001640 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001641 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1644 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001647 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001648 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001649 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001651
Paul Bakker5121ce52009-01-03 21:22:43 +00001652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001654 // Initialize compression
1655 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001657 {
Paul Bakker16770332013-10-11 09:59:44 +02001658 if( ssl->compress_buf == NULL )
1659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001661 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001662 if( ssl->compress_buf == NULL )
1663 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001665 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001666 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1667 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001668 }
1669 }
1670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001672
Paul Bakker48916f92012-09-16 19:57:18 +00001673 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1674 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001675
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001676 if( deflateInit( &transform->ctx_deflate,
1677 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001678 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001681 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1682 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001683 }
1684 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001688end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001689 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1690 mbedtls_platform_zeroize( handshake->randbytes,
1691 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001692 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001693}
1694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695#if defined(MBEDTLS_SSL_PROTO_SSL3)
1696void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001697{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001698 mbedtls_md5_context md5;
1699 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001700 unsigned char pad_1[48];
1701 unsigned char pad_2[48];
1702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001704
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001705 mbedtls_md5_init( &md5 );
1706 mbedtls_sha1_init( &sha1 );
1707
1708 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1709 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001710
Paul Bakker380da532012-04-18 16:10:25 +00001711 memset( pad_1, 0x36, 48 );
1712 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001713
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001714 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1715 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1716 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001717
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001718 mbedtls_md5_starts_ret( &md5 );
1719 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1720 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1721 mbedtls_md5_update_ret( &md5, hash, 16 );
1722 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001723
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001724 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1725 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1726 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001727
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001728 mbedtls_sha1_starts_ret( &sha1 );
1729 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1730 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1731 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1732 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001736
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001737 mbedtls_md5_free( &md5 );
1738 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001739
Paul Bakker380da532012-04-18 16:10:25 +00001740 return;
1741}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1745void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001746{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001747 mbedtls_md5_context md5;
1748 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001751
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001752 mbedtls_md5_init( &md5 );
1753 mbedtls_sha1_init( &sha1 );
1754
1755 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1756 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001757
Andrzej Kurekeb342242019-01-29 09:14:33 -05001758 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001759 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001763
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001764 mbedtls_md5_free( &md5 );
1765 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001766
Paul Bakker380da532012-04-18 16:10:25 +00001767 return;
1768}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1772#if defined(MBEDTLS_SHA256_C)
1773void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001774{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001775#if defined(MBEDTLS_USE_PSA_CRYPTO)
1776 size_t hash_size;
1777 psa_status_t status;
1778 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1779
1780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1781 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1782 if( status != PSA_SUCCESS )
1783 {
1784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1785 return;
1786 }
1787
1788 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1789 if( status != PSA_SUCCESS )
1790 {
1791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1792 return;
1793 }
1794 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1796#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001797 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001798
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001799 mbedtls_sha256_init( &sha256 );
1800
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001802
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001803 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001804 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001808
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001809 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001810#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001811 return;
1812}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815#if defined(MBEDTLS_SHA512_C)
1816void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001817{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001818#if defined(MBEDTLS_USE_PSA_CRYPTO)
1819 size_t hash_size;
1820 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001821 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001822
1823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001824 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001825 if( status != PSA_SUCCESS )
1826 {
1827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1828 return;
1829 }
1830
Andrzej Kurek972fba52019-01-30 03:29:12 -05001831 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001832 if( status != PSA_SUCCESS )
1833 {
1834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1835 return;
1836 }
1837 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1839#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001840 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001841
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001842 mbedtls_sha512_init( &sha512 );
1843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001845
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001846 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001847 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001851
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001852 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001853#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001854 return;
1855}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856#endif /* MBEDTLS_SHA512_C */
1857#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1860int 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 +02001861{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001862 unsigned char *p = ssl->handshake->premaster;
1863 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001864 const unsigned char *psk = ssl->conf->psk;
1865 size_t psk_len = ssl->conf->psk_len;
1866
1867 /* If the psk callback was called, use its result */
1868 if( ssl->handshake->psk != NULL )
1869 {
1870 psk = ssl->handshake->psk;
1871 psk_len = ssl->handshake->psk_len;
1872 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001873
1874 /*
1875 * PMS = struct {
1876 * opaque other_secret<0..2^16-1>;
1877 * opaque psk<0..2^16-1>;
1878 * };
1879 * with "other_secret" depending on the particular key exchange
1880 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1882 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001883 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001884 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001886
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001887 *(p++) = (unsigned char)( psk_len >> 8 );
1888 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001889
1890 if( end < p || (size_t)( end - p ) < psk_len )
1891 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1892
1893 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001894 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001895 }
1896 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1898#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1899 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001900 {
1901 /*
1902 * other_secret already set by the ClientKeyExchange message,
1903 * and is 48 bytes long
1904 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001905 if( end - p < 2 )
1906 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1907
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001908 *p++ = 0;
1909 *p++ = 48;
1910 p += 48;
1911 }
1912 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1914#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1915 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001916 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001917 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001918 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001919
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001920 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001922 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001923 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001926 return( ret );
1927 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001928 *(p++) = (unsigned char)( len >> 8 );
1929 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001930 p += len;
1931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001933 }
1934 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1936#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1937 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001938 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001939 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001940 size_t zlen;
1941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001943 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001944 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001947 return( ret );
1948 }
1949
1950 *(p++) = (unsigned char)( zlen >> 8 );
1951 *(p++) = (unsigned char)( zlen );
1952 p += zlen;
1953
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001954 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1955 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001956 }
1957 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1961 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001962 }
1963
1964 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001965 if( end - p < 2 )
1966 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001967
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001968 *(p++) = (unsigned char)( psk_len >> 8 );
1969 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001970
1971 if( end < p || (size_t)( end - p ) < psk_len )
1972 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1973
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001974 memcpy( p, psk, psk_len );
1975 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001976
1977 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1978
1979 return( 0 );
1980}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001984/*
1985 * SSLv3.0 MAC functions
1986 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001987#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001988static void ssl_mac( mbedtls_md_context_t *md_ctx,
1989 const unsigned char *secret,
1990 const unsigned char *buf, size_t len,
1991 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001992 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001993{
1994 unsigned char header[11];
1995 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001996 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1998 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001999
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002000 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002002 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002003 else
Paul Bakker68884e32013-01-07 18:20:04 +01002004 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002005
2006 memcpy( header, ctr, 8 );
2007 header[ 8] = (unsigned char) type;
2008 header[ 9] = (unsigned char)( len >> 8 );
2009 header[10] = (unsigned char)( len );
2010
Paul Bakker68884e32013-01-07 18:20:04 +01002011 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 mbedtls_md_starts( md_ctx );
2013 mbedtls_md_update( md_ctx, secret, md_size );
2014 mbedtls_md_update( md_ctx, padding, padlen );
2015 mbedtls_md_update( md_ctx, header, 11 );
2016 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002017 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002018
Paul Bakker68884e32013-01-07 18:20:04 +01002019 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 mbedtls_md_starts( md_ctx );
2021 mbedtls_md_update( md_ctx, secret, md_size );
2022 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002023 mbedtls_md_update( md_ctx, out, md_size );
2024 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002025}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002027
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002028/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002029 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002030#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002031 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2032 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2033 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2034/* This function makes sure every byte in the memory region is accessed
2035 * (in ascending addresses order) */
2036static void ssl_read_memory( unsigned char *p, size_t len )
2037{
2038 unsigned char acc = 0;
2039 volatile unsigned char force;
2040
2041 for( ; len != 0; p++, len-- )
2042 acc ^= *p;
2043
2044 force = acc;
2045 (void) force;
2046}
2047#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2048
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002049/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002050 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002051 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002052
Hanno Beckera0e20d02019-05-15 14:03:01 +01002053#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002054/* This functions transforms a DTLS plaintext fragment and a record content
2055 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002056 *
2057 * struct {
2058 * opaque content[DTLSPlaintext.length];
2059 * ContentType real_type;
2060 * uint8 zeros[length_of_padding];
2061 * } DTLSInnerPlaintext;
2062 *
2063 * Input:
2064 * - `content`: The beginning of the buffer holding the
2065 * plaintext to be wrapped.
2066 * - `*content_size`: The length of the plaintext in Bytes.
2067 * - `max_len`: The number of Bytes available starting from
2068 * `content`. This must be `>= *content_size`.
2069 * - `rec_type`: The desired record content type.
2070 *
2071 * Output:
2072 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2073 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2074 *
2075 * Returns:
2076 * - `0` on success.
2077 * - A negative error code if `max_len` didn't offer enough space
2078 * for the expansion.
2079 */
2080static int ssl_cid_build_inner_plaintext( unsigned char *content,
2081 size_t *content_size,
2082 size_t remaining,
2083 uint8_t rec_type )
2084{
2085 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002086 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2087 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2088 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002089
2090 /* Write real content type */
2091 if( remaining == 0 )
2092 return( -1 );
2093 content[ len ] = rec_type;
2094 len++;
2095 remaining--;
2096
2097 if( remaining < pad )
2098 return( -1 );
2099 memset( content + len, 0, pad );
2100 len += pad;
2101 remaining -= pad;
2102
2103 *content_size = len;
2104 return( 0 );
2105}
2106
Hanno Becker07dc97d2019-05-20 15:08:01 +01002107/* This function parses a DTLSInnerPlaintext structure.
2108 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002109static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2110 size_t *content_size,
2111 uint8_t *rec_type )
2112{
2113 size_t remaining = *content_size;
2114
2115 /* Determine length of padding by skipping zeroes from the back. */
2116 do
2117 {
2118 if( remaining == 0 )
2119 return( -1 );
2120 remaining--;
2121 } while( content[ remaining ] == 0 );
2122
2123 *content_size = remaining;
2124 *rec_type = content[ remaining ];
2125
2126 return( 0 );
2127}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002128#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002129
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002130/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002131 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002132static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002133 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002134 mbedtls_record *rec )
2135{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002136 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002137 *
2138 * additional_data = seq_num + TLSCompressed.type +
2139 * TLSCompressed.version + TLSCompressed.length;
2140 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002141 * For the CID extension, this is extended as follows
2142 * (quoting draft-ietf-tls-dtls-connection-id-05,
2143 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002144 *
2145 * additional_data = seq_num + DTLSPlaintext.type +
2146 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002147 * cid +
2148 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002149 * length_of_DTLSInnerPlaintext;
2150 */
2151
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002152 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2153 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002154 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002155
Hanno Beckera0e20d02019-05-15 14:03:01 +01002156#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002157 if( rec->cid_len != 0 )
2158 {
2159 memcpy( add_data + 11, rec->cid, rec->cid_len );
2160 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2161 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2162 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2163 *add_data_len = 13 + 1 + rec->cid_len;
2164 }
2165 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002166#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002167 {
2168 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2169 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2170 *add_data_len = 13;
2171 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002172}
2173
Hanno Beckera18d1322018-01-03 14:27:32 +00002174int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2175 mbedtls_ssl_transform *transform,
2176 mbedtls_record *rec,
2177 int (*f_rng)(void *, unsigned char *, size_t),
2178 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002179{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002181 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002182 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002183 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002184 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002185 size_t post_avail;
2186
2187 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002188#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002189 ((void) ssl);
2190#endif
2191
2192 /* The PRNG is used for dynamic IV generation that's used
2193 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2194#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2195 ( defined(MBEDTLS_AES_C) || \
2196 defined(MBEDTLS_ARIA_C) || \
2197 defined(MBEDTLS_CAMELLIA_C) ) && \
2198 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2199 ((void) f_rng);
2200 ((void) p_rng);
2201#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002204
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002205 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002206 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2208 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2209 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002210 if( rec == NULL
2211 || rec->buf == NULL
2212 || rec->buf_len < rec->data_offset
2213 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002214#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002215 || rec->cid_len != 0
2216#endif
2217 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002218 {
2219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002221 }
2222
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002223 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002224 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002226 data, rec->data_len );
2227
2228 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2229
2230 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2231 {
2232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2233 (unsigned) rec->data_len,
2234 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2235 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2236 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002237
Hanno Beckera0e20d02019-05-15 14:03:01 +01002238#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002239 /*
2240 * Add CID information
2241 */
2242 rec->cid_len = transform->out_cid_len;
2243 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2244 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002245
2246 if( rec->cid_len != 0 )
2247 {
2248 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002249 * Wrap plaintext into DTLSInnerPlaintext structure.
2250 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002251 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002252 * Note that this changes `rec->data_len`, and hence
2253 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002254 */
2255 if( ssl_cid_build_inner_plaintext( data,
2256 &rec->data_len,
2257 post_avail,
2258 rec->type ) != 0 )
2259 {
2260 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2261 }
2262
2263 rec->type = MBEDTLS_SSL_MSG_CID;
2264 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002265#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002266
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002267 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2268
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002270 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 */
Hanno Becker52344c22018-01-03 15:24:20 +00002272#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 if( mode == MBEDTLS_MODE_STREAM ||
2274 ( mode == MBEDTLS_MODE_CBC
2275#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002276 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002277#endif
2278 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002279 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002280 if( post_avail < transform->maclen )
2281 {
2282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2283 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2284 }
2285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002287 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002288 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002289 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002290 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2291 data, rec->data_len, rec->ctr, rec->type, mac );
2292 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002293 }
2294 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002295#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2297 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002298 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002299 {
Hanno Becker992b6872017-11-09 18:57:39 +00002300 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2301
Hanno Beckercab87e62019-04-29 13:52:53 +01002302 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002303
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002304 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002305 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002306 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2307 data, rec->data_len );
2308 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2309 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2310
2311 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002312 }
2313 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002314#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2317 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002318 }
2319
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002320 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2321 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002322
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002323 rec->data_len += transform->maclen;
2324 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002325 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002326 }
Hanno Becker52344c22018-01-03 15:24:20 +00002327#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002328
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002329 /*
2330 * Encrypt
2331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2333 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002334 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002335 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002336 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002338 "including %d bytes of padding",
2339 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002340
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002341 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2342 transform->iv_enc, transform->ivlen,
2343 data, rec->data_len,
2344 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002347 return( ret );
2348 }
2349
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002350 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2353 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002354 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002355 }
Paul Bakker68884e32013-01-07 18:20:04 +01002356 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002358
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002359#if defined(MBEDTLS_GCM_C) || \
2360 defined(MBEDTLS_CCM_C) || \
2361 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002363 mode == MBEDTLS_MODE_CCM ||
2364 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002365 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002366 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002367 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002368 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002369
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002370 /* Check that there's space for both the authentication tag
2371 * and the explicit IV before and after the record content. */
2372 if( post_avail < transform->taglen ||
2373 rec->data_offset < explicit_iv_len )
2374 {
2375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2376 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2377 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002378
Paul Bakker68884e32013-01-07 18:20:04 +01002379 /*
2380 * Generate IV
2381 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002382 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2383 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002384 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002385 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002386 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2387 explicit_iv_len );
2388 /* Prefix record content with explicit IV. */
2389 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002390 }
2391 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2392 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002393 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002394 unsigned char i;
2395
2396 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2397
2398 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002399 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002400 }
2401 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002402 {
2403 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2405 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002406 }
2407
Hanno Beckercab87e62019-04-29 13:52:53 +01002408 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002409
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002410 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2411 iv, transform->ivlen );
2412 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002413 data - explicit_iv_len, explicit_iv_len );
2414 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002415 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002417 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002418 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002419
Paul Bakker68884e32013-01-07 18:20:04 +01002420 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002421 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002422 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002423
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002424 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002425 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002426 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002427 data, rec->data_len, /* source */
2428 data, &rec->data_len, /* destination */
2429 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002432 return( ret );
2433 }
2434
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002435 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2436 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002437
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002438 rec->data_len += transform->taglen + explicit_iv_len;
2439 rec->data_offset -= explicit_iv_len;
2440 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002441 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002442 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002443 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2445#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002446 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002448 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002449 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002450 size_t padlen, i;
2451 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002452
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002453 /* Currently we're always using minimal padding
2454 * (up to 255 bytes would be allowed). */
2455 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2456 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 padlen = 0;
2458
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002459 /* Check there's enough space in the buffer for the padding. */
2460 if( post_avail < padlen + 1 )
2461 {
2462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2463 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2464 }
2465
Paul Bakker5121ce52009-01-03 21:22:43 +00002466 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002467 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002469 rec->data_len += padlen + 1;
2470 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002473 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002474 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2475 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002476 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002477 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002478 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002479 if( f_rng == NULL )
2480 {
2481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2482 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2483 }
2484
2485 if( rec->data_offset < transform->ivlen )
2486 {
2487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2488 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2489 }
2490
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002491 /*
2492 * Generate IV
2493 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002494 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002495 if( ret != 0 )
2496 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002497
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002498 memcpy( data - transform->ivlen, transform->iv_enc,
2499 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002500
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002501 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002505 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002506 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002507 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002508
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002509 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2510 transform->iv_enc,
2511 transform->ivlen,
2512 data, rec->data_len,
2513 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002516 return( ret );
2517 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002518
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002519 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2522 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002523 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002526 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002527 {
2528 /*
2529 * Save IV in SSL3 and TLS1
2530 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002531 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2532 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002533 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002534 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002535#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002536 {
2537 data -= transform->ivlen;
2538 rec->data_offset -= transform->ivlen;
2539 rec->data_len += transform->ivlen;
2540 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002543 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002544 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002545 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2546
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002547 /*
2548 * MAC(MAC_write_key, seq_num +
2549 * TLSCipherText.type +
2550 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002551 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002552 * IV + // except for TLS 1.0
2553 * ENC(content + padding + padding_length));
2554 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002555
2556 if( post_avail < transform->maclen)
2557 {
2558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2559 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2560 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002561
Hanno Beckercab87e62019-04-29 13:52:53 +01002562 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002565 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002566 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002567
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002568 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002569 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002570 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2571 data, rec->data_len );
2572 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2573 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002574
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002575 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002576
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002577 rec->data_len += transform->maclen;
2578 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002579 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002580 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002582 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002583 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002585 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002589 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002590
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002591 /* Make extra sure authentication was performed, exactly once */
2592 if( auth_done != 1 )
2593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2595 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002596 }
2597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002599
2600 return( 0 );
2601}
2602
Hanno Becker605949f2019-07-12 08:23:59 +01002603int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002604 mbedtls_ssl_transform *transform,
2605 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002606{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002607 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002609 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002610#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002611 size_t padlen = 0, correct = 1;
2612#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002613 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002614 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002615 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002616
Hanno Beckera18d1322018-01-03 14:27:32 +00002617#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002618 ((void) ssl);
2619#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002622 if( rec == NULL ||
2623 rec->buf == NULL ||
2624 rec->buf_len < rec->data_offset ||
2625 rec->buf_len - rec->data_offset < rec->data_len )
2626 {
2627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002629 }
2630
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002631 data = rec->buf + rec->data_offset;
2632 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002633
Hanno Beckera0e20d02019-05-15 14:03:01 +01002634#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002635 /*
2636 * Match record's CID with incoming CID.
2637 */
Hanno Becker938489a2019-05-08 13:02:22 +01002638 if( rec->cid_len != transform->in_cid_len ||
2639 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2640 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002641 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002642 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002643#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2646 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002647 {
2648 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002649 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2650 transform->iv_dec,
2651 transform->ivlen,
2652 data, rec->data_len,
2653 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002656 return( ret );
2657 }
2658
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002659 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2662 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002663 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 }
Paul Bakker68884e32013-01-07 18:20:04 +01002665 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002667#if defined(MBEDTLS_GCM_C) || \
2668 defined(MBEDTLS_CCM_C) || \
2669 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002671 mode == MBEDTLS_MODE_CCM ||
2672 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002673 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002674 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002675 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002676
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002677 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002678 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002679 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002680
2681 /* Check that there's enough space for the explicit IV
2682 * (at the beginning of the record) and the MAC (at the
2683 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002684 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002687 "+ taglen (%d)", rec->data_len,
2688 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002690 }
Paul Bakker68884e32013-01-07 18:20:04 +01002691
Hanno Beckerd96a6522019-07-10 13:55:25 +01002692#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002693 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2694 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002695 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002696
Hanno Beckerd96a6522019-07-10 13:55:25 +01002697 /* Fixed */
2698 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2699 /* Explicit */
2700 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002701 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002702 else
2703#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2704#if defined(MBEDTLS_CHACHAPOLY_C)
2705 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002706 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002707 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002708 unsigned char i;
2709
2710 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2711
2712 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002713 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002714 }
2715 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002716#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002717 {
2718 /* Reminder if we ever add an AEAD mode with a different size */
2719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2720 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2721 }
2722
Hanno Beckerd96a6522019-07-10 13:55:25 +01002723 /* Group changes to data, data_len, and add_data, because
2724 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002725 data += explicit_iv_len;
2726 rec->data_offset += explicit_iv_len;
2727 rec->data_len -= explicit_iv_len + transform->taglen;
2728
Hanno Beckercab87e62019-04-29 13:52:53 +01002729 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002730 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002731 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002732
Hanno Beckerd96a6522019-07-10 13:55:25 +01002733 /* Because of the check above, we know that there are
2734 * explicit_iv_len Bytes preceeding data, and taglen
2735 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002736 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002737 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002738
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002739 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002740 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002741 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002742
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002743 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002744 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002745 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002746 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2747 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002748 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002749 data, rec->data_len,
2750 data, &olen,
2751 data + rec->data_len,
2752 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2757 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002758
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002759 return( ret );
2760 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002761 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002762
Hanno Beckerd96a6522019-07-10 13:55:25 +01002763 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002764 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2767 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002768 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002769 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002770 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2772#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002773 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002775 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002776 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002777
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 /*
Paul Bakker45829992013-01-03 14:52:21 +01002779 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002782 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2783 {
2784 /* The ciphertext is prefixed with the CBC IV. */
2785 minlen += transform->ivlen;
2786 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002787#endif
Paul Bakker45829992013-01-03 14:52:21 +01002788
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002789 /* Size considerations:
2790 *
2791 * - The CBC cipher text must not be empty and hence
2792 * at least of size transform->ivlen.
2793 *
2794 * Together with the potential IV-prefix, this explains
2795 * the first of the two checks below.
2796 *
2797 * - The record must contain a MAC, either in plain or
2798 * encrypted, depending on whether Encrypt-then-MAC
2799 * is used or not.
2800 * - If it is, the message contains the IV-prefix,
2801 * the CBC ciphertext, and the MAC.
2802 * - If it is not, the padded plaintext, and hence
2803 * the CBC ciphertext, has at least length maclen + 1
2804 * because there is at least the padding length byte.
2805 *
2806 * As the CBC ciphertext is not empty, both cases give the
2807 * lower bound minlen + maclen + 1 on the record size, which
2808 * we test for in the second check below.
2809 */
2810 if( rec->data_len < minlen + transform->ivlen ||
2811 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002814 "+ 1 ) ( + expl IV )", rec->data_len,
2815 transform->ivlen,
2816 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002818 }
2819
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002820 /*
2821 * Authenticate before decrypt if enabled
2822 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002824 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002825 {
Hanno Becker992b6872017-11-09 18:57:39 +00002826 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002829
Hanno Beckerd96a6522019-07-10 13:55:25 +01002830 /* Update data_len in tandem with add_data.
2831 *
2832 * The subtraction is safe because of the previous check
2833 * data_len >= minlen + maclen + 1.
2834 *
2835 * Afterwards, we know that data + data_len is followed by at
2836 * least maclen Bytes, which justifies the call to
2837 * mbedtls_ssl_safer_memcmp() below.
2838 *
2839 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002840 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002841 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002842
Hanno Beckerd96a6522019-07-10 13:55:25 +01002843 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002844 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2845 add_data_len );
2846 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2847 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002848 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2849 data, rec->data_len );
2850 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2851 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002852
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002853 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2854 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002855 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002856 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002857
Hanno Beckerd96a6522019-07-10 13:55:25 +01002858 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002859 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2860 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002864 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002865 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002866 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002868
2869 /*
2870 * Check length sanity
2871 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002872
2873 /* We know from above that data_len > minlen >= 0,
2874 * so the following check in particular implies that
2875 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002876 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002879 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002881 }
2882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002884 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002885 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002886 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002887 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002888 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002889 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002890 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002891
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002892 data += transform->ivlen;
2893 rec->data_offset += transform->ivlen;
2894 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002895 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002897
Hanno Beckerd96a6522019-07-10 13:55:25 +01002898 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
2899
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002900 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2901 transform->iv_dec, transform->ivlen,
2902 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002905 return( ret );
2906 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002907
Hanno Beckerd96a6522019-07-10 13:55:25 +01002908 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002909 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2912 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002913 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002916 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002917 {
2918 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002919 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
2920 * records is equivalent to CBC decryption of the concatenation
2921 * of the records; in other words, IVs are maintained across
2922 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02002923 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002924 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2925 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002926 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002927#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002928
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002929 /* Safe since data_len >= minlen + maclen + 1, so after having
2930 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01002931 * data_len > 0 (because of data_len % ivlen == 0, it's actually
2932 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002933 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002934
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002935 if( auth_done == 1 )
2936 {
2937 correct *= ( rec->data_len >= padlen + 1 );
2938 padlen *= ( rec->data_len >= padlen + 1 );
2939 }
2940 else
Paul Bakker45829992013-01-03 14:52:21 +01002941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002943 if( rec->data_len < transform->maclen + padlen + 1 )
2944 {
2945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2946 rec->data_len,
2947 transform->maclen,
2948 padlen + 1 ) );
2949 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002950#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002951
2952 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2953 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002954 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002955
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002956 padlen++;
2957
2958 /* Regardless of the validity of the padding,
2959 * we have data_len >= padlen here. */
2960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002962 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002963 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002964 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966#if defined(MBEDTLS_SSL_DEBUG_ALL)
2967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002968 "should be no more than %d",
2969 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002970#endif
Paul Bakker45829992013-01-03 14:52:21 +01002971 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002972 }
2973 }
2974 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2976#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2977 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002978 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002979 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002980 /* The padding check involves a series of up to 256
2981 * consecutive memory reads at the end of the record
2982 * plaintext buffer. In order to hide the length and
2983 * validity of the padding, always perform exactly
2984 * `min(256,plaintext_len)` reads (but take into account
2985 * only the last `padlen` bytes for the padding check). */
2986 size_t pad_count = 0;
2987 size_t real_count = 0;
2988 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002989
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002990 /* Index of first padding byte; it has been ensured above
2991 * that the subtraction is safe. */
2992 size_t const padding_idx = rec->data_len - padlen;
2993 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2994 size_t const start_idx = rec->data_len - num_checks;
2995 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002996
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002997 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002998 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002999 real_count |= ( idx >= padding_idx );
3000 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003001 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003002 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003005 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003007#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003008 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003009 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003010 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3012 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3015 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003016 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003017
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003018 /* If the padding was found to be invalid, padlen == 0
3019 * and the subtraction is safe. If the padding was found valid,
3020 * padlen hasn't been changed and the previous assertion
3021 * data_len >= padlen still holds. */
3022 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003023 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003024 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003026 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3029 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003030 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003031
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003032#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003033 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003034 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003035#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003036
3037 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003038 * Authenticate if not done yet.
3039 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003040 */
Hanno Becker52344c22018-01-03 15:24:20 +00003041#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003042 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003043 {
Hanno Becker992b6872017-11-09 18:57:39 +00003044 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003045
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003046 /* If the initial value of padlen was such that
3047 * data_len < maclen + padlen + 1, then padlen
3048 * got reset to 1, and the initial check
3049 * data_len >= minlen + maclen + 1
3050 * guarantees that at this point we still
3051 * have at least data_len >= maclen.
3052 *
3053 * If the initial value of padlen was such that
3054 * data_len >= maclen + padlen + 1, then we have
3055 * subtracted either padlen + 1 (if the padding was correct)
3056 * or 0 (if the padding was incorrect) since then,
3057 * hence data_len >= maclen in any case.
3058 */
3059 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003060 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003063 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003064 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003065 ssl_mac( &transform->md_ctx_dec,
3066 transform->mac_dec,
3067 data, rec->data_len,
3068 rec->ctr, rec->type,
3069 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003070 }
3071 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3073#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3074 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003075 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003076 {
3077 /*
3078 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003079 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003080 *
3081 * Known timing attacks:
3082 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3083 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003084 * To compensate for different timings for the MAC calculation
3085 * depending on how much padding was removed (which is determined
3086 * by padlen), process extra_run more blocks through the hash
3087 * function.
3088 *
3089 * The formula in the paper is
3090 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3091 * where L1 is the size of the header plus the decrypted message
3092 * plus CBC padding and L2 is the size of the header plus the
3093 * decrypted message. This is for an underlying hash function
3094 * with 64-byte blocks.
3095 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3096 * correctly. We round down instead of up, so -56 is the correct
3097 * value for our calculations instead of -55.
3098 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003099 * Repeat the formula rather than defining a block_size variable.
3100 * This avoids requiring division by a variable at runtime
3101 * (which would be marginally less efficient and would require
3102 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003103 */
3104 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003105 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003106
3107 /*
3108 * The next two sizes are the minimum and maximum values of
3109 * in_msglen over all padlen values.
3110 *
3111 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003112 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003113 *
3114 * Note that max_len + maclen is never more than the buffer
3115 * length, as we previously did in_msglen -= maclen too.
3116 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003117 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003118 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3119
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003120 memset( tmp, 0, sizeof( tmp ) );
3121
3122 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003123 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003124#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3125 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003126 case MBEDTLS_MD_MD5:
3127 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003128 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003129 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003130 extra_run =
3131 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3132 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003133 break;
3134#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003135#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003136 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003137 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003138 extra_run =
3139 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3140 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003141 break;
3142#endif
3143 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003145 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3146 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003147
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003148 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003149
Hanno Beckercab87e62019-04-29 13:52:53 +01003150 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3151 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003152 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3153 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003154 /* Make sure we access everything even when padlen > 0. This
3155 * makes the synchronisation requirements for just-in-time
3156 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003157 ssl_read_memory( data + rec->data_len, padlen );
3158 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003159
3160 /* Call mbedtls_md_process at least once due to cache attacks
3161 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003162 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003163 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003164
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003165 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003166
3167 /* Make sure we access all the memory that could contain the MAC,
3168 * before we check it in the next code block. This makes the
3169 * synchronisation requirements for just-in-time Prime+Probe
3170 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003171 ssl_read_memory( data + min_len,
3172 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003173 }
3174 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3176 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3179 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003180 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003181
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003182#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003183 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3184 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003185#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003186
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003187 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3188 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190#if defined(MBEDTLS_SSL_DEBUG_ALL)
3191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003192#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003193 correct = 0;
3194 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003195 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003196 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003197
3198 /*
3199 * Finally check the correct flag
3200 */
3201 if( correct == 0 )
3202 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003203#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003204
3205 /* Make extra sure authentication was performed, exactly once */
3206 if( auth_done != 1 )
3207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3209 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003210 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003211
Hanno Beckera0e20d02019-05-15 14:03:01 +01003212#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003213 if( rec->cid_len != 0 )
3214 {
3215 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3216 &rec->type );
3217 if( ret != 0 )
3218 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3219 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003220#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003223
3224 return( 0 );
3225}
3226
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003227#undef MAC_NONE
3228#undef MAC_PLAINTEXT
3229#undef MAC_CIPHERTEXT
3230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003232/*
3233 * Compression/decompression functions
3234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003236{
3237 int ret;
3238 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003239 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003240 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003241 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003244
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003245 if( len_pre == 0 )
3246 return( 0 );
3247
Paul Bakker2770fbd2012-07-03 13:30:23 +00003248 memcpy( msg_pre, ssl->out_msg, len_pre );
3249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003251 ssl->out_msglen ) );
3252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003254 ssl->out_msg, ssl->out_msglen );
3255
Paul Bakker48916f92012-09-16 19:57:18 +00003256 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3257 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3258 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003259 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003260
Paul Bakker48916f92012-09-16 19:57:18 +00003261 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003262 if( ret != Z_OK )
3263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3265 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003266 }
3267
Angus Grattond8213d02016-05-25 20:56:48 +10003268 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003269 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003272 ssl->out_msglen ) );
3273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003274 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003275 ssl->out_msg, ssl->out_msglen );
3276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003278
3279 return( 0 );
3280}
3281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003282static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003283{
3284 int ret;
3285 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003286 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003287 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003288 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003291
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003292 if( len_pre == 0 )
3293 return( 0 );
3294
Paul Bakker2770fbd2012-07-03 13:30:23 +00003295 memcpy( msg_pre, ssl->in_msg, len_pre );
3296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003298 ssl->in_msglen ) );
3299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003301 ssl->in_msg, ssl->in_msglen );
3302
Paul Bakker48916f92012-09-16 19:57:18 +00003303 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3304 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3305 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003306 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003307 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003308
Paul Bakker48916f92012-09-16 19:57:18 +00003309 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003310 if( ret != Z_OK )
3311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3313 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003314 }
3315
Angus Grattond8213d02016-05-25 20:56:48 +10003316 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003317 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003320 ssl->in_msglen ) );
3321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003323 ssl->in_msg, ssl->in_msglen );
3324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003326
3327 return( 0 );
3328}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3332static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334#if defined(MBEDTLS_SSL_PROTO_DTLS)
3335static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003336{
3337 /* If renegotiation is not enforced, retransmit until we would reach max
3338 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003339 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003340 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003341 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003342 unsigned char doublings = 1;
3343
3344 while( ratio != 0 )
3345 {
3346 ++doublings;
3347 ratio >>= 1;
3348 }
3349
3350 if( ++ssl->renego_records_seen > doublings )
3351 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003353 return( 0 );
3354 }
3355 }
3356
3357 return( ssl_write_hello_request( ssl ) );
3358}
3359#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003361
Paul Bakker5121ce52009-01-03 21:22:43 +00003362/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003363 * Fill the input message buffer by appending data to it.
3364 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003365 *
3366 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3367 * available (from this read and/or a previous one). Otherwise, an error code
3368 * is returned (possibly EOF or WANT_READ).
3369 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003370 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3371 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3372 * since we always read a whole datagram at once.
3373 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003374 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003375 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003378{
Paul Bakker23986e52011-04-24 08:57:21 +00003379 int ret;
3380 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003383
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003384 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003387 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003389 }
3390
Angus Grattond8213d02016-05-25 20:56:48 +10003391 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3394 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003395 }
3396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003398 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003399 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003400 uint32_t timeout;
3401
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003402 /* Just to be sure */
3403 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3404 {
3405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3406 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3407 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3408 }
3409
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003410 /*
3411 * The point is, we need to always read a full datagram at once, so we
3412 * sometimes read more then requested, and handle the additional data.
3413 * It could be the rest of the current record (while fetching the
3414 * header) and/or some other records in the same datagram.
3415 */
3416
3417 /*
3418 * Move to the next record in the already read datagram if applicable
3419 */
3420 if( ssl->next_record_offset != 0 )
3421 {
3422 if( ssl->in_left < ssl->next_record_offset )
3423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003426 }
3427
3428 ssl->in_left -= ssl->next_record_offset;
3429
3430 if( ssl->in_left != 0 )
3431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003433 ssl->next_record_offset ) );
3434 memmove( ssl->in_hdr,
3435 ssl->in_hdr + ssl->next_record_offset,
3436 ssl->in_left );
3437 }
3438
3439 ssl->next_record_offset = 0;
3440 }
3441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003443 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003444
3445 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003446 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003447 */
3448 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003451 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003452 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003453
3454 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003455 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003456 * are not at the beginning of a new record, the caller did something
3457 * wrong.
3458 */
3459 if( ssl->in_left != 0 )
3460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3462 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003463 }
3464
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003465 /*
3466 * Don't even try to read if time's out already.
3467 * This avoids by-passing the timer when repeatedly receiving messages
3468 * that will end up being dropped.
3469 */
3470 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003471 {
3472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003473 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003474 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003475 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003476 {
Angus Grattond8213d02016-05-25 20:56:48 +10003477 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003480 timeout = ssl->handshake->retransmit_timeout;
3481 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003482 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003485
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003486 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003487 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3488 timeout );
3489 else
3490 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003493
3494 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003496 }
3497
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003498 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003501 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003504 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003505 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003508 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003509 }
3510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003514 return( ret );
3515 }
3516
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003517 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003518 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003520 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003522 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003523 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003525 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003526 return( ret );
3527 }
3528
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003529 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003530 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003532 }
3533
Paul Bakker5121ce52009-01-03 21:22:43 +00003534 if( ret < 0 )
3535 return( ret );
3536
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003537 ssl->in_left = ret;
3538 }
3539 else
3540#endif
3541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003543 ssl->in_left, nb_want ) );
3544
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003545 while( ssl->in_left < nb_want )
3546 {
3547 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003548
3549 if( ssl_check_timer( ssl ) != 0 )
3550 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3551 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003552 {
3553 if( ssl->f_recv_timeout != NULL )
3554 {
3555 ret = ssl->f_recv_timeout( ssl->p_bio,
3556 ssl->in_hdr + ssl->in_left, len,
3557 ssl->conf->read_timeout );
3558 }
3559 else
3560 {
3561 ret = ssl->f_recv( ssl->p_bio,
3562 ssl->in_hdr + ssl->in_left, len );
3563 }
3564 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003567 ssl->in_left, nb_want ) );
3568 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003569
3570 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003572
3573 if( ret < 0 )
3574 return( ret );
3575
mohammad160352aecb92018-03-28 23:41:40 -07003576 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003577 {
Darryl Green11999bb2018-03-13 15:22:58 +00003578 MBEDTLS_SSL_DEBUG_MSG( 1,
3579 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003580 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003581 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3582 }
3583
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003584 ssl->in_left += ret;
3585 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003586 }
3587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003589
3590 return( 0 );
3591}
3592
3593/*
3594 * Flush any data not yet written
3595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003597{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003598 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003599 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003602
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003603 if( ssl->f_send == NULL )
3604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003606 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003608 }
3609
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003610 /* Avoid incrementing counter if data is flushed */
3611 if( ssl->out_left == 0 )
3612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003614 return( 0 );
3615 }
3616
Paul Bakker5121ce52009-01-03 21:22:43 +00003617 while( ssl->out_left > 0 )
3618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003620 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003621
Hanno Becker2b1e3542018-08-06 11:19:13 +01003622 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003623 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003626
3627 if( ret <= 0 )
3628 return( ret );
3629
mohammad160352aecb92018-03-28 23:41:40 -07003630 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003631 {
Darryl Green11999bb2018-03-13 15:22:58 +00003632 MBEDTLS_SSL_DEBUG_MSG( 1,
3633 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003634 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003635 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3636 }
3637
Paul Bakker5121ce52009-01-03 21:22:43 +00003638 ssl->out_left -= ret;
3639 }
3640
Hanno Becker2b1e3542018-08-06 11:19:13 +01003641#if defined(MBEDTLS_SSL_PROTO_DTLS)
3642 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003643 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003644 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003645 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003646 else
3647#endif
3648 {
3649 ssl->out_hdr = ssl->out_buf + 8;
3650 }
3651 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003654
3655 return( 0 );
3656}
3657
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003658/*
3659 * Functions to handle the DTLS retransmission state machine
3660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003662/*
3663 * Append current handshake message to current outgoing flight
3664 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003666{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3669 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3670 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003671
3672 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003673 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003674 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003676 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003677 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003678 }
3679
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003680 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003681 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003684 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003685 }
3686
3687 /* Copy current handshake message with headers */
3688 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3689 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003690 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003691 msg->next = NULL;
3692
3693 /* Append to the current flight */
3694 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003695 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003696 else
3697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003699 while( cur->next != NULL )
3700 cur = cur->next;
3701 cur->next = msg;
3702 }
3703
Hanno Becker3b235902018-08-06 09:54:53 +01003704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003705 return( 0 );
3706}
3707
3708/*
3709 * Free the current flight of handshake messages
3710 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003712{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713 mbedtls_ssl_flight_item *cur = flight;
3714 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003715
3716 while( cur != NULL )
3717 {
3718 next = cur->next;
3719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720 mbedtls_free( cur->p );
3721 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003722
3723 cur = next;
3724 }
3725}
3726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3728static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003729#endif
3730
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003731/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003732 * Swap transform_out and out_ctr with the alternative ones
3733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003735{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003736 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003737 unsigned char tmp_out_ctr[8];
3738
3739 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003742 return;
3743 }
3744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003745 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003746
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003747 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003748 tmp_transform = ssl->transform_out;
3749 ssl->transform_out = ssl->handshake->alt_transform_out;
3750 ssl->handshake->alt_transform_out = tmp_transform;
3751
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003752 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003753 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3754 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003755 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003756
3757 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003758 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3761 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003763 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3766 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003767 }
3768 }
3769#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003770}
3771
3772/*
3773 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003774 */
3775int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3776{
3777 int ret = 0;
3778
3779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3780
3781 ret = mbedtls_ssl_flight_transmit( ssl );
3782
3783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3784
3785 return( ret );
3786}
3787
3788/*
3789 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003790 *
3791 * Need to remember the current message in case flush_output returns
3792 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003793 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003794 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003795int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003796{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003797 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003800 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003801 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003803
3804 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003805 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003806 ssl_swap_epochs( ssl );
3807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003808 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003809 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003810
3811 while( ssl->handshake->cur_msg != NULL )
3812 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003813 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003814 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003815
Hanno Beckere1dcb032018-08-17 16:47:58 +01003816 int const is_finished =
3817 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3818 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3819
Hanno Becker04da1892018-08-14 13:22:10 +01003820 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3821 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3822
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003823 /* Swap epochs before sending Finished: we can't do it after
3824 * sending ChangeCipherSpec, in case write returns WANT_READ.
3825 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003826 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003827 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003829 ssl_swap_epochs( ssl );
3830 }
3831
Hanno Becker67bc7c32018-08-06 11:33:50 +01003832 ret = ssl_get_remaining_payload_in_datagram( ssl );
3833 if( ret < 0 )
3834 return( ret );
3835 max_frag_len = (size_t) ret;
3836
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003837 /* CCS is copied as is, while HS messages may need fragmentation */
3838 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3839 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003840 if( max_frag_len == 0 )
3841 {
3842 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3843 return( ret );
3844
3845 continue;
3846 }
3847
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003848 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003849 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003850 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003851
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003852 /* Update position inside current message */
3853 ssl->handshake->cur_msg_p += cur->len;
3854 }
3855 else
3856 {
3857 const unsigned char * const p = ssl->handshake->cur_msg_p;
3858 const size_t hs_len = cur->len - 12;
3859 const size_t frag_off = p - ( cur->p + 12 );
3860 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003861 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003862
Hanno Beckere1dcb032018-08-17 16:47:58 +01003863 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003864 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003865 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003866 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003867
Hanno Becker67bc7c32018-08-06 11:33:50 +01003868 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3869 return( ret );
3870
3871 continue;
3872 }
3873 max_hs_frag_len = max_frag_len - 12;
3874
3875 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3876 max_hs_frag_len : rem_len;
3877
3878 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003879 {
3880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003881 (unsigned) cur_hs_frag_len,
3882 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003883 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003884
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003885 /* Messages are stored with handshake headers as if not fragmented,
3886 * copy beginning of headers then fill fragmentation fields.
3887 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3888 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003889
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003890 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3891 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3892 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3893
Hanno Becker67bc7c32018-08-06 11:33:50 +01003894 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3895 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3896 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003897
3898 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3899
Hanno Becker3f7b9732018-08-28 09:53:25 +01003900 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003901 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3902 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003903 ssl->out_msgtype = cur->type;
3904
3905 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003906 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003907 }
3908
3909 /* If done with the current message move to the next one if any */
3910 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3911 {
3912 if( cur->next != NULL )
3913 {
3914 ssl->handshake->cur_msg = cur->next;
3915 ssl->handshake->cur_msg_p = cur->next->p + 12;
3916 }
3917 else
3918 {
3919 ssl->handshake->cur_msg = NULL;
3920 ssl->handshake->cur_msg_p = NULL;
3921 }
3922 }
3923
3924 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003925 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003928 return( ret );
3929 }
3930 }
3931
Hanno Becker67bc7c32018-08-06 11:33:50 +01003932 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3933 return( ret );
3934
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003935 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003936 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3937 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003938 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003940 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003941 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3942 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003943
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003945
3946 return( 0 );
3947}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003948
3949/*
3950 * To be called when the last message of an incoming flight is received.
3951 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003952void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003953{
3954 /* We won't need to resend that one any more */
3955 ssl_flight_free( ssl->handshake->flight );
3956 ssl->handshake->flight = NULL;
3957 ssl->handshake->cur_msg = NULL;
3958
3959 /* The next incoming flight will start with this msg_seq */
3960 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3961
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003962 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003963 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003964
Hanno Becker0271f962018-08-16 13:23:47 +01003965 /* Clear future message buffering structure. */
3966 ssl_buffering_free( ssl );
3967
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003968 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003969 ssl_set_timer( ssl, 0 );
3970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3972 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003974 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003975 }
3976 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003977 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003978}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003979
3980/*
3981 * To be called when the last message of an outgoing flight is send.
3982 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003984{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003985 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003986 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003988 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3989 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003991 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003992 }
3993 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003995}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003996#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003997
Paul Bakker5121ce52009-01-03 21:22:43 +00003998/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003999 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004000 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004001
4002/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004003 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004004 *
4005 * - fill in handshake headers
4006 * - update handshake checksum
4007 * - DTLS: save message for resending
4008 * - then pass to the record layer
4009 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004010 * DTLS: except for HelloRequest, messages are only queued, and will only be
4011 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004012 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004013 * Inputs:
4014 * - ssl->out_msglen: 4 + actual handshake message len
4015 * (4 is the size of handshake headers for TLS)
4016 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4017 * - ssl->out_msg + 4: the handshake message body
4018 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004019 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004020 * - ssl->out_msglen: the length of the record contents
4021 * (including handshake headers but excluding record headers)
4022 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004023 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004024int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004025{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004026 int ret;
4027 const size_t hs_len = ssl->out_msglen - 4;
4028 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004029
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4031
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004032 /*
4033 * Sanity checks
4034 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004035 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004036 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4037 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004038 /* In SSLv3, the client might send a NoCertificate alert. */
4039#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4040 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4041 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4042 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4043#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4044 {
4045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4046 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4047 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004048 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004049
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004050 /* Whenever we send anything different from a
4051 * HelloRequest we should be in a handshake - double check. */
4052 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4053 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004054 ssl->handshake == NULL )
4055 {
4056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4057 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4058 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004061 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004062 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004063 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004064 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4066 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004067 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004068#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004069
Hanno Beckerb50a2532018-08-06 11:52:54 +01004070 /* Double-check that we did not exceed the bounds
4071 * of the outgoing record buffer.
4072 * This should never fail as the various message
4073 * writing functions must obey the bounds of the
4074 * outgoing record buffer, but better be safe.
4075 *
4076 * Note: We deliberately do not check for the MTU or MFL here.
4077 */
4078 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4079 {
4080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4081 "size %u, maximum %u",
4082 (unsigned) ssl->out_msglen,
4083 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4084 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4085 }
4086
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004087 /*
4088 * Fill handshake headers
4089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004090 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004091 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004092 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4093 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4094 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004095
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004096 /*
4097 * DTLS has additional fields in the Handshake layer,
4098 * between the length field and the actual payload:
4099 * uint16 message_seq;
4100 * uint24 fragment_offset;
4101 * uint24 fragment_length;
4102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004103#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004104 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004105 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004106 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004107 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004108 {
4109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4110 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004111 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004112 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004113 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4114 }
4115
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004116 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004117 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004118
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004119 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004120 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004121 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004122 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4123 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4124 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004125 }
4126 else
4127 {
4128 ssl->out_msg[4] = 0;
4129 ssl->out_msg[5] = 0;
4130 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004131
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004132 /* Handshake hashes are computed without fragmentation,
4133 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004134 memset( ssl->out_msg + 6, 0x00, 3 );
4135 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004136 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004138
Hanno Becker0207e532018-08-28 10:28:28 +01004139 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004140 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4141 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004142 }
4143
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004144 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004145#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004146 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004147 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4148 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004149 {
4150 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004152 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004153 return( ret );
4154 }
4155 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004156 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004157#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004158 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004159 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004160 {
4161 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4162 return( ret );
4163 }
4164 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004165
4166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4167
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004168 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004169}
4170
4171/*
4172 * Record layer functions
4173 */
4174
4175/*
4176 * Write current record.
4177 *
4178 * Uses:
4179 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4180 * - ssl->out_msglen: length of the record content (excl headers)
4181 * - ssl->out_msg: record content
4182 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004183int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004184{
4185 int ret, done = 0;
4186 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004187 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004188
4189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004192 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004193 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004194 {
4195 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004197 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004198 return( ret );
4199 }
4200
4201 len = ssl->out_msglen;
4202 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004205#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4206 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004210 ret = mbedtls_ssl_hw_record_write( ssl );
4211 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004213 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4214 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004215 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004216
4217 if( ret == 0 )
4218 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004219 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004221 if( !done )
4222 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004223 unsigned i;
4224 size_t protected_record_size;
4225
Hanno Becker6430faf2019-05-08 11:57:13 +01004226 /* Skip writing the record content type to after the encryption,
4227 * as it may change when using the CID extension. */
4228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004229 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004230 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004231
Hanno Becker19859472018-08-06 09:40:20 +01004232 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004233 ssl->out_len[0] = (unsigned char)( len >> 8 );
4234 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004235
Paul Bakker48916f92012-09-16 19:57:18 +00004236 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004237 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004238 mbedtls_record rec;
4239
4240 rec.buf = ssl->out_iv;
4241 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4242 ( ssl->out_iv - ssl->out_buf );
4243 rec.data_len = ssl->out_msglen;
4244 rec.data_offset = ssl->out_msg - rec.buf;
4245
4246 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4247 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4248 ssl->conf->transport, rec.ver );
4249 rec.type = ssl->out_msgtype;
4250
Hanno Beckera0e20d02019-05-15 14:03:01 +01004251#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004252 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004253 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004254#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004255
Hanno Beckera18d1322018-01-03 14:27:32 +00004256 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004257 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004260 return( ret );
4261 }
4262
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004263 if( rec.data_offset != 0 )
4264 {
4265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4266 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4267 }
4268
Hanno Becker6430faf2019-05-08 11:57:13 +01004269 /* Update the record content type and CID. */
4270 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004271#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004272 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004273#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004274 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004275 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4276 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004277 }
4278
Hanno Becker5903de42019-05-03 14:46:38 +01004279 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004280
4281#if defined(MBEDTLS_SSL_PROTO_DTLS)
4282 /* In case of DTLS, double-check that we don't exceed
4283 * the remaining space in the datagram. */
4284 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4285 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004286 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004287 if( ret < 0 )
4288 return( ret );
4289
4290 if( protected_record_size > (size_t) ret )
4291 {
4292 /* Should never happen */
4293 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4294 }
4295 }
4296#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004297
Hanno Becker6430faf2019-05-08 11:57:13 +01004298 /* Now write the potentially updated record content type. */
4299 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004302 "version = [%d:%d], msglen = %d",
4303 ssl->out_hdr[0], ssl->out_hdr[1],
4304 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004306 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004307 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004308
4309 ssl->out_left += protected_record_size;
4310 ssl->out_hdr += protected_record_size;
4311 ssl_update_out_pointers( ssl, ssl->transform_out );
4312
Hanno Becker04484622018-08-06 09:49:38 +01004313 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4314 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4315 break;
4316
4317 /* The loop goes to its end iff the counter is wrapping */
4318 if( i == ssl_ep_len( ssl ) )
4319 {
4320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4321 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4322 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004323 }
4324
Hanno Becker67bc7c32018-08-06 11:33:50 +01004325#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004326 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4327 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004328 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004329 size_t remaining;
4330 ret = ssl_get_remaining_payload_in_datagram( ssl );
4331 if( ret < 0 )
4332 {
4333 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4334 ret );
4335 return( ret );
4336 }
4337
4338 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004339 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004340 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004341 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004342 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004343 else
4344 {
Hanno Becker513815a2018-08-20 11:56:09 +01004345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004346 }
4347 }
4348#endif /* MBEDTLS_SSL_PROTO_DTLS */
4349
4350 if( ( flush == SSL_FORCE_FLUSH ) &&
4351 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004353 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004354 return( ret );
4355 }
4356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004358
4359 return( 0 );
4360}
4361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004362#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004363
4364static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4365{
4366 if( ssl->in_msglen < ssl->in_hslen ||
4367 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4368 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4369 {
4370 return( 1 );
4371 }
4372 return( 0 );
4373}
Hanno Becker44650b72018-08-16 12:51:11 +01004374
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004375static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004376{
4377 return( ( ssl->in_msg[9] << 16 ) |
4378 ( ssl->in_msg[10] << 8 ) |
4379 ssl->in_msg[11] );
4380}
4381
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004382static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004383{
4384 return( ( ssl->in_msg[6] << 16 ) |
4385 ( ssl->in_msg[7] << 8 ) |
4386 ssl->in_msg[8] );
4387}
4388
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004389static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004390{
4391 uint32_t msg_len, frag_off, frag_len;
4392
4393 msg_len = ssl_get_hs_total_len( ssl );
4394 frag_off = ssl_get_hs_frag_off( ssl );
4395 frag_len = ssl_get_hs_frag_len( ssl );
4396
4397 if( frag_off > msg_len )
4398 return( -1 );
4399
4400 if( frag_len > msg_len - frag_off )
4401 return( -1 );
4402
4403 if( frag_len + 12 > ssl->in_msglen )
4404 return( -1 );
4405
4406 return( 0 );
4407}
4408
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004409/*
4410 * Mark bits in bitmask (used for DTLS HS reassembly)
4411 */
4412static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4413{
4414 unsigned int start_bits, end_bits;
4415
4416 start_bits = 8 - ( offset % 8 );
4417 if( start_bits != 8 )
4418 {
4419 size_t first_byte_idx = offset / 8;
4420
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004421 /* Special case */
4422 if( len <= start_bits )
4423 {
4424 for( ; len != 0; len-- )
4425 mask[first_byte_idx] |= 1 << ( start_bits - len );
4426
4427 /* Avoid potential issues with offset or len becoming invalid */
4428 return;
4429 }
4430
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004431 offset += start_bits; /* Now offset % 8 == 0 */
4432 len -= start_bits;
4433
4434 for( ; start_bits != 0; start_bits-- )
4435 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4436 }
4437
4438 end_bits = len % 8;
4439 if( end_bits != 0 )
4440 {
4441 size_t last_byte_idx = ( offset + len ) / 8;
4442
4443 len -= end_bits; /* Now len % 8 == 0 */
4444
4445 for( ; end_bits != 0; end_bits-- )
4446 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4447 }
4448
4449 memset( mask + offset / 8, 0xFF, len / 8 );
4450}
4451
4452/*
4453 * Check that bitmask is full
4454 */
4455static int ssl_bitmask_check( unsigned char *mask, size_t len )
4456{
4457 size_t i;
4458
4459 for( i = 0; i < len / 8; i++ )
4460 if( mask[i] != 0xFF )
4461 return( -1 );
4462
4463 for( i = 0; i < len % 8; i++ )
4464 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4465 return( -1 );
4466
4467 return( 0 );
4468}
4469
Hanno Becker56e205e2018-08-16 09:06:12 +01004470/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004471static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004472 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004473{
Hanno Becker56e205e2018-08-16 09:06:12 +01004474 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004475
Hanno Becker56e205e2018-08-16 09:06:12 +01004476 alloc_len = 12; /* Handshake header */
4477 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004478
Hanno Beckerd07df862018-08-16 09:14:58 +01004479 if( add_bitmap )
4480 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004481
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004482 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004483}
Hanno Becker56e205e2018-08-16 09:06:12 +01004484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004485#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004486
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004487static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004488{
4489 return( ( ssl->in_msg[1] << 16 ) |
4490 ( ssl->in_msg[2] << 8 ) |
4491 ssl->in_msg[3] );
4492}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004493
Simon Butcher99000142016-10-13 17:21:01 +01004494int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004495{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004496 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004499 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004500 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004501 }
4502
Hanno Becker12555c62018-08-16 12:47:53 +01004503 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004505 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004506 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004507 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004509#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004510 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004511 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004512 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004513 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004514
Hanno Becker44650b72018-08-16 12:51:11 +01004515 if( ssl_check_hs_header( ssl ) != 0 )
4516 {
4517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4518 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4519 }
4520
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004521 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004522 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4523 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4524 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4525 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004526 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004527 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4528 {
4529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4530 recv_msg_seq,
4531 ssl->handshake->in_msg_seq ) );
4532 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4533 }
4534
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004535 /* Retransmit only on last message from previous flight, to avoid
4536 * too many retransmissions.
4537 * Besides, No sane server ever retransmits HelloVerifyRequest */
4538 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004542 "message_seq = %d, start_of_flight = %d",
4543 recv_msg_seq,
4544 ssl->handshake->in_flight_start_seq ) );
4545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004546 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004548 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004549 return( ret );
4550 }
4551 }
4552 else
4553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004555 "message_seq = %d, expected = %d",
4556 recv_msg_seq,
4557 ssl->handshake->in_msg_seq ) );
4558 }
4559
Hanno Becker90333da2017-10-10 11:27:13 +01004560 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004561 }
4562 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004563
Hanno Becker6d97ef52018-08-16 13:09:04 +01004564 /* Message reassembly is handled alongside buffering of future
4565 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004566 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004567 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004568 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004571 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004572 }
4573 }
4574 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004575#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004576 /* With TLS we don't handle fragmentation (for now) */
4577 if( ssl->in_msglen < ssl->in_hslen )
4578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4580 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004581 }
4582
Simon Butcher99000142016-10-13 17:21:01 +01004583 return( 0 );
4584}
4585
4586void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4587{
Hanno Becker0271f962018-08-16 13:23:47 +01004588 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004589
Hanno Becker0271f962018-08-16 13:23:47 +01004590 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004591 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004592 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004593 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004594
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004595 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004596#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004597 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004598 ssl->handshake != NULL )
4599 {
Hanno Becker0271f962018-08-16 13:23:47 +01004600 unsigned offset;
4601 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004602
Hanno Becker0271f962018-08-16 13:23:47 +01004603 /* Increment handshake sequence number */
4604 hs->in_msg_seq++;
4605
4606 /*
4607 * Clear up handshake buffering and reassembly structure.
4608 */
4609
4610 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004611 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004612
4613 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004614 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4615 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004616 offset++, hs_buf++ )
4617 {
4618 *hs_buf = *(hs_buf + 1);
4619 }
4620
4621 /* Create a fresh last entry */
4622 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004623 }
4624#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004625}
4626
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004627/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004628 * DTLS anti-replay: RFC 6347 4.1.2.6
4629 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004630 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4631 * Bit n is set iff record number in_window_top - n has been seen.
4632 *
4633 * Usually, in_window_top is the last record number seen and the lsb of
4634 * in_window is set. The only exception is the initial state (record number 0
4635 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004637#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4638static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004639{
4640 ssl->in_window_top = 0;
4641 ssl->in_window = 0;
4642}
4643
4644static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4645{
4646 return( ( (uint64_t) buf[0] << 40 ) |
4647 ( (uint64_t) buf[1] << 32 ) |
4648 ( (uint64_t) buf[2] << 24 ) |
4649 ( (uint64_t) buf[3] << 16 ) |
4650 ( (uint64_t) buf[4] << 8 ) |
4651 ( (uint64_t) buf[5] ) );
4652}
4653
4654/*
4655 * Return 0 if sequence number is acceptable, -1 otherwise
4656 */
Hanno Becker0183d692019-07-12 08:50:37 +01004657int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004658{
4659 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4660 uint64_t bit;
4661
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004662 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004663 return( 0 );
4664
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004665 if( rec_seqnum > ssl->in_window_top )
4666 return( 0 );
4667
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004668 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004669
4670 if( bit >= 64 )
4671 return( -1 );
4672
4673 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4674 return( -1 );
4675
4676 return( 0 );
4677}
4678
4679/*
4680 * Update replay window on new validated record
4681 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004682void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004683{
4684 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4685
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004686 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004687 return;
4688
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004689 if( rec_seqnum > ssl->in_window_top )
4690 {
4691 /* Update window_top and the contents of the window */
4692 uint64_t shift = rec_seqnum - ssl->in_window_top;
4693
4694 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004695 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004696 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004697 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004698 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004699 ssl->in_window |= 1;
4700 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004701
4702 ssl->in_window_top = rec_seqnum;
4703 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004704 else
4705 {
4706 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004707 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004708
4709 if( bit < 64 ) /* Always true, but be extra sure */
4710 ssl->in_window |= (uint64_t) 1 << bit;
4711 }
4712}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004713#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004714
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004715#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004716/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004717static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4718
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004719/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004720 * Without any SSL context, check if a datagram looks like a ClientHello with
4721 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004722 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004723 *
4724 * - if cookie is valid, return 0
4725 * - if ClientHello looks superficially valid but cookie is not,
4726 * fill obuf and set olen, then
4727 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4728 * - otherwise return a specific error code
4729 */
4730static int ssl_check_dtls_clihlo_cookie(
4731 mbedtls_ssl_cookie_write_t *f_cookie_write,
4732 mbedtls_ssl_cookie_check_t *f_cookie_check,
4733 void *p_cookie,
4734 const unsigned char *cli_id, size_t cli_id_len,
4735 const unsigned char *in, size_t in_len,
4736 unsigned char *obuf, size_t buf_len, size_t *olen )
4737{
4738 size_t sid_len, cookie_len;
4739 unsigned char *p;
4740
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004741 /*
4742 * Structure of ClientHello with record and handshake headers,
4743 * and expected values. We don't need to check a lot, more checks will be
4744 * done when actually parsing the ClientHello - skipping those checks
4745 * avoids code duplication and does not make cookie forging any easier.
4746 *
4747 * 0-0 ContentType type; copied, must be handshake
4748 * 1-2 ProtocolVersion version; copied
4749 * 3-4 uint16 epoch; copied, must be 0
4750 * 5-10 uint48 sequence_number; copied
4751 * 11-12 uint16 length; (ignored)
4752 *
4753 * 13-13 HandshakeType msg_type; (ignored)
4754 * 14-16 uint24 length; (ignored)
4755 * 17-18 uint16 message_seq; copied
4756 * 19-21 uint24 fragment_offset; copied, must be 0
4757 * 22-24 uint24 fragment_length; (ignored)
4758 *
4759 * 25-26 ProtocolVersion client_version; (ignored)
4760 * 27-58 Random random; (ignored)
4761 * 59-xx SessionID session_id; 1 byte len + sid_len content
4762 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4763 * ...
4764 *
4765 * Minimum length is 61 bytes.
4766 */
4767 if( in_len < 61 ||
4768 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4769 in[3] != 0 || in[4] != 0 ||
4770 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4771 {
4772 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4773 }
4774
4775 sid_len = in[59];
4776 if( sid_len > in_len - 61 )
4777 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4778
4779 cookie_len = in[60 + sid_len];
4780 if( cookie_len > in_len - 60 )
4781 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4782
4783 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4784 cli_id, cli_id_len ) == 0 )
4785 {
4786 /* Valid cookie */
4787 return( 0 );
4788 }
4789
4790 /*
4791 * If we get here, we've got an invalid cookie, let's prepare HVR.
4792 *
4793 * 0-0 ContentType type; copied
4794 * 1-2 ProtocolVersion version; copied
4795 * 3-4 uint16 epoch; copied
4796 * 5-10 uint48 sequence_number; copied
4797 * 11-12 uint16 length; olen - 13
4798 *
4799 * 13-13 HandshakeType msg_type; hello_verify_request
4800 * 14-16 uint24 length; olen - 25
4801 * 17-18 uint16 message_seq; copied
4802 * 19-21 uint24 fragment_offset; copied
4803 * 22-24 uint24 fragment_length; olen - 25
4804 *
4805 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4806 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4807 *
4808 * Minimum length is 28.
4809 */
4810 if( buf_len < 28 )
4811 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4812
4813 /* Copy most fields and adapt others */
4814 memcpy( obuf, in, 25 );
4815 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4816 obuf[25] = 0xfe;
4817 obuf[26] = 0xff;
4818
4819 /* Generate and write actual cookie */
4820 p = obuf + 28;
4821 if( f_cookie_write( p_cookie,
4822 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4823 {
4824 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4825 }
4826
4827 *olen = p - obuf;
4828
4829 /* Go back and fill length fields */
4830 obuf[27] = (unsigned char)( *olen - 28 );
4831
4832 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4833 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4834 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4835
4836 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4837 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4838
4839 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4840}
4841
4842/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004843 * Handle possible client reconnect with the same UDP quadruplet
4844 * (RFC 6347 Section 4.2.8).
4845 *
4846 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4847 * that looks like a ClientHello.
4848 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004849 * - if the input looks like a ClientHello without cookies,
4850 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004851 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004852 * - if the input looks like a ClientHello with a valid cookie,
4853 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004854 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004855 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004856 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004857 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004858 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4859 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004860 */
4861static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4862{
4863 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004864 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004865
Hanno Becker2fddd372019-07-10 14:37:41 +01004866 if( ssl->conf->f_cookie_write == NULL ||
4867 ssl->conf->f_cookie_check == NULL )
4868 {
4869 /* If we can't use cookies to verify reachability of the peer,
4870 * drop the record. */
4871 return( 0 );
4872 }
4873
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004874 ret = ssl_check_dtls_clihlo_cookie(
4875 ssl->conf->f_cookie_write,
4876 ssl->conf->f_cookie_check,
4877 ssl->conf->p_cookie,
4878 ssl->cli_id, ssl->cli_id_len,
4879 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004880 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004881
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004882 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4883
4884 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004885 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004886 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004887 * If the error is permanent we'll catch it later,
4888 * if it's not, then hopefully it'll work next time. */
4889 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01004890 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004891 }
4892
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004893 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004894 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004895 /* Got a valid cookie, partially reset context */
4896 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4897 {
4898 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4899 return( ret );
4900 }
4901
4902 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004903 }
4904
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004905 return( ret );
4906}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004907#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004908
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004909static int ssl_check_record_type( uint8_t record_type )
4910{
4911 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4912 record_type != MBEDTLS_SSL_MSG_ALERT &&
4913 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4914 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4915 {
4916 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4917 }
4918
4919 return( 0 );
4920}
4921
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004922/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004923 * ContentType type;
4924 * ProtocolVersion version;
4925 * uint16 epoch; // DTLS only
4926 * uint48 sequence_number; // DTLS only
4927 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004928 *
4929 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004930 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004931 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4932 *
4933 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004934 * 1. proceed with the record if this function returns 0
4935 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4936 * 3. return CLIENT_RECONNECT if this function return that value
4937 * 4. drop the whole datagram if this function returns anything else.
4938 * Point 2 is needed when the peer is resending, and we have already received
4939 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004940 */
Hanno Becker331de3d2019-07-12 11:10:16 +01004941static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01004942 unsigned char *buf,
4943 size_t len,
4944 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00004945{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004946 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004947
Hanno Beckere5e7e782019-07-11 12:29:35 +01004948 size_t const rec_hdr_type_offset = 0;
4949 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004950
Hanno Beckere5e7e782019-07-11 12:29:35 +01004951 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
4952 rec_hdr_type_len;
4953 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00004954
Hanno Beckere5e7e782019-07-11 12:29:35 +01004955 size_t const rec_hdr_ctr_len = 8;
4956#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01004957 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004958 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
4959 rec_hdr_version_len;
4960
Hanno Beckera0e20d02019-05-15 14:03:01 +01004961#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01004962 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
4963 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01004964 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004965#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4966#endif /* MBEDTLS_SSL_PROTO_DTLS */
4967
4968 size_t rec_hdr_len_offset; /* To be determined */
4969 size_t const rec_hdr_len_len = 2;
4970
4971 /*
4972 * Check minimum lengths for record header.
4973 */
4974
4975#if defined(MBEDTLS_SSL_PROTO_DTLS)
4976 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4977 {
4978 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
4979 }
4980 else
4981#endif /* MBEDTLS_SSL_PROTO_DTLS */
4982 {
4983 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
4984 }
4985
4986 if( len < rec_hdr_len_offset + rec_hdr_len_len )
4987 {
4988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
4989 (unsigned) len,
4990 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
4991 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4992 }
4993
4994 /*
4995 * Parse and validate record content type
4996 */
4997
4998 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01004999
5000 /* Check record content type */
5001#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5002 rec->cid_len = 0;
5003
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005004 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005005 ssl->conf->cid_len != 0 &&
5006 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005007 {
5008 /* Shift pointers to account for record header including CID
5009 * struct {
5010 * ContentType special_type = tls12_cid;
5011 * ProtocolVersion version;
5012 * uint16 epoch;
5013 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005014 * opaque cid[cid_length]; // Additional field compared to
5015 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005016 * uint16 length;
5017 * opaque enc_content[DTLSCiphertext.length];
5018 * } DTLSCiphertext;
5019 */
5020
5021 /* So far, we only support static CID lengths
5022 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005023 rec_hdr_cid_len = ssl->conf->cid_len;
5024 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005025
Hanno Beckere5e7e782019-07-11 12:29:35 +01005026 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005027 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5029 (unsigned) len,
5030 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005031 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005032 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005033
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02005034 /* configured CID len is guaranteed at most 255, see
5035 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5036 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005037 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005038 }
5039 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005040#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005041 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005042 if( ssl_check_record_type( rec->type ) )
5043 {
Hanno Becker54229812019-07-12 14:40:00 +01005044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5045 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005046 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5047 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005048 }
5049
Hanno Beckere5e7e782019-07-11 12:29:35 +01005050 /*
5051 * Parse and validate record version
5052 */
5053
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005054 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5055 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005056 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5057 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005058 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005059
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005060 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5063 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005064 }
5065
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005066 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5069 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005070 }
5071
Hanno Beckere5e7e782019-07-11 12:29:35 +01005072 /*
5073 * Parse/Copy record sequence number.
5074 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005075
Hanno Beckere5e7e782019-07-11 12:29:35 +01005076#if defined(MBEDTLS_SSL_PROTO_DTLS)
5077 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005078 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005079 /* Copy explicit record sequence number from input buffer. */
5080 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5081 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005082 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005083 else
5084#endif /* MBEDTLS_SSL_PROTO_DTLS */
5085 {
5086 /* Copy implicit record sequence number from SSL context structure. */
5087 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5088 }
Paul Bakker40e46942009-01-03 21:51:57 +00005089
Hanno Beckere5e7e782019-07-11 12:29:35 +01005090 /*
5091 * Parse record length.
5092 */
5093
Hanno Beckere5e7e782019-07-11 12:29:35 +01005094 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005095 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5096 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005097 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00005098
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005099 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005100 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005101 rec->type,
5102 major_ver, minor_ver, rec->data_len ) );
5103
5104 rec->buf = buf;
5105 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005106
Hanno Beckerd417cc92019-07-26 08:20:27 +01005107 if( rec->data_len == 0 )
5108 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005109
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005110 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005111 * DTLS-related tests.
5112 * Check epoch before checking length constraint because
5113 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5114 * message gets duplicated before the corresponding Finished message,
5115 * the second ChangeCipherSpec should be discarded because it belongs
5116 * to an old epoch, but not because its length is shorter than
5117 * the minimum record length for packets using the new record transform.
5118 * Note that these two kinds of failures are handled differently,
5119 * as an unexpected record is silently skipped but an invalid
5120 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005121 */
5122#if defined(MBEDTLS_SSL_PROTO_DTLS)
5123 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5124 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005125 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005126
Hanno Becker955a5c92019-07-10 17:12:07 +01005127 /* Check that the datagram is large enough to contain a record
5128 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005129 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005130 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5132 (unsigned) len,
5133 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005134 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5135 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005136
Hanno Becker37cfe732019-07-10 17:20:01 +01005137 /* Records from other, non-matching epochs are silently discarded.
5138 * (The case of same-port Client reconnects must be considered in
5139 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005140 if( rec_epoch != ssl->in_epoch )
5141 {
5142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5143 "expected %d, received %d",
5144 ssl->in_epoch, rec_epoch ) );
5145
Hanno Becker552f7472019-07-19 10:59:12 +01005146 /* Records from the next epoch are considered for buffering
5147 * (concretely: early Finished messages). */
5148 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005149 {
Hanno Becker552f7472019-07-19 10:59:12 +01005150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5151 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005152 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005153
Hanno Becker2fddd372019-07-10 14:37:41 +01005154 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005155 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005156#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005157 /* For records from the correct epoch, check whether their
5158 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005159 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005160 {
5161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5162 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5163 }
5164#endif
5165 }
5166#endif /* MBEDTLS_SSL_PROTO_DTLS */
5167
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005168 return( 0 );
5169}
Paul Bakker5121ce52009-01-03 21:22:43 +00005170
Paul Bakker5121ce52009-01-03 21:22:43 +00005171
Hanno Becker2fddd372019-07-10 14:37:41 +01005172#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5173static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5174{
5175 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5176
5177 /*
5178 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5179 * access the first byte of record content (handshake type), as we
5180 * have an active transform (possibly iv_len != 0), so use the
5181 * fact that the record header len is 13 instead.
5182 */
5183 if( rec_epoch == 0 &&
5184 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5185 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5186 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5187 ssl->in_left > 13 &&
5188 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5189 {
5190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5191 "from the same port" ) );
5192 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005193 }
5194
5195 return( 0 );
5196}
Hanno Becker2fddd372019-07-10 14:37:41 +01005197#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005198
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005199/*
5200 * If applicable, decrypt (and decompress) record content
5201 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005202static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5203 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005204{
5205 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005207 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005208 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005210#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5211 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005215 ret = mbedtls_ssl_hw_record_read( ssl );
5216 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005218 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5219 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005220 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005221
5222 if( ret == 0 )
5223 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005224 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005225#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005226 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005227 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005228 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005229
Hanno Beckera18d1322018-01-03 14:27:32 +00005230 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005231 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005233 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005234
Hanno Beckera0e20d02019-05-15 14:03:01 +01005235#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005236 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5237 ssl->conf->ignore_unexpected_cid
5238 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5239 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005240 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005241 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005242 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005243#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005244
Paul Bakker5121ce52009-01-03 21:22:43 +00005245 return( ret );
5246 }
5247
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005248 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005249 {
5250 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005251 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005252 }
5253
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005254 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005255 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005256
Hanno Beckera0e20d02019-05-15 14:03:01 +01005257#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005258 /* We have already checked the record content type
5259 * in ssl_parse_record_header(), failing or silently
5260 * dropping the record in the case of an unknown type.
5261 *
5262 * Since with the use of CIDs, the record content type
5263 * might change during decryption, re-check the record
5264 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005265 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005266 {
5267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5268 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5269 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005270#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005271
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005272 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005273 {
5274#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5275 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005276 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005277 {
5278 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5280 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5281 }
5282#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5283
5284 ssl->nb_zero++;
5285
5286 /*
5287 * Three or more empty messages may be a DoS attack
5288 * (excessive CPU consumption).
5289 */
5290 if( ssl->nb_zero > 3 )
5291 {
5292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005293 "messages, possible DoS attack" ) );
5294 /* Treat the records as if they were not properly authenticated,
5295 * thereby failing the connection if we see more than allowed
5296 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005297 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5298 }
5299 }
5300 else
5301 ssl->nb_zero = 0;
5302
5303#if defined(MBEDTLS_SSL_PROTO_DTLS)
5304 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5305 {
5306 ; /* in_ctr read from peer, not maintained internally */
5307 }
5308 else
5309#endif
5310 {
5311 unsigned i;
5312 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5313 if( ++ssl->in_ctr[i - 1] != 0 )
5314 break;
5315
5316 /* The loop goes to its end iff the counter is wrapping */
5317 if( i == ssl_ep_len( ssl ) )
5318 {
5319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5320 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5321 }
5322 }
5323
Paul Bakker5121ce52009-01-03 21:22:43 +00005324 }
5325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005326#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005327 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005328 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005329 {
5330 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005333 return( ret );
5334 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005335 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005336#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005338#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005339 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005341 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005342 }
5343#endif
5344
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005345 /* Check actual (decrypted) record content length against
5346 * configured maximum. */
5347 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5348 {
5349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5350 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5351 }
5352
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005353 return( 0 );
5354}
5355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005356static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005357
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005358/*
5359 * Read a record.
5360 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005361 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5362 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5363 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005364 */
Hanno Becker1097b342018-08-15 14:09:41 +01005365
5366/* Helper functions for mbedtls_ssl_read_record(). */
5367static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005368static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5369static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005370
Hanno Becker327c93b2018-08-15 13:56:18 +01005371int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005372 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005373{
5374 int ret;
5375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005377
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005378 if( ssl->keep_current_message == 0 )
5379 {
5380 do {
Simon Butcher99000142016-10-13 17:21:01 +01005381
Hanno Becker26994592018-08-15 14:14:59 +01005382 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005383 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005384 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005385
Hanno Beckere74d5562018-08-15 14:26:08 +01005386 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005387 {
Hanno Becker40f50842018-08-15 14:48:01 +01005388#if defined(MBEDTLS_SSL_PROTO_DTLS)
5389 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005390
Hanno Becker40f50842018-08-15 14:48:01 +01005391 /* We only check for buffered messages if the
5392 * current datagram is fully consumed. */
5393 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005394 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005395 {
Hanno Becker40f50842018-08-15 14:48:01 +01005396 if( ssl_load_buffered_message( ssl ) == 0 )
5397 have_buffered = 1;
5398 }
5399
5400 if( have_buffered == 0 )
5401#endif /* MBEDTLS_SSL_PROTO_DTLS */
5402 {
5403 ret = ssl_get_next_record( ssl );
5404 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5405 continue;
5406
5407 if( ret != 0 )
5408 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005409 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005410 return( ret );
5411 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005412 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005413 }
5414
5415 ret = mbedtls_ssl_handle_message_type( ssl );
5416
Hanno Becker40f50842018-08-15 14:48:01 +01005417#if defined(MBEDTLS_SSL_PROTO_DTLS)
5418 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5419 {
5420 /* Buffer future message */
5421 ret = ssl_buffer_message( ssl );
5422 if( ret != 0 )
5423 return( ret );
5424
5425 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5426 }
5427#endif /* MBEDTLS_SSL_PROTO_DTLS */
5428
Hanno Becker90333da2017-10-10 11:27:13 +01005429 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5430 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005431
5432 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005433 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005434 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005435 return( ret );
5436 }
5437
Hanno Becker327c93b2018-08-15 13:56:18 +01005438 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005439 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005440 {
5441 mbedtls_ssl_update_handshake_status( ssl );
5442 }
Simon Butcher99000142016-10-13 17:21:01 +01005443 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005444 else
Simon Butcher99000142016-10-13 17:21:01 +01005445 {
Hanno Becker02f59072018-08-15 14:00:24 +01005446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005447 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005448 }
5449
5450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5451
5452 return( 0 );
5453}
5454
Hanno Becker40f50842018-08-15 14:48:01 +01005455#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005456static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005457{
Hanno Becker40f50842018-08-15 14:48:01 +01005458 if( ssl->in_left > ssl->next_record_offset )
5459 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005460
Hanno Becker40f50842018-08-15 14:48:01 +01005461 return( 0 );
5462}
5463
5464static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5465{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005466 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005467 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005468 int ret = 0;
5469
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005470 if( hs == NULL )
5471 return( -1 );
5472
Hanno Beckere00ae372018-08-20 09:39:42 +01005473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5474
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005475 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5476 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5477 {
5478 /* Check if we have seen a ChangeCipherSpec before.
5479 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005480 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005481 {
5482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5483 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005484 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005485 }
5486
Hanno Becker39b8bc92018-08-28 17:17:13 +01005487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005488 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5489 ssl->in_msglen = 1;
5490 ssl->in_msg[0] = 1;
5491
5492 /* As long as they are equal, the exact value doesn't matter. */
5493 ssl->in_left = 0;
5494 ssl->next_record_offset = 0;
5495
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005496 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005497 goto exit;
5498 }
Hanno Becker37f95322018-08-16 13:55:32 +01005499
Hanno Beckerb8f50142018-08-28 10:01:34 +01005500#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005501 /* Debug only */
5502 {
5503 unsigned offset;
5504 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5505 {
5506 hs_buf = &hs->buffering.hs[offset];
5507 if( hs_buf->is_valid == 1 )
5508 {
5509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5510 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005511 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005512 }
5513 }
5514 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005515#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005516
5517 /* Check if we have buffered and/or fully reassembled the
5518 * next handshake message. */
5519 hs_buf = &hs->buffering.hs[0];
5520 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5521 {
5522 /* Synthesize a record containing the buffered HS message. */
5523 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5524 ( hs_buf->data[2] << 8 ) |
5525 hs_buf->data[3];
5526
5527 /* Double-check that we haven't accidentally buffered
5528 * a message that doesn't fit into the input buffer. */
5529 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5530 {
5531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5532 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5533 }
5534
5535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5536 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5537 hs_buf->data, msg_len + 12 );
5538
5539 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5540 ssl->in_hslen = msg_len + 12;
5541 ssl->in_msglen = msg_len + 12;
5542 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5543
5544 ret = 0;
5545 goto exit;
5546 }
5547 else
5548 {
5549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5550 hs->in_msg_seq ) );
5551 }
5552
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005553 ret = -1;
5554
5555exit:
5556
5557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5558 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005559}
5560
Hanno Beckera02b0b42018-08-21 17:20:27 +01005561static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5562 size_t desired )
5563{
5564 int offset;
5565 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5567 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005568
Hanno Becker01315ea2018-08-21 17:22:17 +01005569 /* Get rid of future records epoch first, if such exist. */
5570 ssl_free_buffered_record( ssl );
5571
5572 /* Check if we have enough space available now. */
5573 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5574 hs->buffering.total_bytes_buffered ) )
5575 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005577 return( 0 );
5578 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005579
Hanno Becker4f432ad2018-08-28 10:02:32 +01005580 /* We don't have enough space to buffer the next expected handshake
5581 * message. Remove buffers used for future messages to gain space,
5582 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005583 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5584 offset >= 0; offset-- )
5585 {
5586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5587 offset ) );
5588
Hanno Beckerb309b922018-08-23 13:18:05 +01005589 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005590
5591 /* Check if we have enough space available now. */
5592 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5593 hs->buffering.total_bytes_buffered ) )
5594 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005596 return( 0 );
5597 }
5598 }
5599
5600 return( -1 );
5601}
5602
Hanno Becker40f50842018-08-15 14:48:01 +01005603static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5604{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005605 int ret = 0;
5606 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5607
5608 if( hs == NULL )
5609 return( 0 );
5610
5611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5612
5613 switch( ssl->in_msgtype )
5614 {
5615 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005617
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005618 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005619 break;
5620
5621 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005622 {
5623 unsigned recv_msg_seq_offset;
5624 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5625 mbedtls_ssl_hs_buffer *hs_buf;
5626 size_t msg_len = ssl->in_hslen - 12;
5627
5628 /* We should never receive an old handshake
5629 * message - double-check nonetheless. */
5630 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5631 {
5632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5633 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5634 }
5635
5636 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5637 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5638 {
5639 /* Silently ignore -- message too far in the future */
5640 MBEDTLS_SSL_DEBUG_MSG( 2,
5641 ( "Ignore future HS message with sequence number %u, "
5642 "buffering window %u - %u",
5643 recv_msg_seq, ssl->handshake->in_msg_seq,
5644 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5645
5646 goto exit;
5647 }
5648
5649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5650 recv_msg_seq, recv_msg_seq_offset ) );
5651
5652 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5653
5654 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005655 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005656 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005657 size_t reassembly_buf_sz;
5658
Hanno Becker37f95322018-08-16 13:55:32 +01005659 hs_buf->is_fragmented =
5660 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5661
5662 /* We copy the message back into the input buffer
5663 * after reassembly, so check that it's not too large.
5664 * This is an implementation-specific limitation
5665 * and not one from the standard, hence it is not
5666 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005667 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005668 {
5669 /* Ignore message */
5670 goto exit;
5671 }
5672
Hanno Beckere0b150f2018-08-21 15:51:03 +01005673 /* Check if we have enough space to buffer the message. */
5674 if( hs->buffering.total_bytes_buffered >
5675 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5676 {
5677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5678 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5679 }
5680
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005681 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5682 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005683
5684 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5685 hs->buffering.total_bytes_buffered ) )
5686 {
5687 if( recv_msg_seq_offset > 0 )
5688 {
5689 /* If we can't buffer a future message because
5690 * of space limitations -- ignore. */
5691 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",
5692 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5693 (unsigned) hs->buffering.total_bytes_buffered ) );
5694 goto exit;
5695 }
Hanno Beckere1801392018-08-21 16:51:05 +01005696 else
5697 {
5698 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",
5699 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5700 (unsigned) hs->buffering.total_bytes_buffered ) );
5701 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005702
Hanno Beckera02b0b42018-08-21 17:20:27 +01005703 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005704 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005705 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",
5706 (unsigned) msg_len,
5707 (unsigned) reassembly_buf_sz,
5708 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005709 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005710 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5711 goto exit;
5712 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005713 }
5714
5715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5716 msg_len ) );
5717
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005718 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5719 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005720 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005721 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005722 goto exit;
5723 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005724 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005725
5726 /* Prepare final header: copy msg_type, length and message_seq,
5727 * then add standardised fragment_offset and fragment_length */
5728 memcpy( hs_buf->data, ssl->in_msg, 6 );
5729 memset( hs_buf->data + 6, 0, 3 );
5730 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5731
5732 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005733
5734 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005735 }
5736 else
5737 {
5738 /* Make sure msg_type and length are consistent */
5739 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5740 {
5741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5742 /* Ignore */
5743 goto exit;
5744 }
5745 }
5746
Hanno Becker4422bbb2018-08-20 09:40:19 +01005747 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005748 {
5749 size_t frag_len, frag_off;
5750 unsigned char * const msg = hs_buf->data + 12;
5751
5752 /*
5753 * Check and copy current fragment
5754 */
5755
5756 /* Validation of header fields already done in
5757 * mbedtls_ssl_prepare_handshake_record(). */
5758 frag_off = ssl_get_hs_frag_off( ssl );
5759 frag_len = ssl_get_hs_frag_len( ssl );
5760
5761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5762 frag_off, frag_len ) );
5763 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5764
5765 if( hs_buf->is_fragmented )
5766 {
5767 unsigned char * const bitmask = msg + msg_len;
5768 ssl_bitmask_set( bitmask, frag_off, frag_len );
5769 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5770 msg_len ) == 0 );
5771 }
5772 else
5773 {
5774 hs_buf->is_complete = 1;
5775 }
5776
5777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5778 hs_buf->is_complete ? "" : "not yet " ) );
5779 }
5780
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005781 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005782 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005783
5784 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005785 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005786 break;
5787 }
5788
5789exit:
5790
5791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5792 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005793}
5794#endif /* MBEDTLS_SSL_PROTO_DTLS */
5795
Hanno Becker1097b342018-08-15 14:09:41 +01005796static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005797{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005798 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005799 * Consume last content-layer message and potentially
5800 * update in_msglen which keeps track of the contents'
5801 * consumption state.
5802 *
5803 * (1) Handshake messages:
5804 * Remove last handshake message, move content
5805 * and adapt in_msglen.
5806 *
5807 * (2) Alert messages:
5808 * Consume whole record content, in_msglen = 0.
5809 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005810 * (3) Change cipher spec:
5811 * Consume whole record content, in_msglen = 0.
5812 *
5813 * (4) Application data:
5814 * Don't do anything - the record layer provides
5815 * the application data as a stream transport
5816 * and consumes through mbedtls_ssl_read only.
5817 *
5818 */
5819
5820 /* Case (1): Handshake messages */
5821 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005822 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005823 /* Hard assertion to be sure that no application data
5824 * is in flight, as corrupting ssl->in_msglen during
5825 * ssl->in_offt != NULL is fatal. */
5826 if( ssl->in_offt != NULL )
5827 {
5828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5829 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5830 }
5831
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005832 /*
5833 * Get next Handshake message in the current record
5834 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005835
Hanno Becker4a810fb2017-05-24 16:27:30 +01005836 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005837 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005838 * current handshake content: If DTLS handshake
5839 * fragmentation is used, that's the fragment
5840 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005841 * size here is faulty and should be changed at
5842 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005843 * (2) While it doesn't seem to cause problems, one
5844 * has to be very careful not to assume that in_hslen
5845 * is always <= in_msglen in a sensible communication.
5846 * Again, it's wrong for DTLS handshake fragmentation.
5847 * The following check is therefore mandatory, and
5848 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005849 * Additionally, ssl->in_hslen might be arbitrarily out of
5850 * bounds after handling a DTLS message with an unexpected
5851 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005852 */
5853 if( ssl->in_hslen < ssl->in_msglen )
5854 {
5855 ssl->in_msglen -= ssl->in_hslen;
5856 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5857 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005858
Hanno Becker4a810fb2017-05-24 16:27:30 +01005859 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5860 ssl->in_msg, ssl->in_msglen );
5861 }
5862 else
5863 {
5864 ssl->in_msglen = 0;
5865 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005866
Hanno Becker4a810fb2017-05-24 16:27:30 +01005867 ssl->in_hslen = 0;
5868 }
5869 /* Case (4): Application data */
5870 else if( ssl->in_offt != NULL )
5871 {
5872 return( 0 );
5873 }
5874 /* Everything else (CCS & Alerts) */
5875 else
5876 {
5877 ssl->in_msglen = 0;
5878 }
5879
Hanno Becker1097b342018-08-15 14:09:41 +01005880 return( 0 );
5881}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005882
Hanno Beckere74d5562018-08-15 14:26:08 +01005883static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5884{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005885 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005886 return( 1 );
5887
5888 return( 0 );
5889}
5890
Hanno Becker5f066e72018-08-16 14:56:31 +01005891#if defined(MBEDTLS_SSL_PROTO_DTLS)
5892
5893static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5894{
5895 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5896 if( hs == NULL )
5897 return;
5898
Hanno Becker01315ea2018-08-21 17:22:17 +01005899 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005900 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005901 hs->buffering.total_bytes_buffered -=
5902 hs->buffering.future_record.len;
5903
5904 mbedtls_free( hs->buffering.future_record.data );
5905 hs->buffering.future_record.data = NULL;
5906 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005907}
5908
5909static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5910{
5911 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5912 unsigned char * rec;
5913 size_t rec_len;
5914 unsigned rec_epoch;
5915
5916 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5917 return( 0 );
5918
5919 if( hs == NULL )
5920 return( 0 );
5921
Hanno Becker5f066e72018-08-16 14:56:31 +01005922 rec = hs->buffering.future_record.data;
5923 rec_len = hs->buffering.future_record.len;
5924 rec_epoch = hs->buffering.future_record.epoch;
5925
5926 if( rec == NULL )
5927 return( 0 );
5928
Hanno Becker4cb782d2018-08-20 11:19:05 +01005929 /* Only consider loading future records if the
5930 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005931 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005932 return( 0 );
5933
Hanno Becker5f066e72018-08-16 14:56:31 +01005934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5935
5936 if( rec_epoch != ssl->in_epoch )
5937 {
5938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5939 goto exit;
5940 }
5941
5942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5943
5944 /* Double-check that the record is not too large */
5945 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5946 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5947 {
5948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5949 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5950 }
5951
5952 memcpy( ssl->in_hdr, rec, rec_len );
5953 ssl->in_left = rec_len;
5954 ssl->next_record_offset = 0;
5955
5956 ssl_free_buffered_record( ssl );
5957
5958exit:
5959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5960 return( 0 );
5961}
5962
Hanno Becker519f15d2019-07-11 12:43:20 +01005963static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
5964 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01005965{
5966 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01005967
5968 /* Don't buffer future records outside handshakes. */
5969 if( hs == NULL )
5970 return( 0 );
5971
5972 /* Only buffer handshake records (we are only interested
5973 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01005974 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01005975 return( 0 );
5976
5977 /* Don't buffer more than one future epoch record. */
5978 if( hs->buffering.future_record.data != NULL )
5979 return( 0 );
5980
Hanno Becker01315ea2018-08-21 17:22:17 +01005981 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01005982 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01005983 hs->buffering.total_bytes_buffered ) )
5984 {
5985 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 +01005986 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01005987 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005988 return( 0 );
5989 }
5990
Hanno Becker5f066e72018-08-16 14:56:31 +01005991 /* Buffer record */
5992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5993 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01005994 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01005995
5996 /* ssl_parse_record_header() only considers records
5997 * of the next epoch as candidates for buffering. */
5998 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01005999 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006000
6001 hs->buffering.future_record.data =
6002 mbedtls_calloc( 1, hs->buffering.future_record.len );
6003 if( hs->buffering.future_record.data == NULL )
6004 {
6005 /* If we run out of RAM trying to buffer a
6006 * record from the next epoch, just ignore. */
6007 return( 0 );
6008 }
6009
Hanno Becker519f15d2019-07-11 12:43:20 +01006010 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006011
Hanno Becker519f15d2019-07-11 12:43:20 +01006012 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006013 return( 0 );
6014}
6015
6016#endif /* MBEDTLS_SSL_PROTO_DTLS */
6017
Hanno Beckere74d5562018-08-15 14:26:08 +01006018static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006019{
6020 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006021 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006022
Hanno Becker5f066e72018-08-16 14:56:31 +01006023#if defined(MBEDTLS_SSL_PROTO_DTLS)
6024 /* We might have buffered a future record; if so,
6025 * and if the epoch matches now, load it.
6026 * On success, this call will set ssl->in_left to
6027 * the length of the buffered record, so that
6028 * the calls to ssl_fetch_input() below will
6029 * essentially be no-ops. */
6030 ret = ssl_load_buffered_record( ssl );
6031 if( ret != 0 )
6032 return( ret );
6033#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006034
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006035 /* Ensure that we have enough space available for the default form
6036 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6037 * with no space for CIDs counted in). */
6038 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6039 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006042 return( ret );
6043 }
6044
Hanno Beckere5e7e782019-07-11 12:29:35 +01006045 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6046 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006048#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006049 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006050 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006051 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6052 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006053 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006054 if( ret != 0 )
6055 return( ret );
6056
6057 /* Fall through to handling of unexpected records */
6058 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6059 }
6060
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006061 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6062 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006063#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006064 /* Reset in pointers to default state for TLS/DTLS records,
6065 * assuming no CID and no offset between record content and
6066 * record plaintext. */
6067 ssl_update_in_pointers( ssl );
6068
Hanno Becker7ae20e02019-07-12 08:33:49 +01006069 /* Setup internal message pointers from record structure. */
6070 ssl->in_msgtype = rec.type;
6071#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6072 ssl->in_len = ssl->in_cid + rec.cid_len;
6073#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6074 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6075 ssl->in_msglen = rec.data_len;
6076
Hanno Becker2fddd372019-07-10 14:37:41 +01006077 ret = ssl_check_client_reconnect( ssl );
6078 if( ret != 0 )
6079 return( ret );
6080#endif
6081
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006082 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006083 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006084
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6086 "(header)" ) );
6087 }
6088 else
6089 {
6090 /* Skip invalid record and the rest of the datagram */
6091 ssl->next_record_offset = 0;
6092 ssl->in_left = 0;
6093
6094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6095 "(header)" ) );
6096 }
6097
6098 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006099 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006100 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006101 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006102#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006103 {
6104 return( ret );
6105 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006106 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006108#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006109 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006110 {
Hanno Beckera8814792019-07-10 15:01:45 +01006111 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006112 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006113 if( ssl->next_record_offset < ssl->in_left )
6114 {
6115 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6116 }
6117 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006118 else
6119#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006120 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006121 /*
6122 * Fetch record contents from underlying transport.
6123 */
Hanno Beckera3175662019-07-11 12:50:29 +01006124 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006125 if( ret != 0 )
6126 {
6127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6128 return( ret );
6129 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006130
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006131 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006132 }
6133
6134 /*
6135 * Decrypt record contents.
6136 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006137
Hanno Beckerfdf66042019-07-11 13:07:45 +01006138 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006140#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006141 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006142 {
6143 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006144 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006145 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006146 /* Except when waiting for Finished as a bad mac here
6147 * probably means something went wrong in the handshake
6148 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6149 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6150 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6151 {
6152#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6153 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6154 {
6155 mbedtls_ssl_send_alert_message( ssl,
6156 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6157 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6158 }
6159#endif
6160 return( ret );
6161 }
6162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006163#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006164 if( ssl->conf->badmac_limit != 0 &&
6165 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6168 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006169 }
6170#endif
6171
Hanno Becker4a810fb2017-05-24 16:27:30 +01006172 /* As above, invalid records cause
6173 * dismissal of the whole datagram. */
6174
6175 ssl->next_record_offset = 0;
6176 ssl->in_left = 0;
6177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006179 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006180 }
6181
6182 return( ret );
6183 }
6184 else
6185#endif
6186 {
6187 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006188#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6189 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006191 mbedtls_ssl_send_alert_message( ssl,
6192 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6193 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006194 }
6195#endif
6196 return( ret );
6197 }
6198 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006199
Hanno Becker44d89b22019-07-12 09:40:44 +01006200
6201 /* Reset in pointers to default state for TLS/DTLS records,
6202 * assuming no CID and no offset between record content and
6203 * record plaintext. */
6204 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006205#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6206 ssl->in_len = ssl->in_cid + rec.cid_len;
6207#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6208 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006209
Hanno Becker8685c822019-07-12 09:37:30 +01006210 /* The record content type may change during decryption,
6211 * so re-read it. */
6212 ssl->in_msgtype = rec.type;
6213 /* Also update the input buffer, because unfortunately
6214 * the server-side ssl_parse_client_hello() reparses the
6215 * record header when receiving a ClientHello initiating
6216 * a renegotiation. */
6217 ssl->in_hdr[0] = rec.type;
6218 ssl->in_msg = rec.buf + rec.data_offset;
6219 ssl->in_msglen = rec.data_len;
6220 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6221 ssl->in_len[1] = (unsigned char)( rec.data_len );
6222
Simon Butcher99000142016-10-13 17:21:01 +01006223 return( 0 );
6224}
6225
6226int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6227{
6228 int ret;
6229
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006230 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006231 * Handle particular types of records
6232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006233 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006234 {
Simon Butcher99000142016-10-13 17:21:01 +01006235 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6236 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006237 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006238 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006239 }
6240
Hanno Beckere678eaa2018-08-21 14:57:46 +01006241 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006242 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006243 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006244 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6246 ssl->in_msglen ) );
6247 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006248 }
6249
Hanno Beckere678eaa2018-08-21 14:57:46 +01006250 if( ssl->in_msg[0] != 1 )
6251 {
6252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6253 ssl->in_msg[0] ) );
6254 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6255 }
6256
6257#if defined(MBEDTLS_SSL_PROTO_DTLS)
6258 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6259 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6260 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6261 {
6262 if( ssl->handshake == NULL )
6263 {
6264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6265 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6266 }
6267
6268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6269 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6270 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006271#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006272 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006274 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006275 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006276 if( ssl->in_msglen != 2 )
6277 {
6278 /* Note: Standard allows for more than one 2 byte alert
6279 to be packed in a single message, but Mbed TLS doesn't
6280 currently support this. */
6281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6282 ssl->in_msglen ) );
6283 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6284 }
6285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006287 ssl->in_msg[0], ssl->in_msg[1] ) );
6288
6289 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006290 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006292 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006295 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006297 }
6298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6300 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6303 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006304 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006305
6306#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6307 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6308 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6309 {
Hanno Becker90333da2017-10-10 11:27:13 +01006310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006311 /* Will be handled when trying to parse ServerHello */
6312 return( 0 );
6313 }
6314#endif
6315
6316#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6317 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6318 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6319 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6320 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6321 {
6322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6323 /* Will be handled in mbedtls_ssl_parse_certificate() */
6324 return( 0 );
6325 }
6326#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6327
6328 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006329 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006330 }
6331
Hanno Beckerc76c6192017-06-06 10:03:17 +01006332#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006333 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006334 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006335 /* Drop unexpected ApplicationData records,
6336 * except at the beginning of renegotiations */
6337 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6338 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6339#if defined(MBEDTLS_SSL_RENEGOTIATION)
6340 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6341 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006342#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006343 )
6344 {
6345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6346 return( MBEDTLS_ERR_SSL_NON_FATAL );
6347 }
6348
6349 if( ssl->handshake != NULL &&
6350 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6351 {
6352 ssl_handshake_wrapup_free_hs_transform( ssl );
6353 }
6354 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006355#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006356
Paul Bakker5121ce52009-01-03 21:22:43 +00006357 return( 0 );
6358}
6359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006360int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006361{
6362 int ret;
6363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6365 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6366 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006367 {
6368 return( ret );
6369 }
6370
6371 return( 0 );
6372}
6373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006374int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006375 unsigned char level,
6376 unsigned char message )
6377{
6378 int ret;
6379
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006380 if( ssl == NULL || ssl->conf == NULL )
6381 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006384 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006387 ssl->out_msglen = 2;
6388 ssl->out_msg[0] = level;
6389 ssl->out_msg[1] = message;
6390
Hanno Becker67bc7c32018-08-06 11:33:50 +01006391 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006393 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006394 return( ret );
6395 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006397
6398 return( 0 );
6399}
6400
Hanno Beckerb9d44792019-02-08 07:19:04 +00006401#if defined(MBEDTLS_X509_CRT_PARSE_C)
6402static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6403{
6404#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6405 if( session->peer_cert != NULL )
6406 {
6407 mbedtls_x509_crt_free( session->peer_cert );
6408 mbedtls_free( session->peer_cert );
6409 session->peer_cert = NULL;
6410 }
6411#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6412 if( session->peer_cert_digest != NULL )
6413 {
6414 /* Zeroization is not necessary. */
6415 mbedtls_free( session->peer_cert_digest );
6416 session->peer_cert_digest = NULL;
6417 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6418 session->peer_cert_digest_len = 0;
6419 }
6420#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6421}
6422#endif /* MBEDTLS_X509_CRT_PARSE_C */
6423
Paul Bakker5121ce52009-01-03 21:22:43 +00006424/*
6425 * Handshake functions
6426 */
Hanno Becker21489932019-02-05 13:20:55 +00006427#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006428/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006429int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006430{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006431 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6432 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006435
Hanno Becker7177a882019-02-05 13:36:46 +00006436 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006439 ssl->state++;
6440 return( 0 );
6441 }
6442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6444 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006445}
6446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006448{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006449 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6450 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006453
Hanno Becker7177a882019-02-05 13:36:46 +00006454 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006457 ssl->state++;
6458 return( 0 );
6459 }
6460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6462 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006463}
Gilles Peskinef9828522017-05-03 12:28:43 +02006464
Hanno Becker21489932019-02-05 13:20:55 +00006465#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006466/* Some certificate support -> implement write and parse */
6467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006468int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006469{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006470 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006471 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006473 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6474 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006477
Hanno Becker7177a882019-02-05 13:36:46 +00006478 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006481 ssl->state++;
6482 return( 0 );
6483 }
6484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006485#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006486 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006487 {
6488 if( ssl->client_auth == 0 )
6489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006491 ssl->state++;
6492 return( 0 );
6493 }
6494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006495#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006496 /*
6497 * If using SSLv3 and got no cert, send an Alert message
6498 * (otherwise an empty Certificate message will be sent).
6499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6501 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006502 {
6503 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006504 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6505 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6506 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006509 goto write_msg;
6510 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006512 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513#endif /* MBEDTLS_SSL_CLI_C */
6514#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006515 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006517 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6520 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006521 }
6522 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006523#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006526
6527 /*
6528 * 0 . 0 handshake type
6529 * 1 . 3 handshake length
6530 * 4 . 6 length of all certs
6531 * 7 . 9 length of cert. 1
6532 * 10 . n-1 peer certificate
6533 * n . n+2 length of cert. 2
6534 * n+3 . ... upper level cert, etc.
6535 */
6536 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006537 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006538
Paul Bakker29087132010-03-21 21:03:34 +00006539 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006540 {
6541 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006542 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006545 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006547 }
6548
6549 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6550 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6551 ssl->out_msg[i + 2] = (unsigned char)( n );
6552
6553 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6554 i += n; crt = crt->next;
6555 }
6556
6557 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6558 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6559 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6560
6561 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006562 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6563 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006564
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006565#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006566write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006567#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006568
6569 ssl->state++;
6570
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006571 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006572 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006574 return( ret );
6575 }
6576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006578
Paul Bakkered27a042013-04-18 22:46:23 +02006579 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006580}
6581
Hanno Becker84879e32019-01-31 07:44:03 +00006582#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006583
6584#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006585static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6586 unsigned char *crt_buf,
6587 size_t crt_buf_len )
6588{
6589 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6590
6591 if( peer_crt == NULL )
6592 return( -1 );
6593
6594 if( peer_crt->raw.len != crt_buf_len )
6595 return( -1 );
6596
Hanno Becker46f34d02019-02-08 14:00:04 +00006597 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006598}
Hanno Becker177475a2019-02-05 17:02:46 +00006599#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6600static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6601 unsigned char *crt_buf,
6602 size_t crt_buf_len )
6603{
6604 int ret;
6605 unsigned char const * const peer_cert_digest =
6606 ssl->session->peer_cert_digest;
6607 mbedtls_md_type_t const peer_cert_digest_type =
6608 ssl->session->peer_cert_digest_type;
6609 mbedtls_md_info_t const * const digest_info =
6610 mbedtls_md_info_from_type( peer_cert_digest_type );
6611 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6612 size_t digest_len;
6613
6614 if( peer_cert_digest == NULL || digest_info == NULL )
6615 return( -1 );
6616
6617 digest_len = mbedtls_md_get_size( digest_info );
6618 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6619 return( -1 );
6620
6621 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6622 if( ret != 0 )
6623 return( -1 );
6624
6625 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6626}
6627#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006628#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006629
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006630/*
6631 * Once the certificate message is read, parse it into a cert chain and
6632 * perform basic checks, but leave actual verification to the caller
6633 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006634static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6635 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006636{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006637 int ret;
6638#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6639 int crt_cnt=0;
6640#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006641 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006642 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006647 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6648 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006650 }
6651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6653 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006656 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6657 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006658 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006659 }
6660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006661 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006662
Paul Bakker5121ce52009-01-03 21:22:43 +00006663 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006664 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006665 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006666 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006667
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006668 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006669 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006672 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6673 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006675 }
6676
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006677 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6678 i += 3;
6679
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006680 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006681 while( i < ssl->in_hslen )
6682 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006683 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006684 if ( i + 3 > ssl->in_hslen ) {
6685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006686 mbedtls_ssl_send_alert_message( ssl,
6687 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6688 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006689 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6690 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006691 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6692 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006693 if( ssl->in_msg[i] != 0 )
6694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006696 mbedtls_ssl_send_alert_message( ssl,
6697 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6698 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006700 }
6701
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006702 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006703 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6704 | (unsigned int) ssl->in_msg[i + 2];
6705 i += 3;
6706
6707 if( n < 128 || i + n > ssl->in_hslen )
6708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006710 mbedtls_ssl_send_alert_message( ssl,
6711 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6712 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006714 }
6715
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006716 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006717#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6718 if( crt_cnt++ == 0 &&
6719 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6720 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006721 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006722 /* During client-side renegotiation, check that the server's
6723 * end-CRTs hasn't changed compared to the initial handshake,
6724 * mitigating the triple handshake attack. On success, reuse
6725 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006726 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6727 if( ssl_check_peer_crt_unchanged( ssl,
6728 &ssl->in_msg[i],
6729 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006730 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6732 mbedtls_ssl_send_alert_message( ssl,
6733 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6734 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6735 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006736 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006737
6738 /* Now we can safely free the original chain. */
6739 ssl_clear_peer_cert( ssl->session );
6740 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006741#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6742
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006743 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006744#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006745 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006746#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006747 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006748 * it in-place from the input buffer instead of making a copy. */
6749 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6750#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006751 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006752 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006753 case 0: /*ok*/
6754 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6755 /* Ignore certificate with an unknown algorithm: maybe a
6756 prior certificate was already trusted. */
6757 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006758
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006759 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6760 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6761 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006762
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006763 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6764 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6765 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006766
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006767 default:
6768 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6769 crt_parse_der_failed:
6770 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6771 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6772 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006773 }
6774
6775 i += n;
6776 }
6777
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006778 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006779 return( 0 );
6780}
6781
Hanno Becker4a55f632019-02-05 12:49:06 +00006782#if defined(MBEDTLS_SSL_SRV_C)
6783static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6784{
6785 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6786 return( -1 );
6787
6788#if defined(MBEDTLS_SSL_PROTO_SSL3)
6789 /*
6790 * Check if the client sent an empty certificate
6791 */
6792 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6793 {
6794 if( ssl->in_msglen == 2 &&
6795 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6796 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6797 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6798 {
6799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6800 return( 0 );
6801 }
6802
6803 return( -1 );
6804 }
6805#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6806
6807#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6808 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6809 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6810 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6811 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6812 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6813 {
6814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6815 return( 0 );
6816 }
6817
6818 return( -1 );
6819#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6820 MBEDTLS_SSL_PROTO_TLS1_2 */
6821}
6822#endif /* MBEDTLS_SSL_SRV_C */
6823
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006824/* Check if a certificate message is expected.
6825 * Return either
6826 * - SSL_CERTIFICATE_EXPECTED, or
6827 * - SSL_CERTIFICATE_SKIP
6828 * indicating whether a Certificate message is expected or not.
6829 */
6830#define SSL_CERTIFICATE_EXPECTED 0
6831#define SSL_CERTIFICATE_SKIP 1
6832static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6833 int authmode )
6834{
6835 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006836 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006837
6838 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6839 return( SSL_CERTIFICATE_SKIP );
6840
6841#if defined(MBEDTLS_SSL_SRV_C)
6842 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6843 {
6844 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6845 return( SSL_CERTIFICATE_SKIP );
6846
6847 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6848 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006849 ssl->session_negotiate->verify_result =
6850 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6851 return( SSL_CERTIFICATE_SKIP );
6852 }
6853 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006854#else
6855 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006856#endif /* MBEDTLS_SSL_SRV_C */
6857
6858 return( SSL_CERTIFICATE_EXPECTED );
6859}
6860
Hanno Becker68636192019-02-05 14:36:34 +00006861static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6862 int authmode,
6863 mbedtls_x509_crt *chain,
6864 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006865{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006866 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006867 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006868 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006869 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006870
Hanno Becker8927c832019-04-03 12:52:50 +01006871 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6872 void *p_vrfy;
6873
Hanno Becker68636192019-02-05 14:36:34 +00006874 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6875 return( 0 );
6876
Hanno Becker8927c832019-04-03 12:52:50 +01006877 if( ssl->f_vrfy != NULL )
6878 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006879 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006880 f_vrfy = ssl->f_vrfy;
6881 p_vrfy = ssl->p_vrfy;
6882 }
6883 else
6884 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006885 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006886 f_vrfy = ssl->conf->f_vrfy;
6887 p_vrfy = ssl->conf->p_vrfy;
6888 }
6889
Hanno Becker68636192019-02-05 14:36:34 +00006890 /*
6891 * Main check: verify certificate
6892 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006893#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6894 if( ssl->conf->f_ca_cb != NULL )
6895 {
6896 ((void) rs_ctx);
6897 have_ca_chain = 1;
6898
6899 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006900 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006901 chain,
6902 ssl->conf->f_ca_cb,
6903 ssl->conf->p_ca_cb,
6904 ssl->conf->cert_profile,
6905 ssl->hostname,
6906 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006907 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006908 }
6909 else
6910#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6911 {
6912 mbedtls_x509_crt *ca_chain;
6913 mbedtls_x509_crl *ca_crl;
6914
6915#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6916 if( ssl->handshake->sni_ca_chain != NULL )
6917 {
6918 ca_chain = ssl->handshake->sni_ca_chain;
6919 ca_crl = ssl->handshake->sni_ca_crl;
6920 }
6921 else
6922#endif
6923 {
6924 ca_chain = ssl->conf->ca_chain;
6925 ca_crl = ssl->conf->ca_crl;
6926 }
6927
6928 if( ca_chain != NULL )
6929 have_ca_chain = 1;
6930
6931 ret = mbedtls_x509_crt_verify_restartable(
6932 chain,
6933 ca_chain, ca_crl,
6934 ssl->conf->cert_profile,
6935 ssl->hostname,
6936 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006937 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006938 }
Hanno Becker68636192019-02-05 14:36:34 +00006939
6940 if( ret != 0 )
6941 {
6942 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6943 }
6944
6945#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6946 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6947 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6948#endif
6949
6950 /*
6951 * Secondary checks: always done, but change 'ret' only if it was 0
6952 */
6953
6954#if defined(MBEDTLS_ECP_C)
6955 {
6956 const mbedtls_pk_context *pk = &chain->pk;
6957
6958 /* If certificate uses an EC key, make sure the curve is OK */
6959 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6960 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6961 {
6962 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6963
6964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6965 if( ret == 0 )
6966 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6967 }
6968 }
6969#endif /* MBEDTLS_ECP_C */
6970
6971 if( mbedtls_ssl_check_cert_usage( chain,
6972 ciphersuite_info,
6973 ! ssl->conf->endpoint,
6974 &ssl->session_negotiate->verify_result ) != 0 )
6975 {
6976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6977 if( ret == 0 )
6978 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6979 }
6980
6981 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6982 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6983 * with details encoded in the verification flags. All other kinds
6984 * of error codes, including those from the user provided f_vrfy
6985 * functions, are treated as fatal and lead to a failure of
6986 * ssl_parse_certificate even if verification was optional. */
6987 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6988 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6989 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6990 {
6991 ret = 0;
6992 }
6993
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006994 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006995 {
6996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6997 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6998 }
6999
7000 if( ret != 0 )
7001 {
7002 uint8_t alert;
7003
7004 /* The certificate may have been rejected for several reasons.
7005 Pick one and send the corresponding alert. Which alert to send
7006 may be a subject of debate in some cases. */
7007 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7008 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7009 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7010 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7011 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7012 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7013 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7014 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7015 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7016 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7017 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7018 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7019 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7020 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7021 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7022 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7023 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7024 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7025 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7026 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7027 else
7028 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7029 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7030 alert );
7031 }
7032
7033#if defined(MBEDTLS_DEBUG_C)
7034 if( ssl->session_negotiate->verify_result != 0 )
7035 {
7036 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7037 ssl->session_negotiate->verify_result ) );
7038 }
7039 else
7040 {
7041 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7042 }
7043#endif /* MBEDTLS_DEBUG_C */
7044
7045 return( ret );
7046}
7047
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007048#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7049static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7050 unsigned char *start, size_t len )
7051{
7052 int ret;
7053 /* Remember digest of the peer's end-CRT. */
7054 ssl->session_negotiate->peer_cert_digest =
7055 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7056 if( ssl->session_negotiate->peer_cert_digest == NULL )
7057 {
7058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7059 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7060 mbedtls_ssl_send_alert_message( ssl,
7061 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7062 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7063
7064 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7065 }
7066
7067 ret = mbedtls_md( mbedtls_md_info_from_type(
7068 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7069 start, len,
7070 ssl->session_negotiate->peer_cert_digest );
7071
7072 ssl->session_negotiate->peer_cert_digest_type =
7073 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7074 ssl->session_negotiate->peer_cert_digest_len =
7075 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7076
7077 return( ret );
7078}
7079
7080static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7081 unsigned char *start, size_t len )
7082{
7083 unsigned char *end = start + len;
7084 int ret;
7085
7086 /* Make a copy of the peer's raw public key. */
7087 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7088 ret = mbedtls_pk_parse_subpubkey( &start, end,
7089 &ssl->handshake->peer_pubkey );
7090 if( ret != 0 )
7091 {
7092 /* We should have parsed the public key before. */
7093 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7094 }
7095
7096 return( 0 );
7097}
7098#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7099
Hanno Becker68636192019-02-05 14:36:34 +00007100int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7101{
7102 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007103 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007104#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7105 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7106 ? ssl->handshake->sni_authmode
7107 : ssl->conf->authmode;
7108#else
7109 const int authmode = ssl->conf->authmode;
7110#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007111 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007112 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007113
7114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7115
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007116 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7117 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007118 {
7119 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007120 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007121 }
7122
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007123#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7124 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007125 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007126 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007127 chain = ssl->handshake->ecrs_peer_cert;
7128 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007129 goto crt_verify;
7130 }
7131#endif
7132
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007133 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007134 {
7135 /* mbedtls_ssl_read_record may have sent an alert already. We
7136 let it decide whether to alert. */
7137 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007138 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007139 }
7140
Hanno Becker4a55f632019-02-05 12:49:06 +00007141#if defined(MBEDTLS_SSL_SRV_C)
7142 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7143 {
7144 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007145
7146 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007147 ret = 0;
7148 else
7149 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007150
Hanno Becker6bdfab22019-02-05 13:11:17 +00007151 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007152 }
7153#endif /* MBEDTLS_SSL_SRV_C */
7154
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007155 /* Clear existing peer CRT structure in case we tried to
7156 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007157 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007158
7159 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7160 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007161 {
7162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7163 sizeof( mbedtls_x509_crt ) ) );
7164 mbedtls_ssl_send_alert_message( ssl,
7165 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7166 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007167
Hanno Becker3dad3112019-02-05 17:19:52 +00007168 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7169 goto exit;
7170 }
7171 mbedtls_x509_crt_init( chain );
7172
7173 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007174 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007175 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007176
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007177#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7178 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007179 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007180
7181crt_verify:
7182 if( ssl->handshake->ecrs_enabled)
7183 rs_ctx = &ssl->handshake->ecrs_ctx;
7184#endif
7185
Hanno Becker68636192019-02-05 14:36:34 +00007186 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007187 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007188 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007189 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007190
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007191#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007192 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007193 unsigned char *crt_start, *pk_start;
7194 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007195
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007196 /* We parse the CRT chain without copying, so
7197 * these pointers point into the input buffer,
7198 * and are hence still valid after freeing the
7199 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007200
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007201 crt_start = chain->raw.p;
7202 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007203
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007204 pk_start = chain->pk_raw.p;
7205 pk_len = chain->pk_raw.len;
7206
7207 /* Free the CRT structures before computing
7208 * digest and copying the peer's public key. */
7209 mbedtls_x509_crt_free( chain );
7210 mbedtls_free( chain );
7211 chain = NULL;
7212
7213 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007214 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007215 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007216
7217 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7218 if( ret != 0 )
7219 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007220 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007221#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7222 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007223 ssl->session_negotiate->peer_cert = chain;
7224 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007225#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007228
Hanno Becker6bdfab22019-02-05 13:11:17 +00007229exit:
7230
Hanno Becker3dad3112019-02-05 17:19:52 +00007231 if( ret == 0 )
7232 ssl->state++;
7233
7234#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7235 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7236 {
7237 ssl->handshake->ecrs_peer_cert = chain;
7238 chain = NULL;
7239 }
7240#endif
7241
7242 if( chain != NULL )
7243 {
7244 mbedtls_x509_crt_free( chain );
7245 mbedtls_free( chain );
7246 }
7247
Paul Bakker5121ce52009-01-03 21:22:43 +00007248 return( ret );
7249}
Hanno Becker21489932019-02-05 13:20:55 +00007250#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007253{
7254 int ret;
7255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007259 ssl->out_msglen = 1;
7260 ssl->out_msg[0] = 1;
7261
Paul Bakker5121ce52009-01-03 21:22:43 +00007262 ssl->state++;
7263
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007264 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007265 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007267 return( ret );
7268 }
7269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007271
7272 return( 0 );
7273}
7274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007276{
7277 int ret;
7278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007280
Hanno Becker327c93b2018-08-15 13:56:18 +01007281 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007284 return( ret );
7285 }
7286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007290 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7291 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007292 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007293 }
7294
Hanno Beckere678eaa2018-08-21 14:57:46 +01007295 /* CCS records are only accepted if they have length 1 and content '1',
7296 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007297
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007298 /*
7299 * Switch to our negotiated transform and session parameters for inbound
7300 * data.
7301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007303 ssl->transform_in = ssl->transform_negotiate;
7304 ssl->session_in = ssl->session_negotiate;
7305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007307 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007310 ssl_dtls_replay_reset( ssl );
7311#endif
7312
7313 /* Increment epoch */
7314 if( ++ssl->in_epoch == 0 )
7315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007317 /* This is highly unlikely to happen for legitimate reasons, so
7318 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007320 }
7321 }
7322 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007323#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007324 memset( ssl->in_ctr, 0, 8 );
7325
Hanno Becker79594fd2019-05-08 09:38:41 +01007326 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007328#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7329 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007332 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007334 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7335 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007337 }
7338 }
7339#endif
7340
Paul Bakker5121ce52009-01-03 21:22:43 +00007341 ssl->state++;
7342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007344
7345 return( 0 );
7346}
7347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7349 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007350{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007351 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007353#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7354 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7355 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007356 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007357 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007358#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7360#if defined(MBEDTLS_SHA512_C)
7361 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007362 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7363 else
7364#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007365#if defined(MBEDTLS_SHA256_C)
7366 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007367 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007368 else
7369#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007373 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007374 }
Paul Bakker380da532012-04-18 16:10:25 +00007375}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007378{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7380 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007381 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7382 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007383#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7385#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007386#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007387 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007388 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7389#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007390 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007391#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007392#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007394#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007395 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007396 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007397#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007398 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007399#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007400#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007402}
7403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007405 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007406{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7408 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007409 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7410 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007411#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007412#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7413#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007414#if defined(MBEDTLS_USE_PSA_CRYPTO)
7415 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7416#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007417 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007418#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007419#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007421#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007422 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007423#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007424 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007425#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007426#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007428}
7429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007430#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7431 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7432static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007433 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007434{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007435 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7436 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007437}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007438#endif
Paul Bakker380da532012-04-18 16:10:25 +00007439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7441#if defined(MBEDTLS_SHA256_C)
7442static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007443 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007444{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007445#if defined(MBEDTLS_USE_PSA_CRYPTO)
7446 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7447#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007448 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007449#endif
Paul Bakker380da532012-04-18 16:10:25 +00007450}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007451#endif
Paul Bakker380da532012-04-18 16:10:25 +00007452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007453#if defined(MBEDTLS_SHA512_C)
7454static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007455 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007456{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007457#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007458 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007459#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007460 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007461#endif
Paul Bakker380da532012-04-18 16:10:25 +00007462}
Paul Bakker769075d2012-11-24 11:26:46 +01007463#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007467static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007469{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007470 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007471 mbedtls_md5_context md5;
7472 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007473
Paul Bakker5121ce52009-01-03 21:22:43 +00007474 unsigned char padbuf[48];
7475 unsigned char md5sum[16];
7476 unsigned char sha1sum[20];
7477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007478 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007479 if( !session )
7480 session = ssl->session;
7481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007483
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007484 mbedtls_md5_init( &md5 );
7485 mbedtls_sha1_init( &sha1 );
7486
7487 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7488 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007489
7490 /*
7491 * SSLv3:
7492 * hash =
7493 * MD5( master + pad2 +
7494 * MD5( handshake + sender + master + pad1 ) )
7495 * + SHA1( master + pad2 +
7496 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007497 */
7498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007500 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7501 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007502#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007505 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7506 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007507#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007509 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007510 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007511
Paul Bakker1ef83d62012-04-11 12:09:53 +00007512 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007513
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007514 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7515 mbedtls_md5_update_ret( &md5, session->master, 48 );
7516 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7517 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007518
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007519 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7520 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7521 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7522 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007523
Paul Bakker1ef83d62012-04-11 12:09:53 +00007524 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007525
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007526 mbedtls_md5_starts_ret( &md5 );
7527 mbedtls_md5_update_ret( &md5, session->master, 48 );
7528 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7529 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7530 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007531
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007532 mbedtls_sha1_starts_ret( &sha1 );
7533 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7534 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7535 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7536 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007539
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007540 mbedtls_md5_free( &md5 );
7541 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007542
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007543 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7544 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7545 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007548}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007552static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007554{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007555 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007556 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007557 mbedtls_md5_context md5;
7558 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007559 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007561 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007562 if( !session )
7563 session = ssl->session;
7564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007566
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007567 mbedtls_md5_init( &md5 );
7568 mbedtls_sha1_init( &sha1 );
7569
7570 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7571 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007572
Paul Bakker1ef83d62012-04-11 12:09:53 +00007573 /*
7574 * TLSv1:
7575 * hash = PRF( master, finished_label,
7576 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7577 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007580 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7581 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007582#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007585 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7586 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007587#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007590 ? "client finished"
7591 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007592
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007593 mbedtls_md5_finish_ret( &md5, padbuf );
7594 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007595
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007596 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007597 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007600
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007601 mbedtls_md5_free( &md5 );
7602 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007603
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007604 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007607}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7611#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007612static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007613 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007614{
7615 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007616 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007617 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007618#if defined(MBEDTLS_USE_PSA_CRYPTO)
7619 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007620 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007621 psa_status_t status;
7622#else
7623 mbedtls_sha256_context sha256;
7624#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007627 if( !session )
7628 session = ssl->session;
7629
Andrzej Kurekeb342242019-01-29 09:14:33 -05007630 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7631 ? "client finished"
7632 : "server finished";
7633
7634#if defined(MBEDTLS_USE_PSA_CRYPTO)
7635 sha256_psa = psa_hash_operation_init();
7636
7637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7638
7639 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7640 if( status != PSA_SUCCESS )
7641 {
7642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7643 return;
7644 }
7645
7646 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7647 if( status != PSA_SUCCESS )
7648 {
7649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7650 return;
7651 }
7652 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7653#else
7654
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007655 mbedtls_sha256_init( &sha256 );
7656
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007658
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007659 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007660
7661 /*
7662 * TLSv1.2:
7663 * hash = PRF( master, finished_label,
7664 * Hash( handshake ) )[0.11]
7665 */
7666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007667#if !defined(MBEDTLS_SHA256_ALT)
7668 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007669 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007670#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007671
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007672 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007673 mbedtls_sha256_free( &sha256 );
7674#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007675
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007676 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007677 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007679 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007680
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007681 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007684}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007685#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007688static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007690{
7691 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007692 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007693 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007694#if defined(MBEDTLS_USE_PSA_CRYPTO)
7695 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007696 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007697 psa_status_t status;
7698#else
7699 mbedtls_sha512_context sha512;
7700#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007703 if( !session )
7704 session = ssl->session;
7705
Andrzej Kurekeb342242019-01-29 09:14:33 -05007706 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7707 ? "client finished"
7708 : "server finished";
7709
7710#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007711 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007712
7713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7714
Andrzej Kurek972fba52019-01-30 03:29:12 -05007715 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007716 if( status != PSA_SUCCESS )
7717 {
7718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7719 return;
7720 }
7721
Andrzej Kurek972fba52019-01-30 03:29:12 -05007722 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007723 if( status != PSA_SUCCESS )
7724 {
7725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7726 return;
7727 }
7728 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7729#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007730 mbedtls_sha512_init( &sha512 );
7731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007733
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007734 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +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_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007743 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7744 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007745#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007746
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007747 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007748 mbedtls_sha512_free( &sha512 );
7749#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +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, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007754 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007755
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007756 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007759}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760#endif /* MBEDTLS_SHA512_C */
7761#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007764{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007766
7767 /*
7768 * Free our handshake params
7769 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007770 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007772 ssl->handshake = NULL;
7773
7774 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007775 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007776 */
7777 if( ssl->transform )
7778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007779 mbedtls_ssl_transform_free( ssl->transform );
7780 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007781 }
7782 ssl->transform = ssl->transform_negotiate;
7783 ssl->transform_negotiate = NULL;
7784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007785 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007786}
7787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007789{
7790 int resume = ssl->handshake->resume;
7791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007792 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794#if defined(MBEDTLS_SSL_RENEGOTIATION)
7795 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007797 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007798 ssl->renego_records_seen = 0;
7799 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007800#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007801
7802 /*
7803 * Free the previous session and switch in the current one
7804 */
Paul Bakker0a597072012-09-25 21:55:46 +00007805 if( ssl->session )
7806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007808 /* RFC 7366 3.1: keep the EtM state */
7809 ssl->session_negotiate->encrypt_then_mac =
7810 ssl->session->encrypt_then_mac;
7811#endif
7812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813 mbedtls_ssl_session_free( ssl->session );
7814 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007815 }
7816 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007817 ssl->session_negotiate = NULL;
7818
Paul Bakker0a597072012-09-25 21:55:46 +00007819 /*
7820 * Add cache entry
7821 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007822 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007823 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007824 resume == 0 )
7825 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007826 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007828 }
Paul Bakker0a597072012-09-25 21:55:46 +00007829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007831 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007832 ssl->handshake->flight != NULL )
7833 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007834 /* Cancel handshake timer */
7835 ssl_set_timer( ssl, 0 );
7836
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007837 /* Keep last flight around in case we need to resend it:
7838 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007839 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007840 }
7841 else
7842#endif
7843 ssl_handshake_wrapup_free_hs_transform( ssl );
7844
Paul Bakker48916f92012-09-16 19:57:18 +00007845 ssl->state++;
7846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007848}
7849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007850int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007851{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007852 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007855
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007856 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007857
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007858 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007859
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007860 /*
7861 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7862 * may define some other value. Currently (early 2016), no defined
7863 * ciphersuite does this (and this is unlikely to change as activity has
7864 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007866 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007869 ssl->verify_data_len = hash_len;
7870 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007871#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007872
Paul Bakker5121ce52009-01-03 21:22:43 +00007873 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7875 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007876
7877 /*
7878 * In case of session resuming, invert the client and server
7879 * ChangeCipherSpec messages order.
7880 */
Paul Bakker0a597072012-09-25 21:55:46 +00007881 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007883#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007884 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007886#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007888 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007890#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007891 }
7892 else
7893 ssl->state++;
7894
Paul Bakker48916f92012-09-16 19:57:18 +00007895 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007896 * Switch to our negotiated transform and session parameters for outbound
7897 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007898 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007902 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007903 {
7904 unsigned char i;
7905
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007906 /* Remember current epoch settings for resending */
7907 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007908 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007909
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007910 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007911 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007912
7913 /* Increment epoch */
7914 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007915 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007916 break;
7917
7918 /* The loop goes to its end iff the counter is wrapping */
7919 if( i == 0 )
7920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7922 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007923 }
7924 }
7925 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007927 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007928
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007929 ssl->transform_out = ssl->transform_negotiate;
7930 ssl->session_out = ssl->session_negotiate;
7931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7933 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7938 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007939 }
7940 }
7941#endif
7942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007944 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007946#endif
7947
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007948 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007949 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007950 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007951 return( ret );
7952 }
7953
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007954#if defined(MBEDTLS_SSL_PROTO_DTLS)
7955 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7956 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7957 {
7958 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7959 return( ret );
7960 }
7961#endif
7962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007964
7965 return( 0 );
7966}
7967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007969#define SSL_MAX_HASH_LEN 36
7970#else
7971#define SSL_MAX_HASH_LEN 12
7972#endif
7973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007975{
Paul Bakker23986e52011-04-24 08:57:21 +00007976 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007977 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007978 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007981
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007982 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007983
Hanno Becker327c93b2018-08-15 13:56:18 +01007984 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007987 return( ret );
7988 }
7989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007990 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007993 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7994 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007996 }
7997
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007998 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999#if defined(MBEDTLS_SSL_PROTO_SSL3)
8000 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008001 hash_len = 36;
8002 else
8003#endif
8004 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008006 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8007 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008010 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8011 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008013 }
8014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008015 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008016 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008019 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8020 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008021 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008022 }
8023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008024#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008025 ssl->verify_data_len = hash_len;
8026 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008027#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008028
Paul Bakker0a597072012-09-25 21:55:46 +00008029 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008031#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008032 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008033 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008035#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008036 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008038#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008039 }
8040 else
8041 ssl->state++;
8042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008043#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008044 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008045 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008046#endif
8047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008049
8050 return( 0 );
8051}
8052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008053static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008054{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008055 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008057#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8058 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8059 mbedtls_md5_init( &handshake->fin_md5 );
8060 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008061 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8062 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008063#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008064#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8065#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008066#if defined(MBEDTLS_USE_PSA_CRYPTO)
8067 handshake->fin_sha256_psa = psa_hash_operation_init();
8068 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8069#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008070 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008071 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008072#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008073#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008074#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008075#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008076 handshake->fin_sha384_psa = psa_hash_operation_init();
8077 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008078#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008079 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008080 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008081#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008082#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008083#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008084
8085 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008086
8087#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8088 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8089 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8090#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008092#if defined(MBEDTLS_DHM_C)
8093 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008094#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008095#if defined(MBEDTLS_ECDH_C)
8096 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008097#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008098#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008099 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008100#if defined(MBEDTLS_SSL_CLI_C)
8101 handshake->ecjpake_cache = NULL;
8102 handshake->ecjpake_cache_len = 0;
8103#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008104#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008105
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008106#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008107 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008108#endif
8109
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008110#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8111 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8112#endif
Hanno Becker75173122019-02-06 16:18:31 +00008113
8114#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8115 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8116 mbedtls_pk_init( &handshake->peer_pubkey );
8117#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008118}
8119
Hanno Beckera18d1322018-01-03 14:27:32 +00008120void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008122 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008124 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8125 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008126
Hanno Beckerd56ed242018-01-03 15:32:51 +00008127#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008128 mbedtls_md_init( &transform->md_ctx_enc );
8129 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008130#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008131}
8132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008133void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008134{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008135 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008136}
8137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008139{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008140 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008141 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008142 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008143 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008144 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008145 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008146 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008147
8148 /*
8149 * Either the pointers are now NULL or cleared properly and can be freed.
8150 * Now allocate missing structures.
8151 */
8152 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008153 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008154 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008155 }
Paul Bakker48916f92012-09-16 19:57:18 +00008156
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008157 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008158 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008159 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008160 }
Paul Bakker48916f92012-09-16 19:57:18 +00008161
Paul Bakker82788fb2014-10-20 13:59:19 +02008162 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008163 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008164 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008165 }
Paul Bakker48916f92012-09-16 19:57:18 +00008166
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008167 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008168 if( ssl->handshake == NULL ||
8169 ssl->transform_negotiate == NULL ||
8170 ssl->session_negotiate == NULL )
8171 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008174 mbedtls_free( ssl->handshake );
8175 mbedtls_free( ssl->transform_negotiate );
8176 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008177
8178 ssl->handshake = NULL;
8179 ssl->transform_negotiate = NULL;
8180 ssl->session_negotiate = NULL;
8181
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008182 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008183 }
8184
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008185 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008187 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008188 ssl_handshake_params_init( ssl->handshake );
8189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008191 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8192 {
8193 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008194
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008195 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8196 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8197 else
8198 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008199
8200 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008201 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008202#endif
8203
Paul Bakker48916f92012-09-16 19:57:18 +00008204 return( 0 );
8205}
8206
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008207#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008208/* Dummy cookie callbacks for defaults */
8209static int ssl_cookie_write_dummy( void *ctx,
8210 unsigned char **p, unsigned char *end,
8211 const unsigned char *cli_id, size_t cli_id_len )
8212{
8213 ((void) ctx);
8214 ((void) p);
8215 ((void) end);
8216 ((void) cli_id);
8217 ((void) cli_id_len);
8218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008219 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008220}
8221
8222static int ssl_cookie_check_dummy( void *ctx,
8223 const unsigned char *cookie, size_t cookie_len,
8224 const unsigned char *cli_id, size_t cli_id_len )
8225{
8226 ((void) ctx);
8227 ((void) cookie);
8228 ((void) cookie_len);
8229 ((void) cli_id);
8230 ((void) cli_id_len);
8231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008232 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008233}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008234#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008235
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008236/* Once ssl->out_hdr as the address of the beginning of the
8237 * next outgoing record is set, deduce the other pointers.
8238 *
8239 * Note: For TLS, we save the implicit record sequence number
8240 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8241 * and the caller has to make sure there's space for this.
8242 */
8243
8244static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8245 mbedtls_ssl_transform *transform )
8246{
8247#if defined(MBEDTLS_SSL_PROTO_DTLS)
8248 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8249 {
8250 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008251#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008252 ssl->out_cid = ssl->out_ctr + 8;
8253 ssl->out_len = ssl->out_cid;
8254 if( transform != NULL )
8255 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008256#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008257 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008258#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008259 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008260 }
8261 else
8262#endif
8263 {
8264 ssl->out_ctr = ssl->out_hdr - 8;
8265 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008266#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008267 ssl->out_cid = ssl->out_len;
8268#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008269 ssl->out_iv = ssl->out_hdr + 5;
8270 }
8271
8272 /* Adjust out_msg to make space for explicit IV, if used. */
8273 if( transform != NULL &&
8274 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8275 {
8276 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8277 }
8278 else
8279 ssl->out_msg = ssl->out_iv;
8280}
8281
8282/* Once ssl->in_hdr as the address of the beginning of the
8283 * next incoming record is set, deduce the other pointers.
8284 *
8285 * Note: For TLS, we save the implicit record sequence number
8286 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8287 * and the caller has to make sure there's space for this.
8288 */
8289
Hanno Becker79594fd2019-05-08 09:38:41 +01008290static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008291{
Hanno Becker79594fd2019-05-08 09:38:41 +01008292 /* This function sets the pointers to match the case
8293 * of unprotected TLS/DTLS records, with both ssl->in_iv
8294 * and ssl->in_msg pointing to the beginning of the record
8295 * content.
8296 *
8297 * When decrypting a protected record, ssl->in_msg
8298 * will be shifted to point to the beginning of the
8299 * record plaintext.
8300 */
8301
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008302#if defined(MBEDTLS_SSL_PROTO_DTLS)
8303 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8304 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008305 /* This sets the header pointers to match records
8306 * without CID. When we receive a record containing
8307 * a CID, the fields are shifted accordingly in
8308 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008309 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008310#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008311 ssl->in_cid = ssl->in_ctr + 8;
8312 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008313#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008314 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008315#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008316 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008317 }
8318 else
8319#endif
8320 {
8321 ssl->in_ctr = ssl->in_hdr - 8;
8322 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008323#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008324 ssl->in_cid = ssl->in_len;
8325#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008326 ssl->in_iv = ssl->in_hdr + 5;
8327 }
8328
Hanno Becker79594fd2019-05-08 09:38:41 +01008329 /* This will be adjusted at record decryption time. */
8330 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008331}
8332
Paul Bakker5121ce52009-01-03 21:22:43 +00008333/*
8334 * Initialize an SSL context
8335 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008336void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8337{
8338 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8339}
8340
8341/*
8342 * Setup an SSL context
8343 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008344
8345static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8346{
8347 /* Set the incoming and outgoing record pointers. */
8348#if defined(MBEDTLS_SSL_PROTO_DTLS)
8349 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8350 {
8351 ssl->out_hdr = ssl->out_buf;
8352 ssl->in_hdr = ssl->in_buf;
8353 }
8354 else
8355#endif /* MBEDTLS_SSL_PROTO_DTLS */
8356 {
8357 ssl->out_hdr = ssl->out_buf + 8;
8358 ssl->in_hdr = ssl->in_buf + 8;
8359 }
8360
8361 /* Derive other internal pointers. */
8362 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008363 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008364}
8365
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008366int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008367 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008368{
Paul Bakker48916f92012-09-16 19:57:18 +00008369 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008370
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008371 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008372
8373 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008374 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008375 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008376
8377 /* Set to NULL in case of an error condition */
8378 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008379
Angus Grattond8213d02016-05-25 20:56:48 +10008380 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8381 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008382 {
Angus Grattond8213d02016-05-25 20:56:48 +10008383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008384 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008385 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008386 }
8387
8388 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8389 if( ssl->out_buf == NULL )
8390 {
8391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008392 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008393 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008394 }
8395
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008396 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008397
Paul Bakker48916f92012-09-16 19:57:18 +00008398 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008399 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008400
8401 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008402
8403error:
8404 mbedtls_free( ssl->in_buf );
8405 mbedtls_free( ssl->out_buf );
8406
8407 ssl->conf = NULL;
8408
8409 ssl->in_buf = NULL;
8410 ssl->out_buf = NULL;
8411
8412 ssl->in_hdr = NULL;
8413 ssl->in_ctr = NULL;
8414 ssl->in_len = NULL;
8415 ssl->in_iv = NULL;
8416 ssl->in_msg = NULL;
8417
8418 ssl->out_hdr = NULL;
8419 ssl->out_ctr = NULL;
8420 ssl->out_len = NULL;
8421 ssl->out_iv = NULL;
8422 ssl->out_msg = NULL;
8423
k-stachowiak9f7798e2018-07-31 16:52:32 +02008424 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008425}
8426
8427/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008428 * Reset an initialized and used SSL context for re-use while retaining
8429 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008430 *
8431 * If partial is non-zero, keep data in the input buffer and client ID.
8432 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008433 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008434static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008435{
Paul Bakker48916f92012-09-16 19:57:18 +00008436 int ret;
8437
Hanno Becker7e772132018-08-10 12:38:21 +01008438#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8439 !defined(MBEDTLS_SSL_SRV_C)
8440 ((void) partial);
8441#endif
8442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008444
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008445 /* Cancel any possibly running timer */
8446 ssl_set_timer( ssl, 0 );
8447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008448#if defined(MBEDTLS_SSL_RENEGOTIATION)
8449 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008450 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008451
8452 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008453 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8454 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008455#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008457
Paul Bakker7eb013f2011-10-06 12:37:39 +00008458 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008459 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008460
8461 ssl->in_msgtype = 0;
8462 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008463#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008464 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008465 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008466#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008467#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008468 ssl_dtls_replay_reset( ssl );
8469#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008470
8471 ssl->in_hslen = 0;
8472 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008473
8474 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008475
8476 ssl->out_msgtype = 0;
8477 ssl->out_msglen = 0;
8478 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008479#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8480 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008481 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008482#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008483
Hanno Becker19859472018-08-06 09:40:20 +01008484 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8485
Paul Bakker48916f92012-09-16 19:57:18 +00008486 ssl->transform_in = NULL;
8487 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008488
Hanno Becker78640902018-08-13 16:35:15 +01008489 ssl->session_in = NULL;
8490 ssl->session_out = NULL;
8491
Angus Grattond8213d02016-05-25 20:56:48 +10008492 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008493
8494#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008495 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008496#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8497 {
8498 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008499 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008500 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008502#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8503 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8506 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008508 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8509 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008510 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008511 }
8512#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008513
Paul Bakker48916f92012-09-16 19:57:18 +00008514 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008516 mbedtls_ssl_transform_free( ssl->transform );
8517 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008518 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008519 }
Paul Bakker48916f92012-09-16 19:57:18 +00008520
Paul Bakkerc0463502013-02-14 11:19:38 +01008521 if( ssl->session )
8522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008523 mbedtls_ssl_session_free( ssl->session );
8524 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008525 ssl->session = NULL;
8526 }
8527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008528#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008529 ssl->alpn_chosen = NULL;
8530#endif
8531
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008532#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008533#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008534 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008535#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008536 {
8537 mbedtls_free( ssl->cli_id );
8538 ssl->cli_id = NULL;
8539 ssl->cli_id_len = 0;
8540 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008541#endif
8542
Paul Bakker48916f92012-09-16 19:57:18 +00008543 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8544 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008545
8546 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008547}
8548
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008549/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008550 * Reset an initialized and used SSL context for re-use while retaining
8551 * all application-set variables, function pointers and data.
8552 */
8553int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8554{
8555 return( ssl_session_reset_int( ssl, 0 ) );
8556}
8557
8558/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008559 * SSL set accessors
8560 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008561void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008562{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008563 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008564}
8565
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008566void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008567{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008568 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008569}
8570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008571#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008572void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008573{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008574 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008575}
8576#endif
8577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008579void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008580{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008581 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008582}
8583#endif
8584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008585#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008586
Hanno Becker1841b0a2018-08-24 11:13:57 +01008587void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8588 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008589{
8590 ssl->disable_datagram_packing = !allow_packing;
8591}
8592
8593void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8594 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008595{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008596 conf->hs_timeout_min = min;
8597 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008598}
8599#endif
8600
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008601void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008602{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008603 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008604}
8605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008606#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008607void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008608 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008609 void *p_vrfy )
8610{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008611 conf->f_vrfy = f_vrfy;
8612 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008613}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008615
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008616void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008617 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008618 void *p_rng )
8619{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008620 conf->f_rng = f_rng;
8621 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008622}
8623
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008624void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008625 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008626 void *p_dbg )
8627{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008628 conf->f_dbg = f_dbg;
8629 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008630}
8631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008632void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008633 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008634 mbedtls_ssl_send_t *f_send,
8635 mbedtls_ssl_recv_t *f_recv,
8636 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008637{
8638 ssl->p_bio = p_bio;
8639 ssl->f_send = f_send;
8640 ssl->f_recv = f_recv;
8641 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008642}
8643
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008644#if defined(MBEDTLS_SSL_PROTO_DTLS)
8645void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8646{
8647 ssl->mtu = mtu;
8648}
8649#endif
8650
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008651void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008652{
8653 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008654}
8655
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008656void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8657 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008658 mbedtls_ssl_set_timer_t *f_set_timer,
8659 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008660{
8661 ssl->p_timer = p_timer;
8662 ssl->f_set_timer = f_set_timer;
8663 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008664
8665 /* Make sure we start with no timer running */
8666 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008667}
8668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008669#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008670void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008671 void *p_cache,
8672 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8673 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008674{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008675 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008676 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008677 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008678}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008679#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008681#if defined(MBEDTLS_SSL_CLI_C)
8682int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008683{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008684 int ret;
8685
8686 if( ssl == NULL ||
8687 session == NULL ||
8688 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008689 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008691 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008692 }
8693
Hanno Becker52055ae2019-02-06 14:30:46 +00008694 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8695 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008696 return( ret );
8697
Paul Bakker0a597072012-09-25 21:55:46 +00008698 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008699
8700 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008701}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008702#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008703
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008704void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008705 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008706{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008707 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8708 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8709 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8710 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008711}
8712
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008713void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008714 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008715 int major, int minor )
8716{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008717 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008718 return;
8719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008720 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008721 return;
8722
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008723 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008724}
8725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008726#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008727void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008728 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008729{
8730 conf->cert_profile = profile;
8731}
8732
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008733/* Append a new keycert entry to a (possibly empty) list */
8734static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8735 mbedtls_x509_crt *cert,
8736 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008737{
niisato8ee24222018-06-25 19:05:48 +09008738 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008739
niisato8ee24222018-06-25 19:05:48 +09008740 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8741 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008742 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008743
niisato8ee24222018-06-25 19:05:48 +09008744 new_cert->cert = cert;
8745 new_cert->key = key;
8746 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008747
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008748 /* Update head is the list was null, else add to the end */
8749 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008750 {
niisato8ee24222018-06-25 19:05:48 +09008751 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008752 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008753 else
8754 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008755 mbedtls_ssl_key_cert *cur = *head;
8756 while( cur->next != NULL )
8757 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008758 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008759 }
8760
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008761 return( 0 );
8762}
8763
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008764int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008765 mbedtls_x509_crt *own_cert,
8766 mbedtls_pk_context *pk_key )
8767{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008768 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008769}
8770
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008771void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008772 mbedtls_x509_crt *ca_chain,
8773 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008774{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008775 conf->ca_chain = ca_chain;
8776 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008777
8778#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8779 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8780 * cannot be used together. */
8781 conf->f_ca_cb = NULL;
8782 conf->p_ca_cb = NULL;
8783#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008784}
Hanno Becker5adaad92019-03-27 16:54:37 +00008785
8786#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8787void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008788 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008789 void *p_ca_cb )
8790{
8791 conf->f_ca_cb = f_ca_cb;
8792 conf->p_ca_cb = p_ca_cb;
8793
8794 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8795 * cannot be used together. */
8796 conf->ca_chain = NULL;
8797 conf->ca_crl = NULL;
8798}
8799#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008800#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008801
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008802#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8803int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8804 mbedtls_x509_crt *own_cert,
8805 mbedtls_pk_context *pk_key )
8806{
8807 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8808 own_cert, pk_key ) );
8809}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008810
8811void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8812 mbedtls_x509_crt *ca_chain,
8813 mbedtls_x509_crl *ca_crl )
8814{
8815 ssl->handshake->sni_ca_chain = ca_chain;
8816 ssl->handshake->sni_ca_crl = ca_crl;
8817}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008818
8819void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8820 int authmode )
8821{
8822 ssl->handshake->sni_authmode = authmode;
8823}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008824#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8825
Hanno Becker8927c832019-04-03 12:52:50 +01008826#if defined(MBEDTLS_X509_CRT_PARSE_C)
8827void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8828 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8829 void *p_vrfy )
8830{
8831 ssl->f_vrfy = f_vrfy;
8832 ssl->p_vrfy = p_vrfy;
8833}
8834#endif
8835
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008836#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008837/*
8838 * Set EC J-PAKE password for current handshake
8839 */
8840int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8841 const unsigned char *pw,
8842 size_t pw_len )
8843{
8844 mbedtls_ecjpake_role role;
8845
Janos Follath8eb64132016-06-03 15:40:57 +01008846 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008847 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8848
8849 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8850 role = MBEDTLS_ECJPAKE_SERVER;
8851 else
8852 role = MBEDTLS_ECJPAKE_CLIENT;
8853
8854 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8855 role,
8856 MBEDTLS_MD_SHA256,
8857 MBEDTLS_ECP_DP_SECP256R1,
8858 pw, pw_len ) );
8859}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008860#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008862#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008863
8864static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8865{
8866 /* Remove reference to existing PSK, if any. */
8867#if defined(MBEDTLS_USE_PSA_CRYPTO)
8868 if( conf->psk_opaque != 0 )
8869 {
8870 /* The maintenance of the PSK key slot is the
8871 * user's responsibility. */
8872 conf->psk_opaque = 0;
8873 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008874 /* This and the following branch should never
8875 * be taken simultaenously as we maintain the
8876 * invariant that raw and opaque PSKs are never
8877 * configured simultaneously. As a safeguard,
8878 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008879#endif /* MBEDTLS_USE_PSA_CRYPTO */
8880 if( conf->psk != NULL )
8881 {
8882 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8883
8884 mbedtls_free( conf->psk );
8885 conf->psk = NULL;
8886 conf->psk_len = 0;
8887 }
8888
8889 /* Remove reference to PSK identity, if any. */
8890 if( conf->psk_identity != NULL )
8891 {
8892 mbedtls_free( conf->psk_identity );
8893 conf->psk_identity = NULL;
8894 conf->psk_identity_len = 0;
8895 }
8896}
8897
Hanno Becker7390c712018-11-15 13:33:04 +00008898/* This function assumes that PSK identity in the SSL config is unset.
8899 * It checks that the provided identity is well-formed and attempts
8900 * to make a copy of it in the SSL config.
8901 * On failure, the PSK identity in the config remains unset. */
8902static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8903 unsigned char const *psk_identity,
8904 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008905{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008906 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008907 if( psk_identity == NULL ||
8908 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008909 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008910 {
8911 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8912 }
8913
Hanno Becker7390c712018-11-15 13:33:04 +00008914 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8915 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008916 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008917
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008918 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008919 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008920
8921 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008922}
8923
Hanno Becker7390c712018-11-15 13:33:04 +00008924int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8925 const unsigned char *psk, size_t psk_len,
8926 const unsigned char *psk_identity, size_t psk_identity_len )
8927{
8928 int ret;
8929 /* Remove opaque/raw PSK + PSK Identity */
8930 ssl_conf_remove_psk( conf );
8931
8932 /* Check and set raw PSK */
8933 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8934 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8935 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8936 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8937 conf->psk_len = psk_len;
8938 memcpy( conf->psk, psk, conf->psk_len );
8939
8940 /* Check and set PSK Identity */
8941 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8942 if( ret != 0 )
8943 ssl_conf_remove_psk( conf );
8944
8945 return( ret );
8946}
8947
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008948static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8949{
8950#if defined(MBEDTLS_USE_PSA_CRYPTO)
8951 if( ssl->handshake->psk_opaque != 0 )
8952 {
8953 ssl->handshake->psk_opaque = 0;
8954 }
8955 else
8956#endif /* MBEDTLS_USE_PSA_CRYPTO */
8957 if( ssl->handshake->psk != NULL )
8958 {
8959 mbedtls_platform_zeroize( ssl->handshake->psk,
8960 ssl->handshake->psk_len );
8961 mbedtls_free( ssl->handshake->psk );
8962 ssl->handshake->psk_len = 0;
8963 }
8964}
8965
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008966int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8967 const unsigned char *psk, size_t psk_len )
8968{
8969 if( psk == NULL || ssl->handshake == NULL )
8970 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8971
8972 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8973 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8974
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008975 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008976
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008977 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008978 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008979
8980 ssl->handshake->psk_len = psk_len;
8981 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8982
8983 return( 0 );
8984}
8985
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008986#if defined(MBEDTLS_USE_PSA_CRYPTO)
8987int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008988 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008989 const unsigned char *psk_identity,
8990 size_t psk_identity_len )
8991{
Hanno Becker7390c712018-11-15 13:33:04 +00008992 int ret;
8993 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008994 ssl_conf_remove_psk( conf );
8995
Hanno Becker7390c712018-11-15 13:33:04 +00008996 /* Check and set opaque PSK */
8997 if( psk_slot == 0 )
8998 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008999 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009000
9001 /* Check and set PSK Identity */
9002 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9003 psk_identity_len );
9004 if( ret != 0 )
9005 ssl_conf_remove_psk( conf );
9006
9007 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009008}
9009
9010int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009011 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009012{
9013 if( psk_slot == 0 || ssl->handshake == NULL )
9014 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9015
9016 ssl_remove_psk( ssl );
9017 ssl->handshake->psk_opaque = psk_slot;
9018 return( 0 );
9019}
9020#endif /* MBEDTLS_USE_PSA_CRYPTO */
9021
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009022void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009023 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009024 size_t),
9025 void *p_psk )
9026{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009027 conf->f_psk = f_psk;
9028 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009029}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009030#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009031
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009032#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009033
9034#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009035int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009036{
9037 int ret;
9038
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009039 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9040 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9041 {
9042 mbedtls_mpi_free( &conf->dhm_P );
9043 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009044 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009045 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009046
9047 return( 0 );
9048}
Hanno Becker470a8c42017-10-04 15:28:46 +01009049#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009050
Hanno Beckera90658f2017-10-04 15:29:08 +01009051int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9052 const unsigned char *dhm_P, size_t P_len,
9053 const unsigned char *dhm_G, size_t G_len )
9054{
9055 int ret;
9056
9057 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9058 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9059 {
9060 mbedtls_mpi_free( &conf->dhm_P );
9061 mbedtls_mpi_free( &conf->dhm_G );
9062 return( ret );
9063 }
9064
9065 return( 0 );
9066}
Paul Bakker5121ce52009-01-03 21:22:43 +00009067
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009068int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009069{
9070 int ret;
9071
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009072 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9073 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9074 {
9075 mbedtls_mpi_free( &conf->dhm_P );
9076 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009077 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009078 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009079
9080 return( 0 );
9081}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009082#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009083
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009084#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9085/*
9086 * Set the minimum length for Diffie-Hellman parameters
9087 */
9088void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9089 unsigned int bitlen )
9090{
9091 conf->dhm_min_bitlen = bitlen;
9092}
9093#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9094
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009095#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009096/*
9097 * Set allowed/preferred hashes for handshake signatures
9098 */
9099void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9100 const int *hashes )
9101{
9102 conf->sig_hashes = hashes;
9103}
Hanno Becker947194e2017-04-07 13:25:49 +01009104#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009105
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009106#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009107/*
9108 * Set the allowed elliptic curves
9109 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009110void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009111 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009112{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009113 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009114}
Hanno Becker947194e2017-04-07 13:25:49 +01009115#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009116
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009117#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009118int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009119{
Hanno Becker947194e2017-04-07 13:25:49 +01009120 /* Initialize to suppress unnecessary compiler warning */
9121 size_t hostname_len = 0;
9122
9123 /* Check if new hostname is valid before
9124 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009125 if( hostname != NULL )
9126 {
9127 hostname_len = strlen( hostname );
9128
9129 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9131 }
9132
9133 /* Now it's clear that we will overwrite the old hostname,
9134 * so we can free it safely */
9135
9136 if( ssl->hostname != NULL )
9137 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009138 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009139 mbedtls_free( ssl->hostname );
9140 }
9141
9142 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009143
Paul Bakker5121ce52009-01-03 21:22:43 +00009144 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009145 {
9146 ssl->hostname = NULL;
9147 }
9148 else
9149 {
9150 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009151 if( ssl->hostname == NULL )
9152 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009153
Hanno Becker947194e2017-04-07 13:25:49 +01009154 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009155
Hanno Becker947194e2017-04-07 13:25:49 +01009156 ssl->hostname[hostname_len] = '\0';
9157 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009158
9159 return( 0 );
9160}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009161#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009162
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009163#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009164void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009165 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009166 const unsigned char *, size_t),
9167 void *p_sni )
9168{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009169 conf->f_sni = f_sni;
9170 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009172#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009174#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009175int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009176{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009177 size_t cur_len, tot_len;
9178 const char **p;
9179
9180 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009181 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9182 * MUST NOT be truncated."
9183 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009184 */
9185 tot_len = 0;
9186 for( p = protos; *p != NULL; p++ )
9187 {
9188 cur_len = strlen( *p );
9189 tot_len += cur_len;
9190
9191 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009193 }
9194
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009195 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009196
9197 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009198}
9199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009200const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009201{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009202 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009203}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009204#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009205
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009206void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009207{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009208 conf->max_major_ver = major;
9209 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009210}
9211
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009212void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009213{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009214 conf->min_major_ver = major;
9215 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009216}
9217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009218#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009219void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009220{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009221 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009222}
9223#endif
9224
Janos Follath088ce432017-04-10 12:42:31 +01009225#if defined(MBEDTLS_SSL_SRV_C)
9226void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9227 char cert_req_ca_list )
9228{
9229 conf->cert_req_ca_list = cert_req_ca_list;
9230}
9231#endif
9232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009233#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009234void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009235{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009236 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009237}
9238#endif
9239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009240#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009241void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009242{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009243 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009244}
9245#endif
9246
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009247#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009248void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009249{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009250 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009251}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009252#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009254#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009255int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009256{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009257 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009258 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009260 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009261 }
9262
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009263 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009264
9265 return( 0 );
9266}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009267#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009269#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009270void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009271{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009272 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009273}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009274#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009276#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009277void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009278{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009279 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009280}
9281#endif
9282
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009283void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009284{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009285 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009286}
9287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009288#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009289void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009290{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009291 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009292}
9293
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009294void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009295{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009296 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009297}
9298
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009299void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009300 const unsigned char period[8] )
9301{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009302 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009303}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009306#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009307#if defined(MBEDTLS_SSL_CLI_C)
9308void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009309{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009310 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009311}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009312#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009313
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009314#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009315void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9316 mbedtls_ssl_ticket_write_t *f_ticket_write,
9317 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9318 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009319{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009320 conf->f_ticket_write = f_ticket_write;
9321 conf->f_ticket_parse = f_ticket_parse;
9322 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009323}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009324#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009325#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009326
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009327#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9328void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9329 mbedtls_ssl_export_keys_t *f_export_keys,
9330 void *p_export_keys )
9331{
9332 conf->f_export_keys = f_export_keys;
9333 conf->p_export_keys = p_export_keys;
9334}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009335
9336void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9337 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9338 void *p_export_keys )
9339{
9340 conf->f_export_keys_ext = f_export_keys_ext;
9341 conf->p_export_keys = p_export_keys;
9342}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009343#endif
9344
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009345#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009346void mbedtls_ssl_conf_async_private_cb(
9347 mbedtls_ssl_config *conf,
9348 mbedtls_ssl_async_sign_t *f_async_sign,
9349 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9350 mbedtls_ssl_async_resume_t *f_async_resume,
9351 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009352 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009353{
9354 conf->f_async_sign_start = f_async_sign;
9355 conf->f_async_decrypt_start = f_async_decrypt;
9356 conf->f_async_resume = f_async_resume;
9357 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009358 conf->p_async_config_data = async_config_data;
9359}
9360
Gilles Peskine8f97af72018-04-26 11:46:10 +02009361void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9362{
9363 return( conf->p_async_config_data );
9364}
9365
Gilles Peskine1febfef2018-04-30 11:54:39 +02009366void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009367{
9368 if( ssl->handshake == NULL )
9369 return( NULL );
9370 else
9371 return( ssl->handshake->user_async_ctx );
9372}
9373
Gilles Peskine1febfef2018-04-30 11:54:39 +02009374void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009375 void *ctx )
9376{
9377 if( ssl->handshake != NULL )
9378 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009379}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009380#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009381
Paul Bakker5121ce52009-01-03 21:22:43 +00009382/*
9383 * SSL get accessors
9384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009385size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009386{
9387 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9388}
9389
Hanno Becker8b170a02017-10-10 11:51:19 +01009390int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9391{
9392 /*
9393 * Case A: We're currently holding back
9394 * a message for further processing.
9395 */
9396
9397 if( ssl->keep_current_message == 1 )
9398 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009399 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009400 return( 1 );
9401 }
9402
9403 /*
9404 * Case B: Further records are pending in the current datagram.
9405 */
9406
9407#if defined(MBEDTLS_SSL_PROTO_DTLS)
9408 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9409 ssl->in_left > ssl->next_record_offset )
9410 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009411 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009412 return( 1 );
9413 }
9414#endif /* MBEDTLS_SSL_PROTO_DTLS */
9415
9416 /*
9417 * Case C: A handshake message is being processed.
9418 */
9419
Hanno Becker8b170a02017-10-10 11:51:19 +01009420 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9421 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009422 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009423 return( 1 );
9424 }
9425
9426 /*
9427 * Case D: An application data message is being processed
9428 */
9429 if( ssl->in_offt != NULL )
9430 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009431 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009432 return( 1 );
9433 }
9434
9435 /*
9436 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009437 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009438 * we implement support for multiple alerts in single records.
9439 */
9440
9441 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9442 return( 0 );
9443}
9444
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009445uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009446{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009447 if( ssl->session != NULL )
9448 return( ssl->session->verify_result );
9449
9450 if( ssl->session_negotiate != NULL )
9451 return( ssl->session_negotiate->verify_result );
9452
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009453 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009454}
9455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009456const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009457{
Paul Bakker926c8e42013-03-06 10:23:34 +01009458 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009459 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009461 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009462}
9463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009464const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009465{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009467 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009468 {
9469 switch( ssl->minor_ver )
9470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009471 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009472 return( "DTLSv1.0" );
9473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009474 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009475 return( "DTLSv1.2" );
9476
9477 default:
9478 return( "unknown (DTLS)" );
9479 }
9480 }
9481#endif
9482
Paul Bakker43ca69c2011-01-15 17:35:19 +00009483 switch( ssl->minor_ver )
9484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009485 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009486 return( "SSLv3.0" );
9487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009488 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009489 return( "TLSv1.0" );
9490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009491 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009492 return( "TLSv1.1" );
9493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009494 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009495 return( "TLSv1.2" );
9496
Paul Bakker43ca69c2011-01-15 17:35:19 +00009497 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009498 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009499 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009500}
9501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009502int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009503{
Hanno Becker3136ede2018-08-17 15:28:19 +01009504 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009505 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009506 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009507
Hanno Becker5903de42019-05-03 14:46:38 +01009508 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9509
Hanno Becker78640902018-08-13 16:35:15 +01009510 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009511 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009513#if defined(MBEDTLS_ZLIB_SUPPORT)
9514 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9515 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009516#endif
9517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520 case MBEDTLS_MODE_GCM:
9521 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009522 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009523 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009524 transform_expansion = transform->minlen;
9525 break;
9526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009527 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009528
9529 block_size = mbedtls_cipher_get_block_size(
9530 &transform->cipher_ctx_enc );
9531
Hanno Becker3136ede2018-08-17 15:28:19 +01009532 /* Expansion due to the addition of the MAC. */
9533 transform_expansion += transform->maclen;
9534
9535 /* Expansion due to the addition of CBC padding;
9536 * Theoretically up to 256 bytes, but we never use
9537 * more than the block size of the underlying cipher. */
9538 transform_expansion += block_size;
9539
9540 /* For TLS 1.1 or higher, an explicit IV is added
9541 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009542#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9543 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009544 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009545#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009546
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009547 break;
9548
9549 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009552 }
9553
Hanno Beckera0e20d02019-05-15 14:03:01 +01009554#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009555 if( transform->out_cid_len != 0 )
9556 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009557#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009558
Hanno Becker5903de42019-05-03 14:46:38 +01009559 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009560}
9561
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009562#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9563size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9564{
9565 size_t max_len;
9566
9567 /*
9568 * Assume mfl_code is correct since it was checked when set
9569 */
Angus Grattond8213d02016-05-25 20:56:48 +10009570 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009571
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009572 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009573 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009574 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009575 {
Angus Grattond8213d02016-05-25 20:56:48 +10009576 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009577 }
9578
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009579 /* During a handshake, use the value being negotiated */
9580 if( ssl->session_negotiate != NULL &&
9581 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9582 {
9583 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9584 }
9585
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009586 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009587}
9588#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9589
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009590#if defined(MBEDTLS_SSL_PROTO_DTLS)
9591static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9592{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009593 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9594 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9595 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9596 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9597 return ( 0 );
9598
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009599 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9600 return( ssl->mtu );
9601
9602 if( ssl->mtu == 0 )
9603 return( ssl->handshake->mtu );
9604
9605 return( ssl->mtu < ssl->handshake->mtu ?
9606 ssl->mtu : ssl->handshake->mtu );
9607}
9608#endif /* MBEDTLS_SSL_PROTO_DTLS */
9609
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009610int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9611{
9612 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9613
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009614#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9615 !defined(MBEDTLS_SSL_PROTO_DTLS)
9616 (void) ssl;
9617#endif
9618
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009619#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9620 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9621
9622 if( max_len > mfl )
9623 max_len = mfl;
9624#endif
9625
9626#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009627 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009628 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009629 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009630 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9631 const size_t overhead = (size_t) ret;
9632
9633 if( ret < 0 )
9634 return( ret );
9635
9636 if( mtu <= overhead )
9637 {
9638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9639 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9640 }
9641
9642 if( max_len > mtu - overhead )
9643 max_len = mtu - overhead;
9644 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009645#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009646
Hanno Becker0defedb2018-08-10 12:35:02 +01009647#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9648 !defined(MBEDTLS_SSL_PROTO_DTLS)
9649 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009650#endif
9651
9652 return( (int) max_len );
9653}
9654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009655#if defined(MBEDTLS_X509_CRT_PARSE_C)
9656const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009657{
9658 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009659 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009660
Hanno Beckere6824572019-02-07 13:18:46 +00009661#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009662 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009663#else
9664 return( NULL );
9665#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009666}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009667#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009670int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9671 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009672{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009673 if( ssl == NULL ||
9674 dst == NULL ||
9675 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009676 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009679 }
9680
Hanno Becker52055ae2019-02-06 14:30:46 +00009681 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009682}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009683#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009684
Paul Bakker5121ce52009-01-03 21:22:43 +00009685/*
Paul Bakker1961b702013-01-25 14:49:24 +01009686 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009687 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009688int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009689{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009690 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009691
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009692 if( ssl == NULL || ssl->conf == NULL )
9693 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009695#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009696 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009697 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009698#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009699#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009700 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009701 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009702#endif
9703
Paul Bakker1961b702013-01-25 14:49:24 +01009704 return( ret );
9705}
9706
9707/*
9708 * Perform the SSL handshake
9709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009710int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009711{
9712 int ret = 0;
9713
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009714 if( ssl == NULL || ssl->conf == NULL )
9715 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009719 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009721 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009722
9723 if( ret != 0 )
9724 break;
9725 }
9726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009728
9729 return( ret );
9730}
9731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009732#if defined(MBEDTLS_SSL_RENEGOTIATION)
9733#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009734/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009735 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009737static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009738{
9739 int ret;
9740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009742
9743 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009744 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9745 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009746
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009747 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009748 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009749 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009750 return( ret );
9751 }
9752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009754
9755 return( 0 );
9756}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009757#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009758
9759/*
9760 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009761 * - any side: calling mbedtls_ssl_renegotiate(),
9762 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9763 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009764 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009765 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009766 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009768static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009769{
9770 int ret;
9771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009773
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009774 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9775 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009776
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009777 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9778 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009780 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009781 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009782 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009783 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009784 ssl->handshake->out_msg_seq = 1;
9785 else
9786 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009787 }
9788#endif
9789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009790 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9791 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009793 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009796 return( ret );
9797 }
9798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009800
9801 return( 0 );
9802}
9803
9804/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009805 * Renegotiate current connection on client,
9806 * or request renegotiation on server
9807 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009809{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009810 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009811
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009812 if( ssl == NULL || ssl->conf == NULL )
9813 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009815#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009816 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009817 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9820 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009823
9824 /* Did we already try/start sending HelloRequest? */
9825 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009826 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009827
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009828 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009829 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009832#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009833 /*
9834 * On client, either start the renegotiation process or,
9835 * if already in progress, continue the handshake
9836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9840 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009841
9842 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009845 return( ret );
9846 }
9847 }
9848 else
9849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009853 return( ret );
9854 }
9855 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009856#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009857
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009858 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009859}
9860
9861/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009862 * Check record counters and renegotiate if they're above the limit.
9863 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009864static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009865{
Andres AG2196c7f2016-12-15 17:01:16 +00009866 size_t ep_len = ssl_ep_len( ssl );
9867 int in_ctr_cmp;
9868 int out_ctr_cmp;
9869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009870 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9871 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009872 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009873 {
9874 return( 0 );
9875 }
9876
Andres AG2196c7f2016-12-15 17:01:16 +00009877 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9878 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009879 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009880 ssl->conf->renego_period + ep_len, 8 - ep_len );
9881
9882 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009883 {
9884 return( 0 );
9885 }
9886
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009888 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009889}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009890#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009891
9892/*
9893 * Receive application data decrypted from the SSL layer
9894 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009895int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009896{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009897 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009898 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009899
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009900 if( ssl == NULL || ssl->conf == NULL )
9901 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009903 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009905#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009906 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009908 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009909 return( ret );
9910
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009911 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009912 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009913 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009914 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009915 return( ret );
9916 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009917 }
9918#endif
9919
Hanno Becker4a810fb2017-05-24 16:27:30 +01009920 /*
9921 * Check if renegotiation is necessary and/or handshake is
9922 * in process. If yes, perform/continue, and fall through
9923 * if an unexpected packet is received while the client
9924 * is waiting for the ServerHello.
9925 *
9926 * (There is no equivalent to the last condition on
9927 * the server-side as it is not treated as within
9928 * a handshake while waiting for the ClientHello
9929 * after a renegotiation request.)
9930 */
9931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009932#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009933 ret = ssl_check_ctr_renegotiate( ssl );
9934 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9935 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009937 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009938 return( ret );
9939 }
9940#endif
9941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009942 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009944 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009945 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9946 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009948 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009949 return( ret );
9950 }
9951 }
9952
Hanno Beckere41158b2017-10-23 13:30:32 +01009953 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009954 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009955 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009956 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009957 if( ssl->f_get_timer != NULL &&
9958 ssl->f_get_timer( ssl->p_timer ) == -1 )
9959 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009960 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009961 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009962
Hanno Becker327c93b2018-08-15 13:56:18 +01009963 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009964 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009965 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9966 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009967
Hanno Becker4a810fb2017-05-24 16:27:30 +01009968 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9969 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009970 }
9971
9972 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009973 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009974 {
9975 /*
9976 * OpenSSL sends empty messages to randomize the IV
9977 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009978 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009980 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009981 return( 0 );
9982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009983 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009984 return( ret );
9985 }
9986 }
9987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009988 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009991
Hanno Becker4a810fb2017-05-24 16:27:30 +01009992 /*
9993 * - For client-side, expect SERVER_HELLO_REQUEST.
9994 * - For server-side, expect CLIENT_HELLO.
9995 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9996 */
9997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009998#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009999 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010000 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010001 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010004
10005 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010006#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010007 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010008 {
10009 continue;
10010 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010011#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010012 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010013 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010014#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010015
Hanno Becker4a810fb2017-05-24 16:27:30 +010010016#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010017 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010018 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010021
10022 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010023#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010024 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010025 {
10026 continue;
10027 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010028#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010029 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010030 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010031#endif /* MBEDTLS_SSL_SRV_C */
10032
Hanno Becker21df7f92017-10-17 11:03:26 +010010033#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010034 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010035 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10036 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10037 ssl->conf->allow_legacy_renegotiation ==
10038 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10039 {
10040 /*
10041 * Accept renegotiation request
10042 */
Paul Bakker48916f92012-09-16 19:57:18 +000010043
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010044 /* DTLS clients need to know renego is server-initiated */
10045#if defined(MBEDTLS_SSL_PROTO_DTLS)
10046 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10047 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10048 {
10049 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10050 }
10051#endif
10052 ret = ssl_start_renegotiation( ssl );
10053 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10054 ret != 0 )
10055 {
10056 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10057 return( ret );
10058 }
10059 }
10060 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010061#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010062 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010063 /*
10064 * Refuse renegotiation
10065 */
10066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010067 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010069#if defined(MBEDTLS_SSL_PROTO_SSL3)
10070 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010071 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010072 /* SSLv3 does not have a "no_renegotiation" warning, so
10073 we send a fatal alert and abort the connection. */
10074 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10075 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10076 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010077 }
10078 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010079#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10080#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10081 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10082 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10085 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10086 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010087 {
10088 return( ret );
10089 }
Paul Bakker48916f92012-09-16 19:57:18 +000010090 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010091 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010092#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10093 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10096 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010097 }
Paul Bakker48916f92012-09-16 19:57:18 +000010098 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010099
Hanno Becker90333da2017-10-10 11:27:13 +010010100 /* At this point, we don't know whether the renegotiation has been
10101 * completed or not. The cases to consider are the following:
10102 * 1) The renegotiation is complete. In this case, no new record
10103 * has been read yet.
10104 * 2) The renegotiation is incomplete because the client received
10105 * an application data record while awaiting the ServerHello.
10106 * 3) The renegotiation is incomplete because the client received
10107 * a non-handshake, non-application data message while awaiting
10108 * the ServerHello.
10109 * In each of these case, looping will be the proper action:
10110 * - For 1), the next iteration will read a new record and check
10111 * if it's application data.
10112 * - For 2), the loop condition isn't satisfied as application data
10113 * is present, hence continue is the same as break
10114 * - For 3), the loop condition is satisfied and read_record
10115 * will re-deliver the message that was held back by the client
10116 * when expecting the ServerHello.
10117 */
10118 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010119 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010120#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010121 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010122 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010123 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010124 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010125 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010128 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010129 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010130 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010131 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010132 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010133#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010135 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10136 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010139 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010140 }
10141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010142 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10145 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010146 }
10147
10148 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010149
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010150 /* We're going to return something now, cancel timer,
10151 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010152 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010153 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010154
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010155#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010156 /* If we requested renego but received AppData, resend HelloRequest.
10157 * Do it now, after setting in_offt, to avoid taking this branch
10158 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010159#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010160 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010161 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010162 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010163 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010165 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010166 return( ret );
10167 }
10168 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010169#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010170#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010171 }
10172
10173 n = ( len < ssl->in_msglen )
10174 ? len : ssl->in_msglen;
10175
10176 memcpy( buf, ssl->in_offt, n );
10177 ssl->in_msglen -= n;
10178
10179 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010180 {
10181 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010182 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010183 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010184 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010185 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010186 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010187 /* more data available */
10188 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010189 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010192
Paul Bakker23986e52011-04-24 08:57:21 +000010193 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010194}
10195
10196/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010197 * Send application data to be encrypted by the SSL layer, taking care of max
10198 * fragment length and buffer size.
10199 *
10200 * According to RFC 5246 Section 6.2.1:
10201 *
10202 * Zero-length fragments of Application data MAY be sent as they are
10203 * potentially useful as a traffic analysis countermeasure.
10204 *
10205 * Therefore, it is possible that the input message length is 0 and the
10206 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010207 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010208static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010209 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010210{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010211 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10212 const size_t max_len = (size_t) ret;
10213
10214 if( ret < 0 )
10215 {
10216 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10217 return( ret );
10218 }
10219
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010220 if( len > max_len )
10221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010222#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010223 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010226 "maximum fragment length: %d > %d",
10227 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010228 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010229 }
10230 else
10231#endif
10232 len = max_len;
10233 }
Paul Bakker887bd502011-06-08 13:10:54 +000010234
Paul Bakker5121ce52009-01-03 21:22:43 +000010235 if( ssl->out_left != 0 )
10236 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010237 /*
10238 * The user has previously tried to send the data and
10239 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10240 * written. In this case, we expect the high-level write function
10241 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010243 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010245 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010246 return( ret );
10247 }
10248 }
Paul Bakker887bd502011-06-08 13:10:54 +000010249 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010250 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010251 /*
10252 * The user is trying to send a message the first time, so we need to
10253 * copy the data into the internal buffers and setup the data structure
10254 * to keep track of partial writes
10255 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010256 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010257 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010258 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010259
Hanno Becker67bc7c32018-08-06 11:33:50 +010010260 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010262 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010263 return( ret );
10264 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010265 }
10266
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010267 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010268}
10269
10270/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010271 * Write application data, doing 1/n-1 splitting if necessary.
10272 *
10273 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010274 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010275 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010277#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010278static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010279 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010280{
10281 int ret;
10282
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010283 if( ssl->conf->cbc_record_splitting ==
10284 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010285 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010286 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10287 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10288 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010289 {
10290 return( ssl_write_real( ssl, buf, len ) );
10291 }
10292
10293 if( ssl->split_done == 0 )
10294 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010295 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010296 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010297 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010298 }
10299
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010300 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10301 return( ret );
10302 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010303
10304 return( ret + 1 );
10305}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010306#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010307
10308/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010309 * Write application data (public-facing wrapper)
10310 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010311int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010312{
10313 int ret;
10314
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010316
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010317 if( ssl == NULL || ssl->conf == NULL )
10318 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10319
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010320#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010321 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10322 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010323 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010324 return( ret );
10325 }
10326#endif
10327
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010328 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010329 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010330 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010331 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010332 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010333 return( ret );
10334 }
10335 }
10336
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010337#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010338 ret = ssl_write_split( ssl, buf, len );
10339#else
10340 ret = ssl_write_real( ssl, buf, len );
10341#endif
10342
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010344
10345 return( ret );
10346}
10347
10348/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010349 * Notify the peer that the connection is being closed
10350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010351int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010352{
10353 int ret;
10354
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010355 if( ssl == NULL || ssl->conf == NULL )
10356 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010359
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010360 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010361 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010363 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010365 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10366 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10367 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010369 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010370 return( ret );
10371 }
10372 }
10373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010375
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010376 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010377}
10378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010379void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010380{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010381 if( transform == NULL )
10382 return;
10383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010384#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010385 deflateEnd( &transform->ctx_deflate );
10386 inflateEnd( &transform->ctx_inflate );
10387#endif
10388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010389 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10390 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010391
Hanno Beckerd56ed242018-01-03 15:32:51 +000010392#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010393 mbedtls_md_free( &transform->md_ctx_enc );
10394 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010395#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010396
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010397 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010398}
10399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010400#if defined(MBEDTLS_X509_CRT_PARSE_C)
10401static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010402{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010403 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010404
10405 while( cur != NULL )
10406 {
10407 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010408 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010409 cur = next;
10410 }
10411}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010412#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010413
Hanno Becker0271f962018-08-16 13:23:47 +010010414#if defined(MBEDTLS_SSL_PROTO_DTLS)
10415
10416static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10417{
10418 unsigned offset;
10419 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10420
10421 if( hs == NULL )
10422 return;
10423
Hanno Becker283f5ef2018-08-24 09:34:47 +010010424 ssl_free_buffered_record( ssl );
10425
Hanno Becker0271f962018-08-16 13:23:47 +010010426 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010427 ssl_buffering_free_slot( ssl, offset );
10428}
10429
10430static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10431 uint8_t slot )
10432{
10433 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10434 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010435
10436 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10437 return;
10438
Hanno Beckere605b192018-08-21 15:59:07 +010010439 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010440 {
Hanno Beckere605b192018-08-21 15:59:07 +010010441 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010442 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010443 mbedtls_free( hs_buf->data );
10444 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010445 }
10446}
10447
10448#endif /* MBEDTLS_SSL_PROTO_DTLS */
10449
Gilles Peskine9b562d52018-04-25 20:32:43 +020010450void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010451{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010452 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10453
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010454 if( handshake == NULL )
10455 return;
10456
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010457#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10458 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10459 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010460 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010461 handshake->async_in_progress = 0;
10462 }
10463#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10464
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010465#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10466 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10467 mbedtls_md5_free( &handshake->fin_md5 );
10468 mbedtls_sha1_free( &handshake->fin_sha1 );
10469#endif
10470#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10471#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010472#if defined(MBEDTLS_USE_PSA_CRYPTO)
10473 psa_hash_abort( &handshake->fin_sha256_psa );
10474#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010475 mbedtls_sha256_free( &handshake->fin_sha256 );
10476#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010477#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010478#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010479#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010480 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010481#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010482 mbedtls_sha512_free( &handshake->fin_sha512 );
10483#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010484#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010485#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010487#if defined(MBEDTLS_DHM_C)
10488 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010489#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010490#if defined(MBEDTLS_ECDH_C)
10491 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010492#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010493#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010494 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010495#if defined(MBEDTLS_SSL_CLI_C)
10496 mbedtls_free( handshake->ecjpake_cache );
10497 handshake->ecjpake_cache = NULL;
10498 handshake->ecjpake_cache_len = 0;
10499#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010500#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010501
Janos Follath4ae5c292016-02-10 11:27:43 +000010502#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10503 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010504 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010505 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010506#endif
10507
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010508#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10509 if( handshake->psk != NULL )
10510 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010511 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010512 mbedtls_free( handshake->psk );
10513 }
10514#endif
10515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010516#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10517 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010518 /*
10519 * Free only the linked list wrapper, not the keys themselves
10520 * since the belong to the SNI callback
10521 */
10522 if( handshake->sni_key_cert != NULL )
10523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010524 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010525
10526 while( cur != NULL )
10527 {
10528 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010529 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010530 cur = next;
10531 }
10532 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010533#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010534
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010535#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010536 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010537 if( handshake->ecrs_peer_cert != NULL )
10538 {
10539 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10540 mbedtls_free( handshake->ecrs_peer_cert );
10541 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010542#endif
10543
Hanno Becker75173122019-02-06 16:18:31 +000010544#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10545 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10546 mbedtls_pk_free( &handshake->peer_pubkey );
10547#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010549#if defined(MBEDTLS_SSL_PROTO_DTLS)
10550 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010551 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010552 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010553#endif
10554
Hanno Becker4a63ed42019-01-08 11:39:35 +000010555#if defined(MBEDTLS_ECDH_C) && \
10556 defined(MBEDTLS_USE_PSA_CRYPTO)
10557 psa_destroy_key( handshake->ecdh_psa_privkey );
10558#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10559
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010560 mbedtls_platform_zeroize( handshake,
10561 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010562}
10563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010564void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010565{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010566 if( session == NULL )
10567 return;
10568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010569#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010570 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010571#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010572
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010573#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010574 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010575#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010576
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010577 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010578}
10579
Paul Bakker5121ce52009-01-03 21:22:43 +000010580/*
10581 * Free an SSL context
10582 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010583void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010584{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010585 if( ssl == NULL )
10586 return;
10587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010589
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010590 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010591 {
Angus Grattond8213d02016-05-25 20:56:48 +100010592 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010593 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010594 }
10595
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010596 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010597 {
Angus Grattond8213d02016-05-25 20:56:48 +100010598 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010599 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010600 }
10601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010602#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010603 if( ssl->compress_buf != NULL )
10604 {
Angus Grattond8213d02016-05-25 20:56:48 +100010605 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010606 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010607 }
10608#endif
10609
Paul Bakker48916f92012-09-16 19:57:18 +000010610 if( ssl->transform )
10611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010612 mbedtls_ssl_transform_free( ssl->transform );
10613 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010614 }
10615
10616 if( ssl->handshake )
10617 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010618 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010619 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10620 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010622 mbedtls_free( ssl->handshake );
10623 mbedtls_free( ssl->transform_negotiate );
10624 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010625 }
10626
Paul Bakkerc0463502013-02-14 11:19:38 +010010627 if( ssl->session )
10628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010629 mbedtls_ssl_session_free( ssl->session );
10630 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010631 }
10632
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010633#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010634 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010635 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010636 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010637 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010638 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010639#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010641#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10642 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10645 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010646 }
10647#endif
10648
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010649#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010650 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010651#endif
10652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010654
Paul Bakker86f04f42013-02-14 11:20:09 +010010655 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010656 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010657}
10658
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010659/*
10660 * Initialze mbedtls_ssl_config
10661 */
10662void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10663{
10664 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10665}
10666
Simon Butcherc97b6972015-12-27 23:48:17 +000010667#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010668static int ssl_preset_default_hashes[] = {
10669#if defined(MBEDTLS_SHA512_C)
10670 MBEDTLS_MD_SHA512,
10671 MBEDTLS_MD_SHA384,
10672#endif
10673#if defined(MBEDTLS_SHA256_C)
10674 MBEDTLS_MD_SHA256,
10675 MBEDTLS_MD_SHA224,
10676#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010677#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010678 MBEDTLS_MD_SHA1,
10679#endif
10680 MBEDTLS_MD_NONE
10681};
Simon Butcherc97b6972015-12-27 23:48:17 +000010682#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010683
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010684static int ssl_preset_suiteb_ciphersuites[] = {
10685 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10686 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10687 0
10688};
10689
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010690#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010691static int ssl_preset_suiteb_hashes[] = {
10692 MBEDTLS_MD_SHA256,
10693 MBEDTLS_MD_SHA384,
10694 MBEDTLS_MD_NONE
10695};
10696#endif
10697
10698#if defined(MBEDTLS_ECP_C)
10699static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010700#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010701 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010702#endif
10703#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010704 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010705#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010706 MBEDTLS_ECP_DP_NONE
10707};
10708#endif
10709
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010710/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010711 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010712 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010713int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010714 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010715{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010716#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010717 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010718#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010719
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010720 /* Use the functions here so that they are covered in tests,
10721 * but otherwise access member directly for efficiency */
10722 mbedtls_ssl_conf_endpoint( conf, endpoint );
10723 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010724
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010725 /*
10726 * Things that are common to all presets
10727 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010728#if defined(MBEDTLS_SSL_CLI_C)
10729 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10730 {
10731 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10732#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10733 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10734#endif
10735 }
10736#endif
10737
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010738#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010739 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010740#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010741
10742#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10743 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10744#endif
10745
10746#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10747 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10748#endif
10749
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010750#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10751 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10752#endif
10753
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010754#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010755 conf->f_cookie_write = ssl_cookie_write_dummy;
10756 conf->f_cookie_check = ssl_cookie_check_dummy;
10757#endif
10758
10759#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10760 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10761#endif
10762
Janos Follath088ce432017-04-10 12:42:31 +010010763#if defined(MBEDTLS_SSL_SRV_C)
10764 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10765#endif
10766
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010767#if defined(MBEDTLS_SSL_PROTO_DTLS)
10768 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10769 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10770#endif
10771
10772#if defined(MBEDTLS_SSL_RENEGOTIATION)
10773 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010774 memset( conf->renego_period, 0x00, 2 );
10775 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010776#endif
10777
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010778#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10779 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10780 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010781 const unsigned char dhm_p[] =
10782 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10783 const unsigned char dhm_g[] =
10784 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10785
Hanno Beckera90658f2017-10-04 15:29:08 +010010786 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10787 dhm_p, sizeof( dhm_p ),
10788 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010789 {
10790 return( ret );
10791 }
10792 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010793#endif
10794
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010795 /*
10796 * Preset-specific defaults
10797 */
10798 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010799 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010800 /*
10801 * NSA Suite B
10802 */
10803 case MBEDTLS_SSL_PRESET_SUITEB:
10804 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10805 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10806 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10807 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10808
10809 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10810 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10811 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10812 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10813 ssl_preset_suiteb_ciphersuites;
10814
10815#if defined(MBEDTLS_X509_CRT_PARSE_C)
10816 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010817#endif
10818
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010819#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010820 conf->sig_hashes = ssl_preset_suiteb_hashes;
10821#endif
10822
10823#if defined(MBEDTLS_ECP_C)
10824 conf->curve_list = ssl_preset_suiteb_curves;
10825#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010826 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010827
10828 /*
10829 * Default
10830 */
10831 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010832 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10833 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10834 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10835 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10836 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10837 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10838 MBEDTLS_SSL_MIN_MINOR_VERSION :
10839 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010840 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10841 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10842
10843#if defined(MBEDTLS_SSL_PROTO_DTLS)
10844 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10845 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10846#endif
10847
10848 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10849 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10850 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10851 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10852 mbedtls_ssl_list_ciphersuites();
10853
10854#if defined(MBEDTLS_X509_CRT_PARSE_C)
10855 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10856#endif
10857
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010858#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010859 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010860#endif
10861
10862#if defined(MBEDTLS_ECP_C)
10863 conf->curve_list = mbedtls_ecp_grp_id_list();
10864#endif
10865
10866#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10867 conf->dhm_min_bitlen = 1024;
10868#endif
10869 }
10870
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010871 return( 0 );
10872}
10873
10874/*
10875 * Free mbedtls_ssl_config
10876 */
10877void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10878{
10879#if defined(MBEDTLS_DHM_C)
10880 mbedtls_mpi_free( &conf->dhm_P );
10881 mbedtls_mpi_free( &conf->dhm_G );
10882#endif
10883
10884#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10885 if( conf->psk != NULL )
10886 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010887 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010888 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010889 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010890 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010891 }
10892
10893 if( conf->psk_identity != NULL )
10894 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010895 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010896 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010897 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010898 conf->psk_identity_len = 0;
10899 }
10900#endif
10901
10902#if defined(MBEDTLS_X509_CRT_PARSE_C)
10903 ssl_key_cert_free( conf->key_cert );
10904#endif
10905
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010906 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010907}
10908
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010909#if defined(MBEDTLS_PK_C) && \
10910 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010911/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010912 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010913 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010914unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010915{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010916#if defined(MBEDTLS_RSA_C)
10917 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10918 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010919#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010920#if defined(MBEDTLS_ECDSA_C)
10921 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10922 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010923#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010924 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010925}
10926
Hanno Becker7e5437a2017-04-28 17:15:26 +010010927unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10928{
10929 switch( type ) {
10930 case MBEDTLS_PK_RSA:
10931 return( MBEDTLS_SSL_SIG_RSA );
10932 case MBEDTLS_PK_ECDSA:
10933 case MBEDTLS_PK_ECKEY:
10934 return( MBEDTLS_SSL_SIG_ECDSA );
10935 default:
10936 return( MBEDTLS_SSL_SIG_ANON );
10937 }
10938}
10939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010940mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010941{
10942 switch( sig )
10943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010944#if defined(MBEDTLS_RSA_C)
10945 case MBEDTLS_SSL_SIG_RSA:
10946 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010947#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010948#if defined(MBEDTLS_ECDSA_C)
10949 case MBEDTLS_SSL_SIG_ECDSA:
10950 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010951#endif
10952 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010953 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010954 }
10955}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010956#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010957
Hanno Becker7e5437a2017-04-28 17:15:26 +010010958#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10959 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10960
10961/* Find an entry in a signature-hash set matching a given hash algorithm. */
10962mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10963 mbedtls_pk_type_t sig_alg )
10964{
10965 switch( sig_alg )
10966 {
10967 case MBEDTLS_PK_RSA:
10968 return( set->rsa );
10969 case MBEDTLS_PK_ECDSA:
10970 return( set->ecdsa );
10971 default:
10972 return( MBEDTLS_MD_NONE );
10973 }
10974}
10975
10976/* Add a signature-hash-pair to a signature-hash set */
10977void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10978 mbedtls_pk_type_t sig_alg,
10979 mbedtls_md_type_t md_alg )
10980{
10981 switch( sig_alg )
10982 {
10983 case MBEDTLS_PK_RSA:
10984 if( set->rsa == MBEDTLS_MD_NONE )
10985 set->rsa = md_alg;
10986 break;
10987
10988 case MBEDTLS_PK_ECDSA:
10989 if( set->ecdsa == MBEDTLS_MD_NONE )
10990 set->ecdsa = md_alg;
10991 break;
10992
10993 default:
10994 break;
10995 }
10996}
10997
10998/* Allow exactly one hash algorithm for each signature. */
10999void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11000 mbedtls_md_type_t md_alg )
11001{
11002 set->rsa = md_alg;
11003 set->ecdsa = md_alg;
11004}
11005
11006#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11007 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11008
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011009/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011010 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011012mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011013{
11014 switch( hash )
11015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011016#if defined(MBEDTLS_MD5_C)
11017 case MBEDTLS_SSL_HASH_MD5:
11018 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011019#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011020#if defined(MBEDTLS_SHA1_C)
11021 case MBEDTLS_SSL_HASH_SHA1:
11022 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011023#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011024#if defined(MBEDTLS_SHA256_C)
11025 case MBEDTLS_SSL_HASH_SHA224:
11026 return( MBEDTLS_MD_SHA224 );
11027 case MBEDTLS_SSL_HASH_SHA256:
11028 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011029#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011030#if defined(MBEDTLS_SHA512_C)
11031 case MBEDTLS_SSL_HASH_SHA384:
11032 return( MBEDTLS_MD_SHA384 );
11033 case MBEDTLS_SSL_HASH_SHA512:
11034 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011035#endif
11036 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011037 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011038 }
11039}
11040
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011041/*
11042 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11043 */
11044unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11045{
11046 switch( md )
11047 {
11048#if defined(MBEDTLS_MD5_C)
11049 case MBEDTLS_MD_MD5:
11050 return( MBEDTLS_SSL_HASH_MD5 );
11051#endif
11052#if defined(MBEDTLS_SHA1_C)
11053 case MBEDTLS_MD_SHA1:
11054 return( MBEDTLS_SSL_HASH_SHA1 );
11055#endif
11056#if defined(MBEDTLS_SHA256_C)
11057 case MBEDTLS_MD_SHA224:
11058 return( MBEDTLS_SSL_HASH_SHA224 );
11059 case MBEDTLS_MD_SHA256:
11060 return( MBEDTLS_SSL_HASH_SHA256 );
11061#endif
11062#if defined(MBEDTLS_SHA512_C)
11063 case MBEDTLS_MD_SHA384:
11064 return( MBEDTLS_SSL_HASH_SHA384 );
11065 case MBEDTLS_MD_SHA512:
11066 return( MBEDTLS_SSL_HASH_SHA512 );
11067#endif
11068 default:
11069 return( MBEDTLS_SSL_HASH_NONE );
11070 }
11071}
11072
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011073#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011074/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011075 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011076 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011077 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011078int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011079{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011080 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011081
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011082 if( ssl->conf->curve_list == NULL )
11083 return( -1 );
11084
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011085 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011086 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011087 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011088
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011089 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011090}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011091#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011092
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011093#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011094/*
11095 * Check if a hash proposed by the peer is in our list.
11096 * Return 0 if we're willing to use it, -1 otherwise.
11097 */
11098int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11099 mbedtls_md_type_t md )
11100{
11101 const int *cur;
11102
11103 if( ssl->conf->sig_hashes == NULL )
11104 return( -1 );
11105
11106 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11107 if( *cur == (int) md )
11108 return( 0 );
11109
11110 return( -1 );
11111}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011112#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011114#if defined(MBEDTLS_X509_CRT_PARSE_C)
11115int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11116 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011117 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011118 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011119{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011120 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011121#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011122 int usage = 0;
11123#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011124#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011125 const char *ext_oid;
11126 size_t ext_len;
11127#endif
11128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011129#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11130 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011131 ((void) cert);
11132 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011133 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011134#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011136#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11137 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011138 {
11139 /* Server part of the key exchange */
11140 switch( ciphersuite->key_exchange )
11141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011142 case MBEDTLS_KEY_EXCHANGE_RSA:
11143 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011144 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011145 break;
11146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011147 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11148 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11149 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11150 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011151 break;
11152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011153 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11154 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011155 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011156 break;
11157
11158 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011159 case MBEDTLS_KEY_EXCHANGE_NONE:
11160 case MBEDTLS_KEY_EXCHANGE_PSK:
11161 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11162 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011163 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011164 usage = 0;
11165 }
11166 }
11167 else
11168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011169 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11170 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011171 }
11172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011173 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011174 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011175 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011176 ret = -1;
11177 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011178#else
11179 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011180#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011182#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11183 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011185 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11186 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011187 }
11188 else
11189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011190 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11191 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011192 }
11193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011194 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011195 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011196 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011197 ret = -1;
11198 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011199#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011200
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011201 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011202}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011203#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011204
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011205/*
11206 * Convert version numbers to/from wire format
11207 * and, for DTLS, to/from TLS equivalent.
11208 *
11209 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011210 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011211 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11212 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011214void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011215 unsigned char ver[2] )
11216{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011217#if defined(MBEDTLS_SSL_PROTO_DTLS)
11218 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011220 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011221 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11222
11223 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11224 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11225 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011226 else
11227#else
11228 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011229#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011230 {
11231 ver[0] = (unsigned char) major;
11232 ver[1] = (unsigned char) minor;
11233 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011234}
11235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011236void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011237 const unsigned char ver[2] )
11238{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011239#if defined(MBEDTLS_SSL_PROTO_DTLS)
11240 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011241 {
11242 *major = 255 - ver[0] + 2;
11243 *minor = 255 - ver[1] + 1;
11244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011245 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011246 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11247 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011248 else
11249#else
11250 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011251#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011252 {
11253 *major = ver[0];
11254 *minor = ver[1];
11255 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011256}
11257
Simon Butcher99000142016-10-13 17:21:01 +010011258int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11259{
11260#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11261 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11262 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11263
11264 switch( md )
11265 {
11266#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11267#if defined(MBEDTLS_MD5_C)
11268 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011269 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011270#endif
11271#if defined(MBEDTLS_SHA1_C)
11272 case MBEDTLS_SSL_HASH_SHA1:
11273 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11274 break;
11275#endif
11276#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11277#if defined(MBEDTLS_SHA512_C)
11278 case MBEDTLS_SSL_HASH_SHA384:
11279 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11280 break;
11281#endif
11282#if defined(MBEDTLS_SHA256_C)
11283 case MBEDTLS_SSL_HASH_SHA256:
11284 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11285 break;
11286#endif
11287 default:
11288 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11289 }
11290
11291 return 0;
11292#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11293 (void) ssl;
11294 (void) md;
11295
11296 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11297#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11298}
11299
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011300#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11301 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11302int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11303 unsigned char *output,
11304 unsigned char *data, size_t data_len )
11305{
11306 int ret = 0;
11307 mbedtls_md5_context mbedtls_md5;
11308 mbedtls_sha1_context mbedtls_sha1;
11309
11310 mbedtls_md5_init( &mbedtls_md5 );
11311 mbedtls_sha1_init( &mbedtls_sha1 );
11312
11313 /*
11314 * digitally-signed struct {
11315 * opaque md5_hash[16];
11316 * opaque sha_hash[20];
11317 * };
11318 *
11319 * md5_hash
11320 * MD5(ClientHello.random + ServerHello.random
11321 * + ServerParams);
11322 * sha_hash
11323 * SHA(ClientHello.random + ServerHello.random
11324 * + ServerParams);
11325 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011326 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011327 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011328 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011329 goto exit;
11330 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011331 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011332 ssl->handshake->randbytes, 64 ) ) != 0 )
11333 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011335 goto exit;
11336 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011337 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011338 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011340 goto exit;
11341 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011342 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011343 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011345 goto exit;
11346 }
11347
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011348 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011349 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011350 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011351 goto exit;
11352 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011353 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011354 ssl->handshake->randbytes, 64 ) ) != 0 )
11355 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011356 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011357 goto exit;
11358 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011359 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011360 data_len ) ) != 0 )
11361 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011362 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011363 goto exit;
11364 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011365 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011366 output + 16 ) ) != 0 )
11367 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011368 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011369 goto exit;
11370 }
11371
11372exit:
11373 mbedtls_md5_free( &mbedtls_md5 );
11374 mbedtls_sha1_free( &mbedtls_sha1 );
11375
11376 if( ret != 0 )
11377 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11378 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11379
11380 return( ret );
11381
11382}
11383#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11384 MBEDTLS_SSL_PROTO_TLS1_1 */
11385
11386#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11387 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011388
11389#if defined(MBEDTLS_USE_PSA_CRYPTO)
11390int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11391 unsigned char *hash, size_t *hashlen,
11392 unsigned char *data, size_t data_len,
11393 mbedtls_md_type_t md_alg )
11394{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011395 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011396 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011397 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11398
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011399 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011400
11401 if( ( status = psa_hash_setup( &hash_operation,
11402 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011403 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011404 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011405 goto exit;
11406 }
11407
Andrzej Kurek814feff2019-01-14 04:35:19 -050011408 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11409 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011410 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011411 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011412 goto exit;
11413 }
11414
Andrzej Kurek814feff2019-01-14 04:35:19 -050011415 if( ( status = psa_hash_update( &hash_operation,
11416 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011417 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011418 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011419 goto exit;
11420 }
11421
Andrzej Kurek814feff2019-01-14 04:35:19 -050011422 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11423 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011424 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011425 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011426 goto exit;
11427 }
11428
11429exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011430 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011431 {
11432 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11433 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011434 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011435 {
11436 case PSA_ERROR_NOT_SUPPORTED:
11437 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011438 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011439 case PSA_ERROR_BUFFER_TOO_SMALL:
11440 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11441 case PSA_ERROR_INSUFFICIENT_MEMORY:
11442 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11443 default:
11444 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11445 }
11446 }
11447 return( 0 );
11448}
11449
11450#else
11451
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011452int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011453 unsigned char *hash, size_t *hashlen,
11454 unsigned char *data, size_t data_len,
11455 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011456{
11457 int ret = 0;
11458 mbedtls_md_context_t ctx;
11459 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011460 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011461
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011462 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011463
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011464 mbedtls_md_init( &ctx );
11465
11466 /*
11467 * digitally-signed struct {
11468 * opaque client_random[32];
11469 * opaque server_random[32];
11470 * ServerDHParams params;
11471 * };
11472 */
11473 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11474 {
11475 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11476 goto exit;
11477 }
11478 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11479 {
11480 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11481 goto exit;
11482 }
11483 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11484 {
11485 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11486 goto exit;
11487 }
11488 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11489 {
11490 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11491 goto exit;
11492 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011493 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011494 {
11495 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11496 goto exit;
11497 }
11498
11499exit:
11500 mbedtls_md_free( &ctx );
11501
11502 if( ret != 0 )
11503 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11504 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11505
11506 return( ret );
11507}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011508#endif /* MBEDTLS_USE_PSA_CRYPTO */
11509
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011510#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11511 MBEDTLS_SSL_PROTO_TLS1_2 */
11512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011513#endif /* MBEDTLS_SSL_TLS_C */