blob: a3c692df73673b092c0c9ba4f14f826d19e33c21 [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é-Gonnarde59ae232019-04-30 11:41:40 +0200980/*
981 * This function will ultimetaly only be responsible for populating a
982 * transform structure from data passed as explicit parameters.
983 *
984 * For now however it's doing rather more in a rather less explicit way.
985 */
986static int ssl_populate_transform( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000987{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200988 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000989#if defined(MBEDTLS_USE_PSA_CRYPTO)
990 int psa_fallthrough;
991#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000993 unsigned char keyblk[256];
994 unsigned char *key1;
995 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100996 unsigned char *mac_enc;
997 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000998 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200999 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001000 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001001 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 const mbedtls_cipher_info_t *cipher_info;
1003 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001004
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001005 /* cf. RFC 5246, Section 8.1:
1006 * "The master secret is always exactly 48 bytes in length." */
1007 size_t const master_secret_len = 48;
1008
Hanno Becker35b23c72018-10-23 12:10:41 +01001009#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1010 unsigned char session_hash[48];
1011#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 mbedtls_ssl_session *session = ssl->session_negotiate;
1014 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
1015 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +00001016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
Jaeden Amero2de07f12019-06-05 13:32:08 +01001019#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1020 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001021 transform->encrypt_then_mac = session->encrypt_then_mac;
1022#endif
1023 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001024
1025 ciphersuite_info = handshake->ciphersuite_info;
1026 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001027 if( cipher_info == NULL )
1028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001030 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001032 }
1033
Hanno Beckere694c3e2017-12-27 21:34:08 +00001034 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001035 if( md_info == NULL )
1036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001038 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001040 }
1041
Hanno Beckera0e20d02019-05-15 14:03:01 +01001042#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001043 /* Copy own and peer's CID if the use of the CID
1044 * extension has been negotiated. */
1045 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1046 {
1047 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001048
Hanno Becker05154c32019-05-03 15:23:51 +01001049 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001050 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001051 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001052 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001053
1054 transform->out_cid_len = ssl->handshake->peer_cid_len;
1055 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1056 ssl->handshake->peer_cid_len );
1057 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1058 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001059 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001060#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001061
Paul Bakker5121ce52009-01-03 21:22:43 +00001062 /*
1063 * SSLv3:
1064 * master =
1065 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1066 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1067 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001068 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001069 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001070 * master = PRF( premaster, "master secret", randbytes )[0..47]
1071 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001072 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001073 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001074 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1075 }
1076 else
1077 {
1078 /* The label for the KDF used for key expansion.
1079 * This is either "master secret" or "extended master secret"
1080 * depending on whether the Extended Master Secret extension
1081 * is used. */
1082 char const *lbl = "master secret";
1083
1084 /* The salt for the KDF used for key expansion.
1085 * - If the Extended Master Secret extension is not used,
1086 * this is ClientHello.Random + ServerHello.Random
1087 * (see Sect. 8.1 in RFC 5246).
1088 * - If the Extended Master Secret extension is used,
1089 * this is the transcript of the handshake so far.
1090 * (see Sect. 4 in RFC 7627). */
1091 unsigned char const *salt = handshake->randbytes;
1092 size_t salt_len = 64;
1093
1094#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001098
Hanno Becker35b23c72018-10-23 12:10:41 +01001099 lbl = "extended master secret";
1100 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001101 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1103 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001106 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1107 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001108 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001109 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001110 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001111#endif /* MBEDTLS_SHA512_C */
1112 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001113 }
1114 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001116 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001117
Hanno Becker35b23c72018-10-23 12:10:41 +01001118 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001119 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001120#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1121
Hanno Becker7d0a5692018-10-23 15:26:22 +01001122#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1123 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1124 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1125 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1126 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001127 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001128 /* Perform PSK-to-MS expansion in a single step. */
1129 psa_status_t status;
1130 psa_algorithm_t alg;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001131 psa_key_handle_t psk;
Janos Follathda6ac012019-08-16 13:47:29 +01001132 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +01001133 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001134
1135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1136
1137 psk = ssl->conf->psk_opaque;
1138 if( ssl->handshake->psk_opaque != 0 )
1139 psk = ssl->handshake->psk_opaque;
1140
Hanno Becker22bf1452019-04-05 11:21:08 +01001141 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001142 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1143 else
1144 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1145
Janos Follathda6ac012019-08-16 13:47:29 +01001146 status = psa_key_derivation( &derivation, psk, alg,
Hanno Becker7d0a5692018-10-23 15:26:22 +01001147 salt, salt_len,
1148 (unsigned char const *) lbl,
1149 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001150 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001151 if( status != PSA_SUCCESS )
1152 {
Janos Follathda6ac012019-08-16 13:47:29 +01001153 psa_key_derivation_abort( &derivation );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001154 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1155 }
1156
Janos Follathda6ac012019-08-16 13:47:29 +01001157 status = psa_key_derivation_output_bytes( &derivation,
Janos Follath6de99db2019-07-30 12:55:06 +01001158 session->master,
1159 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001160 if( status != PSA_SUCCESS )
1161 {
Janos Follathda6ac012019-08-16 13:47:29 +01001162 psa_key_derivation_abort( &derivation );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001163 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1164 }
1165
Janos Follathda6ac012019-08-16 13:47:29 +01001166 status = psa_key_derivation_abort( &derivation );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001167 if( status != PSA_SUCCESS )
1168 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001169 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001170 else
1171#endif
1172 {
1173 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1174 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001175 session->master,
1176 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001177 if( ret != 0 )
1178 {
1179 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1180 return( ret );
1181 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001182
Hanno Becker7d0a5692018-10-23 15:26:22 +01001183 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1184 handshake->premaster,
1185 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001186
Hanno Becker7d0a5692018-10-23 15:26:22 +01001187 mbedtls_platform_zeroize( handshake->premaster,
1188 sizeof(handshake->premaster) );
1189 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001190 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001191
1192 /*
1193 * Swap the client and server random values.
1194 */
Paul Bakker48916f92012-09-16 19:57:18 +00001195 memcpy( tmp, handshake->randbytes, 64 );
1196 memcpy( handshake->randbytes, tmp + 32, 32 );
1197 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001198 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001199
1200 /*
1201 * SSLv3:
1202 * key block =
1203 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1204 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1205 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1206 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1207 * ...
1208 *
1209 * TLSv1:
1210 * key block = PRF( master, "key expansion", randbytes )
1211 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001212 ret = handshake->tls_prf( session->master, 48, "key expansion",
1213 handshake->randbytes, 64, keyblk, 256 );
1214 if( ret != 0 )
1215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001217 return( ret );
1218 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1221 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1222 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1223 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1224 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001225
Paul Bakker5121ce52009-01-03 21:22:43 +00001226 /*
1227 * Determine the appropriate key, IV and MAC length.
1228 */
Paul Bakker68884e32013-01-07 18:20:04 +01001229
Hanno Becker88aaf652017-12-27 08:17:40 +00001230 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001231
Hanno Becker8031d062018-01-03 15:32:31 +00001232#if defined(MBEDTLS_GCM_C) || \
1233 defined(MBEDTLS_CCM_C) || \
1234 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001236 cipher_info->mode == MBEDTLS_MODE_CCM ||
1237 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001239 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001240
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001241 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001242 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001243 transform->taglen =
1244 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001245
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001246 /* All modes haves 96-bit IVs;
1247 * GCM and CCM has 4 implicit and 8 explicit bytes
1248 * ChachaPoly has all 12 bytes implicit
1249 */
Paul Bakker68884e32013-01-07 18:20:04 +01001250 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001251 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1252 transform->fixed_ivlen = 12;
1253 else
1254 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001255
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001256 /* Minimum length of encrypted record */
1257 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001258 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001259 }
1260 else
Hanno Becker8031d062018-01-03 15:32:31 +00001261#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1262#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1263 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1264 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001265 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001266 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1268 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001271 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001272 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001273
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001274 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001275 mac_key_len = mbedtls_md_get_size( md_info );
1276 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001279 /*
1280 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1281 * (rfc 6066 page 13 or rfc 2104 section 4),
1282 * so we only need to adjust the length here.
1283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001287
1288#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1289 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001290 * HMAC implementation which also truncates the key
1291 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001292 mac_key_len = transform->maclen;
1293#endif
1294 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001296
1297 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001298 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001300 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001302 transform->minlen = transform->maclen;
1303 else
Paul Bakker68884e32013-01-07 18:20:04 +01001304 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001305 /*
1306 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001307 * 1. if EtM is in use: one block plus MAC
1308 * otherwise: * first multiple of blocklen greater than maclen
1309 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1312 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001313 {
1314 transform->minlen = transform->maclen
1315 + cipher_info->block_size;
1316 }
1317 else
1318#endif
1319 {
1320 transform->minlen = transform->maclen
1321 + cipher_info->block_size
1322 - transform->maclen % cipher_info->block_size;
1323 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1326 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1327 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001328 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001329 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001330#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1332 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1333 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001334 {
1335 transform->minlen += transform->ivlen;
1336 }
1337 else
1338#endif
1339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001341 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1342 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001343 }
Paul Bakker68884e32013-01-07 18:20:04 +01001344 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 }
Hanno Becker8031d062018-01-03 15:32:31 +00001346 else
1347#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1348 {
1349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1350 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1351 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001352
Hanno Becker88aaf652017-12-27 08:17:40 +00001353 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1354 (unsigned) keylen,
1355 (unsigned) transform->minlen,
1356 (unsigned) transform->ivlen,
1357 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358
1359 /*
1360 * Finally setup the cipher contexts, IVs and MAC secrets.
1361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001363 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001365 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001366 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001367
Paul Bakker68884e32013-01-07 18:20:04 +01001368 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001369 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001371 /*
1372 * This is not used in TLS v1.1.
1373 */
Paul Bakker48916f92012-09-16 19:57:18 +00001374 iv_copy_len = ( transform->fixed_ivlen ) ?
1375 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001376 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1377 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001378 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 }
1380 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381#endif /* MBEDTLS_SSL_CLI_C */
1382#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001383 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001384 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001385 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001386 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001387
Hanno Becker81c7b182017-11-09 18:39:33 +00001388 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001389 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001391 /*
1392 * This is not used in TLS v1.1.
1393 */
Paul Bakker48916f92012-09-16 19:57:18 +00001394 iv_copy_len = ( transform->fixed_ivlen ) ?
1395 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001396 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1397 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001398 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001400 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001404 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1405 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001406 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001407
Hanno Beckerd56ed242018-01-03 15:32:51 +00001408#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#if defined(MBEDTLS_SSL_PROTO_SSL3)
1410 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001411 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001412 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001415 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1416 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001417 }
1418
Hanno Becker81c7b182017-11-09 18:39:33 +00001419 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1420 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001421 }
1422 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1424#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1425 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1426 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001427 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001428 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1429 For AEAD-based ciphersuites, there is nothing to do here. */
1430 if( mac_key_len != 0 )
1431 {
1432 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1433 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1434 }
Paul Bakker68884e32013-01-07 18:20:04 +01001435 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001436 else
1437#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001440 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1441 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001442 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001443#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1446 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001447 {
1448 int ret = 0;
1449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001451
Hanno Becker88aaf652017-12-27 08:17:40 +00001452 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001453 transform->iv_enc, transform->iv_dec,
1454 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001455 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001456 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001459 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1460 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001461 }
1462 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001463#else
1464 ((void) mac_dec);
1465 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001467
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001468#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1469 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001470 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001471 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1472 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001473 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001474 iv_copy_len );
1475 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001476
1477 if( ssl->conf->f_export_keys_ext != NULL )
1478 {
1479 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1480 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001481 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001482 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001483 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001484 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001485 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001486 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001487#endif
1488
Hanno Beckerf704bef2018-11-16 15:21:18 +00001489#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001490
1491 /* Only use PSA-based ciphers for TLS-1.2.
1492 * That's relevant at least for TLS-1.0, where
1493 * we assume that mbedtls_cipher_crypt() updates
1494 * the structure field for the IV, which the PSA-based
1495 * implementation currently doesn't. */
1496#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1497 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001498 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001499 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001500 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001501 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1502 {
1503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001504 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001505 }
1506
1507 if( ret == 0 )
1508 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001509 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001510 psa_fallthrough = 0;
1511 }
1512 else
1513 {
1514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1515 psa_fallthrough = 1;
1516 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001517 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001518 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001519 psa_fallthrough = 1;
1520#else
1521 psa_fallthrough = 1;
1522#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001523
Hanno Beckercb1cc802018-11-17 22:27:38 +00001524 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001525#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001526 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001527 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001528 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001529 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001530 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001531 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001532
Hanno Beckerf704bef2018-11-16 15:21:18 +00001533#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001534 /* Only use PSA-based ciphers for TLS-1.2.
1535 * That's relevant at least for TLS-1.0, where
1536 * we assume that mbedtls_cipher_crypt() updates
1537 * the structure field for the IV, which the PSA-based
1538 * implementation currently doesn't. */
1539#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1540 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001541 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001542 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001543 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001544 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1545 {
1546 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001547 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001548 }
1549
1550 if( ret == 0 )
1551 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001552 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001553 psa_fallthrough = 0;
1554 }
1555 else
1556 {
1557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1558 psa_fallthrough = 1;
1559 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001560 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001561 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001562 psa_fallthrough = 1;
1563#else
1564 psa_fallthrough = 1;
1565#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001566
Hanno Beckercb1cc802018-11-17 22:27:38 +00001567 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001568#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001569 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001570 cipher_info ) ) != 0 )
1571 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001572 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001573 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001574 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001577 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001581 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001582 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001585 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001589 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001590 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592#if defined(MBEDTLS_CIPHER_MODE_CBC)
1593 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1596 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001599 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001600 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1603 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001606 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001607 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001608 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001610
Paul Bakker5121ce52009-01-03 21:22:43 +00001611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001613 // Initialize compression
1614 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001616 {
Paul Bakker16770332013-10-11 09:59:44 +02001617 if( ssl->compress_buf == NULL )
1618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001620 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001621 if( ssl->compress_buf == NULL )
1622 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001624 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001625 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1626 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001627 }
1628 }
1629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001631
Paul Bakker48916f92012-09-16 19:57:18 +00001632 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1633 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001634
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001635 if( deflateInit( &transform->ctx_deflate,
1636 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001637 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001640 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1641 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001642 }
1643 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001647end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001648 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1649 mbedtls_platform_zeroize( handshake->randbytes,
1650 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001651 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001652}
1653
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001654static int ssl_set_handshake_prfs( mbedtls_ssl_context *ssl )
1655{
1656 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1657 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1658 ciphersuite_info = handshake->ciphersuite_info;
1659
1660 /*
1661 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
1662 */
1663#if defined(MBEDTLS_SSL_PROTO_SSL3)
1664 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1665 {
1666 handshake->tls_prf = ssl3_prf;
1667 handshake->calc_verify = ssl_calc_verify_ssl;
1668 handshake->calc_finished = ssl_calc_finished_ssl;
1669 }
1670 else
1671#endif
1672#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1673 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
1674 {
1675 handshake->tls_prf = tls1_prf;
1676 handshake->calc_verify = ssl_calc_verify_tls;
1677 handshake->calc_finished = ssl_calc_finished_tls;
1678 }
1679 else
1680#endif
1681#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1682#if defined(MBEDTLS_SHA512_C)
1683 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1684 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1685 {
1686 handshake->tls_prf = tls_prf_sha384;
1687 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1688 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1689 }
1690 else
1691#endif
1692#if defined(MBEDTLS_SHA256_C)
1693 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1694 {
1695 handshake->tls_prf = tls_prf_sha256;
1696 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1697 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1698 }
1699 else
1700#endif
1701#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1702 {
1703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1704 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1705 }
1706
1707 return( 0 );
1708}
1709
1710
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001711int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1712{
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001713 int ret;
1714
1715 ret = ssl_set_handshake_prfs( ssl );
1716 if( ret != 0 )
1717 return( ret );
1718
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001719 return( ssl_populate_transform( ssl ) );
1720}
1721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722#if defined(MBEDTLS_SSL_PROTO_SSL3)
1723void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001724{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001725 mbedtls_md5_context md5;
1726 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001727 unsigned char pad_1[48];
1728 unsigned char pad_2[48];
1729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001731
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001732 mbedtls_md5_init( &md5 );
1733 mbedtls_sha1_init( &sha1 );
1734
1735 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1736 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001737
Paul Bakker380da532012-04-18 16:10:25 +00001738 memset( pad_1, 0x36, 48 );
1739 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001740
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001741 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1742 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1743 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001744
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001745 mbedtls_md5_starts_ret( &md5 );
1746 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1747 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1748 mbedtls_md5_update_ret( &md5, hash, 16 );
1749 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001750
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001751 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1752 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1753 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001754
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001755 mbedtls_sha1_starts_ret( &sha1 );
1756 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1757 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1758 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1759 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_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1772void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001773{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001774 mbedtls_md5_context md5;
1775 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001778
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001779 mbedtls_md5_init( &md5 );
1780 mbedtls_sha1_init( &sha1 );
1781
1782 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1783 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001784
Andrzej Kurekeb342242019-01-29 09:14:33 -05001785 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001786 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001790
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001791 mbedtls_md5_free( &md5 );
1792 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001793
Paul Bakker380da532012-04-18 16:10:25 +00001794 return;
1795}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1799#if defined(MBEDTLS_SHA256_C)
1800void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001801{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001802#if defined(MBEDTLS_USE_PSA_CRYPTO)
1803 size_t hash_size;
1804 psa_status_t status;
1805 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1806
1807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1808 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1809 if( status != PSA_SUCCESS )
1810 {
1811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1812 return;
1813 }
1814
1815 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1816 if( status != PSA_SUCCESS )
1817 {
1818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1819 return;
1820 }
1821 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1823#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001824 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001825
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001826 mbedtls_sha256_init( &sha256 );
1827
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001829
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001830 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001831 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001835
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001836 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001837#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001838 return;
1839}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842#if defined(MBEDTLS_SHA512_C)
1843void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001844{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001845#if defined(MBEDTLS_USE_PSA_CRYPTO)
1846 size_t hash_size;
1847 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001848 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001849
1850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001851 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001852 if( status != PSA_SUCCESS )
1853 {
1854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1855 return;
1856 }
1857
Andrzej Kurek972fba52019-01-30 03:29:12 -05001858 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001859 if( status != PSA_SUCCESS )
1860 {
1861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1862 return;
1863 }
1864 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1866#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001867 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001868
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001869 mbedtls_sha512_init( &sha512 );
1870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001872
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001873 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001874 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001878
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001879 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001880#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001881 return;
1882}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883#endif /* MBEDTLS_SHA512_C */
1884#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1887int 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 +02001888{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001889 unsigned char *p = ssl->handshake->premaster;
1890 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001891 const unsigned char *psk = ssl->conf->psk;
1892 size_t psk_len = ssl->conf->psk_len;
1893
1894 /* If the psk callback was called, use its result */
1895 if( ssl->handshake->psk != NULL )
1896 {
1897 psk = ssl->handshake->psk;
1898 psk_len = ssl->handshake->psk_len;
1899 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001900
1901 /*
1902 * PMS = struct {
1903 * opaque other_secret<0..2^16-1>;
1904 * opaque psk<0..2^16-1>;
1905 * };
1906 * with "other_secret" depending on the particular key exchange
1907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1909 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001910 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001911 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001913
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001914 *(p++) = (unsigned char)( psk_len >> 8 );
1915 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001916
1917 if( end < p || (size_t)( end - p ) < psk_len )
1918 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1919
1920 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001921 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001922 }
1923 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1925#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1926 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001927 {
1928 /*
1929 * other_secret already set by the ClientKeyExchange message,
1930 * and is 48 bytes long
1931 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001932 if( end - p < 2 )
1933 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1934
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001935 *p++ = 0;
1936 *p++ = 48;
1937 p += 48;
1938 }
1939 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1941#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1942 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001943 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001944 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001945 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001946
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001947 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001949 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001950 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001953 return( ret );
1954 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001955 *(p++) = (unsigned char)( len >> 8 );
1956 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001957 p += len;
1958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001960 }
1961 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1963#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1964 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001965 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001966 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001967 size_t zlen;
1968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001970 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001971 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001974 return( ret );
1975 }
1976
1977 *(p++) = (unsigned char)( zlen >> 8 );
1978 *(p++) = (unsigned char)( zlen );
1979 p += zlen;
1980
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001981 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1982 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001983 }
1984 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1988 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001989 }
1990
1991 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001992 if( end - p < 2 )
1993 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001994
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001995 *(p++) = (unsigned char)( psk_len >> 8 );
1996 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001997
1998 if( end < p || (size_t)( end - p ) < psk_len )
1999 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2000
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002001 memcpy( p, psk, psk_len );
2002 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002003
2004 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2005
2006 return( 0 );
2007}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002011/*
2012 * SSLv3.0 MAC functions
2013 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002014#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002015static void ssl_mac( mbedtls_md_context_t *md_ctx,
2016 const unsigned char *secret,
2017 const unsigned char *buf, size_t len,
2018 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002019 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002020{
2021 unsigned char header[11];
2022 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002023 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2025 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002026
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002027 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002029 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002030 else
Paul Bakker68884e32013-01-07 18:20:04 +01002031 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002032
2033 memcpy( header, ctr, 8 );
2034 header[ 8] = (unsigned char) type;
2035 header[ 9] = (unsigned char)( len >> 8 );
2036 header[10] = (unsigned char)( len );
2037
Paul Bakker68884e32013-01-07 18:20:04 +01002038 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039 mbedtls_md_starts( md_ctx );
2040 mbedtls_md_update( md_ctx, secret, md_size );
2041 mbedtls_md_update( md_ctx, padding, padlen );
2042 mbedtls_md_update( md_ctx, header, 11 );
2043 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002044 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002045
Paul Bakker68884e32013-01-07 18:20:04 +01002046 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 mbedtls_md_starts( md_ctx );
2048 mbedtls_md_update( md_ctx, secret, md_size );
2049 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002050 mbedtls_md_update( md_ctx, out, md_size );
2051 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002052}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002054
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002055/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002056 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002057#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002058 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2059 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2060 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2061/* This function makes sure every byte in the memory region is accessed
2062 * (in ascending addresses order) */
2063static void ssl_read_memory( unsigned char *p, size_t len )
2064{
2065 unsigned char acc = 0;
2066 volatile unsigned char force;
2067
2068 for( ; len != 0; p++, len-- )
2069 acc ^= *p;
2070
2071 force = acc;
2072 (void) force;
2073}
2074#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2075
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002076/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002077 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002078 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002079
Hanno Beckera0e20d02019-05-15 14:03:01 +01002080#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002081/* This functions transforms a DTLS plaintext fragment and a record content
2082 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002083 *
2084 * struct {
2085 * opaque content[DTLSPlaintext.length];
2086 * ContentType real_type;
2087 * uint8 zeros[length_of_padding];
2088 * } DTLSInnerPlaintext;
2089 *
2090 * Input:
2091 * - `content`: The beginning of the buffer holding the
2092 * plaintext to be wrapped.
2093 * - `*content_size`: The length of the plaintext in Bytes.
2094 * - `max_len`: The number of Bytes available starting from
2095 * `content`. This must be `>= *content_size`.
2096 * - `rec_type`: The desired record content type.
2097 *
2098 * Output:
2099 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2100 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2101 *
2102 * Returns:
2103 * - `0` on success.
2104 * - A negative error code if `max_len` didn't offer enough space
2105 * for the expansion.
2106 */
2107static int ssl_cid_build_inner_plaintext( unsigned char *content,
2108 size_t *content_size,
2109 size_t remaining,
2110 uint8_t rec_type )
2111{
2112 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002113 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2114 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2115 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002116
2117 /* Write real content type */
2118 if( remaining == 0 )
2119 return( -1 );
2120 content[ len ] = rec_type;
2121 len++;
2122 remaining--;
2123
2124 if( remaining < pad )
2125 return( -1 );
2126 memset( content + len, 0, pad );
2127 len += pad;
2128 remaining -= pad;
2129
2130 *content_size = len;
2131 return( 0 );
2132}
2133
Hanno Becker07dc97d2019-05-20 15:08:01 +01002134/* This function parses a DTLSInnerPlaintext structure.
2135 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002136static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2137 size_t *content_size,
2138 uint8_t *rec_type )
2139{
2140 size_t remaining = *content_size;
2141
2142 /* Determine length of padding by skipping zeroes from the back. */
2143 do
2144 {
2145 if( remaining == 0 )
2146 return( -1 );
2147 remaining--;
2148 } while( content[ remaining ] == 0 );
2149
2150 *content_size = remaining;
2151 *rec_type = content[ remaining ];
2152
2153 return( 0 );
2154}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002155#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002156
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002157/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002158 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002159static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002160 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002161 mbedtls_record *rec )
2162{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002163 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002164 *
2165 * additional_data = seq_num + TLSCompressed.type +
2166 * TLSCompressed.version + TLSCompressed.length;
2167 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002168 * For the CID extension, this is extended as follows
2169 * (quoting draft-ietf-tls-dtls-connection-id-05,
2170 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002171 *
2172 * additional_data = seq_num + DTLSPlaintext.type +
2173 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002174 * cid +
2175 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002176 * length_of_DTLSInnerPlaintext;
2177 */
2178
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002179 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2180 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002181 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002182
Hanno Beckera0e20d02019-05-15 14:03:01 +01002183#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002184 if( rec->cid_len != 0 )
2185 {
2186 memcpy( add_data + 11, rec->cid, rec->cid_len );
2187 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2188 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2189 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2190 *add_data_len = 13 + 1 + rec->cid_len;
2191 }
2192 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002193#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002194 {
2195 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2196 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2197 *add_data_len = 13;
2198 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002199}
2200
Hanno Beckera18d1322018-01-03 14:27:32 +00002201int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2202 mbedtls_ssl_transform *transform,
2203 mbedtls_record *rec,
2204 int (*f_rng)(void *, unsigned char *, size_t),
2205 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002206{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002208 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002209 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002210 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002211 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002212 size_t post_avail;
2213
2214 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002215#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002216 ((void) ssl);
2217#endif
2218
2219 /* The PRNG is used for dynamic IV generation that's used
2220 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2221#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2222 ( defined(MBEDTLS_AES_C) || \
2223 defined(MBEDTLS_ARIA_C) || \
2224 defined(MBEDTLS_CAMELLIA_C) ) && \
2225 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2226 ((void) f_rng);
2227 ((void) p_rng);
2228#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002231
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002232 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002233 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2235 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2236 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002237 if( rec == NULL
2238 || rec->buf == NULL
2239 || rec->buf_len < rec->data_offset
2240 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002241#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002242 || rec->cid_len != 0
2243#endif
2244 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002245 {
2246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002248 }
2249
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002250 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002251 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002253 data, rec->data_len );
2254
2255 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2256
2257 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2258 {
2259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2260 (unsigned) rec->data_len,
2261 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2262 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2263 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002264
Hanno Beckera0e20d02019-05-15 14:03:01 +01002265#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002266 /*
2267 * Add CID information
2268 */
2269 rec->cid_len = transform->out_cid_len;
2270 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2271 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002272
2273 if( rec->cid_len != 0 )
2274 {
2275 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002276 * Wrap plaintext into DTLSInnerPlaintext structure.
2277 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002278 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002279 * Note that this changes `rec->data_len`, and hence
2280 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002281 */
2282 if( ssl_cid_build_inner_plaintext( data,
2283 &rec->data_len,
2284 post_avail,
2285 rec->type ) != 0 )
2286 {
2287 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2288 }
2289
2290 rec->type = MBEDTLS_SSL_MSG_CID;
2291 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002292#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002293
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002294 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2295
Paul Bakker5121ce52009-01-03 21:22:43 +00002296 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002297 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002298 */
Hanno Becker52344c22018-01-03 15:24:20 +00002299#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 if( mode == MBEDTLS_MODE_STREAM ||
2301 ( mode == MBEDTLS_MODE_CBC
2302#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002303 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002304#endif
2305 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002306 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002307 if( post_avail < transform->maclen )
2308 {
2309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2310 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2311 }
2312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002314 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002315 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002316 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002317 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2318 data, rec->data_len, rec->ctr, rec->type, mac );
2319 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002320 }
2321 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002322#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2324 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002325 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002326 {
Hanno Becker992b6872017-11-09 18:57:39 +00002327 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2328
Hanno Beckercab87e62019-04-29 13:52:53 +01002329 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002330
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002331 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002332 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002333 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2334 data, rec->data_len );
2335 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2336 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2337
2338 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002339 }
2340 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002341#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2344 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002345 }
2346
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002347 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2348 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002349
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002350 rec->data_len += transform->maclen;
2351 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002352 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002353 }
Hanno Becker52344c22018-01-03 15:24:20 +00002354#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002355
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002356 /*
2357 * Encrypt
2358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2360 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002361 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002362 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002363 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002365 "including %d bytes of padding",
2366 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002367
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002368 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2369 transform->iv_enc, transform->ivlen,
2370 data, rec->data_len,
2371 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002372 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002374 return( ret );
2375 }
2376
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002377 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2380 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002381 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002382 }
Paul Bakker68884e32013-01-07 18:20:04 +01002383 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002385
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002386#if defined(MBEDTLS_GCM_C) || \
2387 defined(MBEDTLS_CCM_C) || \
2388 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002390 mode == MBEDTLS_MODE_CCM ||
2391 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002392 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002393 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002394 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002395 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002396
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002397 /* Check that there's space for both the authentication tag
2398 * and the explicit IV before and after the record content. */
2399 if( post_avail < transform->taglen ||
2400 rec->data_offset < explicit_iv_len )
2401 {
2402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2403 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2404 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002405
Paul Bakker68884e32013-01-07 18:20:04 +01002406 /*
2407 * Generate IV
2408 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002409 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2410 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002411 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002412 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002413 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2414 explicit_iv_len );
2415 /* Prefix record content with explicit IV. */
2416 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002417 }
2418 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2419 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002420 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002421 unsigned char i;
2422
2423 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2424
2425 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002426 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002427 }
2428 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002429 {
2430 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2432 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002433 }
2434
Hanno Beckercab87e62019-04-29 13:52:53 +01002435 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002436
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002437 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2438 iv, transform->ivlen );
2439 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002440 data - explicit_iv_len, explicit_iv_len );
2441 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002442 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002444 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002445 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002446
Paul Bakker68884e32013-01-07 18:20:04 +01002447 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002448 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002449 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002450
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002451 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002452 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002453 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002454 data, rec->data_len, /* source */
2455 data, &rec->data_len, /* destination */
2456 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002459 return( ret );
2460 }
2461
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002462 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2463 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002464
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002465 rec->data_len += transform->taglen + explicit_iv_len;
2466 rec->data_offset -= explicit_iv_len;
2467 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002468 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002469 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2472#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002473 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002476 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002477 size_t padlen, i;
2478 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002479
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002480 /* Currently we're always using minimal padding
2481 * (up to 255 bytes would be allowed). */
2482 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2483 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002484 padlen = 0;
2485
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002486 /* Check there's enough space in the buffer for the padding. */
2487 if( post_avail < padlen + 1 )
2488 {
2489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2490 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2491 }
2492
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002494 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002495
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002496 rec->data_len += padlen + 1;
2497 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002500 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002501 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2502 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002503 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002504 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002505 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002506 if( f_rng == NULL )
2507 {
2508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2509 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2510 }
2511
2512 if( rec->data_offset < transform->ivlen )
2513 {
2514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2515 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2516 }
2517
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002518 /*
2519 * Generate IV
2520 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002521 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002522 if( ret != 0 )
2523 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002524
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002525 memcpy( data - transform->ivlen, transform->iv_enc,
2526 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002527
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002528 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002532 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002533 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002534 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002536 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2537 transform->iv_enc,
2538 transform->ivlen,
2539 data, rec->data_len,
2540 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002543 return( ret );
2544 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002545
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002546 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2549 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002550 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002553 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002554 {
2555 /*
2556 * Save IV in SSL3 and TLS1
2557 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002558 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2559 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002561 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002562#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002563 {
2564 data -= transform->ivlen;
2565 rec->data_offset -= transform->ivlen;
2566 rec->data_len += transform->ivlen;
2567 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002570 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002571 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002572 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2573
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002574 /*
2575 * MAC(MAC_write_key, seq_num +
2576 * TLSCipherText.type +
2577 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002578 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002579 * IV + // except for TLS 1.0
2580 * ENC(content + padding + padding_length));
2581 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002582
2583 if( post_avail < transform->maclen)
2584 {
2585 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2586 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2587 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002588
Hanno Beckercab87e62019-04-29 13:52:53 +01002589 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002592 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002593 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002594
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002595 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002596 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002597 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2598 data, rec->data_len );
2599 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2600 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002601
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002602 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002603
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002604 rec->data_len += transform->maclen;
2605 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002606 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002607 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002610 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002612 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2615 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002616 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002617
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002618 /* Make extra sure authentication was performed, exactly once */
2619 if( auth_done != 1 )
2620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2622 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002623 }
2624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002626
2627 return( 0 );
2628}
2629
Hanno Becker605949f2019-07-12 08:23:59 +01002630int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002631 mbedtls_ssl_transform *transform,
2632 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002633{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002634 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002636 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002637#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002638 size_t padlen = 0, correct = 1;
2639#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002640 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002641 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002642 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002643
Hanno Beckera18d1322018-01-03 14:27:32 +00002644#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002645 ((void) ssl);
2646#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002649 if( rec == NULL ||
2650 rec->buf == NULL ||
2651 rec->buf_len < rec->data_offset ||
2652 rec->buf_len - rec->data_offset < rec->data_len )
2653 {
2654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002656 }
2657
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002658 data = rec->buf + rec->data_offset;
2659 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Hanno Beckera0e20d02019-05-15 14:03:01 +01002661#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002662 /*
2663 * Match record's CID with incoming CID.
2664 */
Hanno Becker938489a2019-05-08 13:02:22 +01002665 if( rec->cid_len != transform->in_cid_len ||
2666 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2667 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002668 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002669 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002670#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2673 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002674 {
2675 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002676 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2677 transform->iv_dec,
2678 transform->ivlen,
2679 data, rec->data_len,
2680 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002683 return( ret );
2684 }
2685
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002686 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2689 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002690 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 }
Paul Bakker68884e32013-01-07 18:20:04 +01002692 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002694#if defined(MBEDTLS_GCM_C) || \
2695 defined(MBEDTLS_CCM_C) || \
2696 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002698 mode == MBEDTLS_MODE_CCM ||
2699 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002700 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002701 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002702 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002703
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002704 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002705 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002706 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002707
2708 /* Check that there's enough space for the explicit IV
2709 * (at the beginning of the record) and the MAC (at the
2710 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002711 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002714 "+ taglen (%d)", rec->data_len,
2715 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002717 }
Paul Bakker68884e32013-01-07 18:20:04 +01002718
Hanno Beckerd96a6522019-07-10 13:55:25 +01002719#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002720 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2721 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002722 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002723
Hanno Beckerd96a6522019-07-10 13:55:25 +01002724 /* Fixed */
2725 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2726 /* Explicit */
2727 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002728 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002729 else
2730#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2731#if defined(MBEDTLS_CHACHAPOLY_C)
2732 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002733 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002734 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002735 unsigned char i;
2736
2737 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2738
2739 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002740 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002741 }
2742 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002743#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002744 {
2745 /* Reminder if we ever add an AEAD mode with a different size */
2746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2747 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2748 }
2749
Hanno Beckerd96a6522019-07-10 13:55:25 +01002750 /* Group changes to data, data_len, and add_data, because
2751 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002752 data += explicit_iv_len;
2753 rec->data_offset += explicit_iv_len;
2754 rec->data_len -= explicit_iv_len + transform->taglen;
2755
Hanno Beckercab87e62019-04-29 13:52:53 +01002756 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002757 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002758 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002759
Hanno Beckerd96a6522019-07-10 13:55:25 +01002760 /* Because of the check above, we know that there are
2761 * explicit_iv_len Bytes preceeding data, and taglen
2762 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002763 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002764 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002765
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002766 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002767 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002768 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002769
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002770 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002771 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002772 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002773 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2774 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002775 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002776 data, rec->data_len,
2777 data, &olen,
2778 data + rec->data_len,
2779 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2784 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002785
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002786 return( ret );
2787 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002788 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002789
Hanno Beckerd96a6522019-07-10 13:55:25 +01002790 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002791 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2794 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002795 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002796 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002797 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2799#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002800 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002802 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002803 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002804
Paul Bakker5121ce52009-01-03 21:22:43 +00002805 /*
Paul Bakker45829992013-01-03 14:52:21 +01002806 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002807 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002809 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2810 {
2811 /* The ciphertext is prefixed with the CBC IV. */
2812 minlen += transform->ivlen;
2813 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002814#endif
Paul Bakker45829992013-01-03 14:52:21 +01002815
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002816 /* Size considerations:
2817 *
2818 * - The CBC cipher text must not be empty and hence
2819 * at least of size transform->ivlen.
2820 *
2821 * Together with the potential IV-prefix, this explains
2822 * the first of the two checks below.
2823 *
2824 * - The record must contain a MAC, either in plain or
2825 * encrypted, depending on whether Encrypt-then-MAC
2826 * is used or not.
2827 * - If it is, the message contains the IV-prefix,
2828 * the CBC ciphertext, and the MAC.
2829 * - If it is not, the padded plaintext, and hence
2830 * the CBC ciphertext, has at least length maclen + 1
2831 * because there is at least the padding length byte.
2832 *
2833 * As the CBC ciphertext is not empty, both cases give the
2834 * lower bound minlen + maclen + 1 on the record size, which
2835 * we test for in the second check below.
2836 */
2837 if( rec->data_len < minlen + transform->ivlen ||
2838 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002841 "+ 1 ) ( + expl IV )", rec->data_len,
2842 transform->ivlen,
2843 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002845 }
2846
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002847 /*
2848 * Authenticate before decrypt if enabled
2849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002851 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002852 {
Hanno Becker992b6872017-11-09 18:57:39 +00002853 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002856
Hanno Beckerd96a6522019-07-10 13:55:25 +01002857 /* Update data_len in tandem with add_data.
2858 *
2859 * The subtraction is safe because of the previous check
2860 * data_len >= minlen + maclen + 1.
2861 *
2862 * Afterwards, we know that data + data_len is followed by at
2863 * least maclen Bytes, which justifies the call to
2864 * mbedtls_ssl_safer_memcmp() below.
2865 *
2866 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002867 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002868 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002869
Hanno Beckerd96a6522019-07-10 13:55:25 +01002870 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002871 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2872 add_data_len );
2873 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2874 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002875 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2876 data, rec->data_len );
2877 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2878 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002879
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002880 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2881 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002882 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002883 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002884
Hanno Beckerd96a6522019-07-10 13:55:25 +01002885 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002886 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2887 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002891 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002892 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002893 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002895
2896 /*
2897 * Check length sanity
2898 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002899
2900 /* We know from above that data_len > minlen >= 0,
2901 * so the following check in particular implies that
2902 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002903 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002906 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002908 }
2909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002911 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002912 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002913 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002914 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002915 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002916 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002917 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002918
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002919 data += transform->ivlen;
2920 rec->data_offset += transform->ivlen;
2921 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002922 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002924
Hanno Beckerd96a6522019-07-10 13:55:25 +01002925 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
2926
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002927 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2928 transform->iv_dec, transform->ivlen,
2929 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002932 return( ret );
2933 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002934
Hanno Beckerd96a6522019-07-10 13:55:25 +01002935 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002936 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2939 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002940 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002943 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002944 {
2945 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002946 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
2947 * records is equivalent to CBC decryption of the concatenation
2948 * of the records; in other words, IVs are maintained across
2949 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02002950 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002951 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2952 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002953 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002954#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002955
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002956 /* Safe since data_len >= minlen + maclen + 1, so after having
2957 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01002958 * data_len > 0 (because of data_len % ivlen == 0, it's actually
2959 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002960 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002961
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002962 if( auth_done == 1 )
2963 {
2964 correct *= ( rec->data_len >= padlen + 1 );
2965 padlen *= ( rec->data_len >= padlen + 1 );
2966 }
2967 else
Paul Bakker45829992013-01-03 14:52:21 +01002968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002970 if( rec->data_len < transform->maclen + padlen + 1 )
2971 {
2972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2973 rec->data_len,
2974 transform->maclen,
2975 padlen + 1 ) );
2976 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002977#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002978
2979 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2980 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002981 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002982
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002983 padlen++;
2984
2985 /* Regardless of the validity of the padding,
2986 * we have data_len >= padlen here. */
2987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002989 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002990 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002991 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993#if defined(MBEDTLS_SSL_DEBUG_ALL)
2994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002995 "should be no more than %d",
2996 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002997#endif
Paul Bakker45829992013-01-03 14:52:21 +01002998 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002999 }
3000 }
3001 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3003#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3004 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003005 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003006 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003007 /* The padding check involves a series of up to 256
3008 * consecutive memory reads at the end of the record
3009 * plaintext buffer. In order to hide the length and
3010 * validity of the padding, always perform exactly
3011 * `min(256,plaintext_len)` reads (but take into account
3012 * only the last `padlen` bytes for the padding check). */
3013 size_t pad_count = 0;
3014 size_t real_count = 0;
3015 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003016
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003017 /* Index of first padding byte; it has been ensured above
3018 * that the subtraction is safe. */
3019 size_t const padding_idx = rec->data_len - padlen;
3020 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3021 size_t const start_idx = rec->data_len - num_checks;
3022 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003023
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003024 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003025 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003026 real_count |= ( idx >= padding_idx );
3027 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003028 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003029 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003032 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003034#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003035 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003036 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003037 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3039 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3042 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003043 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003044
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003045 /* If the padding was found to be invalid, padlen == 0
3046 * and the subtraction is safe. If the padding was found valid,
3047 * padlen hasn't been changed and the previous assertion
3048 * data_len >= padlen still holds. */
3049 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003050 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003051 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003053 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3056 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003057 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003059#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003061 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003062#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003063
3064 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003065 * Authenticate if not done yet.
3066 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003067 */
Hanno Becker52344c22018-01-03 15:24:20 +00003068#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003069 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003070 {
Hanno Becker992b6872017-11-09 18:57:39 +00003071 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003072
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003073 /* If the initial value of padlen was such that
3074 * data_len < maclen + padlen + 1, then padlen
3075 * got reset to 1, and the initial check
3076 * data_len >= minlen + maclen + 1
3077 * guarantees that at this point we still
3078 * have at least data_len >= maclen.
3079 *
3080 * If the initial value of padlen was such that
3081 * data_len >= maclen + padlen + 1, then we have
3082 * subtracted either padlen + 1 (if the padding was correct)
3083 * or 0 (if the padding was incorrect) since then,
3084 * hence data_len >= maclen in any case.
3085 */
3086 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003087 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003090 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003091 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003092 ssl_mac( &transform->md_ctx_dec,
3093 transform->mac_dec,
3094 data, rec->data_len,
3095 rec->ctr, rec->type,
3096 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003097 }
3098 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3100#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3101 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003102 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003103 {
3104 /*
3105 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003106 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003107 *
3108 * Known timing attacks:
3109 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3110 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003111 * To compensate for different timings for the MAC calculation
3112 * depending on how much padding was removed (which is determined
3113 * by padlen), process extra_run more blocks through the hash
3114 * function.
3115 *
3116 * The formula in the paper is
3117 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3118 * where L1 is the size of the header plus the decrypted message
3119 * plus CBC padding and L2 is the size of the header plus the
3120 * decrypted message. This is for an underlying hash function
3121 * with 64-byte blocks.
3122 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3123 * correctly. We round down instead of up, so -56 is the correct
3124 * value for our calculations instead of -55.
3125 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003126 * Repeat the formula rather than defining a block_size variable.
3127 * This avoids requiring division by a variable at runtime
3128 * (which would be marginally less efficient and would require
3129 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003130 */
3131 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003132 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003133
3134 /*
3135 * The next two sizes are the minimum and maximum values of
3136 * in_msglen over all padlen values.
3137 *
3138 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003139 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003140 *
3141 * Note that max_len + maclen is never more than the buffer
3142 * length, as we previously did in_msglen -= maclen too.
3143 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003144 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003145 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3146
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003147 memset( tmp, 0, sizeof( tmp ) );
3148
3149 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003150 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003151#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3152 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003153 case MBEDTLS_MD_MD5:
3154 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003155 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003156 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003157 extra_run =
3158 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3159 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003160 break;
3161#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003162#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003163 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003164 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003165 extra_run =
3166 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3167 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003168 break;
3169#endif
3170 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003172 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3173 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003174
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003175 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003176
Hanno Beckercab87e62019-04-29 13:52:53 +01003177 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3178 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003179 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3180 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003181 /* Make sure we access everything even when padlen > 0. This
3182 * makes the synchronisation requirements for just-in-time
3183 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003184 ssl_read_memory( data + rec->data_len, padlen );
3185 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003186
3187 /* Call mbedtls_md_process at least once due to cache attacks
3188 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003189 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003190 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003191
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003192 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003193
3194 /* Make sure we access all the memory that could contain the MAC,
3195 * before we check it in the next code block. This makes the
3196 * synchronisation requirements for just-in-time Prime+Probe
3197 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003198 ssl_read_memory( data + min_len,
3199 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003200 }
3201 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3203 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3206 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003207 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003208
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003209#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003210 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3211 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003212#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003213
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003214 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3215 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217#if defined(MBEDTLS_SSL_DEBUG_ALL)
3218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003219#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003220 correct = 0;
3221 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003222 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003223 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003224
3225 /*
3226 * Finally check the correct flag
3227 */
3228 if( correct == 0 )
3229 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003230#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003231
3232 /* Make extra sure authentication was performed, exactly once */
3233 if( auth_done != 1 )
3234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3236 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003237 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003238
Hanno Beckera0e20d02019-05-15 14:03:01 +01003239#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003240 if( rec->cid_len != 0 )
3241 {
3242 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3243 &rec->type );
3244 if( ret != 0 )
3245 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3246 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003247#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003250
3251 return( 0 );
3252}
3253
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003254#undef MAC_NONE
3255#undef MAC_PLAINTEXT
3256#undef MAC_CIPHERTEXT
3257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003259/*
3260 * Compression/decompression functions
3261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003263{
3264 int ret;
3265 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003266 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003267 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003268 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003271
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003272 if( len_pre == 0 )
3273 return( 0 );
3274
Paul Bakker2770fbd2012-07-03 13:30:23 +00003275 memcpy( msg_pre, ssl->out_msg, len_pre );
3276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003277 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003278 ssl->out_msglen ) );
3279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003281 ssl->out_msg, ssl->out_msglen );
3282
Paul Bakker48916f92012-09-16 19:57:18 +00003283 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3284 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3285 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003286 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003287
Paul Bakker48916f92012-09-16 19:57:18 +00003288 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003289 if( ret != Z_OK )
3290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3292 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003293 }
3294
Angus Grattond8213d02016-05-25 20:56:48 +10003295 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003296 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003299 ssl->out_msglen ) );
3300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003302 ssl->out_msg, ssl->out_msglen );
3303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003305
3306 return( 0 );
3307}
3308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003310{
3311 int ret;
3312 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003313 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003314 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003315 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003318
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003319 if( len_pre == 0 )
3320 return( 0 );
3321
Paul Bakker2770fbd2012-07-03 13:30:23 +00003322 memcpy( msg_pre, ssl->in_msg, len_pre );
3323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003325 ssl->in_msglen ) );
3326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003327 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003328 ssl->in_msg, ssl->in_msglen );
3329
Paul Bakker48916f92012-09-16 19:57:18 +00003330 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3331 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3332 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003333 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003334 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003335
Paul Bakker48916f92012-09-16 19:57:18 +00003336 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003337 if( ret != Z_OK )
3338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3340 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003341 }
3342
Angus Grattond8213d02016-05-25 20:56:48 +10003343 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003344 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003347 ssl->in_msglen ) );
3348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003350 ssl->in_msg, ssl->in_msglen );
3351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003353
3354 return( 0 );
3355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3359static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361#if defined(MBEDTLS_SSL_PROTO_DTLS)
3362static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003363{
3364 /* If renegotiation is not enforced, retransmit until we would reach max
3365 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003366 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003367 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003368 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003369 unsigned char doublings = 1;
3370
3371 while( ratio != 0 )
3372 {
3373 ++doublings;
3374 ratio >>= 1;
3375 }
3376
3377 if( ++ssl->renego_records_seen > doublings )
3378 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003380 return( 0 );
3381 }
3382 }
3383
3384 return( ssl_write_hello_request( ssl ) );
3385}
3386#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003388
Paul Bakker5121ce52009-01-03 21:22:43 +00003389/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003390 * Fill the input message buffer by appending data to it.
3391 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003392 *
3393 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3394 * available (from this read and/or a previous one). Otherwise, an error code
3395 * is returned (possibly EOF or WANT_READ).
3396 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003397 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3398 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3399 * since we always read a whole datagram at once.
3400 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003401 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003402 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003403 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003405{
Paul Bakker23986e52011-04-24 08:57:21 +00003406 int ret;
3407 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003410
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003411 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003414 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003416 }
3417
Angus Grattond8213d02016-05-25 20:56:48 +10003418 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3421 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003422 }
3423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003425 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003426 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003427 uint32_t timeout;
3428
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003429 /* Just to be sure */
3430 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3431 {
3432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3433 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3434 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3435 }
3436
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003437 /*
3438 * The point is, we need to always read a full datagram at once, so we
3439 * sometimes read more then requested, and handle the additional data.
3440 * It could be the rest of the current record (while fetching the
3441 * header) and/or some other records in the same datagram.
3442 */
3443
3444 /*
3445 * Move to the next record in the already read datagram if applicable
3446 */
3447 if( ssl->next_record_offset != 0 )
3448 {
3449 if( ssl->in_left < ssl->next_record_offset )
3450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3452 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003453 }
3454
3455 ssl->in_left -= ssl->next_record_offset;
3456
3457 if( ssl->in_left != 0 )
3458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003460 ssl->next_record_offset ) );
3461 memmove( ssl->in_hdr,
3462 ssl->in_hdr + ssl->next_record_offset,
3463 ssl->in_left );
3464 }
3465
3466 ssl->next_record_offset = 0;
3467 }
3468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003470 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003471
3472 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003473 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003474 */
3475 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003478 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003479 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003480
3481 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003482 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003483 * are not at the beginning of a new record, the caller did something
3484 * wrong.
3485 */
3486 if( ssl->in_left != 0 )
3487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003490 }
3491
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003492 /*
3493 * Don't even try to read if time's out already.
3494 * This avoids by-passing the timer when repeatedly receiving messages
3495 * that will end up being dropped.
3496 */
3497 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003498 {
3499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003500 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003501 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003502 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003503 {
Angus Grattond8213d02016-05-25 20:56:48 +10003504 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003507 timeout = ssl->handshake->retransmit_timeout;
3508 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003509 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003512
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003513 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003514 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3515 timeout );
3516 else
3517 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003520
3521 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003523 }
3524
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003525 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003528 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003531 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003532 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003535 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003536 }
3537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003541 return( ret );
3542 }
3543
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003544 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003545 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003547 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003549 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003550 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003553 return( ret );
3554 }
3555
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003556 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003557 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003558#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003559 }
3560
Paul Bakker5121ce52009-01-03 21:22:43 +00003561 if( ret < 0 )
3562 return( ret );
3563
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003564 ssl->in_left = ret;
3565 }
3566 else
3567#endif
3568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003570 ssl->in_left, nb_want ) );
3571
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003572 while( ssl->in_left < nb_want )
3573 {
3574 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003575
3576 if( ssl_check_timer( ssl ) != 0 )
3577 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3578 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003579 {
3580 if( ssl->f_recv_timeout != NULL )
3581 {
3582 ret = ssl->f_recv_timeout( ssl->p_bio,
3583 ssl->in_hdr + ssl->in_left, len,
3584 ssl->conf->read_timeout );
3585 }
3586 else
3587 {
3588 ret = ssl->f_recv( ssl->p_bio,
3589 ssl->in_hdr + ssl->in_left, len );
3590 }
3591 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003594 ssl->in_left, nb_want ) );
3595 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003596
3597 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003598 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003599
3600 if( ret < 0 )
3601 return( ret );
3602
mohammad160352aecb92018-03-28 23:41:40 -07003603 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003604 {
Darryl Green11999bb2018-03-13 15:22:58 +00003605 MBEDTLS_SSL_DEBUG_MSG( 1,
3606 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003607 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003608 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3609 }
3610
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003611 ssl->in_left += ret;
3612 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003613 }
3614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003616
3617 return( 0 );
3618}
3619
3620/*
3621 * Flush any data not yet written
3622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003624{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003625 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003626 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003629
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003630 if( ssl->f_send == NULL )
3631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003633 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003634 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003635 }
3636
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003637 /* Avoid incrementing counter if data is flushed */
3638 if( ssl->out_left == 0 )
3639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003641 return( 0 );
3642 }
3643
Paul Bakker5121ce52009-01-03 21:22:43 +00003644 while( ssl->out_left > 0 )
3645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003647 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003648
Hanno Becker2b1e3542018-08-06 11:19:13 +01003649 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003650 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003653
3654 if( ret <= 0 )
3655 return( ret );
3656
mohammad160352aecb92018-03-28 23:41:40 -07003657 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003658 {
Darryl Green11999bb2018-03-13 15:22:58 +00003659 MBEDTLS_SSL_DEBUG_MSG( 1,
3660 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003661 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003662 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3663 }
3664
Paul Bakker5121ce52009-01-03 21:22:43 +00003665 ssl->out_left -= ret;
3666 }
3667
Hanno Becker2b1e3542018-08-06 11:19:13 +01003668#if defined(MBEDTLS_SSL_PROTO_DTLS)
3669 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003670 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003671 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003672 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003673 else
3674#endif
3675 {
3676 ssl->out_hdr = ssl->out_buf + 8;
3677 }
3678 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003681
3682 return( 0 );
3683}
3684
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003685/*
3686 * Functions to handle the DTLS retransmission state machine
3687 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003689/*
3690 * Append current handshake message to current outgoing flight
3691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003692static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003693{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3696 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3697 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003698
3699 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003700 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003701 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003704 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003705 }
3706
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003707 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003708 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003711 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003712 }
3713
3714 /* Copy current handshake message with headers */
3715 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3716 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003717 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003718 msg->next = NULL;
3719
3720 /* Append to the current flight */
3721 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003722 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003723 else
3724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003726 while( cur->next != NULL )
3727 cur = cur->next;
3728 cur->next = msg;
3729 }
3730
Hanno Becker3b235902018-08-06 09:54:53 +01003731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003732 return( 0 );
3733}
3734
3735/*
3736 * Free the current flight of handshake messages
3737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003739{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740 mbedtls_ssl_flight_item *cur = flight;
3741 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003742
3743 while( cur != NULL )
3744 {
3745 next = cur->next;
3746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003747 mbedtls_free( cur->p );
3748 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003749
3750 cur = next;
3751 }
3752}
3753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003754#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3755static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003756#endif
3757
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003758/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003759 * Swap transform_out and out_ctr with the alternative ones
3760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003762{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003763 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003764 unsigned char tmp_out_ctr[8];
3765
3766 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003768 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003769 return;
3770 }
3771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003773
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003774 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003775 tmp_transform = ssl->transform_out;
3776 ssl->transform_out = ssl->handshake->alt_transform_out;
3777 ssl->handshake->alt_transform_out = tmp_transform;
3778
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003779 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003780 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3781 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003782 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003783
3784 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003785 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3788 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3793 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003794 }
3795 }
3796#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003797}
3798
3799/*
3800 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003801 */
3802int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3803{
3804 int ret = 0;
3805
3806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3807
3808 ret = mbedtls_ssl_flight_transmit( ssl );
3809
3810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3811
3812 return( ret );
3813}
3814
3815/*
3816 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003817 *
3818 * Need to remember the current message in case flush_output returns
3819 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003820 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003821 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003822int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003823{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003824 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003827 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003828 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003830
3831 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003832 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003833 ssl_swap_epochs( ssl );
3834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003836 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003837
3838 while( ssl->handshake->cur_msg != NULL )
3839 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003840 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003841 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003842
Hanno Beckere1dcb032018-08-17 16:47:58 +01003843 int const is_finished =
3844 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3845 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3846
Hanno Becker04da1892018-08-14 13:22:10 +01003847 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3848 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3849
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003850 /* Swap epochs before sending Finished: we can't do it after
3851 * sending ChangeCipherSpec, in case write returns WANT_READ.
3852 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003853 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003854 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003856 ssl_swap_epochs( ssl );
3857 }
3858
Hanno Becker67bc7c32018-08-06 11:33:50 +01003859 ret = ssl_get_remaining_payload_in_datagram( ssl );
3860 if( ret < 0 )
3861 return( ret );
3862 max_frag_len = (size_t) ret;
3863
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003864 /* CCS is copied as is, while HS messages may need fragmentation */
3865 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3866 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003867 if( max_frag_len == 0 )
3868 {
3869 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3870 return( ret );
3871
3872 continue;
3873 }
3874
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003875 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003876 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003877 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003878
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003879 /* Update position inside current message */
3880 ssl->handshake->cur_msg_p += cur->len;
3881 }
3882 else
3883 {
3884 const unsigned char * const p = ssl->handshake->cur_msg_p;
3885 const size_t hs_len = cur->len - 12;
3886 const size_t frag_off = p - ( cur->p + 12 );
3887 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003888 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003889
Hanno Beckere1dcb032018-08-17 16:47:58 +01003890 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003891 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003892 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003893 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003894
Hanno Becker67bc7c32018-08-06 11:33:50 +01003895 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3896 return( ret );
3897
3898 continue;
3899 }
3900 max_hs_frag_len = max_frag_len - 12;
3901
3902 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3903 max_hs_frag_len : rem_len;
3904
3905 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003906 {
3907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003908 (unsigned) cur_hs_frag_len,
3909 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003910 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003911
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003912 /* Messages are stored with handshake headers as if not fragmented,
3913 * copy beginning of headers then fill fragmentation fields.
3914 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3915 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003916
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003917 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3918 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3919 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3920
Hanno Becker67bc7c32018-08-06 11:33:50 +01003921 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3922 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3923 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003924
3925 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3926
Hanno Becker3f7b9732018-08-28 09:53:25 +01003927 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003928 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3929 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003930 ssl->out_msgtype = cur->type;
3931
3932 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003933 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003934 }
3935
3936 /* If done with the current message move to the next one if any */
3937 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3938 {
3939 if( cur->next != NULL )
3940 {
3941 ssl->handshake->cur_msg = cur->next;
3942 ssl->handshake->cur_msg_p = cur->next->p + 12;
3943 }
3944 else
3945 {
3946 ssl->handshake->cur_msg = NULL;
3947 ssl->handshake->cur_msg_p = NULL;
3948 }
3949 }
3950
3951 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003952 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003955 return( ret );
3956 }
3957 }
3958
Hanno Becker67bc7c32018-08-06 11:33:50 +01003959 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3960 return( ret );
3961
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003962 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003963 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3964 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003965 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003968 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3969 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003970
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003972
3973 return( 0 );
3974}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003975
3976/*
3977 * To be called when the last message of an incoming flight is received.
3978 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003979void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003980{
3981 /* We won't need to resend that one any more */
3982 ssl_flight_free( ssl->handshake->flight );
3983 ssl->handshake->flight = NULL;
3984 ssl->handshake->cur_msg = NULL;
3985
3986 /* The next incoming flight will start with this msg_seq */
3987 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3988
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003989 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003990 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003991
Hanno Becker0271f962018-08-16 13:23:47 +01003992 /* Clear future message buffering structure. */
3993 ssl_buffering_free( ssl );
3994
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003995 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003996 ssl_set_timer( ssl, 0 );
3997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3999 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004002 }
4003 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004004 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004005}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004006
4007/*
4008 * To be called when the last message of an outgoing flight is send.
4009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004010void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004011{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004012 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004013 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4016 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004019 }
4020 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004021 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004022}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004024
Paul Bakker5121ce52009-01-03 21:22:43 +00004025/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004026 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004027 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004028
4029/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004030 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004031 *
4032 * - fill in handshake headers
4033 * - update handshake checksum
4034 * - DTLS: save message for resending
4035 * - then pass to the record layer
4036 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004037 * DTLS: except for HelloRequest, messages are only queued, and will only be
4038 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004039 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004040 * Inputs:
4041 * - ssl->out_msglen: 4 + actual handshake message len
4042 * (4 is the size of handshake headers for TLS)
4043 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4044 * - ssl->out_msg + 4: the handshake message body
4045 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004046 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004047 * - ssl->out_msglen: the length of the record contents
4048 * (including handshake headers but excluding record headers)
4049 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004050 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004051int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004052{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004053 int ret;
4054 const size_t hs_len = ssl->out_msglen - 4;
4055 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004056
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4058
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004059 /*
4060 * Sanity checks
4061 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004062 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004063 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4064 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004065 /* In SSLv3, the client might send a NoCertificate alert. */
4066#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4067 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4068 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4069 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4070#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4071 {
4072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4074 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004075 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004076
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004077 /* Whenever we send anything different from a
4078 * HelloRequest we should be in a handshake - double check. */
4079 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4080 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004081 ssl->handshake == NULL )
4082 {
4083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4084 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4085 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004088 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004089 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004090 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004091 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4093 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004094 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004095#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004096
Hanno Beckerb50a2532018-08-06 11:52:54 +01004097 /* Double-check that we did not exceed the bounds
4098 * of the outgoing record buffer.
4099 * This should never fail as the various message
4100 * writing functions must obey the bounds of the
4101 * outgoing record buffer, but better be safe.
4102 *
4103 * Note: We deliberately do not check for the MTU or MFL here.
4104 */
4105 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4106 {
4107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4108 "size %u, maximum %u",
4109 (unsigned) ssl->out_msglen,
4110 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4111 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4112 }
4113
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004114 /*
4115 * Fill handshake headers
4116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004117 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004118 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004119 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4120 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4121 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004122
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004123 /*
4124 * DTLS has additional fields in the Handshake layer,
4125 * between the length field and the actual payload:
4126 * uint16 message_seq;
4127 * uint24 fragment_offset;
4128 * uint24 fragment_length;
4129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004130#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004131 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004132 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004133 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004134 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004135 {
4136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4137 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004138 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004139 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004140 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4141 }
4142
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004143 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004144 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004145
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004146 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004147 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004148 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004149 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4150 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4151 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004152 }
4153 else
4154 {
4155 ssl->out_msg[4] = 0;
4156 ssl->out_msg[5] = 0;
4157 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004158
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004159 /* Handshake hashes are computed without fragmentation,
4160 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004161 memset( ssl->out_msg + 6, 0x00, 3 );
4162 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004163 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004165
Hanno Becker0207e532018-08-28 10:28:28 +01004166 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004167 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4168 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004169 }
4170
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004171 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004172#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004173 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004174 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4175 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004176 {
4177 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004179 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004180 return( ret );
4181 }
4182 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004183 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004184#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004185 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004186 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004187 {
4188 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4189 return( ret );
4190 }
4191 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004192
4193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4194
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004195 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004196}
4197
4198/*
4199 * Record layer functions
4200 */
4201
4202/*
4203 * Write current record.
4204 *
4205 * Uses:
4206 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4207 * - ssl->out_msglen: length of the record content (excl headers)
4208 * - ssl->out_msg: record content
4209 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004210int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004211{
4212 int ret, done = 0;
4213 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004214 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004215
4216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004219 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004221 {
4222 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004224 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004225 return( ret );
4226 }
4227
4228 len = ssl->out_msglen;
4229 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004230#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004232#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4233 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237 ret = mbedtls_ssl_hw_record_write( ssl );
4238 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004240 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4241 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004242 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004243
4244 if( ret == 0 )
4245 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004246 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004247#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004248 if( !done )
4249 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004250 unsigned i;
4251 size_t protected_record_size;
4252
Hanno Becker6430faf2019-05-08 11:57:13 +01004253 /* Skip writing the record content type to after the encryption,
4254 * as it may change when using the CID extension. */
4255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004256 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004257 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004258
Hanno Becker19859472018-08-06 09:40:20 +01004259 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004260 ssl->out_len[0] = (unsigned char)( len >> 8 );
4261 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004262
Paul Bakker48916f92012-09-16 19:57:18 +00004263 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004264 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004265 mbedtls_record rec;
4266
4267 rec.buf = ssl->out_iv;
4268 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4269 ( ssl->out_iv - ssl->out_buf );
4270 rec.data_len = ssl->out_msglen;
4271 rec.data_offset = ssl->out_msg - rec.buf;
4272
4273 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4274 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4275 ssl->conf->transport, rec.ver );
4276 rec.type = ssl->out_msgtype;
4277
Hanno Beckera0e20d02019-05-15 14:03:01 +01004278#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004279 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004280 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004281#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004282
Hanno Beckera18d1322018-01-03 14:27:32 +00004283 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004284 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004286 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004287 return( ret );
4288 }
4289
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004290 if( rec.data_offset != 0 )
4291 {
4292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4293 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4294 }
4295
Hanno Becker6430faf2019-05-08 11:57:13 +01004296 /* Update the record content type and CID. */
4297 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004298#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004299 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004300#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004301 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004302 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4303 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004304 }
4305
Hanno Becker5903de42019-05-03 14:46:38 +01004306 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004307
4308#if defined(MBEDTLS_SSL_PROTO_DTLS)
4309 /* In case of DTLS, double-check that we don't exceed
4310 * the remaining space in the datagram. */
4311 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4312 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004313 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004314 if( ret < 0 )
4315 return( ret );
4316
4317 if( protected_record_size > (size_t) ret )
4318 {
4319 /* Should never happen */
4320 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4321 }
4322 }
4323#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004324
Hanno Becker6430faf2019-05-08 11:57:13 +01004325 /* Now write the potentially updated record content type. */
4326 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004329 "version = [%d:%d], msglen = %d",
4330 ssl->out_hdr[0], ssl->out_hdr[1],
4331 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004334 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004335
4336 ssl->out_left += protected_record_size;
4337 ssl->out_hdr += protected_record_size;
4338 ssl_update_out_pointers( ssl, ssl->transform_out );
4339
Hanno Becker04484622018-08-06 09:49:38 +01004340 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4341 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4342 break;
4343
4344 /* The loop goes to its end iff the counter is wrapping */
4345 if( i == ssl_ep_len( ssl ) )
4346 {
4347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4348 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4349 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004350 }
4351
Hanno Becker67bc7c32018-08-06 11:33:50 +01004352#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004353 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4354 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004355 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004356 size_t remaining;
4357 ret = ssl_get_remaining_payload_in_datagram( ssl );
4358 if( ret < 0 )
4359 {
4360 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4361 ret );
4362 return( ret );
4363 }
4364
4365 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004366 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004367 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004368 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004369 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004370 else
4371 {
Hanno Becker513815a2018-08-20 11:56:09 +01004372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004373 }
4374 }
4375#endif /* MBEDTLS_SSL_PROTO_DTLS */
4376
4377 if( ( flush == SSL_FORCE_FLUSH ) &&
4378 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004380 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004381 return( ret );
4382 }
4383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004385
4386 return( 0 );
4387}
4388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004389#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004390
4391static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4392{
4393 if( ssl->in_msglen < ssl->in_hslen ||
4394 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4395 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4396 {
4397 return( 1 );
4398 }
4399 return( 0 );
4400}
Hanno Becker44650b72018-08-16 12:51:11 +01004401
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004402static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004403{
4404 return( ( ssl->in_msg[9] << 16 ) |
4405 ( ssl->in_msg[10] << 8 ) |
4406 ssl->in_msg[11] );
4407}
4408
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004409static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004410{
4411 return( ( ssl->in_msg[6] << 16 ) |
4412 ( ssl->in_msg[7] << 8 ) |
4413 ssl->in_msg[8] );
4414}
4415
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004416static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004417{
4418 uint32_t msg_len, frag_off, frag_len;
4419
4420 msg_len = ssl_get_hs_total_len( ssl );
4421 frag_off = ssl_get_hs_frag_off( ssl );
4422 frag_len = ssl_get_hs_frag_len( ssl );
4423
4424 if( frag_off > msg_len )
4425 return( -1 );
4426
4427 if( frag_len > msg_len - frag_off )
4428 return( -1 );
4429
4430 if( frag_len + 12 > ssl->in_msglen )
4431 return( -1 );
4432
4433 return( 0 );
4434}
4435
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004436/*
4437 * Mark bits in bitmask (used for DTLS HS reassembly)
4438 */
4439static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4440{
4441 unsigned int start_bits, end_bits;
4442
4443 start_bits = 8 - ( offset % 8 );
4444 if( start_bits != 8 )
4445 {
4446 size_t first_byte_idx = offset / 8;
4447
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004448 /* Special case */
4449 if( len <= start_bits )
4450 {
4451 for( ; len != 0; len-- )
4452 mask[first_byte_idx] |= 1 << ( start_bits - len );
4453
4454 /* Avoid potential issues with offset or len becoming invalid */
4455 return;
4456 }
4457
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004458 offset += start_bits; /* Now offset % 8 == 0 */
4459 len -= start_bits;
4460
4461 for( ; start_bits != 0; start_bits-- )
4462 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4463 }
4464
4465 end_bits = len % 8;
4466 if( end_bits != 0 )
4467 {
4468 size_t last_byte_idx = ( offset + len ) / 8;
4469
4470 len -= end_bits; /* Now len % 8 == 0 */
4471
4472 for( ; end_bits != 0; end_bits-- )
4473 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4474 }
4475
4476 memset( mask + offset / 8, 0xFF, len / 8 );
4477}
4478
4479/*
4480 * Check that bitmask is full
4481 */
4482static int ssl_bitmask_check( unsigned char *mask, size_t len )
4483{
4484 size_t i;
4485
4486 for( i = 0; i < len / 8; i++ )
4487 if( mask[i] != 0xFF )
4488 return( -1 );
4489
4490 for( i = 0; i < len % 8; i++ )
4491 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4492 return( -1 );
4493
4494 return( 0 );
4495}
4496
Hanno Becker56e205e2018-08-16 09:06:12 +01004497/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004498static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004499 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004500{
Hanno Becker56e205e2018-08-16 09:06:12 +01004501 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004502
Hanno Becker56e205e2018-08-16 09:06:12 +01004503 alloc_len = 12; /* Handshake header */
4504 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004505
Hanno Beckerd07df862018-08-16 09:14:58 +01004506 if( add_bitmap )
4507 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004508
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004509 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004510}
Hanno Becker56e205e2018-08-16 09:06:12 +01004511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004512#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004513
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004514static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004515{
4516 return( ( ssl->in_msg[1] << 16 ) |
4517 ( ssl->in_msg[2] << 8 ) |
4518 ssl->in_msg[3] );
4519}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004520
Simon Butcher99000142016-10-13 17:21:01 +01004521int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004522{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004523 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004526 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004527 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004528 }
4529
Hanno Becker12555c62018-08-16 12:47:53 +01004530 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004532 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004533 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004534 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004536#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004537 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004538 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004539 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004540 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004541
Hanno Becker44650b72018-08-16 12:51:11 +01004542 if( ssl_check_hs_header( ssl ) != 0 )
4543 {
4544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4545 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4546 }
4547
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004548 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004549 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4550 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4551 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4552 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004553 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004554 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4555 {
4556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4557 recv_msg_seq,
4558 ssl->handshake->in_msg_seq ) );
4559 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4560 }
4561
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004562 /* Retransmit only on last message from previous flight, to avoid
4563 * too many retransmissions.
4564 * Besides, No sane server ever retransmits HelloVerifyRequest */
4565 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004566 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004569 "message_seq = %d, start_of_flight = %d",
4570 recv_msg_seq,
4571 ssl->handshake->in_flight_start_seq ) );
4572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004575 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004576 return( ret );
4577 }
4578 }
4579 else
4580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004582 "message_seq = %d, expected = %d",
4583 recv_msg_seq,
4584 ssl->handshake->in_msg_seq ) );
4585 }
4586
Hanno Becker90333da2017-10-10 11:27:13 +01004587 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004588 }
4589 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004590
Hanno Becker6d97ef52018-08-16 13:09:04 +01004591 /* Message reassembly is handled alongside buffering of future
4592 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004593 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004594 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004595 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004598 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004599 }
4600 }
4601 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004602#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004603 /* With TLS we don't handle fragmentation (for now) */
4604 if( ssl->in_msglen < ssl->in_hslen )
4605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4607 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004608 }
4609
Simon Butcher99000142016-10-13 17:21:01 +01004610 return( 0 );
4611}
4612
4613void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4614{
Hanno Becker0271f962018-08-16 13:23:47 +01004615 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004616
Hanno Becker0271f962018-08-16 13:23:47 +01004617 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004618 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004619 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004620 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004621
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004622 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004623#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004624 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004625 ssl->handshake != NULL )
4626 {
Hanno Becker0271f962018-08-16 13:23:47 +01004627 unsigned offset;
4628 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004629
Hanno Becker0271f962018-08-16 13:23:47 +01004630 /* Increment handshake sequence number */
4631 hs->in_msg_seq++;
4632
4633 /*
4634 * Clear up handshake buffering and reassembly structure.
4635 */
4636
4637 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004638 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004639
4640 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004641 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4642 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004643 offset++, hs_buf++ )
4644 {
4645 *hs_buf = *(hs_buf + 1);
4646 }
4647
4648 /* Create a fresh last entry */
4649 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004650 }
4651#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004652}
4653
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004654/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004655 * DTLS anti-replay: RFC 6347 4.1.2.6
4656 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004657 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4658 * Bit n is set iff record number in_window_top - n has been seen.
4659 *
4660 * Usually, in_window_top is the last record number seen and the lsb of
4661 * in_window is set. The only exception is the initial state (record number 0
4662 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004664#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4665static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004666{
4667 ssl->in_window_top = 0;
4668 ssl->in_window = 0;
4669}
4670
4671static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4672{
4673 return( ( (uint64_t) buf[0] << 40 ) |
4674 ( (uint64_t) buf[1] << 32 ) |
4675 ( (uint64_t) buf[2] << 24 ) |
4676 ( (uint64_t) buf[3] << 16 ) |
4677 ( (uint64_t) buf[4] << 8 ) |
4678 ( (uint64_t) buf[5] ) );
4679}
4680
4681/*
4682 * Return 0 if sequence number is acceptable, -1 otherwise
4683 */
Hanno Becker0183d692019-07-12 08:50:37 +01004684int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004685{
4686 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4687 uint64_t bit;
4688
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004689 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004690 return( 0 );
4691
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004692 if( rec_seqnum > ssl->in_window_top )
4693 return( 0 );
4694
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004695 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004696
4697 if( bit >= 64 )
4698 return( -1 );
4699
4700 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4701 return( -1 );
4702
4703 return( 0 );
4704}
4705
4706/*
4707 * Update replay window on new validated record
4708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004709void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004710{
4711 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4712
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004713 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004714 return;
4715
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004716 if( rec_seqnum > ssl->in_window_top )
4717 {
4718 /* Update window_top and the contents of the window */
4719 uint64_t shift = rec_seqnum - ssl->in_window_top;
4720
4721 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004722 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004723 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004724 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004725 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004726 ssl->in_window |= 1;
4727 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004728
4729 ssl->in_window_top = rec_seqnum;
4730 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004731 else
4732 {
4733 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004734 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004735
4736 if( bit < 64 ) /* Always true, but be extra sure */
4737 ssl->in_window |= (uint64_t) 1 << bit;
4738 }
4739}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004740#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004741
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004742#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004743/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004744static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4745
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004746/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004747 * Without any SSL context, check if a datagram looks like a ClientHello with
4748 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004749 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004750 *
4751 * - if cookie is valid, return 0
4752 * - if ClientHello looks superficially valid but cookie is not,
4753 * fill obuf and set olen, then
4754 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4755 * - otherwise return a specific error code
4756 */
4757static int ssl_check_dtls_clihlo_cookie(
4758 mbedtls_ssl_cookie_write_t *f_cookie_write,
4759 mbedtls_ssl_cookie_check_t *f_cookie_check,
4760 void *p_cookie,
4761 const unsigned char *cli_id, size_t cli_id_len,
4762 const unsigned char *in, size_t in_len,
4763 unsigned char *obuf, size_t buf_len, size_t *olen )
4764{
4765 size_t sid_len, cookie_len;
4766 unsigned char *p;
4767
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004768 /*
4769 * Structure of ClientHello with record and handshake headers,
4770 * and expected values. We don't need to check a lot, more checks will be
4771 * done when actually parsing the ClientHello - skipping those checks
4772 * avoids code duplication and does not make cookie forging any easier.
4773 *
4774 * 0-0 ContentType type; copied, must be handshake
4775 * 1-2 ProtocolVersion version; copied
4776 * 3-4 uint16 epoch; copied, must be 0
4777 * 5-10 uint48 sequence_number; copied
4778 * 11-12 uint16 length; (ignored)
4779 *
4780 * 13-13 HandshakeType msg_type; (ignored)
4781 * 14-16 uint24 length; (ignored)
4782 * 17-18 uint16 message_seq; copied
4783 * 19-21 uint24 fragment_offset; copied, must be 0
4784 * 22-24 uint24 fragment_length; (ignored)
4785 *
4786 * 25-26 ProtocolVersion client_version; (ignored)
4787 * 27-58 Random random; (ignored)
4788 * 59-xx SessionID session_id; 1 byte len + sid_len content
4789 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4790 * ...
4791 *
4792 * Minimum length is 61 bytes.
4793 */
4794 if( in_len < 61 ||
4795 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4796 in[3] != 0 || in[4] != 0 ||
4797 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4798 {
4799 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4800 }
4801
4802 sid_len = in[59];
4803 if( sid_len > in_len - 61 )
4804 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4805
4806 cookie_len = in[60 + sid_len];
4807 if( cookie_len > in_len - 60 )
4808 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4809
4810 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4811 cli_id, cli_id_len ) == 0 )
4812 {
4813 /* Valid cookie */
4814 return( 0 );
4815 }
4816
4817 /*
4818 * If we get here, we've got an invalid cookie, let's prepare HVR.
4819 *
4820 * 0-0 ContentType type; copied
4821 * 1-2 ProtocolVersion version; copied
4822 * 3-4 uint16 epoch; copied
4823 * 5-10 uint48 sequence_number; copied
4824 * 11-12 uint16 length; olen - 13
4825 *
4826 * 13-13 HandshakeType msg_type; hello_verify_request
4827 * 14-16 uint24 length; olen - 25
4828 * 17-18 uint16 message_seq; copied
4829 * 19-21 uint24 fragment_offset; copied
4830 * 22-24 uint24 fragment_length; olen - 25
4831 *
4832 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4833 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4834 *
4835 * Minimum length is 28.
4836 */
4837 if( buf_len < 28 )
4838 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4839
4840 /* Copy most fields and adapt others */
4841 memcpy( obuf, in, 25 );
4842 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4843 obuf[25] = 0xfe;
4844 obuf[26] = 0xff;
4845
4846 /* Generate and write actual cookie */
4847 p = obuf + 28;
4848 if( f_cookie_write( p_cookie,
4849 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4850 {
4851 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4852 }
4853
4854 *olen = p - obuf;
4855
4856 /* Go back and fill length fields */
4857 obuf[27] = (unsigned char)( *olen - 28 );
4858
4859 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4860 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4861 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4862
4863 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4864 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4865
4866 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4867}
4868
4869/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004870 * Handle possible client reconnect with the same UDP quadruplet
4871 * (RFC 6347 Section 4.2.8).
4872 *
4873 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4874 * that looks like a ClientHello.
4875 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004876 * - if the input looks like a ClientHello without cookies,
4877 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004878 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004879 * - if the input looks like a ClientHello with a valid cookie,
4880 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004881 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004882 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004883 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004884 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004885 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4886 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004887 */
4888static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4889{
4890 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004891 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004892
Hanno Becker2fddd372019-07-10 14:37:41 +01004893 if( ssl->conf->f_cookie_write == NULL ||
4894 ssl->conf->f_cookie_check == NULL )
4895 {
4896 /* If we can't use cookies to verify reachability of the peer,
4897 * drop the record. */
4898 return( 0 );
4899 }
4900
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004901 ret = ssl_check_dtls_clihlo_cookie(
4902 ssl->conf->f_cookie_write,
4903 ssl->conf->f_cookie_check,
4904 ssl->conf->p_cookie,
4905 ssl->cli_id, ssl->cli_id_len,
4906 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004907 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004908
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004909 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4910
4911 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004912 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004913 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004914 * If the error is permanent we'll catch it later,
4915 * if it's not, then hopefully it'll work next time. */
4916 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01004917 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004918 }
4919
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004920 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004921 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004922 /* Got a valid cookie, partially reset context */
4923 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4924 {
4925 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4926 return( ret );
4927 }
4928
4929 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004930 }
4931
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004932 return( ret );
4933}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004934#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004935
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004936static int ssl_check_record_type( uint8_t record_type )
4937{
4938 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4939 record_type != MBEDTLS_SSL_MSG_ALERT &&
4940 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4941 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4942 {
4943 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4944 }
4945
4946 return( 0 );
4947}
4948
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004949/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004950 * ContentType type;
4951 * ProtocolVersion version;
4952 * uint16 epoch; // DTLS only
4953 * uint48 sequence_number; // DTLS only
4954 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004955 *
4956 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004957 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004958 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4959 *
4960 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004961 * 1. proceed with the record if this function returns 0
4962 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4963 * 3. return CLIENT_RECONNECT if this function return that value
4964 * 4. drop the whole datagram if this function returns anything else.
4965 * Point 2 is needed when the peer is resending, and we have already received
4966 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004967 */
Hanno Becker331de3d2019-07-12 11:10:16 +01004968static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01004969 unsigned char *buf,
4970 size_t len,
4971 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00004972{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004973 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004974
Hanno Beckere5e7e782019-07-11 12:29:35 +01004975 size_t const rec_hdr_type_offset = 0;
4976 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004977
Hanno Beckere5e7e782019-07-11 12:29:35 +01004978 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
4979 rec_hdr_type_len;
4980 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00004981
Hanno Beckere5e7e782019-07-11 12:29:35 +01004982 size_t const rec_hdr_ctr_len = 8;
4983#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01004984 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004985 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
4986 rec_hdr_version_len;
4987
Hanno Beckera0e20d02019-05-15 14:03:01 +01004988#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01004989 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
4990 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01004991 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004992#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4993#endif /* MBEDTLS_SSL_PROTO_DTLS */
4994
4995 size_t rec_hdr_len_offset; /* To be determined */
4996 size_t const rec_hdr_len_len = 2;
4997
4998 /*
4999 * Check minimum lengths for record header.
5000 */
5001
5002#if defined(MBEDTLS_SSL_PROTO_DTLS)
5003 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5004 {
5005 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5006 }
5007 else
5008#endif /* MBEDTLS_SSL_PROTO_DTLS */
5009 {
5010 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5011 }
5012
5013 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5014 {
5015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5016 (unsigned) len,
5017 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5018 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5019 }
5020
5021 /*
5022 * Parse and validate record content type
5023 */
5024
5025 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005026
5027 /* Check record content type */
5028#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5029 rec->cid_len = 0;
5030
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005031 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005032 ssl->conf->cid_len != 0 &&
5033 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005034 {
5035 /* Shift pointers to account for record header including CID
5036 * struct {
5037 * ContentType special_type = tls12_cid;
5038 * ProtocolVersion version;
5039 * uint16 epoch;
5040 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005041 * opaque cid[cid_length]; // Additional field compared to
5042 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005043 * uint16 length;
5044 * opaque enc_content[DTLSCiphertext.length];
5045 * } DTLSCiphertext;
5046 */
5047
5048 /* So far, we only support static CID lengths
5049 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005050 rec_hdr_cid_len = ssl->conf->cid_len;
5051 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005052
Hanno Beckere5e7e782019-07-11 12:29:35 +01005053 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005054 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5056 (unsigned) len,
5057 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005058 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005059 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005060
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02005061 /* configured CID len is guaranteed at most 255, see
5062 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5063 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005064 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005065 }
5066 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005067#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005068 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005069 if( ssl_check_record_type( rec->type ) )
5070 {
Hanno Becker54229812019-07-12 14:40:00 +01005071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5072 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005073 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5074 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005075 }
5076
Hanno Beckere5e7e782019-07-11 12:29:35 +01005077 /*
5078 * Parse and validate record version
5079 */
5080
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005081 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5082 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005083 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5084 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005085 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005086
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005087 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5090 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005091 }
5092
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005093 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5096 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005097 }
5098
Hanno Beckere5e7e782019-07-11 12:29:35 +01005099 /*
5100 * Parse/Copy record sequence number.
5101 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005102
Hanno Beckere5e7e782019-07-11 12:29:35 +01005103#if defined(MBEDTLS_SSL_PROTO_DTLS)
5104 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005105 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005106 /* Copy explicit record sequence number from input buffer. */
5107 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5108 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005109 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005110 else
5111#endif /* MBEDTLS_SSL_PROTO_DTLS */
5112 {
5113 /* Copy implicit record sequence number from SSL context structure. */
5114 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5115 }
Paul Bakker40e46942009-01-03 21:51:57 +00005116
Hanno Beckere5e7e782019-07-11 12:29:35 +01005117 /*
5118 * Parse record length.
5119 */
5120
Hanno Beckere5e7e782019-07-11 12:29:35 +01005121 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005122 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5123 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005124 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00005125
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005126 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005127 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005128 rec->type,
5129 major_ver, minor_ver, rec->data_len ) );
5130
5131 rec->buf = buf;
5132 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005133
Hanno Beckerd417cc92019-07-26 08:20:27 +01005134 if( rec->data_len == 0 )
5135 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005136
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005137 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005138 * DTLS-related tests.
5139 * Check epoch before checking length constraint because
5140 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5141 * message gets duplicated before the corresponding Finished message,
5142 * the second ChangeCipherSpec should be discarded because it belongs
5143 * to an old epoch, but not because its length is shorter than
5144 * the minimum record length for packets using the new record transform.
5145 * Note that these two kinds of failures are handled differently,
5146 * as an unexpected record is silently skipped but an invalid
5147 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005148 */
5149#if defined(MBEDTLS_SSL_PROTO_DTLS)
5150 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5151 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005152 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005153
Hanno Becker955a5c92019-07-10 17:12:07 +01005154 /* Check that the datagram is large enough to contain a record
5155 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005156 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005157 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5159 (unsigned) len,
5160 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005161 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5162 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005163
Hanno Becker37cfe732019-07-10 17:20:01 +01005164 /* Records from other, non-matching epochs are silently discarded.
5165 * (The case of same-port Client reconnects must be considered in
5166 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005167 if( rec_epoch != ssl->in_epoch )
5168 {
5169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5170 "expected %d, received %d",
5171 ssl->in_epoch, rec_epoch ) );
5172
Hanno Becker552f7472019-07-19 10:59:12 +01005173 /* Records from the next epoch are considered for buffering
5174 * (concretely: early Finished messages). */
5175 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005176 {
Hanno Becker552f7472019-07-19 10:59:12 +01005177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5178 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005179 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005180
Hanno Becker2fddd372019-07-10 14:37:41 +01005181 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005182 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005183#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005184 /* For records from the correct epoch, check whether their
5185 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005186 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005187 {
5188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5189 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5190 }
5191#endif
5192 }
5193#endif /* MBEDTLS_SSL_PROTO_DTLS */
5194
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005195 return( 0 );
5196}
Paul Bakker5121ce52009-01-03 21:22:43 +00005197
Paul Bakker5121ce52009-01-03 21:22:43 +00005198
Hanno Becker2fddd372019-07-10 14:37:41 +01005199#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5200static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5201{
5202 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5203
5204 /*
5205 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5206 * access the first byte of record content (handshake type), as we
5207 * have an active transform (possibly iv_len != 0), so use the
5208 * fact that the record header len is 13 instead.
5209 */
5210 if( rec_epoch == 0 &&
5211 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5212 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5213 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5214 ssl->in_left > 13 &&
5215 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5216 {
5217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5218 "from the same port" ) );
5219 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005220 }
5221
5222 return( 0 );
5223}
Hanno Becker2fddd372019-07-10 14:37:41 +01005224#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005225
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005226/*
5227 * If applicable, decrypt (and decompress) record content
5228 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005229static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5230 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005231{
5232 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005234 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005235 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005237#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5238 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005242 ret = mbedtls_ssl_hw_record_read( ssl );
5243 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005245 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5246 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005247 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005248
5249 if( ret == 0 )
5250 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005251 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005252#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005253 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005254 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005255 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005256
Hanno Beckera18d1322018-01-03 14:27:32 +00005257 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005258 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005260 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005261
Hanno Beckera0e20d02019-05-15 14:03:01 +01005262#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005263 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5264 ssl->conf->ignore_unexpected_cid
5265 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5266 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005267 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005268 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005269 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005270#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005271
Paul Bakker5121ce52009-01-03 21:22:43 +00005272 return( ret );
5273 }
5274
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005275 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005276 {
5277 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005278 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005279 }
5280
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005281 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005282 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005283
Hanno Beckera0e20d02019-05-15 14:03:01 +01005284#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005285 /* We have already checked the record content type
5286 * in ssl_parse_record_header(), failing or silently
5287 * dropping the record in the case of an unknown type.
5288 *
5289 * Since with the use of CIDs, the record content type
5290 * might change during decryption, re-check the record
5291 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005292 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005293 {
5294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5295 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5296 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005297#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005298
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005299 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005300 {
5301#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5302 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005303 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005304 {
5305 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5307 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5308 }
5309#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5310
5311 ssl->nb_zero++;
5312
5313 /*
5314 * Three or more empty messages may be a DoS attack
5315 * (excessive CPU consumption).
5316 */
5317 if( ssl->nb_zero > 3 )
5318 {
5319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005320 "messages, possible DoS attack" ) );
5321 /* Treat the records as if they were not properly authenticated,
5322 * thereby failing the connection if we see more than allowed
5323 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005324 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5325 }
5326 }
5327 else
5328 ssl->nb_zero = 0;
5329
5330#if defined(MBEDTLS_SSL_PROTO_DTLS)
5331 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5332 {
5333 ; /* in_ctr read from peer, not maintained internally */
5334 }
5335 else
5336#endif
5337 {
5338 unsigned i;
5339 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5340 if( ++ssl->in_ctr[i - 1] != 0 )
5341 break;
5342
5343 /* The loop goes to its end iff the counter is wrapping */
5344 if( i == ssl_ep_len( ssl ) )
5345 {
5346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5347 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5348 }
5349 }
5350
Paul Bakker5121ce52009-01-03 21:22:43 +00005351 }
5352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005354 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005355 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005356 {
5357 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005360 return( ret );
5361 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005362 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005363#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005365#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005366 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005368 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005369 }
5370#endif
5371
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005372 /* Check actual (decrypted) record content length against
5373 * configured maximum. */
5374 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5375 {
5376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5377 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5378 }
5379
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005380 return( 0 );
5381}
5382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005383static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005384
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005385/*
5386 * Read a record.
5387 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005388 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5389 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5390 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005391 */
Hanno Becker1097b342018-08-15 14:09:41 +01005392
5393/* Helper functions for mbedtls_ssl_read_record(). */
5394static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005395static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5396static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005397
Hanno Becker327c93b2018-08-15 13:56:18 +01005398int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005399 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005400{
5401 int ret;
5402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005404
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005405 if( ssl->keep_current_message == 0 )
5406 {
5407 do {
Simon Butcher99000142016-10-13 17:21:01 +01005408
Hanno Becker26994592018-08-15 14:14:59 +01005409 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005410 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005411 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005412
Hanno Beckere74d5562018-08-15 14:26:08 +01005413 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005414 {
Hanno Becker40f50842018-08-15 14:48:01 +01005415#if defined(MBEDTLS_SSL_PROTO_DTLS)
5416 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005417
Hanno Becker40f50842018-08-15 14:48:01 +01005418 /* We only check for buffered messages if the
5419 * current datagram is fully consumed. */
5420 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005421 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005422 {
Hanno Becker40f50842018-08-15 14:48:01 +01005423 if( ssl_load_buffered_message( ssl ) == 0 )
5424 have_buffered = 1;
5425 }
5426
5427 if( have_buffered == 0 )
5428#endif /* MBEDTLS_SSL_PROTO_DTLS */
5429 {
5430 ret = ssl_get_next_record( ssl );
5431 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5432 continue;
5433
5434 if( ret != 0 )
5435 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005436 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005437 return( ret );
5438 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005439 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005440 }
5441
5442 ret = mbedtls_ssl_handle_message_type( ssl );
5443
Hanno Becker40f50842018-08-15 14:48:01 +01005444#if defined(MBEDTLS_SSL_PROTO_DTLS)
5445 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5446 {
5447 /* Buffer future message */
5448 ret = ssl_buffer_message( ssl );
5449 if( ret != 0 )
5450 return( ret );
5451
5452 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5453 }
5454#endif /* MBEDTLS_SSL_PROTO_DTLS */
5455
Hanno Becker90333da2017-10-10 11:27:13 +01005456 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5457 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005458
5459 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005460 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005461 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005462 return( ret );
5463 }
5464
Hanno Becker327c93b2018-08-15 13:56:18 +01005465 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005466 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005467 {
5468 mbedtls_ssl_update_handshake_status( ssl );
5469 }
Simon Butcher99000142016-10-13 17:21:01 +01005470 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005471 else
Simon Butcher99000142016-10-13 17:21:01 +01005472 {
Hanno Becker02f59072018-08-15 14:00:24 +01005473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005474 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005475 }
5476
5477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5478
5479 return( 0 );
5480}
5481
Hanno Becker40f50842018-08-15 14:48:01 +01005482#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005483static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005484{
Hanno Becker40f50842018-08-15 14:48:01 +01005485 if( ssl->in_left > ssl->next_record_offset )
5486 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005487
Hanno Becker40f50842018-08-15 14:48:01 +01005488 return( 0 );
5489}
5490
5491static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5492{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005493 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005494 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005495 int ret = 0;
5496
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005497 if( hs == NULL )
5498 return( -1 );
5499
Hanno Beckere00ae372018-08-20 09:39:42 +01005500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5501
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005502 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5503 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5504 {
5505 /* Check if we have seen a ChangeCipherSpec before.
5506 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005507 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005508 {
5509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5510 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005511 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005512 }
5513
Hanno Becker39b8bc92018-08-28 17:17:13 +01005514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005515 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5516 ssl->in_msglen = 1;
5517 ssl->in_msg[0] = 1;
5518
5519 /* As long as they are equal, the exact value doesn't matter. */
5520 ssl->in_left = 0;
5521 ssl->next_record_offset = 0;
5522
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005523 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005524 goto exit;
5525 }
Hanno Becker37f95322018-08-16 13:55:32 +01005526
Hanno Beckerb8f50142018-08-28 10:01:34 +01005527#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005528 /* Debug only */
5529 {
5530 unsigned offset;
5531 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5532 {
5533 hs_buf = &hs->buffering.hs[offset];
5534 if( hs_buf->is_valid == 1 )
5535 {
5536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5537 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005538 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005539 }
5540 }
5541 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005542#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005543
5544 /* Check if we have buffered and/or fully reassembled the
5545 * next handshake message. */
5546 hs_buf = &hs->buffering.hs[0];
5547 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5548 {
5549 /* Synthesize a record containing the buffered HS message. */
5550 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5551 ( hs_buf->data[2] << 8 ) |
5552 hs_buf->data[3];
5553
5554 /* Double-check that we haven't accidentally buffered
5555 * a message that doesn't fit into the input buffer. */
5556 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5557 {
5558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5559 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5560 }
5561
5562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5563 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5564 hs_buf->data, msg_len + 12 );
5565
5566 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5567 ssl->in_hslen = msg_len + 12;
5568 ssl->in_msglen = msg_len + 12;
5569 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5570
5571 ret = 0;
5572 goto exit;
5573 }
5574 else
5575 {
5576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5577 hs->in_msg_seq ) );
5578 }
5579
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005580 ret = -1;
5581
5582exit:
5583
5584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5585 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005586}
5587
Hanno Beckera02b0b42018-08-21 17:20:27 +01005588static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5589 size_t desired )
5590{
5591 int offset;
5592 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5594 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005595
Hanno Becker01315ea2018-08-21 17:22:17 +01005596 /* Get rid of future records epoch first, if such exist. */
5597 ssl_free_buffered_record( ssl );
5598
5599 /* Check if we have enough space available now. */
5600 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5601 hs->buffering.total_bytes_buffered ) )
5602 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005604 return( 0 );
5605 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005606
Hanno Becker4f432ad2018-08-28 10:02:32 +01005607 /* We don't have enough space to buffer the next expected handshake
5608 * message. Remove buffers used for future messages to gain space,
5609 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005610 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5611 offset >= 0; offset-- )
5612 {
5613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5614 offset ) );
5615
Hanno Beckerb309b922018-08-23 13:18:05 +01005616 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005617
5618 /* Check if we have enough space available now. */
5619 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5620 hs->buffering.total_bytes_buffered ) )
5621 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005623 return( 0 );
5624 }
5625 }
5626
5627 return( -1 );
5628}
5629
Hanno Becker40f50842018-08-15 14:48:01 +01005630static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5631{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005632 int ret = 0;
5633 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5634
5635 if( hs == NULL )
5636 return( 0 );
5637
5638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5639
5640 switch( ssl->in_msgtype )
5641 {
5642 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005644
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005645 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005646 break;
5647
5648 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005649 {
5650 unsigned recv_msg_seq_offset;
5651 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5652 mbedtls_ssl_hs_buffer *hs_buf;
5653 size_t msg_len = ssl->in_hslen - 12;
5654
5655 /* We should never receive an old handshake
5656 * message - double-check nonetheless. */
5657 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5658 {
5659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5660 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5661 }
5662
5663 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5664 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5665 {
5666 /* Silently ignore -- message too far in the future */
5667 MBEDTLS_SSL_DEBUG_MSG( 2,
5668 ( "Ignore future HS message with sequence number %u, "
5669 "buffering window %u - %u",
5670 recv_msg_seq, ssl->handshake->in_msg_seq,
5671 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5672
5673 goto exit;
5674 }
5675
5676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5677 recv_msg_seq, recv_msg_seq_offset ) );
5678
5679 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5680
5681 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005682 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005683 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005684 size_t reassembly_buf_sz;
5685
Hanno Becker37f95322018-08-16 13:55:32 +01005686 hs_buf->is_fragmented =
5687 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5688
5689 /* We copy the message back into the input buffer
5690 * after reassembly, so check that it's not too large.
5691 * This is an implementation-specific limitation
5692 * and not one from the standard, hence it is not
5693 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005694 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005695 {
5696 /* Ignore message */
5697 goto exit;
5698 }
5699
Hanno Beckere0b150f2018-08-21 15:51:03 +01005700 /* Check if we have enough space to buffer the message. */
5701 if( hs->buffering.total_bytes_buffered >
5702 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5703 {
5704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5705 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5706 }
5707
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005708 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5709 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005710
5711 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5712 hs->buffering.total_bytes_buffered ) )
5713 {
5714 if( recv_msg_seq_offset > 0 )
5715 {
5716 /* If we can't buffer a future message because
5717 * of space limitations -- ignore. */
5718 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",
5719 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5720 (unsigned) hs->buffering.total_bytes_buffered ) );
5721 goto exit;
5722 }
Hanno Beckere1801392018-08-21 16:51:05 +01005723 else
5724 {
5725 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",
5726 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5727 (unsigned) hs->buffering.total_bytes_buffered ) );
5728 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005729
Hanno Beckera02b0b42018-08-21 17:20:27 +01005730 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005731 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005732 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",
5733 (unsigned) msg_len,
5734 (unsigned) reassembly_buf_sz,
5735 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005736 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005737 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5738 goto exit;
5739 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005740 }
5741
5742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5743 msg_len ) );
5744
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005745 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5746 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005747 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005748 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005749 goto exit;
5750 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005751 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005752
5753 /* Prepare final header: copy msg_type, length and message_seq,
5754 * then add standardised fragment_offset and fragment_length */
5755 memcpy( hs_buf->data, ssl->in_msg, 6 );
5756 memset( hs_buf->data + 6, 0, 3 );
5757 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5758
5759 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005760
5761 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005762 }
5763 else
5764 {
5765 /* Make sure msg_type and length are consistent */
5766 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5767 {
5768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5769 /* Ignore */
5770 goto exit;
5771 }
5772 }
5773
Hanno Becker4422bbb2018-08-20 09:40:19 +01005774 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005775 {
5776 size_t frag_len, frag_off;
5777 unsigned char * const msg = hs_buf->data + 12;
5778
5779 /*
5780 * Check and copy current fragment
5781 */
5782
5783 /* Validation of header fields already done in
5784 * mbedtls_ssl_prepare_handshake_record(). */
5785 frag_off = ssl_get_hs_frag_off( ssl );
5786 frag_len = ssl_get_hs_frag_len( ssl );
5787
5788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5789 frag_off, frag_len ) );
5790 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5791
5792 if( hs_buf->is_fragmented )
5793 {
5794 unsigned char * const bitmask = msg + msg_len;
5795 ssl_bitmask_set( bitmask, frag_off, frag_len );
5796 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5797 msg_len ) == 0 );
5798 }
5799 else
5800 {
5801 hs_buf->is_complete = 1;
5802 }
5803
5804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5805 hs_buf->is_complete ? "" : "not yet " ) );
5806 }
5807
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005808 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005809 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005810
5811 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005812 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005813 break;
5814 }
5815
5816exit:
5817
5818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5819 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005820}
5821#endif /* MBEDTLS_SSL_PROTO_DTLS */
5822
Hanno Becker1097b342018-08-15 14:09:41 +01005823static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005824{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005825 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005826 * Consume last content-layer message and potentially
5827 * update in_msglen which keeps track of the contents'
5828 * consumption state.
5829 *
5830 * (1) Handshake messages:
5831 * Remove last handshake message, move content
5832 * and adapt in_msglen.
5833 *
5834 * (2) Alert messages:
5835 * Consume whole record content, in_msglen = 0.
5836 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005837 * (3) Change cipher spec:
5838 * Consume whole record content, in_msglen = 0.
5839 *
5840 * (4) Application data:
5841 * Don't do anything - the record layer provides
5842 * the application data as a stream transport
5843 * and consumes through mbedtls_ssl_read only.
5844 *
5845 */
5846
5847 /* Case (1): Handshake messages */
5848 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005849 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005850 /* Hard assertion to be sure that no application data
5851 * is in flight, as corrupting ssl->in_msglen during
5852 * ssl->in_offt != NULL is fatal. */
5853 if( ssl->in_offt != NULL )
5854 {
5855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5856 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5857 }
5858
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005859 /*
5860 * Get next Handshake message in the current record
5861 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005862
Hanno Becker4a810fb2017-05-24 16:27:30 +01005863 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005864 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005865 * current handshake content: If DTLS handshake
5866 * fragmentation is used, that's the fragment
5867 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005868 * size here is faulty and should be changed at
5869 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005870 * (2) While it doesn't seem to cause problems, one
5871 * has to be very careful not to assume that in_hslen
5872 * is always <= in_msglen in a sensible communication.
5873 * Again, it's wrong for DTLS handshake fragmentation.
5874 * The following check is therefore mandatory, and
5875 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005876 * Additionally, ssl->in_hslen might be arbitrarily out of
5877 * bounds after handling a DTLS message with an unexpected
5878 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005879 */
5880 if( ssl->in_hslen < ssl->in_msglen )
5881 {
5882 ssl->in_msglen -= ssl->in_hslen;
5883 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5884 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005885
Hanno Becker4a810fb2017-05-24 16:27:30 +01005886 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5887 ssl->in_msg, ssl->in_msglen );
5888 }
5889 else
5890 {
5891 ssl->in_msglen = 0;
5892 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005893
Hanno Becker4a810fb2017-05-24 16:27:30 +01005894 ssl->in_hslen = 0;
5895 }
5896 /* Case (4): Application data */
5897 else if( ssl->in_offt != NULL )
5898 {
5899 return( 0 );
5900 }
5901 /* Everything else (CCS & Alerts) */
5902 else
5903 {
5904 ssl->in_msglen = 0;
5905 }
5906
Hanno Becker1097b342018-08-15 14:09:41 +01005907 return( 0 );
5908}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005909
Hanno Beckere74d5562018-08-15 14:26:08 +01005910static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5911{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005912 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005913 return( 1 );
5914
5915 return( 0 );
5916}
5917
Hanno Becker5f066e72018-08-16 14:56:31 +01005918#if defined(MBEDTLS_SSL_PROTO_DTLS)
5919
5920static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5921{
5922 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5923 if( hs == NULL )
5924 return;
5925
Hanno Becker01315ea2018-08-21 17:22:17 +01005926 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005927 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005928 hs->buffering.total_bytes_buffered -=
5929 hs->buffering.future_record.len;
5930
5931 mbedtls_free( hs->buffering.future_record.data );
5932 hs->buffering.future_record.data = NULL;
5933 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005934}
5935
5936static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5937{
5938 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5939 unsigned char * rec;
5940 size_t rec_len;
5941 unsigned rec_epoch;
5942
5943 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5944 return( 0 );
5945
5946 if( hs == NULL )
5947 return( 0 );
5948
Hanno Becker5f066e72018-08-16 14:56:31 +01005949 rec = hs->buffering.future_record.data;
5950 rec_len = hs->buffering.future_record.len;
5951 rec_epoch = hs->buffering.future_record.epoch;
5952
5953 if( rec == NULL )
5954 return( 0 );
5955
Hanno Becker4cb782d2018-08-20 11:19:05 +01005956 /* Only consider loading future records if the
5957 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005958 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005959 return( 0 );
5960
Hanno Becker5f066e72018-08-16 14:56:31 +01005961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5962
5963 if( rec_epoch != ssl->in_epoch )
5964 {
5965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5966 goto exit;
5967 }
5968
5969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5970
5971 /* Double-check that the record is not too large */
5972 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5973 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5974 {
5975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5976 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5977 }
5978
5979 memcpy( ssl->in_hdr, rec, rec_len );
5980 ssl->in_left = rec_len;
5981 ssl->next_record_offset = 0;
5982
5983 ssl_free_buffered_record( ssl );
5984
5985exit:
5986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5987 return( 0 );
5988}
5989
Hanno Becker519f15d2019-07-11 12:43:20 +01005990static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
5991 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01005992{
5993 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01005994
5995 /* Don't buffer future records outside handshakes. */
5996 if( hs == NULL )
5997 return( 0 );
5998
5999 /* Only buffer handshake records (we are only interested
6000 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01006001 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006002 return( 0 );
6003
6004 /* Don't buffer more than one future epoch record. */
6005 if( hs->buffering.future_record.data != NULL )
6006 return( 0 );
6007
Hanno Becker01315ea2018-08-21 17:22:17 +01006008 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01006009 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006010 hs->buffering.total_bytes_buffered ) )
6011 {
6012 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 +01006013 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006014 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006015 return( 0 );
6016 }
6017
Hanno Becker5f066e72018-08-16 14:56:31 +01006018 /* Buffer record */
6019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6020 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01006021 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006022
6023 /* ssl_parse_record_header() only considers records
6024 * of the next epoch as candidates for buffering. */
6025 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006026 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006027
6028 hs->buffering.future_record.data =
6029 mbedtls_calloc( 1, hs->buffering.future_record.len );
6030 if( hs->buffering.future_record.data == NULL )
6031 {
6032 /* If we run out of RAM trying to buffer a
6033 * record from the next epoch, just ignore. */
6034 return( 0 );
6035 }
6036
Hanno Becker519f15d2019-07-11 12:43:20 +01006037 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006038
Hanno Becker519f15d2019-07-11 12:43:20 +01006039 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006040 return( 0 );
6041}
6042
6043#endif /* MBEDTLS_SSL_PROTO_DTLS */
6044
Hanno Beckere74d5562018-08-15 14:26:08 +01006045static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006046{
6047 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006048 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006049
Hanno Becker5f066e72018-08-16 14:56:31 +01006050#if defined(MBEDTLS_SSL_PROTO_DTLS)
6051 /* We might have buffered a future record; if so,
6052 * and if the epoch matches now, load it.
6053 * On success, this call will set ssl->in_left to
6054 * the length of the buffered record, so that
6055 * the calls to ssl_fetch_input() below will
6056 * essentially be no-ops. */
6057 ret = ssl_load_buffered_record( ssl );
6058 if( ret != 0 )
6059 return( ret );
6060#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006061
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006062 /* Ensure that we have enough space available for the default form
6063 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6064 * with no space for CIDs counted in). */
6065 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6066 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006069 return( ret );
6070 }
6071
Hanno Beckere5e7e782019-07-11 12:29:35 +01006072 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6073 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006075#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006076 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006077 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006078 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6079 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006080 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006081 if( ret != 0 )
6082 return( ret );
6083
6084 /* Fall through to handling of unexpected records */
6085 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6086 }
6087
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006088 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6089 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006090#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006091 /* Reset in pointers to default state for TLS/DTLS records,
6092 * assuming no CID and no offset between record content and
6093 * record plaintext. */
6094 ssl_update_in_pointers( ssl );
6095
Hanno Becker7ae20e02019-07-12 08:33:49 +01006096 /* Setup internal message pointers from record structure. */
6097 ssl->in_msgtype = rec.type;
6098#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6099 ssl->in_len = ssl->in_cid + rec.cid_len;
6100#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6101 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6102 ssl->in_msglen = rec.data_len;
6103
Hanno Becker2fddd372019-07-10 14:37:41 +01006104 ret = ssl_check_client_reconnect( ssl );
6105 if( ret != 0 )
6106 return( ret );
6107#endif
6108
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006109 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006110 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006111
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6113 "(header)" ) );
6114 }
6115 else
6116 {
6117 /* Skip invalid record and the rest of the datagram */
6118 ssl->next_record_offset = 0;
6119 ssl->in_left = 0;
6120
6121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6122 "(header)" ) );
6123 }
6124
6125 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006126 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006127 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006128 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006129#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006130 {
6131 return( ret );
6132 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006133 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006135#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006136 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006137 {
Hanno Beckera8814792019-07-10 15:01:45 +01006138 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006139 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006140 if( ssl->next_record_offset < ssl->in_left )
6141 {
6142 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6143 }
6144 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006145 else
6146#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006147 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006148 /*
6149 * Fetch record contents from underlying transport.
6150 */
Hanno Beckera3175662019-07-11 12:50:29 +01006151 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006152 if( ret != 0 )
6153 {
6154 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6155 return( ret );
6156 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006157
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006158 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006159 }
6160
6161 /*
6162 * Decrypt record contents.
6163 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006164
Hanno Beckerfdf66042019-07-11 13:07:45 +01006165 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006168 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006169 {
6170 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006171 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006172 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006173 /* Except when waiting for Finished as a bad mac here
6174 * probably means something went wrong in the handshake
6175 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6176 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6177 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6178 {
6179#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6180 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6181 {
6182 mbedtls_ssl_send_alert_message( ssl,
6183 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6184 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6185 }
6186#endif
6187 return( ret );
6188 }
6189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006191 if( ssl->conf->badmac_limit != 0 &&
6192 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6195 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006196 }
6197#endif
6198
Hanno Becker4a810fb2017-05-24 16:27:30 +01006199 /* As above, invalid records cause
6200 * dismissal of the whole datagram. */
6201
6202 ssl->next_record_offset = 0;
6203 ssl->in_left = 0;
6204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006206 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006207 }
6208
6209 return( ret );
6210 }
6211 else
6212#endif
6213 {
6214 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6216 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 mbedtls_ssl_send_alert_message( ssl,
6219 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6220 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006221 }
6222#endif
6223 return( ret );
6224 }
6225 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006226
Hanno Becker44d89b22019-07-12 09:40:44 +01006227
6228 /* Reset in pointers to default state for TLS/DTLS records,
6229 * assuming no CID and no offset between record content and
6230 * record plaintext. */
6231 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006232#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6233 ssl->in_len = ssl->in_cid + rec.cid_len;
6234#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6235 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006236
Hanno Becker8685c822019-07-12 09:37:30 +01006237 /* The record content type may change during decryption,
6238 * so re-read it. */
6239 ssl->in_msgtype = rec.type;
6240 /* Also update the input buffer, because unfortunately
6241 * the server-side ssl_parse_client_hello() reparses the
6242 * record header when receiving a ClientHello initiating
6243 * a renegotiation. */
6244 ssl->in_hdr[0] = rec.type;
6245 ssl->in_msg = rec.buf + rec.data_offset;
6246 ssl->in_msglen = rec.data_len;
6247 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6248 ssl->in_len[1] = (unsigned char)( rec.data_len );
6249
Simon Butcher99000142016-10-13 17:21:01 +01006250 return( 0 );
6251}
6252
6253int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6254{
6255 int ret;
6256
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006257 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006258 * Handle particular types of records
6259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006260 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006261 {
Simon Butcher99000142016-10-13 17:21:01 +01006262 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6263 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006264 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006265 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006266 }
6267
Hanno Beckere678eaa2018-08-21 14:57:46 +01006268 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006269 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006270 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006271 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6273 ssl->in_msglen ) );
6274 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006275 }
6276
Hanno Beckere678eaa2018-08-21 14:57:46 +01006277 if( ssl->in_msg[0] != 1 )
6278 {
6279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6280 ssl->in_msg[0] ) );
6281 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6282 }
6283
6284#if defined(MBEDTLS_SSL_PROTO_DTLS)
6285 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6286 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6287 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6288 {
6289 if( ssl->handshake == NULL )
6290 {
6291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6292 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6293 }
6294
6295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6296 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6297 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006298#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006299 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006301 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006302 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006303 if( ssl->in_msglen != 2 )
6304 {
6305 /* Note: Standard allows for more than one 2 byte alert
6306 to be packed in a single message, but Mbed TLS doesn't
6307 currently support this. */
6308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6309 ssl->in_msglen ) );
6310 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6311 }
6312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006314 ssl->in_msg[0], ssl->in_msg[1] ) );
6315
6316 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006317 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006322 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006324 }
6325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006326 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6327 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6330 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006331 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006332
6333#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6334 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6335 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6336 {
Hanno Becker90333da2017-10-10 11:27:13 +01006337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006338 /* Will be handled when trying to parse ServerHello */
6339 return( 0 );
6340 }
6341#endif
6342
6343#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6344 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6345 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6346 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6347 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6348 {
6349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6350 /* Will be handled in mbedtls_ssl_parse_certificate() */
6351 return( 0 );
6352 }
6353#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6354
6355 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006356 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006357 }
6358
Hanno Beckerc76c6192017-06-06 10:03:17 +01006359#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006360 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006361 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006362 /* Drop unexpected ApplicationData records,
6363 * except at the beginning of renegotiations */
6364 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6365 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6366#if defined(MBEDTLS_SSL_RENEGOTIATION)
6367 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6368 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006369#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006370 )
6371 {
6372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6373 return( MBEDTLS_ERR_SSL_NON_FATAL );
6374 }
6375
6376 if( ssl->handshake != NULL &&
6377 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6378 {
6379 ssl_handshake_wrapup_free_hs_transform( ssl );
6380 }
6381 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006382#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006383
Paul Bakker5121ce52009-01-03 21:22:43 +00006384 return( 0 );
6385}
6386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006387int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006388{
6389 int ret;
6390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006391 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6392 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6393 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006394 {
6395 return( ret );
6396 }
6397
6398 return( 0 );
6399}
6400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006401int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006402 unsigned char level,
6403 unsigned char message )
6404{
6405 int ret;
6406
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006407 if( ssl == NULL || ssl->conf == NULL )
6408 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006411 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006413 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006414 ssl->out_msglen = 2;
6415 ssl->out_msg[0] = level;
6416 ssl->out_msg[1] = message;
6417
Hanno Becker67bc7c32018-08-06 11:33:50 +01006418 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006421 return( ret );
6422 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006424
6425 return( 0 );
6426}
6427
Hanno Beckerb9d44792019-02-08 07:19:04 +00006428#if defined(MBEDTLS_X509_CRT_PARSE_C)
6429static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6430{
6431#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6432 if( session->peer_cert != NULL )
6433 {
6434 mbedtls_x509_crt_free( session->peer_cert );
6435 mbedtls_free( session->peer_cert );
6436 session->peer_cert = NULL;
6437 }
6438#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6439 if( session->peer_cert_digest != NULL )
6440 {
6441 /* Zeroization is not necessary. */
6442 mbedtls_free( session->peer_cert_digest );
6443 session->peer_cert_digest = NULL;
6444 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6445 session->peer_cert_digest_len = 0;
6446 }
6447#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6448}
6449#endif /* MBEDTLS_X509_CRT_PARSE_C */
6450
Paul Bakker5121ce52009-01-03 21:22:43 +00006451/*
6452 * Handshake functions
6453 */
Hanno Becker21489932019-02-05 13:20:55 +00006454#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006455/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006456int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006457{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006458 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6459 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006462
Hanno Becker7177a882019-02-05 13:36:46 +00006463 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006466 ssl->state++;
6467 return( 0 );
6468 }
6469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6471 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006472}
6473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006475{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006476 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6477 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006480
Hanno Becker7177a882019-02-05 13:36:46 +00006481 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006484 ssl->state++;
6485 return( 0 );
6486 }
6487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006490}
Gilles Peskinef9828522017-05-03 12:28:43 +02006491
Hanno Becker21489932019-02-05 13:20:55 +00006492#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006493/* Some certificate support -> implement write and parse */
6494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006495int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006496{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006498 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006499 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006500 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6501 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006504
Hanno Becker7177a882019-02-05 13:36:46 +00006505 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006508 ssl->state++;
6509 return( 0 );
6510 }
6511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006512#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006513 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006514 {
6515 if( ssl->client_auth == 0 )
6516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006518 ssl->state++;
6519 return( 0 );
6520 }
6521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006522#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006523 /*
6524 * If using SSLv3 and got no cert, send an Alert message
6525 * (otherwise an empty Certificate message will be sent).
6526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006527 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6528 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006529 {
6530 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006531 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6532 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6533 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006536 goto write_msg;
6537 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006539 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006540#endif /* MBEDTLS_SSL_CLI_C */
6541#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006542 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6547 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006548 }
6549 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006550#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006553
6554 /*
6555 * 0 . 0 handshake type
6556 * 1 . 3 handshake length
6557 * 4 . 6 length of all certs
6558 * 7 . 9 length of cert. 1
6559 * 10 . n-1 peer certificate
6560 * n . n+2 length of cert. 2
6561 * n+3 . ... upper level cert, etc.
6562 */
6563 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006565
Paul Bakker29087132010-03-21 21:03:34 +00006566 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006567 {
6568 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006569 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006572 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006574 }
6575
6576 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6577 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6578 ssl->out_msg[i + 2] = (unsigned char)( n );
6579
6580 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6581 i += n; crt = crt->next;
6582 }
6583
6584 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6585 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6586 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6587
6588 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006589 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6590 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006591
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006592#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006593write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006594#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006595
6596 ssl->state++;
6597
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006598 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006599 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006600 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006601 return( ret );
6602 }
6603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006605
Paul Bakkered27a042013-04-18 22:46:23 +02006606 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006607}
6608
Hanno Becker84879e32019-01-31 07:44:03 +00006609#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006610
6611#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006612static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6613 unsigned char *crt_buf,
6614 size_t crt_buf_len )
6615{
6616 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6617
6618 if( peer_crt == NULL )
6619 return( -1 );
6620
6621 if( peer_crt->raw.len != crt_buf_len )
6622 return( -1 );
6623
Hanno Becker46f34d02019-02-08 14:00:04 +00006624 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006625}
Hanno Becker177475a2019-02-05 17:02:46 +00006626#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6627static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6628 unsigned char *crt_buf,
6629 size_t crt_buf_len )
6630{
6631 int ret;
6632 unsigned char const * const peer_cert_digest =
6633 ssl->session->peer_cert_digest;
6634 mbedtls_md_type_t const peer_cert_digest_type =
6635 ssl->session->peer_cert_digest_type;
6636 mbedtls_md_info_t const * const digest_info =
6637 mbedtls_md_info_from_type( peer_cert_digest_type );
6638 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6639 size_t digest_len;
6640
6641 if( peer_cert_digest == NULL || digest_info == NULL )
6642 return( -1 );
6643
6644 digest_len = mbedtls_md_get_size( digest_info );
6645 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6646 return( -1 );
6647
6648 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6649 if( ret != 0 )
6650 return( -1 );
6651
6652 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6653}
6654#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006655#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006656
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006657/*
6658 * Once the certificate message is read, parse it into a cert chain and
6659 * perform basic checks, but leave actual verification to the caller
6660 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006661static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6662 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006663{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006664 int ret;
6665#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6666 int crt_cnt=0;
6667#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006668 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006669 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006671 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006674 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6675 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006677 }
6678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006679 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6680 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006683 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6684 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006685 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006686 }
6687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006689
Paul Bakker5121ce52009-01-03 21:22:43 +00006690 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006692 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006693 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006694
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006695 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006699 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6700 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006702 }
6703
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006704 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6705 i += 3;
6706
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006707 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006708 while( i < ssl->in_hslen )
6709 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006710 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006711 if ( i + 3 > ssl->in_hslen ) {
6712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006713 mbedtls_ssl_send_alert_message( ssl,
6714 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6715 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006716 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6717 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006718 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6719 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006720 if( ssl->in_msg[i] != 0 )
6721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006723 mbedtls_ssl_send_alert_message( ssl,
6724 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6725 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006726 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006727 }
6728
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006729 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006730 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6731 | (unsigned int) ssl->in_msg[i + 2];
6732 i += 3;
6733
6734 if( n < 128 || i + n > ssl->in_hslen )
6735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006737 mbedtls_ssl_send_alert_message( ssl,
6738 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6739 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006741 }
6742
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006743 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006744#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6745 if( crt_cnt++ == 0 &&
6746 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6747 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006748 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006749 /* During client-side renegotiation, check that the server's
6750 * end-CRTs hasn't changed compared to the initial handshake,
6751 * mitigating the triple handshake attack. On success, reuse
6752 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006753 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6754 if( ssl_check_peer_crt_unchanged( ssl,
6755 &ssl->in_msg[i],
6756 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006757 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6759 mbedtls_ssl_send_alert_message( ssl,
6760 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6761 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6762 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006763 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006764
6765 /* Now we can safely free the original chain. */
6766 ssl_clear_peer_cert( ssl->session );
6767 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006768#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6769
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006770 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006771#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006772 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006773#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006774 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006775 * it in-place from the input buffer instead of making a copy. */
6776 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6777#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006778 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006779 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006780 case 0: /*ok*/
6781 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6782 /* Ignore certificate with an unknown algorithm: maybe a
6783 prior certificate was already trusted. */
6784 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006785
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006786 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6787 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6788 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006789
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006790 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6791 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6792 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006793
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006794 default:
6795 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6796 crt_parse_der_failed:
6797 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6798 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6799 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006800 }
6801
6802 i += n;
6803 }
6804
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006805 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006806 return( 0 );
6807}
6808
Hanno Becker4a55f632019-02-05 12:49:06 +00006809#if defined(MBEDTLS_SSL_SRV_C)
6810static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6811{
6812 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6813 return( -1 );
6814
6815#if defined(MBEDTLS_SSL_PROTO_SSL3)
6816 /*
6817 * Check if the client sent an empty certificate
6818 */
6819 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6820 {
6821 if( ssl->in_msglen == 2 &&
6822 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6823 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6824 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6825 {
6826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6827 return( 0 );
6828 }
6829
6830 return( -1 );
6831 }
6832#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6833
6834#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6835 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6836 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6837 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6838 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6839 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6840 {
6841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6842 return( 0 );
6843 }
6844
6845 return( -1 );
6846#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6847 MBEDTLS_SSL_PROTO_TLS1_2 */
6848}
6849#endif /* MBEDTLS_SSL_SRV_C */
6850
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006851/* Check if a certificate message is expected.
6852 * Return either
6853 * - SSL_CERTIFICATE_EXPECTED, or
6854 * - SSL_CERTIFICATE_SKIP
6855 * indicating whether a Certificate message is expected or not.
6856 */
6857#define SSL_CERTIFICATE_EXPECTED 0
6858#define SSL_CERTIFICATE_SKIP 1
6859static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6860 int authmode )
6861{
6862 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006863 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006864
6865 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6866 return( SSL_CERTIFICATE_SKIP );
6867
6868#if defined(MBEDTLS_SSL_SRV_C)
6869 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6870 {
6871 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6872 return( SSL_CERTIFICATE_SKIP );
6873
6874 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6875 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006876 ssl->session_negotiate->verify_result =
6877 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6878 return( SSL_CERTIFICATE_SKIP );
6879 }
6880 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006881#else
6882 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006883#endif /* MBEDTLS_SSL_SRV_C */
6884
6885 return( SSL_CERTIFICATE_EXPECTED );
6886}
6887
Hanno Becker68636192019-02-05 14:36:34 +00006888static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6889 int authmode,
6890 mbedtls_x509_crt *chain,
6891 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006892{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006893 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006894 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006895 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006896 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006897
Hanno Becker8927c832019-04-03 12:52:50 +01006898 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6899 void *p_vrfy;
6900
Hanno Becker68636192019-02-05 14:36:34 +00006901 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6902 return( 0 );
6903
Hanno Becker8927c832019-04-03 12:52:50 +01006904 if( ssl->f_vrfy != NULL )
6905 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006906 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006907 f_vrfy = ssl->f_vrfy;
6908 p_vrfy = ssl->p_vrfy;
6909 }
6910 else
6911 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006912 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006913 f_vrfy = ssl->conf->f_vrfy;
6914 p_vrfy = ssl->conf->p_vrfy;
6915 }
6916
Hanno Becker68636192019-02-05 14:36:34 +00006917 /*
6918 * Main check: verify certificate
6919 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006920#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6921 if( ssl->conf->f_ca_cb != NULL )
6922 {
6923 ((void) rs_ctx);
6924 have_ca_chain = 1;
6925
6926 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006927 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006928 chain,
6929 ssl->conf->f_ca_cb,
6930 ssl->conf->p_ca_cb,
6931 ssl->conf->cert_profile,
6932 ssl->hostname,
6933 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006934 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006935 }
6936 else
6937#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6938 {
6939 mbedtls_x509_crt *ca_chain;
6940 mbedtls_x509_crl *ca_crl;
6941
6942#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6943 if( ssl->handshake->sni_ca_chain != NULL )
6944 {
6945 ca_chain = ssl->handshake->sni_ca_chain;
6946 ca_crl = ssl->handshake->sni_ca_crl;
6947 }
6948 else
6949#endif
6950 {
6951 ca_chain = ssl->conf->ca_chain;
6952 ca_crl = ssl->conf->ca_crl;
6953 }
6954
6955 if( ca_chain != NULL )
6956 have_ca_chain = 1;
6957
6958 ret = mbedtls_x509_crt_verify_restartable(
6959 chain,
6960 ca_chain, ca_crl,
6961 ssl->conf->cert_profile,
6962 ssl->hostname,
6963 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006964 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006965 }
Hanno Becker68636192019-02-05 14:36:34 +00006966
6967 if( ret != 0 )
6968 {
6969 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6970 }
6971
6972#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6973 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6974 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6975#endif
6976
6977 /*
6978 * Secondary checks: always done, but change 'ret' only if it was 0
6979 */
6980
6981#if defined(MBEDTLS_ECP_C)
6982 {
6983 const mbedtls_pk_context *pk = &chain->pk;
6984
6985 /* If certificate uses an EC key, make sure the curve is OK */
6986 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6987 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6988 {
6989 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6990
6991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6992 if( ret == 0 )
6993 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6994 }
6995 }
6996#endif /* MBEDTLS_ECP_C */
6997
6998 if( mbedtls_ssl_check_cert_usage( chain,
6999 ciphersuite_info,
7000 ! ssl->conf->endpoint,
7001 &ssl->session_negotiate->verify_result ) != 0 )
7002 {
7003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7004 if( ret == 0 )
7005 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7006 }
7007
7008 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7009 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7010 * with details encoded in the verification flags. All other kinds
7011 * of error codes, including those from the user provided f_vrfy
7012 * functions, are treated as fatal and lead to a failure of
7013 * ssl_parse_certificate even if verification was optional. */
7014 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7015 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7016 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7017 {
7018 ret = 0;
7019 }
7020
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007021 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007022 {
7023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7024 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7025 }
7026
7027 if( ret != 0 )
7028 {
7029 uint8_t alert;
7030
7031 /* The certificate may have been rejected for several reasons.
7032 Pick one and send the corresponding alert. Which alert to send
7033 may be a subject of debate in some cases. */
7034 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7035 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7036 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7037 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7038 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7039 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7040 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7041 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7042 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7043 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7044 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7045 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7046 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7047 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7048 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7049 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7050 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7051 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7052 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7053 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7054 else
7055 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7056 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7057 alert );
7058 }
7059
7060#if defined(MBEDTLS_DEBUG_C)
7061 if( ssl->session_negotiate->verify_result != 0 )
7062 {
7063 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7064 ssl->session_negotiate->verify_result ) );
7065 }
7066 else
7067 {
7068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7069 }
7070#endif /* MBEDTLS_DEBUG_C */
7071
7072 return( ret );
7073}
7074
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007075#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7076static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7077 unsigned char *start, size_t len )
7078{
7079 int ret;
7080 /* Remember digest of the peer's end-CRT. */
7081 ssl->session_negotiate->peer_cert_digest =
7082 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7083 if( ssl->session_negotiate->peer_cert_digest == NULL )
7084 {
7085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7086 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7087 mbedtls_ssl_send_alert_message( ssl,
7088 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7089 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7090
7091 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7092 }
7093
7094 ret = mbedtls_md( mbedtls_md_info_from_type(
7095 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7096 start, len,
7097 ssl->session_negotiate->peer_cert_digest );
7098
7099 ssl->session_negotiate->peer_cert_digest_type =
7100 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7101 ssl->session_negotiate->peer_cert_digest_len =
7102 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7103
7104 return( ret );
7105}
7106
7107static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7108 unsigned char *start, size_t len )
7109{
7110 unsigned char *end = start + len;
7111 int ret;
7112
7113 /* Make a copy of the peer's raw public key. */
7114 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7115 ret = mbedtls_pk_parse_subpubkey( &start, end,
7116 &ssl->handshake->peer_pubkey );
7117 if( ret != 0 )
7118 {
7119 /* We should have parsed the public key before. */
7120 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7121 }
7122
7123 return( 0 );
7124}
7125#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7126
Hanno Becker68636192019-02-05 14:36:34 +00007127int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7128{
7129 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007130 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007131#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7132 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7133 ? ssl->handshake->sni_authmode
7134 : ssl->conf->authmode;
7135#else
7136 const int authmode = ssl->conf->authmode;
7137#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007138 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007139 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007140
7141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7142
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007143 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7144 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007145 {
7146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007147 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007148 }
7149
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007150#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7151 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007152 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007153 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007154 chain = ssl->handshake->ecrs_peer_cert;
7155 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007156 goto crt_verify;
7157 }
7158#endif
7159
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007160 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007161 {
7162 /* mbedtls_ssl_read_record may have sent an alert already. We
7163 let it decide whether to alert. */
7164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007165 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007166 }
7167
Hanno Becker4a55f632019-02-05 12:49:06 +00007168#if defined(MBEDTLS_SSL_SRV_C)
7169 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7170 {
7171 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007172
7173 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007174 ret = 0;
7175 else
7176 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007177
Hanno Becker6bdfab22019-02-05 13:11:17 +00007178 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007179 }
7180#endif /* MBEDTLS_SSL_SRV_C */
7181
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007182 /* Clear existing peer CRT structure in case we tried to
7183 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007184 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007185
7186 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7187 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007188 {
7189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7190 sizeof( mbedtls_x509_crt ) ) );
7191 mbedtls_ssl_send_alert_message( ssl,
7192 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7193 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007194
Hanno Becker3dad3112019-02-05 17:19:52 +00007195 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7196 goto exit;
7197 }
7198 mbedtls_x509_crt_init( chain );
7199
7200 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007201 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007202 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007203
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007204#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7205 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007206 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007207
7208crt_verify:
7209 if( ssl->handshake->ecrs_enabled)
7210 rs_ctx = &ssl->handshake->ecrs_ctx;
7211#endif
7212
Hanno Becker68636192019-02-05 14:36:34 +00007213 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007214 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007215 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007216 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007217
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007218#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007219 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007220 unsigned char *crt_start, *pk_start;
7221 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007222
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007223 /* We parse the CRT chain without copying, so
7224 * these pointers point into the input buffer,
7225 * and are hence still valid after freeing the
7226 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007227
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007228 crt_start = chain->raw.p;
7229 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007230
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007231 pk_start = chain->pk_raw.p;
7232 pk_len = chain->pk_raw.len;
7233
7234 /* Free the CRT structures before computing
7235 * digest and copying the peer's public key. */
7236 mbedtls_x509_crt_free( chain );
7237 mbedtls_free( chain );
7238 chain = NULL;
7239
7240 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007241 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007242 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007243
7244 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7245 if( ret != 0 )
7246 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007247 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007248#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7249 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007250 ssl->session_negotiate->peer_cert = chain;
7251 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007252#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007255
Hanno Becker6bdfab22019-02-05 13:11:17 +00007256exit:
7257
Hanno Becker3dad3112019-02-05 17:19:52 +00007258 if( ret == 0 )
7259 ssl->state++;
7260
7261#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7262 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7263 {
7264 ssl->handshake->ecrs_peer_cert = chain;
7265 chain = NULL;
7266 }
7267#endif
7268
7269 if( chain != NULL )
7270 {
7271 mbedtls_x509_crt_free( chain );
7272 mbedtls_free( chain );
7273 }
7274
Paul Bakker5121ce52009-01-03 21:22:43 +00007275 return( ret );
7276}
Hanno Becker21489932019-02-05 13:20:55 +00007277#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007280{
7281 int ret;
7282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007286 ssl->out_msglen = 1;
7287 ssl->out_msg[0] = 1;
7288
Paul Bakker5121ce52009-01-03 21:22:43 +00007289 ssl->state++;
7290
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007291 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007292 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007293 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007294 return( ret );
7295 }
7296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007298
7299 return( 0 );
7300}
7301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007303{
7304 int ret;
7305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007307
Hanno Becker327c93b2018-08-15 13:56:18 +01007308 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007311 return( ret );
7312 }
7313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007317 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7318 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007320 }
7321
Hanno Beckere678eaa2018-08-21 14:57:46 +01007322 /* CCS records are only accepted if they have length 1 and content '1',
7323 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007324
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007325 /*
7326 * Switch to our negotiated transform and session parameters for inbound
7327 * data.
7328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007329 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007330 ssl->transform_in = ssl->transform_negotiate;
7331 ssl->session_in = ssl->session_negotiate;
7332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007334 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007337 ssl_dtls_replay_reset( ssl );
7338#endif
7339
7340 /* Increment epoch */
7341 if( ++ssl->in_epoch == 0 )
7342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007344 /* This is highly unlikely to happen for legitimate reasons, so
7345 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007347 }
7348 }
7349 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007351 memset( ssl->in_ctr, 0, 8 );
7352
Hanno Becker79594fd2019-05-08 09:38:41 +01007353 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007355#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7356 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007358 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007361 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7362 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007363 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007364 }
7365 }
7366#endif
7367
Paul Bakker5121ce52009-01-03 21:22:43 +00007368 ssl->state++;
7369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007371
7372 return( 0 );
7373}
7374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7376 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007377{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007378 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7381 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7382 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007383 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007384 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007385#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7387#if defined(MBEDTLS_SHA512_C)
7388 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007389 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7390 else
7391#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392#if defined(MBEDTLS_SHA256_C)
7393 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007394 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007395 else
7396#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007397#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007400 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007401 }
Paul Bakker380da532012-04-18 16:10:25 +00007402}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007405{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007406#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7407 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007408 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7409 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007410#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7412#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007413#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007414 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007415 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7416#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007417 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +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 Kurek2ad22972019-01-30 03:32:12 -05007422 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007423 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007424#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007425 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007426#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007427#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007429}
7430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007432 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007433{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7435 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007436 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7437 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007438#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007439#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7440#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007441#if defined(MBEDTLS_USE_PSA_CRYPTO)
7442 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7443#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007444 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007445#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007446#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007448#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007449 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007450#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007451 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007452#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007453#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007455}
7456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7458 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7459static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007460 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007461{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007462 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7463 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007464}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007465#endif
Paul Bakker380da532012-04-18 16:10:25 +00007466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007467#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7468#if defined(MBEDTLS_SHA256_C)
7469static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007470 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007471{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007472#if defined(MBEDTLS_USE_PSA_CRYPTO)
7473 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7474#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007475 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007476#endif
Paul Bakker380da532012-04-18 16:10:25 +00007477}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007478#endif
Paul Bakker380da532012-04-18 16:10:25 +00007479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007480#if defined(MBEDTLS_SHA512_C)
7481static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007482 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007483{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007484#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007485 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007486#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007487 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007488#endif
Paul Bakker380da532012-04-18 16:10:25 +00007489}
Paul Bakker769075d2012-11-24 11:26:46 +01007490#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007494static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007496{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007497 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007498 mbedtls_md5_context md5;
7499 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007500
Paul Bakker5121ce52009-01-03 21:22:43 +00007501 unsigned char padbuf[48];
7502 unsigned char md5sum[16];
7503 unsigned char sha1sum[20];
7504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007506 if( !session )
7507 session = ssl->session;
7508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007510
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007511 mbedtls_md5_init( &md5 );
7512 mbedtls_sha1_init( &sha1 );
7513
7514 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7515 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007516
7517 /*
7518 * SSLv3:
7519 * hash =
7520 * MD5( master + pad2 +
7521 * MD5( handshake + sender + master + pad1 ) )
7522 * + SHA1( master + pad2 +
7523 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007524 */
7525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007527 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7528 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007529#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007532 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7533 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007534#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007536 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007537 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007538
Paul Bakker1ef83d62012-04-11 12:09:53 +00007539 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007540
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007541 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7542 mbedtls_md5_update_ret( &md5, session->master, 48 );
7543 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7544 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007545
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007546 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7547 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7548 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7549 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007550
Paul Bakker1ef83d62012-04-11 12:09:53 +00007551 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007552
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007553 mbedtls_md5_starts_ret( &md5 );
7554 mbedtls_md5_update_ret( &md5, session->master, 48 );
7555 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7556 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7557 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007558
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007559 mbedtls_sha1_starts_ret( &sha1 );
7560 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7561 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7562 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7563 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007566
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007567 mbedtls_md5_free( &md5 );
7568 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007569
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007570 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7571 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7572 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007576#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007579static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007581{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007582 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007583 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007584 mbedtls_md5_context md5;
7585 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007586 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007588 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007589 if( !session )
7590 session = ssl->session;
7591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007593
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007594 mbedtls_md5_init( &md5 );
7595 mbedtls_sha1_init( &sha1 );
7596
7597 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7598 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007599
Paul Bakker1ef83d62012-04-11 12:09:53 +00007600 /*
7601 * TLSv1:
7602 * hash = PRF( master, finished_label,
7603 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7604 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007607 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7608 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007609#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007612 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7613 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007614#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007617 ? "client finished"
7618 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007619
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007620 mbedtls_md5_finish_ret( &md5, padbuf );
7621 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007622
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007623 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007624 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007627
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007628 mbedtls_md5_free( &md5 );
7629 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007630
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007631 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007634}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007635#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007637#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7638#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007639static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007641{
7642 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007643 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007644 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007645#if defined(MBEDTLS_USE_PSA_CRYPTO)
7646 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007647 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007648 psa_status_t status;
7649#else
7650 mbedtls_sha256_context sha256;
7651#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007654 if( !session )
7655 session = ssl->session;
7656
Andrzej Kurekeb342242019-01-29 09:14:33 -05007657 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7658 ? "client finished"
7659 : "server finished";
7660
7661#if defined(MBEDTLS_USE_PSA_CRYPTO)
7662 sha256_psa = psa_hash_operation_init();
7663
7664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7665
7666 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7667 if( status != PSA_SUCCESS )
7668 {
7669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7670 return;
7671 }
7672
7673 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7674 if( status != PSA_SUCCESS )
7675 {
7676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7677 return;
7678 }
7679 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7680#else
7681
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007682 mbedtls_sha256_init( &sha256 );
7683
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007685
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007686 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007687
7688 /*
7689 * TLSv1.2:
7690 * hash = PRF( master, finished_label,
7691 * Hash( handshake ) )[0.11]
7692 */
7693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007694#if !defined(MBEDTLS_SHA256_ALT)
7695 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007696 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007697#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007698
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007699 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007700 mbedtls_sha256_free( &sha256 );
7701#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007702
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007703 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007704 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007707
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007708 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007711}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007712#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007714#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007715static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007717{
7718 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007719 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007720 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007721#if defined(MBEDTLS_USE_PSA_CRYPTO)
7722 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007723 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007724 psa_status_t status;
7725#else
7726 mbedtls_sha512_context sha512;
7727#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007730 if( !session )
7731 session = ssl->session;
7732
Andrzej Kurekeb342242019-01-29 09:14:33 -05007733 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7734 ? "client finished"
7735 : "server finished";
7736
7737#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007738 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007739
7740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7741
Andrzej Kurek972fba52019-01-30 03:29:12 -05007742 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007743 if( status != PSA_SUCCESS )
7744 {
7745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7746 return;
7747 }
7748
Andrzej Kurek972fba52019-01-30 03:29:12 -05007749 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007750 if( status != PSA_SUCCESS )
7751 {
7752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7753 return;
7754 }
7755 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7756#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007757 mbedtls_sha512_init( &sha512 );
7758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007760
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007761 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007762
7763 /*
7764 * TLSv1.2:
7765 * hash = PRF( master, finished_label,
7766 * Hash( handshake ) )[0.11]
7767 */
7768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007770 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7771 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007772#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007773
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007774 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007775 mbedtls_sha512_free( &sha512 );
7776#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007777
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007778 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007779 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007781 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007782
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007783 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007786}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007787#endif /* MBEDTLS_SHA512_C */
7788#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007790static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007791{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007792 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007793
7794 /*
7795 * Free our handshake params
7796 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007797 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007798 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007799 ssl->handshake = NULL;
7800
7801 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007802 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007803 */
7804 if( ssl->transform )
7805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007806 mbedtls_ssl_transform_free( ssl->transform );
7807 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007808 }
7809 ssl->transform = ssl->transform_negotiate;
7810 ssl->transform_negotiate = NULL;
7811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007813}
7814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007816{
7817 int resume = ssl->handshake->resume;
7818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007819 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007821#if defined(MBEDTLS_SSL_RENEGOTIATION)
7822 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007824 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007825 ssl->renego_records_seen = 0;
7826 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007827#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007828
7829 /*
7830 * Free the previous session and switch in the current one
7831 */
Paul Bakker0a597072012-09-25 21:55:46 +00007832 if( ssl->session )
7833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007835 /* RFC 7366 3.1: keep the EtM state */
7836 ssl->session_negotiate->encrypt_then_mac =
7837 ssl->session->encrypt_then_mac;
7838#endif
7839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840 mbedtls_ssl_session_free( ssl->session );
7841 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007842 }
7843 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007844 ssl->session_negotiate = NULL;
7845
Paul Bakker0a597072012-09-25 21:55:46 +00007846 /*
7847 * Add cache entry
7848 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007849 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007850 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007851 resume == 0 )
7852 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007853 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007855 }
Paul Bakker0a597072012-09-25 21:55:46 +00007856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007858 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007859 ssl->handshake->flight != NULL )
7860 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007861 /* Cancel handshake timer */
7862 ssl_set_timer( ssl, 0 );
7863
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007864 /* Keep last flight around in case we need to resend it:
7865 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007866 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007867 }
7868 else
7869#endif
7870 ssl_handshake_wrapup_free_hs_transform( ssl );
7871
Paul Bakker48916f92012-09-16 19:57:18 +00007872 ssl->state++;
7873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007875}
7876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007878{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007879 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007882
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007883 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007884
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007885 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007886
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007887 /*
7888 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7889 * may define some other value. Currently (early 2016), no defined
7890 * ciphersuite does this (and this is unlikely to change as activity has
7891 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007895#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007896 ssl->verify_data_len = hash_len;
7897 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007898#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007899
Paul Bakker5121ce52009-01-03 21:22:43 +00007900 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7902 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007903
7904 /*
7905 * In case of session resuming, invert the client and server
7906 * ChangeCipherSpec messages order.
7907 */
Paul Bakker0a597072012-09-25 21:55:46 +00007908 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007911 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007913#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007914#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007915 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007917#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007918 }
7919 else
7920 ssl->state++;
7921
Paul Bakker48916f92012-09-16 19:57:18 +00007922 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007923 * Switch to our negotiated transform and session parameters for outbound
7924 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007925 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007929 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007930 {
7931 unsigned char i;
7932
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007933 /* Remember current epoch settings for resending */
7934 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007935 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007936
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007937 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007938 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007939
7940 /* Increment epoch */
7941 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007942 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007943 break;
7944
7945 /* The loop goes to its end iff the counter is wrapping */
7946 if( i == 0 )
7947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7949 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007950 }
7951 }
7952 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007953#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007954 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007955
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007956 ssl->transform_out = ssl->transform_negotiate;
7957 ssl->session_out = ssl->session_negotiate;
7958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7960 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007962 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7965 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007966 }
7967 }
7968#endif
7969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007971 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007973#endif
7974
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007975 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007976 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007977 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007978 return( ret );
7979 }
7980
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007981#if defined(MBEDTLS_SSL_PROTO_DTLS)
7982 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7983 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7984 {
7985 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7986 return( ret );
7987 }
7988#endif
7989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007991
7992 return( 0 );
7993}
7994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007996#define SSL_MAX_HASH_LEN 36
7997#else
7998#define SSL_MAX_HASH_LEN 12
7999#endif
8000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008002{
Paul Bakker23986e52011-04-24 08:57:21 +00008003 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008004 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008005 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008008
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008009 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008010
Hanno Becker327c93b2018-08-15 13:56:18 +01008011 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008014 return( ret );
8015 }
8016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008017 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008020 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8021 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008023 }
8024
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008025 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008026#if defined(MBEDTLS_SSL_PROTO_SSL3)
8027 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008028 hash_len = 36;
8029 else
8030#endif
8031 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008033 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8034 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008037 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8038 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008039 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008040 }
8041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008042 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008043 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008046 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8047 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008048 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008049 }
8050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008051#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008052 ssl->verify_data_len = hash_len;
8053 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008054#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008055
Paul Bakker0a597072012-09-25 21:55:46 +00008056 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008058#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008059 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008060 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008061#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008062#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008063 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008064 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008065#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008066 }
8067 else
8068 ssl->state++;
8069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008070#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008071 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008072 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008073#endif
8074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008076
8077 return( 0 );
8078}
8079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008081{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008082 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008084#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8085 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8086 mbedtls_md5_init( &handshake->fin_md5 );
8087 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008088 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8089 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008090#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008091#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8092#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008093#if defined(MBEDTLS_USE_PSA_CRYPTO)
8094 handshake->fin_sha256_psa = psa_hash_operation_init();
8095 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8096#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008097 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008098 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008099#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008100#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008101#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008102#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008103 handshake->fin_sha384_psa = psa_hash_operation_init();
8104 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008105#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008106 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008107 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008108#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008109#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008110#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008111
8112 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008113
8114#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8115 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8116 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8117#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008119#if defined(MBEDTLS_DHM_C)
8120 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008121#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008122#if defined(MBEDTLS_ECDH_C)
8123 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008124#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008125#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008126 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008127#if defined(MBEDTLS_SSL_CLI_C)
8128 handshake->ecjpake_cache = NULL;
8129 handshake->ecjpake_cache_len = 0;
8130#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008131#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008132
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008133#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008134 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008135#endif
8136
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008137#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8138 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8139#endif
Hanno Becker75173122019-02-06 16:18:31 +00008140
8141#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8142 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8143 mbedtls_pk_init( &handshake->peer_pubkey );
8144#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008145}
8146
Hanno Beckera18d1322018-01-03 14:27:32 +00008147void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008148{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008149 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008151 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8152 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008153
Hanno Beckerd56ed242018-01-03 15:32:51 +00008154#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008155 mbedtls_md_init( &transform->md_ctx_enc );
8156 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008157#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008158}
8159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008160void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008161{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008162 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008163}
8164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008166{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008167 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008168 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008169 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008170 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008171 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008172 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008173 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008174
8175 /*
8176 * Either the pointers are now NULL or cleared properly and can be freed.
8177 * Now allocate missing structures.
8178 */
8179 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008180 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008181 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008182 }
Paul Bakker48916f92012-09-16 19:57:18 +00008183
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008184 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008185 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008186 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008187 }
Paul Bakker48916f92012-09-16 19:57:18 +00008188
Paul Bakker82788fb2014-10-20 13:59:19 +02008189 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008190 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008191 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008192 }
Paul Bakker48916f92012-09-16 19:57:18 +00008193
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008194 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008195 if( ssl->handshake == NULL ||
8196 ssl->transform_negotiate == NULL ||
8197 ssl->session_negotiate == NULL )
8198 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201 mbedtls_free( ssl->handshake );
8202 mbedtls_free( ssl->transform_negotiate );
8203 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008204
8205 ssl->handshake = NULL;
8206 ssl->transform_negotiate = NULL;
8207 ssl->session_negotiate = NULL;
8208
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008209 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008210 }
8211
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008212 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008213 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008214 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008215 ssl_handshake_params_init( ssl->handshake );
8216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008218 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8219 {
8220 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008221
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008222 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8223 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8224 else
8225 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008226
8227 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008228 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008229#endif
8230
Paul Bakker48916f92012-09-16 19:57:18 +00008231 return( 0 );
8232}
8233
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008234#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008235/* Dummy cookie callbacks for defaults */
8236static int ssl_cookie_write_dummy( void *ctx,
8237 unsigned char **p, unsigned char *end,
8238 const unsigned char *cli_id, size_t cli_id_len )
8239{
8240 ((void) ctx);
8241 ((void) p);
8242 ((void) end);
8243 ((void) cli_id);
8244 ((void) cli_id_len);
8245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008246 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008247}
8248
8249static int ssl_cookie_check_dummy( void *ctx,
8250 const unsigned char *cookie, size_t cookie_len,
8251 const unsigned char *cli_id, size_t cli_id_len )
8252{
8253 ((void) ctx);
8254 ((void) cookie);
8255 ((void) cookie_len);
8256 ((void) cli_id);
8257 ((void) cli_id_len);
8258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008259 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008260}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008261#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008262
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008263/* Once ssl->out_hdr as the address of the beginning of the
8264 * next outgoing record is set, deduce the other pointers.
8265 *
8266 * Note: For TLS, we save the implicit record sequence number
8267 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8268 * and the caller has to make sure there's space for this.
8269 */
8270
8271static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8272 mbedtls_ssl_transform *transform )
8273{
8274#if defined(MBEDTLS_SSL_PROTO_DTLS)
8275 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8276 {
8277 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008278#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008279 ssl->out_cid = ssl->out_ctr + 8;
8280 ssl->out_len = ssl->out_cid;
8281 if( transform != NULL )
8282 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008283#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008284 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008285#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008286 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008287 }
8288 else
8289#endif
8290 {
8291 ssl->out_ctr = ssl->out_hdr - 8;
8292 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008293#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008294 ssl->out_cid = ssl->out_len;
8295#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008296 ssl->out_iv = ssl->out_hdr + 5;
8297 }
8298
8299 /* Adjust out_msg to make space for explicit IV, if used. */
8300 if( transform != NULL &&
8301 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8302 {
8303 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8304 }
8305 else
8306 ssl->out_msg = ssl->out_iv;
8307}
8308
8309/* Once ssl->in_hdr as the address of the beginning of the
8310 * next incoming record is set, deduce the other pointers.
8311 *
8312 * Note: For TLS, we save the implicit record sequence number
8313 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8314 * and the caller has to make sure there's space for this.
8315 */
8316
Hanno Becker79594fd2019-05-08 09:38:41 +01008317static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008318{
Hanno Becker79594fd2019-05-08 09:38:41 +01008319 /* This function sets the pointers to match the case
8320 * of unprotected TLS/DTLS records, with both ssl->in_iv
8321 * and ssl->in_msg pointing to the beginning of the record
8322 * content.
8323 *
8324 * When decrypting a protected record, ssl->in_msg
8325 * will be shifted to point to the beginning of the
8326 * record plaintext.
8327 */
8328
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008329#if defined(MBEDTLS_SSL_PROTO_DTLS)
8330 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8331 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008332 /* This sets the header pointers to match records
8333 * without CID. When we receive a record containing
8334 * a CID, the fields are shifted accordingly in
8335 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008336 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008337#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008338 ssl->in_cid = ssl->in_ctr + 8;
8339 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008340#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008341 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008342#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008343 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008344 }
8345 else
8346#endif
8347 {
8348 ssl->in_ctr = ssl->in_hdr - 8;
8349 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008350#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008351 ssl->in_cid = ssl->in_len;
8352#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008353 ssl->in_iv = ssl->in_hdr + 5;
8354 }
8355
Hanno Becker79594fd2019-05-08 09:38:41 +01008356 /* This will be adjusted at record decryption time. */
8357 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008358}
8359
Paul Bakker5121ce52009-01-03 21:22:43 +00008360/*
8361 * Initialize an SSL context
8362 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008363void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8364{
8365 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8366}
8367
8368/*
8369 * Setup an SSL context
8370 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008371
8372static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8373{
8374 /* Set the incoming and outgoing record pointers. */
8375#if defined(MBEDTLS_SSL_PROTO_DTLS)
8376 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8377 {
8378 ssl->out_hdr = ssl->out_buf;
8379 ssl->in_hdr = ssl->in_buf;
8380 }
8381 else
8382#endif /* MBEDTLS_SSL_PROTO_DTLS */
8383 {
8384 ssl->out_hdr = ssl->out_buf + 8;
8385 ssl->in_hdr = ssl->in_buf + 8;
8386 }
8387
8388 /* Derive other internal pointers. */
8389 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008390 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008391}
8392
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008393int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008394 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008395{
Paul Bakker48916f92012-09-16 19:57:18 +00008396 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008397
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008398 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008399
8400 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008401 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008402 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008403
8404 /* Set to NULL in case of an error condition */
8405 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008406
Angus Grattond8213d02016-05-25 20:56:48 +10008407 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8408 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008409 {
Angus Grattond8213d02016-05-25 20:56:48 +10008410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008411 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008412 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008413 }
8414
8415 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8416 if( ssl->out_buf == NULL )
8417 {
8418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008419 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008420 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008421 }
8422
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008423 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008424
Paul Bakker48916f92012-09-16 19:57:18 +00008425 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008426 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008427
8428 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008429
8430error:
8431 mbedtls_free( ssl->in_buf );
8432 mbedtls_free( ssl->out_buf );
8433
8434 ssl->conf = NULL;
8435
8436 ssl->in_buf = NULL;
8437 ssl->out_buf = NULL;
8438
8439 ssl->in_hdr = NULL;
8440 ssl->in_ctr = NULL;
8441 ssl->in_len = NULL;
8442 ssl->in_iv = NULL;
8443 ssl->in_msg = NULL;
8444
8445 ssl->out_hdr = NULL;
8446 ssl->out_ctr = NULL;
8447 ssl->out_len = NULL;
8448 ssl->out_iv = NULL;
8449 ssl->out_msg = NULL;
8450
k-stachowiak9f7798e2018-07-31 16:52:32 +02008451 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008452}
8453
8454/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008455 * Reset an initialized and used SSL context for re-use while retaining
8456 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008457 *
8458 * If partial is non-zero, keep data in the input buffer and client ID.
8459 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008460 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008461static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008462{
Paul Bakker48916f92012-09-16 19:57:18 +00008463 int ret;
8464
Hanno Becker7e772132018-08-10 12:38:21 +01008465#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8466 !defined(MBEDTLS_SSL_SRV_C)
8467 ((void) partial);
8468#endif
8469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008470 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008471
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008472 /* Cancel any possibly running timer */
8473 ssl_set_timer( ssl, 0 );
8474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008475#if defined(MBEDTLS_SSL_RENEGOTIATION)
8476 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008477 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008478
8479 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008480 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8481 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008482#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008483 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008484
Paul Bakker7eb013f2011-10-06 12:37:39 +00008485 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008486 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008487
8488 ssl->in_msgtype = 0;
8489 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008490#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008491 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008492 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008493#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008494#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008495 ssl_dtls_replay_reset( ssl );
8496#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008497
8498 ssl->in_hslen = 0;
8499 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008500
8501 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008502
8503 ssl->out_msgtype = 0;
8504 ssl->out_msglen = 0;
8505 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008506#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8507 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008508 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008509#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008510
Hanno Becker19859472018-08-06 09:40:20 +01008511 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8512
Paul Bakker48916f92012-09-16 19:57:18 +00008513 ssl->transform_in = NULL;
8514 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008515
Hanno Becker78640902018-08-13 16:35:15 +01008516 ssl->session_in = NULL;
8517 ssl->session_out = NULL;
8518
Angus Grattond8213d02016-05-25 20:56:48 +10008519 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008520
8521#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008522 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008523#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8524 {
8525 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008526 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008527 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008529#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8530 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8533 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8536 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008537 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008538 }
8539#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008540
Paul Bakker48916f92012-09-16 19:57:18 +00008541 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008543 mbedtls_ssl_transform_free( ssl->transform );
8544 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008545 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008546 }
Paul Bakker48916f92012-09-16 19:57:18 +00008547
Paul Bakkerc0463502013-02-14 11:19:38 +01008548 if( ssl->session )
8549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008550 mbedtls_ssl_session_free( ssl->session );
8551 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008552 ssl->session = NULL;
8553 }
8554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008555#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008556 ssl->alpn_chosen = NULL;
8557#endif
8558
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008559#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008560#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008561 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008562#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008563 {
8564 mbedtls_free( ssl->cli_id );
8565 ssl->cli_id = NULL;
8566 ssl->cli_id_len = 0;
8567 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008568#endif
8569
Paul Bakker48916f92012-09-16 19:57:18 +00008570 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8571 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008572
8573 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008574}
8575
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008576/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008577 * Reset an initialized and used SSL context for re-use while retaining
8578 * all application-set variables, function pointers and data.
8579 */
8580int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8581{
8582 return( ssl_session_reset_int( ssl, 0 ) );
8583}
8584
8585/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008586 * SSL set accessors
8587 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008588void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008589{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008590 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008591}
8592
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008593void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008594{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008595 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008596}
8597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008598#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008599void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008600{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008601 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008602}
8603#endif
8604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008605#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008606void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008607{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008608 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008609}
8610#endif
8611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008612#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008613
Hanno Becker1841b0a2018-08-24 11:13:57 +01008614void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8615 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008616{
8617 ssl->disable_datagram_packing = !allow_packing;
8618}
8619
8620void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8621 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008622{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008623 conf->hs_timeout_min = min;
8624 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008625}
8626#endif
8627
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008628void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008629{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008630 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008631}
8632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008633#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008634void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008635 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008636 void *p_vrfy )
8637{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008638 conf->f_vrfy = f_vrfy;
8639 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008640}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008642
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008643void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008644 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008645 void *p_rng )
8646{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008647 conf->f_rng = f_rng;
8648 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008649}
8650
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008651void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008652 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008653 void *p_dbg )
8654{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008655 conf->f_dbg = f_dbg;
8656 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008657}
8658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008659void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008660 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008661 mbedtls_ssl_send_t *f_send,
8662 mbedtls_ssl_recv_t *f_recv,
8663 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008664{
8665 ssl->p_bio = p_bio;
8666 ssl->f_send = f_send;
8667 ssl->f_recv = f_recv;
8668 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008669}
8670
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008671#if defined(MBEDTLS_SSL_PROTO_DTLS)
8672void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8673{
8674 ssl->mtu = mtu;
8675}
8676#endif
8677
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008678void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008679{
8680 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008681}
8682
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008683void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8684 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008685 mbedtls_ssl_set_timer_t *f_set_timer,
8686 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008687{
8688 ssl->p_timer = p_timer;
8689 ssl->f_set_timer = f_set_timer;
8690 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008691
8692 /* Make sure we start with no timer running */
8693 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008694}
8695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008696#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008697void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008698 void *p_cache,
8699 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8700 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008701{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008702 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008703 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008704 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008705}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008706#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008708#if defined(MBEDTLS_SSL_CLI_C)
8709int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008710{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008711 int ret;
8712
8713 if( ssl == NULL ||
8714 session == NULL ||
8715 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008716 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008718 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008719 }
8720
Hanno Becker52055ae2019-02-06 14:30:46 +00008721 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8722 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008723 return( ret );
8724
Paul Bakker0a597072012-09-25 21:55:46 +00008725 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008726
8727 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008728}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008729#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008730
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008731void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008732 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008733{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008734 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8735 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8736 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8737 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008738}
8739
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008740void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008741 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008742 int major, int minor )
8743{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008744 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008745 return;
8746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008747 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008748 return;
8749
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008750 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008751}
8752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008753#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008754void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008755 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008756{
8757 conf->cert_profile = profile;
8758}
8759
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008760/* Append a new keycert entry to a (possibly empty) list */
8761static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8762 mbedtls_x509_crt *cert,
8763 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008764{
niisato8ee24222018-06-25 19:05:48 +09008765 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008766
niisato8ee24222018-06-25 19:05:48 +09008767 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8768 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008769 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008770
niisato8ee24222018-06-25 19:05:48 +09008771 new_cert->cert = cert;
8772 new_cert->key = key;
8773 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008774
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008775 /* Update head is the list was null, else add to the end */
8776 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008777 {
niisato8ee24222018-06-25 19:05:48 +09008778 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008779 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008780 else
8781 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008782 mbedtls_ssl_key_cert *cur = *head;
8783 while( cur->next != NULL )
8784 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008785 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008786 }
8787
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008788 return( 0 );
8789}
8790
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008791int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008792 mbedtls_x509_crt *own_cert,
8793 mbedtls_pk_context *pk_key )
8794{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008795 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008796}
8797
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008798void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008799 mbedtls_x509_crt *ca_chain,
8800 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008801{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008802 conf->ca_chain = ca_chain;
8803 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008804
8805#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8806 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8807 * cannot be used together. */
8808 conf->f_ca_cb = NULL;
8809 conf->p_ca_cb = NULL;
8810#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008811}
Hanno Becker5adaad92019-03-27 16:54:37 +00008812
8813#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8814void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008815 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008816 void *p_ca_cb )
8817{
8818 conf->f_ca_cb = f_ca_cb;
8819 conf->p_ca_cb = p_ca_cb;
8820
8821 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8822 * cannot be used together. */
8823 conf->ca_chain = NULL;
8824 conf->ca_crl = NULL;
8825}
8826#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008827#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008828
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008829#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8830int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8831 mbedtls_x509_crt *own_cert,
8832 mbedtls_pk_context *pk_key )
8833{
8834 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8835 own_cert, pk_key ) );
8836}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008837
8838void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8839 mbedtls_x509_crt *ca_chain,
8840 mbedtls_x509_crl *ca_crl )
8841{
8842 ssl->handshake->sni_ca_chain = ca_chain;
8843 ssl->handshake->sni_ca_crl = ca_crl;
8844}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008845
8846void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8847 int authmode )
8848{
8849 ssl->handshake->sni_authmode = authmode;
8850}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008851#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8852
Hanno Becker8927c832019-04-03 12:52:50 +01008853#if defined(MBEDTLS_X509_CRT_PARSE_C)
8854void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8855 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8856 void *p_vrfy )
8857{
8858 ssl->f_vrfy = f_vrfy;
8859 ssl->p_vrfy = p_vrfy;
8860}
8861#endif
8862
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008863#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008864/*
8865 * Set EC J-PAKE password for current handshake
8866 */
8867int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8868 const unsigned char *pw,
8869 size_t pw_len )
8870{
8871 mbedtls_ecjpake_role role;
8872
Janos Follath8eb64132016-06-03 15:40:57 +01008873 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008874 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8875
8876 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8877 role = MBEDTLS_ECJPAKE_SERVER;
8878 else
8879 role = MBEDTLS_ECJPAKE_CLIENT;
8880
8881 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8882 role,
8883 MBEDTLS_MD_SHA256,
8884 MBEDTLS_ECP_DP_SECP256R1,
8885 pw, pw_len ) );
8886}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008887#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008889#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008890
8891static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8892{
8893 /* Remove reference to existing PSK, if any. */
8894#if defined(MBEDTLS_USE_PSA_CRYPTO)
8895 if( conf->psk_opaque != 0 )
8896 {
8897 /* The maintenance of the PSK key slot is the
8898 * user's responsibility. */
8899 conf->psk_opaque = 0;
8900 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008901 /* This and the following branch should never
8902 * be taken simultaenously as we maintain the
8903 * invariant that raw and opaque PSKs are never
8904 * configured simultaneously. As a safeguard,
8905 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008906#endif /* MBEDTLS_USE_PSA_CRYPTO */
8907 if( conf->psk != NULL )
8908 {
8909 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8910
8911 mbedtls_free( conf->psk );
8912 conf->psk = NULL;
8913 conf->psk_len = 0;
8914 }
8915
8916 /* Remove reference to PSK identity, if any. */
8917 if( conf->psk_identity != NULL )
8918 {
8919 mbedtls_free( conf->psk_identity );
8920 conf->psk_identity = NULL;
8921 conf->psk_identity_len = 0;
8922 }
8923}
8924
Hanno Becker7390c712018-11-15 13:33:04 +00008925/* This function assumes that PSK identity in the SSL config is unset.
8926 * It checks that the provided identity is well-formed and attempts
8927 * to make a copy of it in the SSL config.
8928 * On failure, the PSK identity in the config remains unset. */
8929static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8930 unsigned char const *psk_identity,
8931 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008932{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008933 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008934 if( psk_identity == NULL ||
8935 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008936 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008937 {
8938 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8939 }
8940
Hanno Becker7390c712018-11-15 13:33:04 +00008941 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8942 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008943 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008944
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008945 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008946 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008947
8948 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008949}
8950
Hanno Becker7390c712018-11-15 13:33:04 +00008951int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8952 const unsigned char *psk, size_t psk_len,
8953 const unsigned char *psk_identity, size_t psk_identity_len )
8954{
8955 int ret;
8956 /* Remove opaque/raw PSK + PSK Identity */
8957 ssl_conf_remove_psk( conf );
8958
8959 /* Check and set raw PSK */
8960 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8962 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8963 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8964 conf->psk_len = psk_len;
8965 memcpy( conf->psk, psk, conf->psk_len );
8966
8967 /* Check and set PSK Identity */
8968 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8969 if( ret != 0 )
8970 ssl_conf_remove_psk( conf );
8971
8972 return( ret );
8973}
8974
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008975static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8976{
8977#if defined(MBEDTLS_USE_PSA_CRYPTO)
8978 if( ssl->handshake->psk_opaque != 0 )
8979 {
8980 ssl->handshake->psk_opaque = 0;
8981 }
8982 else
8983#endif /* MBEDTLS_USE_PSA_CRYPTO */
8984 if( ssl->handshake->psk != NULL )
8985 {
8986 mbedtls_platform_zeroize( ssl->handshake->psk,
8987 ssl->handshake->psk_len );
8988 mbedtls_free( ssl->handshake->psk );
8989 ssl->handshake->psk_len = 0;
8990 }
8991}
8992
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008993int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8994 const unsigned char *psk, size_t psk_len )
8995{
8996 if( psk == NULL || ssl->handshake == NULL )
8997 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8998
8999 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9000 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9001
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009002 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009003
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009004 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009005 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009006
9007 ssl->handshake->psk_len = psk_len;
9008 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
9009
9010 return( 0 );
9011}
9012
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009013#if defined(MBEDTLS_USE_PSA_CRYPTO)
9014int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009015 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009016 const unsigned char *psk_identity,
9017 size_t psk_identity_len )
9018{
Hanno Becker7390c712018-11-15 13:33:04 +00009019 int ret;
9020 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009021 ssl_conf_remove_psk( conf );
9022
Hanno Becker7390c712018-11-15 13:33:04 +00009023 /* Check and set opaque PSK */
9024 if( psk_slot == 0 )
9025 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009026 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009027
9028 /* Check and set PSK Identity */
9029 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9030 psk_identity_len );
9031 if( ret != 0 )
9032 ssl_conf_remove_psk( conf );
9033
9034 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009035}
9036
9037int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009038 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009039{
9040 if( psk_slot == 0 || ssl->handshake == NULL )
9041 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9042
9043 ssl_remove_psk( ssl );
9044 ssl->handshake->psk_opaque = psk_slot;
9045 return( 0 );
9046}
9047#endif /* MBEDTLS_USE_PSA_CRYPTO */
9048
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009049void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009050 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009051 size_t),
9052 void *p_psk )
9053{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009054 conf->f_psk = f_psk;
9055 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009056}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009057#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009058
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009059#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009060
9061#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009062int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009063{
9064 int ret;
9065
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009066 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9067 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9068 {
9069 mbedtls_mpi_free( &conf->dhm_P );
9070 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009071 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009072 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009073
9074 return( 0 );
9075}
Hanno Becker470a8c42017-10-04 15:28:46 +01009076#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009077
Hanno Beckera90658f2017-10-04 15:29:08 +01009078int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9079 const unsigned char *dhm_P, size_t P_len,
9080 const unsigned char *dhm_G, size_t G_len )
9081{
9082 int ret;
9083
9084 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9085 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9086 {
9087 mbedtls_mpi_free( &conf->dhm_P );
9088 mbedtls_mpi_free( &conf->dhm_G );
9089 return( ret );
9090 }
9091
9092 return( 0 );
9093}
Paul Bakker5121ce52009-01-03 21:22:43 +00009094
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009095int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009096{
9097 int ret;
9098
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009099 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9100 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9101 {
9102 mbedtls_mpi_free( &conf->dhm_P );
9103 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009104 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009105 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009106
9107 return( 0 );
9108}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009109#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009110
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009111#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9112/*
9113 * Set the minimum length for Diffie-Hellman parameters
9114 */
9115void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9116 unsigned int bitlen )
9117{
9118 conf->dhm_min_bitlen = bitlen;
9119}
9120#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9121
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009122#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009123/*
9124 * Set allowed/preferred hashes for handshake signatures
9125 */
9126void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9127 const int *hashes )
9128{
9129 conf->sig_hashes = hashes;
9130}
Hanno Becker947194e2017-04-07 13:25:49 +01009131#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009132
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009133#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009134/*
9135 * Set the allowed elliptic curves
9136 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009137void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009138 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009139{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009140 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009141}
Hanno Becker947194e2017-04-07 13:25:49 +01009142#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009143
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009144#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009145int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009146{
Hanno Becker947194e2017-04-07 13:25:49 +01009147 /* Initialize to suppress unnecessary compiler warning */
9148 size_t hostname_len = 0;
9149
9150 /* Check if new hostname is valid before
9151 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009152 if( hostname != NULL )
9153 {
9154 hostname_len = strlen( hostname );
9155
9156 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9157 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9158 }
9159
9160 /* Now it's clear that we will overwrite the old hostname,
9161 * so we can free it safely */
9162
9163 if( ssl->hostname != NULL )
9164 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009165 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009166 mbedtls_free( ssl->hostname );
9167 }
9168
9169 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009170
Paul Bakker5121ce52009-01-03 21:22:43 +00009171 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009172 {
9173 ssl->hostname = NULL;
9174 }
9175 else
9176 {
9177 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009178 if( ssl->hostname == NULL )
9179 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009180
Hanno Becker947194e2017-04-07 13:25:49 +01009181 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009182
Hanno Becker947194e2017-04-07 13:25:49 +01009183 ssl->hostname[hostname_len] = '\0';
9184 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009185
9186 return( 0 );
9187}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009188#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009189
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009190#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009191void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009192 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009193 const unsigned char *, size_t),
9194 void *p_sni )
9195{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009196 conf->f_sni = f_sni;
9197 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009198}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009199#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009201#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009202int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009203{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009204 size_t cur_len, tot_len;
9205 const char **p;
9206
9207 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009208 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9209 * MUST NOT be truncated."
9210 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009211 */
9212 tot_len = 0;
9213 for( p = protos; *p != NULL; p++ )
9214 {
9215 cur_len = strlen( *p );
9216 tot_len += cur_len;
9217
9218 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009219 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009220 }
9221
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009222 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009223
9224 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009225}
9226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009227const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009228{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009229 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009231#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009232
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009233void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009234{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009235 conf->max_major_ver = major;
9236 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009237}
9238
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009239void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009240{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009241 conf->min_major_ver = major;
9242 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009243}
9244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009245#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009246void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009247{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009248 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009249}
9250#endif
9251
Janos Follath088ce432017-04-10 12:42:31 +01009252#if defined(MBEDTLS_SSL_SRV_C)
9253void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9254 char cert_req_ca_list )
9255{
9256 conf->cert_req_ca_list = cert_req_ca_list;
9257}
9258#endif
9259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009260#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009261void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009262{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009263 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009264}
9265#endif
9266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009267#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009268void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009269{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009270 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009271}
9272#endif
9273
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009274#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009275void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009276{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009277 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009278}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009279#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009281#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009282int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009283{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009284 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009285 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009287 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009288 }
9289
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009290 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009291
9292 return( 0 );
9293}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009294#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009297void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009298{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009299 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009301#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009304void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009305{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009306 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009307}
9308#endif
9309
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009310void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009311{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009312 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009313}
9314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009316void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009317{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009318 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009319}
9320
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009321void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009322{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009323 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009324}
9325
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009326void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009327 const unsigned char period[8] )
9328{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009329 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009330}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009331#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009333#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009334#if defined(MBEDTLS_SSL_CLI_C)
9335void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009336{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009337 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009338}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009339#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009340
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009341#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009342void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9343 mbedtls_ssl_ticket_write_t *f_ticket_write,
9344 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9345 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009346{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009347 conf->f_ticket_write = f_ticket_write;
9348 conf->f_ticket_parse = f_ticket_parse;
9349 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009350}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009351#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009352#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009353
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009354#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9355void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9356 mbedtls_ssl_export_keys_t *f_export_keys,
9357 void *p_export_keys )
9358{
9359 conf->f_export_keys = f_export_keys;
9360 conf->p_export_keys = p_export_keys;
9361}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009362
9363void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9364 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9365 void *p_export_keys )
9366{
9367 conf->f_export_keys_ext = f_export_keys_ext;
9368 conf->p_export_keys = p_export_keys;
9369}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009370#endif
9371
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009372#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009373void mbedtls_ssl_conf_async_private_cb(
9374 mbedtls_ssl_config *conf,
9375 mbedtls_ssl_async_sign_t *f_async_sign,
9376 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9377 mbedtls_ssl_async_resume_t *f_async_resume,
9378 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009379 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009380{
9381 conf->f_async_sign_start = f_async_sign;
9382 conf->f_async_decrypt_start = f_async_decrypt;
9383 conf->f_async_resume = f_async_resume;
9384 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009385 conf->p_async_config_data = async_config_data;
9386}
9387
Gilles Peskine8f97af72018-04-26 11:46:10 +02009388void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9389{
9390 return( conf->p_async_config_data );
9391}
9392
Gilles Peskine1febfef2018-04-30 11:54:39 +02009393void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009394{
9395 if( ssl->handshake == NULL )
9396 return( NULL );
9397 else
9398 return( ssl->handshake->user_async_ctx );
9399}
9400
Gilles Peskine1febfef2018-04-30 11:54:39 +02009401void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009402 void *ctx )
9403{
9404 if( ssl->handshake != NULL )
9405 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009406}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009407#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009408
Paul Bakker5121ce52009-01-03 21:22:43 +00009409/*
9410 * SSL get accessors
9411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009412size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009413{
9414 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9415}
9416
Hanno Becker8b170a02017-10-10 11:51:19 +01009417int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9418{
9419 /*
9420 * Case A: We're currently holding back
9421 * a message for further processing.
9422 */
9423
9424 if( ssl->keep_current_message == 1 )
9425 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009426 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009427 return( 1 );
9428 }
9429
9430 /*
9431 * Case B: Further records are pending in the current datagram.
9432 */
9433
9434#if defined(MBEDTLS_SSL_PROTO_DTLS)
9435 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9436 ssl->in_left > ssl->next_record_offset )
9437 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009438 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009439 return( 1 );
9440 }
9441#endif /* MBEDTLS_SSL_PROTO_DTLS */
9442
9443 /*
9444 * Case C: A handshake message is being processed.
9445 */
9446
Hanno Becker8b170a02017-10-10 11:51:19 +01009447 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9448 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009449 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009450 return( 1 );
9451 }
9452
9453 /*
9454 * Case D: An application data message is being processed
9455 */
9456 if( ssl->in_offt != NULL )
9457 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009458 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009459 return( 1 );
9460 }
9461
9462 /*
9463 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009464 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009465 * we implement support for multiple alerts in single records.
9466 */
9467
9468 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9469 return( 0 );
9470}
9471
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009472uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009473{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009474 if( ssl->session != NULL )
9475 return( ssl->session->verify_result );
9476
9477 if( ssl->session_negotiate != NULL )
9478 return( ssl->session_negotiate->verify_result );
9479
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009480 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009481}
9482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009483const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009484{
Paul Bakker926c8e42013-03-06 10:23:34 +01009485 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009486 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009488 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009489}
9490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009491const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009492{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009493#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009494 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009495 {
9496 switch( ssl->minor_ver )
9497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009498 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009499 return( "DTLSv1.0" );
9500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009502 return( "DTLSv1.2" );
9503
9504 default:
9505 return( "unknown (DTLS)" );
9506 }
9507 }
9508#endif
9509
Paul Bakker43ca69c2011-01-15 17:35:19 +00009510 switch( ssl->minor_ver )
9511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009513 return( "SSLv3.0" );
9514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009515 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009516 return( "TLSv1.0" );
9517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009519 return( "TLSv1.1" );
9520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009521 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009522 return( "TLSv1.2" );
9523
Paul Bakker43ca69c2011-01-15 17:35:19 +00009524 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009525 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009526 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009527}
9528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009530{
Hanno Becker3136ede2018-08-17 15:28:19 +01009531 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009533 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009534
Hanno Becker5903de42019-05-03 14:46:38 +01009535 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9536
Hanno Becker78640902018-08-13 16:35:15 +01009537 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009538 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009540#if defined(MBEDTLS_ZLIB_SUPPORT)
9541 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9542 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009543#endif
9544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009545 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009547 case MBEDTLS_MODE_GCM:
9548 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009549 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009550 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009551 transform_expansion = transform->minlen;
9552 break;
9553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009554 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009555
9556 block_size = mbedtls_cipher_get_block_size(
9557 &transform->cipher_ctx_enc );
9558
Hanno Becker3136ede2018-08-17 15:28:19 +01009559 /* Expansion due to the addition of the MAC. */
9560 transform_expansion += transform->maclen;
9561
9562 /* Expansion due to the addition of CBC padding;
9563 * Theoretically up to 256 bytes, but we never use
9564 * more than the block size of the underlying cipher. */
9565 transform_expansion += block_size;
9566
9567 /* For TLS 1.1 or higher, an explicit IV is added
9568 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009569#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9570 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009571 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009572#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009573
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009574 break;
9575
9576 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009578 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009579 }
9580
Hanno Beckera0e20d02019-05-15 14:03:01 +01009581#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009582 if( transform->out_cid_len != 0 )
9583 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009584#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009585
Hanno Becker5903de42019-05-03 14:46:38 +01009586 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009587}
9588
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009589#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9590size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9591{
9592 size_t max_len;
9593
9594 /*
9595 * Assume mfl_code is correct since it was checked when set
9596 */
Angus Grattond8213d02016-05-25 20:56:48 +10009597 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009598
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009599 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009600 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009601 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009602 {
Angus Grattond8213d02016-05-25 20:56:48 +10009603 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009604 }
9605
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009606 /* During a handshake, use the value being negotiated */
9607 if( ssl->session_negotiate != NULL &&
9608 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9609 {
9610 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9611 }
9612
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009613 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009614}
9615#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9616
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009617#if defined(MBEDTLS_SSL_PROTO_DTLS)
9618static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9619{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009620 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9621 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9622 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9623 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9624 return ( 0 );
9625
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009626 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9627 return( ssl->mtu );
9628
9629 if( ssl->mtu == 0 )
9630 return( ssl->handshake->mtu );
9631
9632 return( ssl->mtu < ssl->handshake->mtu ?
9633 ssl->mtu : ssl->handshake->mtu );
9634}
9635#endif /* MBEDTLS_SSL_PROTO_DTLS */
9636
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009637int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9638{
9639 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9640
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009641#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9642 !defined(MBEDTLS_SSL_PROTO_DTLS)
9643 (void) ssl;
9644#endif
9645
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009646#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9647 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9648
9649 if( max_len > mfl )
9650 max_len = mfl;
9651#endif
9652
9653#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009654 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009655 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009656 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009657 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9658 const size_t overhead = (size_t) ret;
9659
9660 if( ret < 0 )
9661 return( ret );
9662
9663 if( mtu <= overhead )
9664 {
9665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9666 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9667 }
9668
9669 if( max_len > mtu - overhead )
9670 max_len = mtu - overhead;
9671 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009672#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009673
Hanno Becker0defedb2018-08-10 12:35:02 +01009674#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9675 !defined(MBEDTLS_SSL_PROTO_DTLS)
9676 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009677#endif
9678
9679 return( (int) max_len );
9680}
9681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009682#if defined(MBEDTLS_X509_CRT_PARSE_C)
9683const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009684{
9685 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009686 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009687
Hanno Beckere6824572019-02-07 13:18:46 +00009688#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009689 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009690#else
9691 return( NULL );
9692#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009693}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009694#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009696#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009697int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9698 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009699{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009700 if( ssl == NULL ||
9701 dst == NULL ||
9702 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009703 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009705 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009706 }
9707
Hanno Becker52055ae2019-02-06 14:30:46 +00009708 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009709}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009710#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009711
Paul Bakker5121ce52009-01-03 21:22:43 +00009712/*
Paul Bakker1961b702013-01-25 14:49:24 +01009713 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009715int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009716{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009717 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009718
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009719 if( ssl == NULL || ssl->conf == NULL )
9720 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009722#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009723 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009724 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009725#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009726#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009727 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009728 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009729#endif
9730
Paul Bakker1961b702013-01-25 14:49:24 +01009731 return( ret );
9732}
9733
9734/*
9735 * Perform the SSL handshake
9736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009737int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009738{
9739 int ret = 0;
9740
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009741 if( ssl == NULL || ssl->conf == NULL )
9742 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009746 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009748 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009749
9750 if( ret != 0 )
9751 break;
9752 }
9753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009755
9756 return( ret );
9757}
9758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009759#if defined(MBEDTLS_SSL_RENEGOTIATION)
9760#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009761/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009762 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009764static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009765{
9766 int ret;
9767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009769
9770 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9772 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009773
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009774 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009775 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009776 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009777 return( ret );
9778 }
9779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009781
9782 return( 0 );
9783}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009784#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009785
9786/*
9787 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009788 * - any side: calling mbedtls_ssl_renegotiate(),
9789 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9790 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009791 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009792 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009793 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009794 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009795static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009796{
9797 int ret;
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
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009801 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9802 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009803
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009804 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9805 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009807 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009809 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009810 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009811 ssl->handshake->out_msg_seq = 1;
9812 else
9813 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009814 }
9815#endif
9816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009817 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9818 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009823 return( ret );
9824 }
9825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009827
9828 return( 0 );
9829}
9830
9831/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009832 * Renegotiate current connection on client,
9833 * or request renegotiation on server
9834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009835int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009836{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009838
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009839 if( ssl == NULL || ssl->conf == NULL )
9840 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009843 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009844 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009846 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9847 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009850
9851 /* Did we already try/start sending HelloRequest? */
9852 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009853 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009854
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009855 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009856 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009859#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009860 /*
9861 * On client, either start the renegotiation process or,
9862 * if already in progress, continue the handshake
9863 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009864 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009866 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9867 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009868
9869 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009871 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009872 return( ret );
9873 }
9874 }
9875 else
9876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009877 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009880 return( ret );
9881 }
9882 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009884
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009885 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009886}
9887
9888/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009889 * Check record counters and renegotiate if they're above the limit.
9890 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009891static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009892{
Andres AG2196c7f2016-12-15 17:01:16 +00009893 size_t ep_len = ssl_ep_len( ssl );
9894 int in_ctr_cmp;
9895 int out_ctr_cmp;
9896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009897 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9898 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009899 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009900 {
9901 return( 0 );
9902 }
9903
Andres AG2196c7f2016-12-15 17:01:16 +00009904 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9905 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009906 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009907 ssl->conf->renego_period + ep_len, 8 - ep_len );
9908
9909 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009910 {
9911 return( 0 );
9912 }
9913
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009915 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009916}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009917#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009918
9919/*
9920 * Receive application data decrypted from the SSL layer
9921 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009922int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009923{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009924 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009925 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009926
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009927 if( ssl == NULL || ssl->conf == NULL )
9928 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009932#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009933 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009935 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009936 return( ret );
9937
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009938 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009939 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009940 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009941 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009942 return( ret );
9943 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009944 }
9945#endif
9946
Hanno Becker4a810fb2017-05-24 16:27:30 +01009947 /*
9948 * Check if renegotiation is necessary and/or handshake is
9949 * in process. If yes, perform/continue, and fall through
9950 * if an unexpected packet is received while the client
9951 * is waiting for the ServerHello.
9952 *
9953 * (There is no equivalent to the last condition on
9954 * the server-side as it is not treated as within
9955 * a handshake while waiting for the ClientHello
9956 * after a renegotiation request.)
9957 */
9958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009959#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009960 ret = ssl_check_ctr_renegotiate( ssl );
9961 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9962 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009964 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009965 return( ret );
9966 }
9967#endif
9968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009969 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009971 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009972 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9973 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009975 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009976 return( ret );
9977 }
9978 }
9979
Hanno Beckere41158b2017-10-23 13:30:32 +01009980 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009981 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009982 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009983 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009984 if( ssl->f_get_timer != NULL &&
9985 ssl->f_get_timer( ssl->p_timer ) == -1 )
9986 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009987 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009988 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009989
Hanno Becker327c93b2018-08-15 13:56:18 +01009990 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009991 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009992 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9993 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009994
Hanno Becker4a810fb2017-05-24 16:27:30 +01009995 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9996 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009997 }
9998
9999 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010000 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010001 {
10002 /*
10003 * OpenSSL sends empty messages to randomize the IV
10004 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010005 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010007 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010008 return( 0 );
10009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010011 return( ret );
10012 }
10013 }
10014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010015 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010018
Hanno Becker4a810fb2017-05-24 16:27:30 +010010019 /*
10020 * - For client-side, expect SERVER_HELLO_REQUEST.
10021 * - For server-side, expect CLIENT_HELLO.
10022 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10023 */
10024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010025#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010026 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010027 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010028 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010031
10032 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010033#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010034 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010035 {
10036 continue;
10037 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010039 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010040 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010041#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010042
Hanno Becker4a810fb2017-05-24 16:27:30 +010010043#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010044 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010045 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010048
10049 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010050#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010051 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010052 {
10053 continue;
10054 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010055#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010056 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010057 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010058#endif /* MBEDTLS_SSL_SRV_C */
10059
Hanno Becker21df7f92017-10-17 11:03:26 +010010060#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010061 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010062 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10063 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10064 ssl->conf->allow_legacy_renegotiation ==
10065 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10066 {
10067 /*
10068 * Accept renegotiation request
10069 */
Paul Bakker48916f92012-09-16 19:57:18 +000010070
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010071 /* DTLS clients need to know renego is server-initiated */
10072#if defined(MBEDTLS_SSL_PROTO_DTLS)
10073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10074 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10075 {
10076 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10077 }
10078#endif
10079 ret = ssl_start_renegotiation( ssl );
10080 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10081 ret != 0 )
10082 {
10083 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10084 return( ret );
10085 }
10086 }
10087 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010088#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010089 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010090 /*
10091 * Refuse renegotiation
10092 */
10093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010096#if defined(MBEDTLS_SSL_PROTO_SSL3)
10097 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010098 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010099 /* SSLv3 does not have a "no_renegotiation" warning, so
10100 we send a fatal alert and abort the connection. */
10101 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10102 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10103 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010104 }
10105 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010106#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10107#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10108 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10109 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010111 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10112 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10113 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010114 {
10115 return( ret );
10116 }
Paul Bakker48916f92012-09-16 19:57:18 +000010117 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010118 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010119#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10120 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10123 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010124 }
Paul Bakker48916f92012-09-16 19:57:18 +000010125 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010126
Hanno Becker90333da2017-10-10 11:27:13 +010010127 /* At this point, we don't know whether the renegotiation has been
10128 * completed or not. The cases to consider are the following:
10129 * 1) The renegotiation is complete. In this case, no new record
10130 * has been read yet.
10131 * 2) The renegotiation is incomplete because the client received
10132 * an application data record while awaiting the ServerHello.
10133 * 3) The renegotiation is incomplete because the client received
10134 * a non-handshake, non-application data message while awaiting
10135 * the ServerHello.
10136 * In each of these case, looping will be the proper action:
10137 * - For 1), the next iteration will read a new record and check
10138 * if it's application data.
10139 * - For 2), the loop condition isn't satisfied as application data
10140 * is present, hence continue is the same as break
10141 * - For 3), the loop condition is satisfied and read_record
10142 * will re-deliver the message that was held back by the client
10143 * when expecting the ServerHello.
10144 */
10145 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010146 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010147#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010148 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010149 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010150 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010151 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010152 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010155 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010156 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010157 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010158 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010159 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010160#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010162 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10163 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010166 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010167 }
10168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010169 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10172 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010173 }
10174
10175 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010176
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010177 /* We're going to return something now, cancel timer,
10178 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010179 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010180 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010181
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010182#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010183 /* If we requested renego but received AppData, resend HelloRequest.
10184 * Do it now, after setting in_offt, to avoid taking this branch
10185 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010186#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010187 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010188 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010189 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010190 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010192 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010193 return( ret );
10194 }
10195 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010196#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010197#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010198 }
10199
10200 n = ( len < ssl->in_msglen )
10201 ? len : ssl->in_msglen;
10202
10203 memcpy( buf, ssl->in_offt, n );
10204 ssl->in_msglen -= n;
10205
10206 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010207 {
10208 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010209 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010210 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010211 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010212 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010213 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010214 /* more data available */
10215 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010216 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010219
Paul Bakker23986e52011-04-24 08:57:21 +000010220 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010221}
10222
10223/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010224 * Send application data to be encrypted by the SSL layer, taking care of max
10225 * fragment length and buffer size.
10226 *
10227 * According to RFC 5246 Section 6.2.1:
10228 *
10229 * Zero-length fragments of Application data MAY be sent as they are
10230 * potentially useful as a traffic analysis countermeasure.
10231 *
10232 * Therefore, it is possible that the input message length is 0 and the
10233 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010234 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010235static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010236 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010237{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010238 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10239 const size_t max_len = (size_t) ret;
10240
10241 if( ret < 0 )
10242 {
10243 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10244 return( ret );
10245 }
10246
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010247 if( len > max_len )
10248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010249#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010250 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010253 "maximum fragment length: %d > %d",
10254 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010255 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010256 }
10257 else
10258#endif
10259 len = max_len;
10260 }
Paul Bakker887bd502011-06-08 13:10:54 +000010261
Paul Bakker5121ce52009-01-03 21:22:43 +000010262 if( ssl->out_left != 0 )
10263 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010264 /*
10265 * The user has previously tried to send the data and
10266 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10267 * written. In this case, we expect the high-level write function
10268 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010270 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010272 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010273 return( ret );
10274 }
10275 }
Paul Bakker887bd502011-06-08 13:10:54 +000010276 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010277 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010278 /*
10279 * The user is trying to send a message the first time, so we need to
10280 * copy the data into the internal buffers and setup the data structure
10281 * to keep track of partial writes
10282 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010283 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010284 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010285 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010286
Hanno Becker67bc7c32018-08-06 11:33:50 +010010287 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010289 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010290 return( ret );
10291 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010292 }
10293
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010294 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010295}
10296
10297/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010298 * Write application data, doing 1/n-1 splitting if necessary.
10299 *
10300 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010301 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010302 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010304#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010305static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010306 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010307{
10308 int ret;
10309
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010310 if( ssl->conf->cbc_record_splitting ==
10311 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010312 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010313 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10314 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10315 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010316 {
10317 return( ssl_write_real( ssl, buf, len ) );
10318 }
10319
10320 if( ssl->split_done == 0 )
10321 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010322 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010323 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010324 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010325 }
10326
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010327 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10328 return( ret );
10329 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010330
10331 return( ret + 1 );
10332}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010333#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010334
10335/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010336 * Write application data (public-facing wrapper)
10337 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010338int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010339{
10340 int ret;
10341
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010343
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010344 if( ssl == NULL || ssl->conf == NULL )
10345 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10346
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010347#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010348 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10349 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010350 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010351 return( ret );
10352 }
10353#endif
10354
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010355 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010356 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010357 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010358 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010360 return( ret );
10361 }
10362 }
10363
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010364#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010365 ret = ssl_write_split( ssl, buf, len );
10366#else
10367 ret = ssl_write_real( ssl, buf, len );
10368#endif
10369
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010371
10372 return( ret );
10373}
10374
10375/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010376 * Notify the peer that the connection is being closed
10377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010378int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010379{
10380 int ret;
10381
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010382 if( ssl == NULL || ssl->conf == NULL )
10383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010386
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010387 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010388 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010390 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010392 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10393 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10394 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010396 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010397 return( ret );
10398 }
10399 }
10400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010402
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010403 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010404}
10405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010406void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010407{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010408 if( transform == NULL )
10409 return;
10410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010411#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010412 deflateEnd( &transform->ctx_deflate );
10413 inflateEnd( &transform->ctx_inflate );
10414#endif
10415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010416 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10417 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010418
Hanno Beckerd56ed242018-01-03 15:32:51 +000010419#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010420 mbedtls_md_free( &transform->md_ctx_enc );
10421 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010422#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010423
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010424 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010425}
10426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010427#if defined(MBEDTLS_X509_CRT_PARSE_C)
10428static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010429{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010430 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010431
10432 while( cur != NULL )
10433 {
10434 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010435 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010436 cur = next;
10437 }
10438}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010439#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010440
Hanno Becker0271f962018-08-16 13:23:47 +010010441#if defined(MBEDTLS_SSL_PROTO_DTLS)
10442
10443static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10444{
10445 unsigned offset;
10446 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10447
10448 if( hs == NULL )
10449 return;
10450
Hanno Becker283f5ef2018-08-24 09:34:47 +010010451 ssl_free_buffered_record( ssl );
10452
Hanno Becker0271f962018-08-16 13:23:47 +010010453 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010454 ssl_buffering_free_slot( ssl, offset );
10455}
10456
10457static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10458 uint8_t slot )
10459{
10460 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10461 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010462
10463 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10464 return;
10465
Hanno Beckere605b192018-08-21 15:59:07 +010010466 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010467 {
Hanno Beckere605b192018-08-21 15:59:07 +010010468 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010469 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010470 mbedtls_free( hs_buf->data );
10471 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010472 }
10473}
10474
10475#endif /* MBEDTLS_SSL_PROTO_DTLS */
10476
Gilles Peskine9b562d52018-04-25 20:32:43 +020010477void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010478{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010479 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10480
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010481 if( handshake == NULL )
10482 return;
10483
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010484#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10485 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10486 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010487 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010488 handshake->async_in_progress = 0;
10489 }
10490#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10491
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010492#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10493 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10494 mbedtls_md5_free( &handshake->fin_md5 );
10495 mbedtls_sha1_free( &handshake->fin_sha1 );
10496#endif
10497#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10498#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010499#if defined(MBEDTLS_USE_PSA_CRYPTO)
10500 psa_hash_abort( &handshake->fin_sha256_psa );
10501#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010502 mbedtls_sha256_free( &handshake->fin_sha256 );
10503#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010504#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010505#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010506#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010507 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010508#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010509 mbedtls_sha512_free( &handshake->fin_sha512 );
10510#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010511#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010512#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010514#if defined(MBEDTLS_DHM_C)
10515 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010516#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010517#if defined(MBEDTLS_ECDH_C)
10518 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010519#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010520#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010521 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010522#if defined(MBEDTLS_SSL_CLI_C)
10523 mbedtls_free( handshake->ecjpake_cache );
10524 handshake->ecjpake_cache = NULL;
10525 handshake->ecjpake_cache_len = 0;
10526#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010527#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010528
Janos Follath4ae5c292016-02-10 11:27:43 +000010529#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10530 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010531 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010532 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010533#endif
10534
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010535#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10536 if( handshake->psk != NULL )
10537 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010538 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010539 mbedtls_free( handshake->psk );
10540 }
10541#endif
10542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010543#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10544 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010545 /*
10546 * Free only the linked list wrapper, not the keys themselves
10547 * since the belong to the SNI callback
10548 */
10549 if( handshake->sni_key_cert != NULL )
10550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010551 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010552
10553 while( cur != NULL )
10554 {
10555 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010556 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010557 cur = next;
10558 }
10559 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010560#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010561
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010562#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010563 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010564 if( handshake->ecrs_peer_cert != NULL )
10565 {
10566 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10567 mbedtls_free( handshake->ecrs_peer_cert );
10568 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010569#endif
10570
Hanno Becker75173122019-02-06 16:18:31 +000010571#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10572 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10573 mbedtls_pk_free( &handshake->peer_pubkey );
10574#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010576#if defined(MBEDTLS_SSL_PROTO_DTLS)
10577 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010578 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010579 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010580#endif
10581
Hanno Becker4a63ed42019-01-08 11:39:35 +000010582#if defined(MBEDTLS_ECDH_C) && \
10583 defined(MBEDTLS_USE_PSA_CRYPTO)
10584 psa_destroy_key( handshake->ecdh_psa_privkey );
10585#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10586
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010587 mbedtls_platform_zeroize( handshake,
10588 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010589}
10590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010591void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010592{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010593 if( session == NULL )
10594 return;
10595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010596#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010597 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010598#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010599
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010600#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010601 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010602#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010603
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010604 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010605}
10606
Paul Bakker5121ce52009-01-03 21:22:43 +000010607/*
10608 * Free an SSL context
10609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010610void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010611{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010612 if( ssl == NULL )
10613 return;
10614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010616
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010617 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010618 {
Angus Grattond8213d02016-05-25 20:56:48 +100010619 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010620 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010621 }
10622
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010623 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010624 {
Angus Grattond8213d02016-05-25 20:56:48 +100010625 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010626 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010627 }
10628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010629#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010630 if( ssl->compress_buf != NULL )
10631 {
Angus Grattond8213d02016-05-25 20:56:48 +100010632 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010633 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010634 }
10635#endif
10636
Paul Bakker48916f92012-09-16 19:57:18 +000010637 if( ssl->transform )
10638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010639 mbedtls_ssl_transform_free( ssl->transform );
10640 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010641 }
10642
10643 if( ssl->handshake )
10644 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010645 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010646 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10647 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010649 mbedtls_free( ssl->handshake );
10650 mbedtls_free( ssl->transform_negotiate );
10651 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010652 }
10653
Paul Bakkerc0463502013-02-14 11:19:38 +010010654 if( ssl->session )
10655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010656 mbedtls_ssl_session_free( ssl->session );
10657 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010658 }
10659
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010660#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010661 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010662 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010663 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010664 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010665 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010666#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010668#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10669 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10672 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010673 }
10674#endif
10675
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010676#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010677 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010678#endif
10679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010681
Paul Bakker86f04f42013-02-14 11:20:09 +010010682 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010683 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010684}
10685
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010686/*
10687 * Initialze mbedtls_ssl_config
10688 */
10689void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10690{
10691 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10692}
10693
Simon Butcherc97b6972015-12-27 23:48:17 +000010694#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010695static int ssl_preset_default_hashes[] = {
10696#if defined(MBEDTLS_SHA512_C)
10697 MBEDTLS_MD_SHA512,
10698 MBEDTLS_MD_SHA384,
10699#endif
10700#if defined(MBEDTLS_SHA256_C)
10701 MBEDTLS_MD_SHA256,
10702 MBEDTLS_MD_SHA224,
10703#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010704#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010705 MBEDTLS_MD_SHA1,
10706#endif
10707 MBEDTLS_MD_NONE
10708};
Simon Butcherc97b6972015-12-27 23:48:17 +000010709#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010710
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010711static int ssl_preset_suiteb_ciphersuites[] = {
10712 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10713 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10714 0
10715};
10716
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010717#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010718static int ssl_preset_suiteb_hashes[] = {
10719 MBEDTLS_MD_SHA256,
10720 MBEDTLS_MD_SHA384,
10721 MBEDTLS_MD_NONE
10722};
10723#endif
10724
10725#if defined(MBEDTLS_ECP_C)
10726static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010727#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010728 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010729#endif
10730#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010731 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010732#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010733 MBEDTLS_ECP_DP_NONE
10734};
10735#endif
10736
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010737/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010738 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010739 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010740int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010741 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010742{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010743#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010744 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010745#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010746
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010747 /* Use the functions here so that they are covered in tests,
10748 * but otherwise access member directly for efficiency */
10749 mbedtls_ssl_conf_endpoint( conf, endpoint );
10750 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010751
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010752 /*
10753 * Things that are common to all presets
10754 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010755#if defined(MBEDTLS_SSL_CLI_C)
10756 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10757 {
10758 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10759#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10760 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10761#endif
10762 }
10763#endif
10764
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010765#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010766 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010767#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010768
10769#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10770 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10771#endif
10772
10773#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10774 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10775#endif
10776
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010777#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10778 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10779#endif
10780
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010781#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010782 conf->f_cookie_write = ssl_cookie_write_dummy;
10783 conf->f_cookie_check = ssl_cookie_check_dummy;
10784#endif
10785
10786#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10787 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10788#endif
10789
Janos Follath088ce432017-04-10 12:42:31 +010010790#if defined(MBEDTLS_SSL_SRV_C)
10791 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10792#endif
10793
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010794#if defined(MBEDTLS_SSL_PROTO_DTLS)
10795 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10796 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10797#endif
10798
10799#if defined(MBEDTLS_SSL_RENEGOTIATION)
10800 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010801 memset( conf->renego_period, 0x00, 2 );
10802 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010803#endif
10804
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010805#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10806 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10807 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010808 const unsigned char dhm_p[] =
10809 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10810 const unsigned char dhm_g[] =
10811 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10812
Hanno Beckera90658f2017-10-04 15:29:08 +010010813 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10814 dhm_p, sizeof( dhm_p ),
10815 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010816 {
10817 return( ret );
10818 }
10819 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010820#endif
10821
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010822 /*
10823 * Preset-specific defaults
10824 */
10825 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010826 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010827 /*
10828 * NSA Suite B
10829 */
10830 case MBEDTLS_SSL_PRESET_SUITEB:
10831 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10832 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10833 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10834 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10835
10836 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10837 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10838 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10839 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10840 ssl_preset_suiteb_ciphersuites;
10841
10842#if defined(MBEDTLS_X509_CRT_PARSE_C)
10843 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010844#endif
10845
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010846#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010847 conf->sig_hashes = ssl_preset_suiteb_hashes;
10848#endif
10849
10850#if defined(MBEDTLS_ECP_C)
10851 conf->curve_list = ssl_preset_suiteb_curves;
10852#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010853 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010854
10855 /*
10856 * Default
10857 */
10858 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010859 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10860 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10861 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10862 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10863 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10864 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10865 MBEDTLS_SSL_MIN_MINOR_VERSION :
10866 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010867 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10868 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10869
10870#if defined(MBEDTLS_SSL_PROTO_DTLS)
10871 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10872 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10873#endif
10874
10875 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10876 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10877 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10878 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10879 mbedtls_ssl_list_ciphersuites();
10880
10881#if defined(MBEDTLS_X509_CRT_PARSE_C)
10882 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10883#endif
10884
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010885#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010886 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010887#endif
10888
10889#if defined(MBEDTLS_ECP_C)
10890 conf->curve_list = mbedtls_ecp_grp_id_list();
10891#endif
10892
10893#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10894 conf->dhm_min_bitlen = 1024;
10895#endif
10896 }
10897
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010898 return( 0 );
10899}
10900
10901/*
10902 * Free mbedtls_ssl_config
10903 */
10904void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10905{
10906#if defined(MBEDTLS_DHM_C)
10907 mbedtls_mpi_free( &conf->dhm_P );
10908 mbedtls_mpi_free( &conf->dhm_G );
10909#endif
10910
10911#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10912 if( conf->psk != NULL )
10913 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010914 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010915 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010916 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010917 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010918 }
10919
10920 if( conf->psk_identity != NULL )
10921 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010922 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010923 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010924 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010925 conf->psk_identity_len = 0;
10926 }
10927#endif
10928
10929#if defined(MBEDTLS_X509_CRT_PARSE_C)
10930 ssl_key_cert_free( conf->key_cert );
10931#endif
10932
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010933 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010934}
10935
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010936#if defined(MBEDTLS_PK_C) && \
10937 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010938/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010939 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010940 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010941unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010942{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010943#if defined(MBEDTLS_RSA_C)
10944 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10945 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010946#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010947#if defined(MBEDTLS_ECDSA_C)
10948 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10949 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010950#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010951 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010952}
10953
Hanno Becker7e5437a2017-04-28 17:15:26 +010010954unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10955{
10956 switch( type ) {
10957 case MBEDTLS_PK_RSA:
10958 return( MBEDTLS_SSL_SIG_RSA );
10959 case MBEDTLS_PK_ECDSA:
10960 case MBEDTLS_PK_ECKEY:
10961 return( MBEDTLS_SSL_SIG_ECDSA );
10962 default:
10963 return( MBEDTLS_SSL_SIG_ANON );
10964 }
10965}
10966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010967mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010968{
10969 switch( sig )
10970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010971#if defined(MBEDTLS_RSA_C)
10972 case MBEDTLS_SSL_SIG_RSA:
10973 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010974#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010975#if defined(MBEDTLS_ECDSA_C)
10976 case MBEDTLS_SSL_SIG_ECDSA:
10977 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010978#endif
10979 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010980 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010981 }
10982}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010983#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010984
Hanno Becker7e5437a2017-04-28 17:15:26 +010010985#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10986 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10987
10988/* Find an entry in a signature-hash set matching a given hash algorithm. */
10989mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10990 mbedtls_pk_type_t sig_alg )
10991{
10992 switch( sig_alg )
10993 {
10994 case MBEDTLS_PK_RSA:
10995 return( set->rsa );
10996 case MBEDTLS_PK_ECDSA:
10997 return( set->ecdsa );
10998 default:
10999 return( MBEDTLS_MD_NONE );
11000 }
11001}
11002
11003/* Add a signature-hash-pair to a signature-hash set */
11004void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
11005 mbedtls_pk_type_t sig_alg,
11006 mbedtls_md_type_t md_alg )
11007{
11008 switch( sig_alg )
11009 {
11010 case MBEDTLS_PK_RSA:
11011 if( set->rsa == MBEDTLS_MD_NONE )
11012 set->rsa = md_alg;
11013 break;
11014
11015 case MBEDTLS_PK_ECDSA:
11016 if( set->ecdsa == MBEDTLS_MD_NONE )
11017 set->ecdsa = md_alg;
11018 break;
11019
11020 default:
11021 break;
11022 }
11023}
11024
11025/* Allow exactly one hash algorithm for each signature. */
11026void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11027 mbedtls_md_type_t md_alg )
11028{
11029 set->rsa = md_alg;
11030 set->ecdsa = md_alg;
11031}
11032
11033#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11034 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11035
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011036/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011037 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011038 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011039mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011040{
11041 switch( hash )
11042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011043#if defined(MBEDTLS_MD5_C)
11044 case MBEDTLS_SSL_HASH_MD5:
11045 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011046#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011047#if defined(MBEDTLS_SHA1_C)
11048 case MBEDTLS_SSL_HASH_SHA1:
11049 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011050#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011051#if defined(MBEDTLS_SHA256_C)
11052 case MBEDTLS_SSL_HASH_SHA224:
11053 return( MBEDTLS_MD_SHA224 );
11054 case MBEDTLS_SSL_HASH_SHA256:
11055 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011056#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011057#if defined(MBEDTLS_SHA512_C)
11058 case MBEDTLS_SSL_HASH_SHA384:
11059 return( MBEDTLS_MD_SHA384 );
11060 case MBEDTLS_SSL_HASH_SHA512:
11061 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011062#endif
11063 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011064 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011065 }
11066}
11067
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011068/*
11069 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11070 */
11071unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11072{
11073 switch( md )
11074 {
11075#if defined(MBEDTLS_MD5_C)
11076 case MBEDTLS_MD_MD5:
11077 return( MBEDTLS_SSL_HASH_MD5 );
11078#endif
11079#if defined(MBEDTLS_SHA1_C)
11080 case MBEDTLS_MD_SHA1:
11081 return( MBEDTLS_SSL_HASH_SHA1 );
11082#endif
11083#if defined(MBEDTLS_SHA256_C)
11084 case MBEDTLS_MD_SHA224:
11085 return( MBEDTLS_SSL_HASH_SHA224 );
11086 case MBEDTLS_MD_SHA256:
11087 return( MBEDTLS_SSL_HASH_SHA256 );
11088#endif
11089#if defined(MBEDTLS_SHA512_C)
11090 case MBEDTLS_MD_SHA384:
11091 return( MBEDTLS_SSL_HASH_SHA384 );
11092 case MBEDTLS_MD_SHA512:
11093 return( MBEDTLS_SSL_HASH_SHA512 );
11094#endif
11095 default:
11096 return( MBEDTLS_SSL_HASH_NONE );
11097 }
11098}
11099
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011100#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011101/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011102 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011103 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011104 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011105int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011106{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011107 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011108
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011109 if( ssl->conf->curve_list == NULL )
11110 return( -1 );
11111
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011112 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011113 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011114 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011115
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011116 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011117}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011118#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011119
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011120#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011121/*
11122 * Check if a hash proposed by the peer is in our list.
11123 * Return 0 if we're willing to use it, -1 otherwise.
11124 */
11125int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11126 mbedtls_md_type_t md )
11127{
11128 const int *cur;
11129
11130 if( ssl->conf->sig_hashes == NULL )
11131 return( -1 );
11132
11133 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11134 if( *cur == (int) md )
11135 return( 0 );
11136
11137 return( -1 );
11138}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011139#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011141#if defined(MBEDTLS_X509_CRT_PARSE_C)
11142int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11143 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011144 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011145 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011146{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011147 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011148#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011149 int usage = 0;
11150#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011151#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011152 const char *ext_oid;
11153 size_t ext_len;
11154#endif
11155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011156#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11157 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011158 ((void) cert);
11159 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011160 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011161#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011163#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11164 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011165 {
11166 /* Server part of the key exchange */
11167 switch( ciphersuite->key_exchange )
11168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011169 case MBEDTLS_KEY_EXCHANGE_RSA:
11170 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011171 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011172 break;
11173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011174 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11175 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11176 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11177 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011178 break;
11179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011180 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11181 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011182 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011183 break;
11184
11185 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011186 case MBEDTLS_KEY_EXCHANGE_NONE:
11187 case MBEDTLS_KEY_EXCHANGE_PSK:
11188 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11189 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011190 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011191 usage = 0;
11192 }
11193 }
11194 else
11195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011196 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11197 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011198 }
11199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011200 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011201 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011202 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011203 ret = -1;
11204 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011205#else
11206 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011207#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011209#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11210 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011212 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11213 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011214 }
11215 else
11216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011217 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11218 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011219 }
11220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011221 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011222 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011223 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011224 ret = -1;
11225 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011226#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011227
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011228 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011229}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011230#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011231
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011232/*
11233 * Convert version numbers to/from wire format
11234 * and, for DTLS, to/from TLS equivalent.
11235 *
11236 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011237 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011238 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11239 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011241void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011242 unsigned char ver[2] )
11243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011244#if defined(MBEDTLS_SSL_PROTO_DTLS)
11245 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011247 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011248 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11249
11250 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11251 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11252 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011253 else
11254#else
11255 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011256#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011257 {
11258 ver[0] = (unsigned char) major;
11259 ver[1] = (unsigned char) minor;
11260 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011261}
11262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011263void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011264 const unsigned char ver[2] )
11265{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011266#if defined(MBEDTLS_SSL_PROTO_DTLS)
11267 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011268 {
11269 *major = 255 - ver[0] + 2;
11270 *minor = 255 - ver[1] + 1;
11271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011272 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011273 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11274 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011275 else
11276#else
11277 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011278#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011279 {
11280 *major = ver[0];
11281 *minor = ver[1];
11282 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011283}
11284
Simon Butcher99000142016-10-13 17:21:01 +010011285int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11286{
11287#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11288 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11289 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11290
11291 switch( md )
11292 {
11293#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11294#if defined(MBEDTLS_MD5_C)
11295 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011296 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011297#endif
11298#if defined(MBEDTLS_SHA1_C)
11299 case MBEDTLS_SSL_HASH_SHA1:
11300 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11301 break;
11302#endif
11303#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11304#if defined(MBEDTLS_SHA512_C)
11305 case MBEDTLS_SSL_HASH_SHA384:
11306 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11307 break;
11308#endif
11309#if defined(MBEDTLS_SHA256_C)
11310 case MBEDTLS_SSL_HASH_SHA256:
11311 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11312 break;
11313#endif
11314 default:
11315 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11316 }
11317
11318 return 0;
11319#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11320 (void) ssl;
11321 (void) md;
11322
11323 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11324#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11325}
11326
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011327#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11328 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11329int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11330 unsigned char *output,
11331 unsigned char *data, size_t data_len )
11332{
11333 int ret = 0;
11334 mbedtls_md5_context mbedtls_md5;
11335 mbedtls_sha1_context mbedtls_sha1;
11336
11337 mbedtls_md5_init( &mbedtls_md5 );
11338 mbedtls_sha1_init( &mbedtls_sha1 );
11339
11340 /*
11341 * digitally-signed struct {
11342 * opaque md5_hash[16];
11343 * opaque sha_hash[20];
11344 * };
11345 *
11346 * md5_hash
11347 * MD5(ClientHello.random + ServerHello.random
11348 * + ServerParams);
11349 * sha_hash
11350 * SHA(ClientHello.random + ServerHello.random
11351 * + ServerParams);
11352 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011353 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011354 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011355 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011356 goto exit;
11357 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011358 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011359 ssl->handshake->randbytes, 64 ) ) != 0 )
11360 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011362 goto exit;
11363 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011364 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011365 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011366 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011367 goto exit;
11368 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011369 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011370 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011372 goto exit;
11373 }
11374
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011375 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011376 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011377 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011378 goto exit;
11379 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011380 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011381 ssl->handshake->randbytes, 64 ) ) != 0 )
11382 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011384 goto exit;
11385 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011386 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011387 data_len ) ) != 0 )
11388 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011389 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011390 goto exit;
11391 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011392 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011393 output + 16 ) ) != 0 )
11394 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011395 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011396 goto exit;
11397 }
11398
11399exit:
11400 mbedtls_md5_free( &mbedtls_md5 );
11401 mbedtls_sha1_free( &mbedtls_sha1 );
11402
11403 if( ret != 0 )
11404 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11405 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11406
11407 return( ret );
11408
11409}
11410#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11411 MBEDTLS_SSL_PROTO_TLS1_1 */
11412
11413#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11414 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011415
11416#if defined(MBEDTLS_USE_PSA_CRYPTO)
11417int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11418 unsigned char *hash, size_t *hashlen,
11419 unsigned char *data, size_t data_len,
11420 mbedtls_md_type_t md_alg )
11421{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011422 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011423 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011424 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11425
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011426 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011427
11428 if( ( status = psa_hash_setup( &hash_operation,
11429 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011430 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011431 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011432 goto exit;
11433 }
11434
Andrzej Kurek814feff2019-01-14 04:35:19 -050011435 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11436 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011437 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011438 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011439 goto exit;
11440 }
11441
Andrzej Kurek814feff2019-01-14 04:35:19 -050011442 if( ( status = psa_hash_update( &hash_operation,
11443 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011444 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011445 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011446 goto exit;
11447 }
11448
Andrzej Kurek814feff2019-01-14 04:35:19 -050011449 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11450 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011451 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011452 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011453 goto exit;
11454 }
11455
11456exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011457 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011458 {
11459 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11460 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011461 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011462 {
11463 case PSA_ERROR_NOT_SUPPORTED:
11464 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011465 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011466 case PSA_ERROR_BUFFER_TOO_SMALL:
11467 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11468 case PSA_ERROR_INSUFFICIENT_MEMORY:
11469 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11470 default:
11471 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11472 }
11473 }
11474 return( 0 );
11475}
11476
11477#else
11478
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011479int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011480 unsigned char *hash, size_t *hashlen,
11481 unsigned char *data, size_t data_len,
11482 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011483{
11484 int ret = 0;
11485 mbedtls_md_context_t ctx;
11486 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011487 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011488
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011489 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011490
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011491 mbedtls_md_init( &ctx );
11492
11493 /*
11494 * digitally-signed struct {
11495 * opaque client_random[32];
11496 * opaque server_random[32];
11497 * ServerDHParams params;
11498 * };
11499 */
11500 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11501 {
11502 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11503 goto exit;
11504 }
11505 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11506 {
11507 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11508 goto exit;
11509 }
11510 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11511 {
11512 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11513 goto exit;
11514 }
11515 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11516 {
11517 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11518 goto exit;
11519 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011520 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011521 {
11522 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11523 goto exit;
11524 }
11525
11526exit:
11527 mbedtls_md_free( &ctx );
11528
11529 if( ret != 0 )
11530 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11531 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11532
11533 return( ret );
11534}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011535#endif /* MBEDTLS_USE_PSA_CRYPTO */
11536
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011537#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11538 MBEDTLS_SSL_PROTO_TLS1_2 */
11539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011540#endif /* MBEDTLS_SSL_TLS_C */